consumer 0.1.4 consumer: ^0.1.4 copied to clipboard
consumer is like react-consumer state manage, use Stream at dart, consumer have memo and shoudWidgetUpdate function to intercept update.
consumer #
flutter consumer is like react-consumer state manage, use Stream at dart, consumer have memo and shoudWidgetUpdate function to intercept update.
API Document:
- https://pub.flutter-io.cn/documentation/consumer/latest/
- https://pub-web.flutter-io.cn/documentation/consumer/latest/
Install consumer #
Change pubspec.yaml
:
dependencies:
consumer: ^0.1.2
Getting Started #
1. Register Stream use state: #
// Create app's state, one project only need one app's state
class AppState {
String name = '';
}
void main() async {
// Use app's state init stream.
Store.initState(AppState());
runApp(MyApp());
}
2. Linsten state: #
class SomePage extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Text('Welcome use consumer'),
// Only update this widget
Consumer<AppState>(
memo: (state) => [state.name],
builder: (ctx, state) {
return Text('name ${state.name}');
},
),
TextField(
onChanged: (v) {
// Triggle stream listen
// At business project, move this function to actions scripts fold, please.
Store.setState<AppState>((state) {
state.name = v;
});
},
),
],
),
),
);
}
}
That's all #
Thank you read and use consumer