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());//同步变化
}
}