Last active
December 30, 2019 06:20
-
-
Save suragch/1a48fd05ea198cca132ee48f34c3f0ac 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 StreamProvider<MyModel>( // <--- StreamProvider | |
initialData: MyModel(someValue: 'default value'), | |
create: (context) => getStreamOfMyModel(), | |
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: (){ | |
myModel.doSomething(); | |
}, | |
); | |
}, | |
) | |
), | |
Container( | |
padding: const EdgeInsets.all(35), | |
color: Colors.blue[200], | |
child: Consumer<MyModel>( // <--- Consumer | |
builder: (context, myModel, child) { | |
return Text(myModel.someValue); | |
}, | |
), | |
), | |
], | |
), | |
), | |
), | |
); | |
} | |
} | |
Stream<MyModel> getStreamOfMyModel() { // <--- Stream | |
return Stream<MyModel>.periodic(Duration(seconds: 1), | |
(x) => MyModel(someValue: '$x')) | |
.take(10); | |
} | |
class MyModel { // <--- MyModel | |
MyModel({this.someValue}); | |
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