Last active
December 30, 2019 06:13
-
-
Save suragch/638b5b168bed6d72fc3912b4bd1e8788 to your computer and use it in GitHub Desktop.
This file contains 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 Provider<MyModel>( // <--- Provider | |
create: (context) => MyModel(), | |
child: MaterialApp( | |
home: Scaffold( | |
appBar: AppBar(title: Text('My App')), | |
body: Row( | |
mainAxisAlignment: MainAxisAlignment.center, | |
children: <Widget>[ | |
Container( | |
padding: const EdgeInsets.all(20), | |
color: Colors.green[200], | |
child: Consumer<MyModel>( // <--- Consumer | |
builder: (context, myModel, child) { | |
return RaisedButton( | |
child: Text('Do something'), | |
onPressed: (){ | |
// We have access to the model. | |
myModel.doSomething(); | |
}, | |
); | |
}, | |
) | |
), | |
Container( | |
padding: const EdgeInsets.all(35), | |
color: Colors.blue[200], | |
child: Consumer<MyModel>( // <--- Consumer | |
builder: (context, myModel, child) { | |
return Text(myModel.someValue); | |
}, | |
), | |
), | |
], | |
), | |
), | |
), | |
); | |
} | |
} | |
class MyModel { // <--- MyModel | |
String someValue = 'Hello'; | |
void doSomething() { | |
someValue = 'Goodbye'; | |
print(someValue); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment