Created
December 24, 2019 07:27
-
-
Save ksdme/8c4a2f87cf1b5a86a6cfebc7431554c9 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
diff --git a/lib/main.dart b/lib/main.dart | |
index c2b13d3..2362ab4 100644 | |
--- a/lib/main.dart | |
+++ b/lib/main.dart | |
@@ -1,58 +1,55 @@ | |
import 'package:flutter/material.dart'; | |
+import 'package:states_rebuilder/states_rebuilder.dart'; | |
void main() => runApp(MyApp()); | |
+class CounterBloc { | |
+ int counter = 0; | |
+ static final instance = CounterBloc(); | |
+} | |
+ | |
class MyApp extends StatelessWidget { | |
@override | |
Widget build(BuildContext context) { | |
return MaterialApp( | |
title: 'Flutter Demo', | |
- debugShowCheckedModeBanner: false, | |
theme: ThemeData( | |
- primarySwatch: Colors.blue, | |
+ primarySwatch: Colors.green, | |
+ ), | |
+ home: Injector( | |
+ inject: [Inject(() => CounterBloc.instance)], | |
+ builder: (_) => HomePage(), | |
), | |
- home: MyHomePage(title: 'Flutter Demo Home Page'), | |
); | |
} | |
} | |
-class MyHomePage extends StatefulWidget { | |
- MyHomePage({Key key, this.title}) : super(key: key); | |
- | |
- final String title; | |
- | |
- @override | |
- _MyHomePageState createState() => _MyHomePageState(); | |
-} | |
- | |
-class _MyHomePageState extends State<MyHomePage> { | |
- int _counter = 0; | |
- | |
- void _incrementCounter() { | |
- setState(() { | |
- _counter++; | |
- }); | |
- } | |
- | |
+class HomePage extends StatelessWidget { | |
@override | |
Widget build(BuildContext context) { | |
+ final model = Injector.getAsReactive<CounterBloc>( | |
+ context: context, | |
+ ); | |
+ | |
return Scaffold( | |
appBar: AppBar( | |
- title: Text(widget.title), | |
+ title: Text( | |
+ 'States Rebuilder Counter', | |
+ ), | |
), | |
body: Center( | |
child: Column( | |
mainAxisAlignment: MainAxisAlignment.center, | |
children: <Widget>[ | |
Text( | |
- '$_counter', | |
+ '${model.state.counter}', | |
style: Theme.of(context).textTheme.display1, | |
), | |
], | |
), | |
), | |
floatingActionButton: FloatingActionButton( | |
- onPressed: _incrementCounter, | |
+ onPressed: () => model.setState((state) => state.counter++), | |
tooltip: 'Increment', | |
child: Icon(Icons.add), | |
), |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment