「 flutter 必知必会 」贴心解析:状态管理与数据共享 InheritedWidget 完整使用方案,为你铺平大前端学习之路
由于
Flutter
采用节点树的方式组织页面,以致于一个普通页面的节点层级会很深。 此时,我们如果还是一层层传递数据,当需要修改数据时,就会比较麻烦。 《Flutter 实战》中讲到:InheritedWidget
是Flutter
中非常重要的一个功能型组件,它提供了一种数据在widget
树中从上到下传递、共享的方式 比如我们在应用的根widget
中通过InheritedWidget
共享了一个数据,那么我们便可以在任意子widget
中来获取该共享的数据! 这个特性在一些需要在widget
树中共享数据的场景中非常方便!如Flutter SDK
中正是通过InheritedWidget
来共享应用主题(Theme
)和Locale
(当前语言环境)信息的。
InheritedWidget
的使用InheritedWidget
new flutter project
时,会自动生成如下模版void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
visualDensity: VisualDensity.adaptivePlatformDensity,
),
home: MyHomePage(title: 'Flutter Demo Home Page'),
);
}
}
class MyHomePage extends StatefulWidget {
MyHomePage({Key key, this.title}) : super(key: key);
final String title;
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
int _counter = 0;
void _incrementCounter() {
setState(() {
_counter++;
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Text(
'You have pushed the button this many times:',
),
Text(
'$_counter',
style: Theme.of(context).textTheme.headline4,
),
],
),
),
floatingActionButton: FloatingActionButton(
onPressed: _incrementCounter,
tooltip: 'Increment',
child: Icon(Icons.add),
), // This trailing comma makes auto-formatting nicer for build methods.
);
}
}
InheritedWidget
对这段代码进行改造看看效果flutter
模版进行改造Text
和 FloatingButton
的效果EnvConfig
class EnvConfig {
int count;
}
InheritedWidget
class _InheritedWidget extends InheritedWidget {
_InheritedWidget({
Key key,
@required Widget child,
@required this.data,
}) : super(key: key, child: child);
final _ConfigWrapperState data;
@override
bool updateShouldNotify(_InheritedWidget oldWidget) {
return true;
}
}
InheritedWidget
外在包一层 StatefulWidget
class ConfigWrapper extends StatefulWidget {
final EnvConfig config;
final Widget child;
ConfigWrapper({Key key, this.config, this.child});
@override
State<StatefulWidget> createState() => _ConfigWrapperState();
static _ConfigWrapperState of(BuildContext context) {
final _InheritedWidget inheritedConfig =
context.dependOnInheritedWidgetOfExactType<_InheritedWidget>();
return inheritedConfig.data;
}
}
class _ConfigWrapperState extends State<ConfigWrapper> {
void incrementCounter() {
setState(() {
widget.config.count++;
});
}
@override
Widget build(BuildContext context) {
return new _InheritedWidget(data: this, child: widget.child);
}
}
ConfigWrapper
用于包装,方便管理class ConfigWrapper extends StatelessWidget {
ConfigWrapper({Key key, this.config, this.child});
@override
Widget build(BuildContext context) {
return new _InheritedWidget(data: this.config, child: this.child);
}
static EnvConfig of(BuildContext context) {
final _InheritedWidget inheritedConfig =
context.dependOnInheritedWidgetOfExactType<_InheritedWidget>();
return inheritedConfig.data;
}
final EnvConfig config;
final Widget child;
}
app
入口 MyApp/MyHomePage
独立出来ConfigWrapper.of(context)...
方法ConfigWrapper.of(context).incrementCounter
回调class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
visualDensity: VisualDensity.adaptivePlatformDensity,
),
home: MyHomePage(),
);
}
}
class MyHomePage extends StatefulWidget {
MyHomePage({Key key}) : super(key: key);
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Flutter Demo Home Page'),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
_TitleText(),
_CountText()
],
),
),
floatingActionButton: _FloatBtn()
);
}
}
class _TitleText extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Text(
'You have pushed the button this many times:',
);
}
}
class _CountText extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Text(
'${ConfigWrapper.of(context).widget.config.count}',
style: Theme.of(context).textTheme.headline4,
);
}
}
class _FloatBtn extends StatelessWidget {
@override
Widget build(BuildContext context) {
return FloatingActionButton(
onPressed: ConfigWrapper.of(context).incrementCounter,
tooltip: 'Increment',
child: Icon(Icons.add),
); // This trailing comma makes auto-formatting nicer for build methods.
}
}
main
修改为:void main() {
runApp(ConfigWrapper(
config: EnvConfig(),
child: MyApp())
);
}
Text
和悬浮按钮发生了更新InheritedWidget
的较复杂使用,如果不需要刷新数据会容易更多如果对
InheritedWidget
还有疑问的可以参考: 1、InheritedWidget使用说明 2、数据共享(InheritedWidget)