Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
一个使用InheritedWidget的建议范例
import 'package:flutter/material.dart';
void main() {
runApp(MaterialApp(
home: Scaffold(
body: MyWidget(
Column(
children: <Widget>[
WidgetA(),
WidgetB()
],
)
),
),
));
}
class Wrapper extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MyWidget(
Column(
children: <Widget>[
WidgetA(),
WidgetB()
],
)
);
}
}
class MyWidget extends StatefulWidget {
final Widget child;
MyWidget(this.child);
@override
State<StatefulWidget> createState() {
return MyState();
}
}
class MyState extends State<MyWidget> {
int tmpData = 0;
@override
Widget build(BuildContext context) {
print('MyWidget build');
return MyInherited(
data: tmpData,
child: Container(
child: Column(
children: <Widget>[
widget.child,
FlatButton(
child: Text("Click me"),
onPressed: () {
setState(() {
print('onPressed');
tmpData += 1;
});
},
),
],
),
),
);
}
}
class MyInherited extends InheritedWidget {
final int data;
MyInherited({this.data, Widget child}) : super(child : child) {
print('MyInherited construct');
}
@override
bool updateShouldNotify(MyInherited oldWidget) {
bool result = oldWidget.data != this.data;
print('MyInherited updateShouldNotify result = $result');
return result;
}
static MyInherited of(BuildContext context) {
return context.inheritFromWidgetOfExactType(MyInherited);
}
}
class WidgetA extends StatelessWidget {
@override
Widget build(BuildContext context) {
print('WidgetA build');
int data = MyInherited.of(context).data;
return Text('WidgetA data = $data');
}
}
class WidgetB extends StatelessWidget {
@override
Widget build(BuildContext context) {
print('WidgetB build');
return Text('WidgetB');
}
}
@chenyucheng97

This comment has been minimized.

Copy link

commented Sep 15, 2019

这个代码运行不起来啊

@whataa

This comment has been minimized.

Copy link
Owner Author

commented Sep 15, 2019

疏忽了,在Scaffold外面再嵌套一层MaterialApp

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.