|
|
@@ -1,6 +1,8 @@
|
|
|
import 'package:flutter/material.dart';
|
|
|
+import 'package:provide/provide.dart';
|
|
|
|
|
|
-void main() => runApp(MyApp());
|
|
|
+void main() => runApp(ProviderNode(
|
|
|
+ child: MyApp(), providers: Providers()..provideValue(CountViewModel(0))));
|
|
|
|
|
|
class MyApp extends StatelessWidget {
|
|
|
// This widget is the root of your application.
|
|
|
@@ -44,19 +46,6 @@ class MyHomePage extends StatefulWidget {
|
|
|
}
|
|
|
|
|
|
class _MyHomePageState extends State<MyHomePage> {
|
|
|
- int _counter = 0;
|
|
|
-
|
|
|
- void _incrementCounter() {
|
|
|
- setState(() {
|
|
|
- // This call to setState tells the Flutter framework that something has
|
|
|
- // changed in this State, which causes it to rerun the build method below
|
|
|
- // so that the display can reflect the updated values. If we changed
|
|
|
- // _counter without calling setState(), then the build method would not be
|
|
|
- // called again, and so nothing would appear to happen.
|
|
|
- _counter++;
|
|
|
- });
|
|
|
- }
|
|
|
-
|
|
|
@override
|
|
|
Widget build(BuildContext context) {
|
|
|
// This method is rerun every time setState is called, for instance as done
|
|
|
@@ -69,8 +58,7 @@ class _MyHomePageState extends State<MyHomePage> {
|
|
|
appBar: AppBar(
|
|
|
// Here we take the value from the MyHomePage object that was created by
|
|
|
// the App.build method, and use it to set our appbar title.
|
|
|
- title: Text(widget.title
|
|
|
- ),
|
|
|
+ title: Text(widget.title),
|
|
|
),
|
|
|
body: Center(
|
|
|
// Center is a layout widget. It takes a single child and positions it
|
|
|
@@ -92,21 +80,41 @@ class _MyHomePageState extends State<MyHomePage> {
|
|
|
// horizontal).
|
|
|
mainAxisAlignment: MainAxisAlignment.center,
|
|
|
children: <Widget>[
|
|
|
- Text(
|
|
|
+ const Text(
|
|
|
'You have pushed the button this many times:',
|
|
|
),
|
|
|
- Text(
|
|
|
- '$_counter',
|
|
|
- style: Theme.of(context).textTheme.display1,
|
|
|
+ Provide<CountViewModel>(
|
|
|
+ builder:
|
|
|
+ (BuildContext context, Widget child, CountViewModel value) =>
|
|
|
+ Text('${value.value}',
|
|
|
+ style: Theme.of(context).textTheme.display1,
|
|
|
+ ),
|
|
|
),
|
|
|
],
|
|
|
),
|
|
|
),
|
|
|
- floatingActionButton: FloatingActionButton(
|
|
|
- onPressed: _incrementCounter,
|
|
|
- tooltip: 'Increment',
|
|
|
- child: Icon(Icons.add),
|
|
|
+ floatingActionButton: Provide<CountViewModel>(
|
|
|
+ builder: (BuildContext context, Widget child,CountViewModel value) =>
|
|
|
+ FloatingActionButton(
|
|
|
+ onPressed: value.increment,
|
|
|
+ tooltip: 'Increment',
|
|
|
+ child: Icon(Icons.add),
|
|
|
+ ),
|
|
|
), // This trailing comma makes auto-formatting nicer for build methods.
|
|
|
);
|
|
|
}
|
|
|
}
|
|
|
+
|
|
|
+class CountViewModel extends ChangeNotifier {
|
|
|
+ int _value;
|
|
|
+
|
|
|
+ int get value => _value;
|
|
|
+
|
|
|
+ CountViewModel(this._value);
|
|
|
+
|
|
|
+ void increment() {
|
|
|
+ _value++;
|
|
|
+ notifyListeners();
|
|
|
+ }
|
|
|
+
|
|
|
+}
|