Last active
December 1, 2020 07:29
-
-
Save kmvignesh/23d02f9e7c3a716a479eed10c6884c8f 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
| import 'package:flutter/material.dart'; | |
| import 'package:provider/provider.dart'; | |
| void main() { | |
| runApp(MyApp()); | |
| } | |
| class MyApp extends StatelessWidget { | |
| @override | |
| Widget build(BuildContext context) { | |
| return MaterialApp( | |
| title: 'Flutter Demo', | |
| theme: ThemeData( | |
| primarySwatch: Colors.blue, | |
| visualDensity: VisualDensity.adaptivePlatformDensity, | |
| ), | |
| home: ChangeNotifierProvider( | |
| create: (_) => NumberViewModel(), child: MyHomePage()), | |
| ); | |
| } | |
| } | |
| class MyHomePage extends StatefulWidget { | |
| @override | |
| _MyHomePageState createState() => _MyHomePageState(); | |
| } | |
| class _MyHomePageState extends State<MyHomePage> { | |
| NumberViewModel model; | |
| @override | |
| void initState() { | |
| model = Provider.of<NumberViewModel>(context, listen: false); | |
| super.initState(); | |
| } | |
| @override | |
| Widget build(BuildContext context) { | |
| print("build : MyHomePage"); | |
| return Scaffold( | |
| appBar: AppBar(title: Text("Provider")), | |
| body: Column( | |
| children: [ | |
| Row( | |
| children: [ | |
| RaisedButton( | |
| child: Text("num1"), | |
| onPressed: () { | |
| model.incrementNum1(); | |
| }, | |
| ), | |
| SizedBox(width: 16), | |
| RaisedButton( | |
| child: Text("num2"), | |
| onPressed: () { | |
| model.incrementNum2(); | |
| }, | |
| ), | |
| SizedBox(width: 16), | |
| RaisedButton( | |
| child: Text("num3"), | |
| onPressed: () { | |
| model.incrementNum3(); | |
| }, | |
| ), | |
| SizedBox(width: 16), | |
| ], | |
| ), | |
| SizedBox(height: 16), | |
| Consumer<NumberViewModel>(builder: (_, value, __) { | |
| print("build : Consumer"); | |
| return Text("num 1 value : ${value.num1}"); | |
| }), | |
| SizedBox(height: 16), | |
| Selector<NumberViewModel, int>( | |
| selector: (_, viewModel) => viewModel.num2, | |
| builder: (_, value, __) { | |
| print("build : Selector"); | |
| return Text("num 2 value : $value"); | |
| }), | |
| SizedBox(height: 16), | |
| ChangeNotifierProvider<ValueNotifier<int>>.value( | |
| value: model.num3Notifier, | |
| child: TextWidget(), | |
| ) | |
| ], | |
| ), | |
| ); | |
| } | |
| } | |
| class TextWidget extends StatelessWidget { | |
| @override | |
| Widget build(BuildContext context) { | |
| print("build : TextWidget"); | |
| int data = Provider.of<ValueNotifier<int>>(context).value; | |
| return Text("num 3 value : $data"); | |
| } | |
| } | |
| class NumberViewModel extends ChangeNotifier { | |
| int num1 = 0; | |
| int num2 = 0; | |
| var num3Notifier = ValueNotifier<int>(0); | |
| void incrementNum1() { | |
| num1++; | |
| notifyListeners(); | |
| } | |
| void incrementNum2() { | |
| num2++; | |
| notifyListeners(); | |
| } | |
| void incrementNum3() { | |
| // num3Notifier.value++ won't work because both | |
| // num3Notifier.value & currentValue will point | |
| // to same location which means no value change happened | |
| int currentValue = num3Notifier.value; | |
| currentValue++; | |
| num3Notifier.value = currentValue; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment