-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathShareDataInheritedWidget.dart
155 lines (138 loc) · 5.17 KB
/
ShareDataInheritedWidget.dart
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
import 'package:flutter/material.dart';
import 'package:flutter_app/common/CommonShowModel.dart';
///author: yang yi
///email: yangyirunning@163.com
class ShareDataInheritedWidget extends StatefulWidget {
@override
State<StatefulWidget> createState() {
// TODO: implement createState
return ShareDataInheritedState();
}
}
class ShareDataInheritedState extends State<ShareDataInheritedWidget> {
String text = "好!";
@override
Widget build(BuildContext context) {
// TODO: implement build
final arg = ModalRoute.of(context).settings.arguments;
return Scaffold(
appBar: getAppBar(arg),
body: Container(
color: Colors.grey[100],
padding: EdgeInsets.all(10),
width: MediaQuery.of(context).size.width,
child: Column(
children: <Widget>[
RaisedButton(
child: Text("点击追加一个String"),
onPressed: () {
setState(() {
text += "好!";
});
},
),
Expanded(
flex: 4,
child: Container(
width: MediaQuery.of(context).size.width,
color: Colors.amberAccent[100],
//数据共享控件ShareDataWidget
child: ShareDataWidget(
text: text,
//传递给自定义的子控件MyDependOnTextWidget,观测输出
child: MyDependOnTextWidget(),
),
),
),
Expanded(
flex: 4,
child: Container(
width: MediaQuery.of(context).size.width,
color: Colors.blue[100],
//数据共享控件ShareDataWidget
child: ShareDataWidget(
text: text,
//传递给自定义的子控件MyDependOnTextWidget,观测输出
child: MyUnDependOnTextWidget(),
),
),
)
],
),
),
);
}
}
//---------------------------------------------------------------------------
//继承InheritedWidget实现一个共享数据控件
class ShareDataWidget extends InheritedWidget {
final String text;
ShareDataWidget({@required this.text, Widget child}) : super(child: child);
//获得一个子控件可依赖的共享控件(dependOnInheritedWidgetOfExactType为获得一个注册了子Widget的依赖关系的Widget)
static ShareDataWidget getDependOnShareDataWidget(BuildContext context) {
return context.dependOnInheritedWidgetOfExactType<ShareDataWidget>() ??
Text("虚位以待");
}
//获得一个子控件不可依赖的共享控件(getElementForInheritedWidgetOfExactType为获得一个没有注册子Widget的依赖关系的Widget)
static ShareDataWidget getUnDependOnShareDataWidget(BuildContext context) {
return context.findAncestorWidgetOfExactType<ShareDataWidget>() ??
Text("虚位以待");
}
@override
bool updateShouldNotify(ShareDataWidget oldWidget) {
// TODO: implement updateShouldNotify
// 更新之后是否通知子控件,true为通知,false为不通知
// 新旧值不相等时,通知子树中依赖data的Widget,子树中依赖data的Widget的state的didChangeDependencies周期函数会被调用
return oldWidget.text != text;
}
}
//---------------------------------------------------------------------------
//实现一个特殊的Text控件,直接依赖ShareDataWidget控件中的text数据
class MyDependOnTextWidget extends StatefulWidget {
@override
State<StatefulWidget> createState() {
// TODO: implement createState
return MyDependOnTextState();
}
}
class MyDependOnTextState extends State<MyDependOnTextWidget> {
@override
Widget build(BuildContext context) {
// TODO: implement build
return Text(
ShareDataWidget.getDependOnShareDataWidget(context).text,
style: TextStyle(fontSize: 18, color: Colors.grey),
);
}
@override
void didChangeDependencies() {
//if ( 当父Widget中通过dependOnInheritedWidgetOfExactType获取Widget && updateShouldNotify返回true)调用此周期方法
super.didChangeDependencies();
print("MyDependOnTextState 引用共享数据控件中的text值发生了变化");
}
}
//---------------------------------------------------------------------------
//实现一个特殊的Text控件,不依赖ShareDataWidget控件中的text数据
class MyUnDependOnTextWidget extends StatefulWidget {
@override
State<StatefulWidget> createState() {
// TODO: implement createState
return MyUnDependOnTextState();
}
}
class MyUnDependOnTextState extends State<MyUnDependOnTextWidget> {
@override
Widget build(BuildContext context) {
// TODO: implement build
return Text(
ShareDataWidget.getUnDependOnShareDataWidget(context).text,
style: TextStyle(fontSize: 18, color: Colors.grey),
);
}
@override
void didChangeDependencies() {
super.didChangeDependencies();
//正常情况下因为此控件没有注册与子控件的依赖关系,所以此行在控制台不会输出
print("MyUnDependOnTextWidget 引用共享数据控件中的text值发生了变化");
}
}