Redux is a popular state management framework that originated from the JavaScript community and has been widely adopted in various front-end frameworks. In Flutter, Redux provides a predictable state container that helps manage application state and facilitates an easy flow of data throughout the app.
To use Redux in Flutter, we need to add the redux and flutter_redux packages to our project's dependencies in the pubspec.yaml
file. Once the packages are added, we can start utilizing Redux in our app.
The first step in using Redux is to create a store that holds the application's state. The store is responsible for dispatching actions and updating the state based on those actions.
import 'package:flutter/material.dart';
import 'package:flutter_redux/flutter_redux.dart';
import 'package:redux/redux.dart';
// Define actions
enum CounterActions { increment, decrement }
// Define reducer
int counterReducer(int state, dynamic action) {
if (action == CounterActions.increment) {
return state + 1;
} else if (action == CounterActions.decrement) {
return state - 1;
}
return state;
}
void main() {
final store = Store(counterReducer, initialState: 0);
runApp(MyApp(store: store));
}
class MyApp extends StatelessWidget {
final Store store;
MyApp({required this.store});
@override
Widget build(BuildContext context) {
return StoreProvider(
store: store,
child: MaterialApp(
title: 'Flutter Redux Demo',
home: CounterPage(),
),
);
}
}
class CounterPage extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Counter'),
),
body: StoreConnector(
converter: (store) => store.state,
builder: (context, count) {
return Center(
child: Text(
'Count: $count',
style: TextStyle(fontSize: 24),
),
);
},
),
floatingActionButton: Column(
mainAxisAlignment: MainAxisAlignment.end,
children: [
FloatingActionButton(
onPressed: () =>
StoreProvider.of(context).dispatch(CounterActions.increment),
child: Icon(Icons.add),
),
SizedBox(height: 16),
FloatingActionButton(
onPressed: () =>
StoreProvider.of(context).dispatch(CounterActions.decrement),
child: Icon(Icons.remove),
),
],
),
);
}
}
In the above code snippet, we define a simple counter app using Redux. The store is created with an initial state of 0 and a reducer function that handles the state updates based on dispatched actions.
The CounterPage
widget is connected to the store using the StoreConnector
widget from the flutter_redux
package. It converts the state from the store to an integer value using the converter
function.
The current count value is displayed in the center of the screen. The floating action buttons dispatch actions to increment or decrement the count. The actions are dispatched through the StoreProvider
widget, which is responsible for providing access to the store in the widget tree.
Flutter Redux is a powerful state management framework that provides a predictable way to manage and access application state. With Redux, you can easily dispatch actions and update the state in a consistent manner. It simplifies the process of managing complex application state and makes it easier to reason about data flow in your app.