Want to set state from another widget in flutter? You can use combine Listenable/Stream with respectively ValueListenableBuilder and StreamBuilder which both do the listening/update part for you.
Below is a quick example with Listenable:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 | class MyHomePage extends StatelessWidget { final number = new ValueNotifier(0); @override Widget build(BuildContext context) { return Scaffold( body: ValueListenableBuilder<int>( valueListenable: number, builder: (context, value, child) { return Center( child: RaisedButton( onPressed: () { number.value++; }, child: MyWidget(number), ), ); }, ), ); } } class MyWidget extends StatelessWidget { final ValueListenable<int> number; MyWidget(this.number); @override Widget build(BuildContext context) { return new Text(number.value.toString()); } } |
If you like this question & answer and want to contribute, then write your question & answer and email to freewebmentor[@]gmail.com. Your question and answer will appear on FreeWebMentor.com and help other developers.