ditclear 7 år sedan
förälder
incheckning
6cd55fb9db
2 ändrade filer med 33 tillägg och 24 borttagningar
  1. 32 24
      lib/main.dart
  2. 1 0
      pubspec.yaml

+ 32 - 24
lib/main.dart

@@ -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();
+  }
+
+}

+ 1 - 0
pubspec.yaml

@@ -19,6 +19,7 @@ dependencies:
   # The following adds the Cupertino Icons font to your application.
   # Use with the CupertinoIcons class for iOS style icons.
   cupertino_icons: ^0.1.2
+  provide: ^1.0.2
 
 dev_dependencies:
   flutter_test: