Created
February 7, 2019 02:22
-
-
Save benznest/d5debe790c5e65b7467f80debb4b8373 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'package:flutter/material.dart'; | |
import 'package:flutter_hello/my_inherited_widget_data.dart'; | |
void main() => runApp(MyApp()); | |
class MyApp extends StatelessWidget { | |
@override | |
Widget build(BuildContext context) { | |
return MaterialApp( | |
title: 'Flutter Demo', | |
theme: ThemeData( | |
primarySwatch: Colors.blue, | |
), | |
home: HomePage(), | |
); | |
} | |
} | |
class HomePage extends StatefulWidget { | |
@override | |
_HomePageState createState() => new _HomePageState(); | |
} | |
class _HomePageState extends State<HomePage> { | |
@override | |
Widget build(BuildContext context) { | |
return MyInheritedWidgetData( | |
child: Scaffold( | |
appBar: AppBar( | |
title: Text('My App'), | |
), | |
body: Column( | |
children: <Widget>[ | |
FlatButton( | |
child: Text('rebuild all'), | |
color: Colors.red, | |
textColor: Colors.white, | |
onPressed: () { | |
setState(() {}); | |
}, | |
), | |
WidgetA(), | |
Container( | |
child: Column( | |
children: <Widget>[ | |
WidgetB(), | |
WidgetC(), | |
], | |
), | |
), | |
], | |
), | |
), | |
); | |
} | |
} | |
class WidgetA extends StatelessWidget { | |
@override | |
Widget build(BuildContext context) { | |
print("build Widget A"); | |
final MyInheritedWidgetDataState state = MyInheritedWidgetData.of(context, false); | |
return Container( | |
child: FlatButton( | |
child: Text('Commit'), | |
color: Colors.orange, | |
textColor: Colors.white, | |
onPressed: () { | |
state.setData("Second commit"); | |
}, | |
), | |
); | |
} | |
} | |
class WidgetB extends StatelessWidget { | |
@override | |
Widget build(BuildContext context) { | |
print("build Widget B"); | |
final MyInheritedWidgetDataState widget = MyInheritedWidgetData.of(context); | |
return Text('${widget.data}'); | |
} | |
} | |
class WidgetC extends StatelessWidget { | |
@override | |
Widget build(BuildContext context) { | |
print("build Widget C"); | |
return Text('I am robot.'); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'package:flutter/material.dart'; | |
import 'package:flutter_hello/my_inherited_widget_data.dart'; | |
class MyInheritedWidget extends InheritedWidget { | |
final Widget child; | |
final MyInheritedWidgetDataState state; | |
MyInheritedWidget({Key key, @required this.child, @required this.state}) | |
: super(key: key, child: child); | |
@override | |
bool updateShouldNotify(MyInheritedWidget oldWidget) { | |
return true; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'package:flutter/material.dart'; | |
import 'package:flutter_hello/my_inherited_widget.dart'; | |
class MyInheritedWidgetData extends StatefulWidget { | |
final Widget child; | |
MyInheritedWidgetData({@required this.child}); | |
@override | |
MyInheritedWidgetDataState createState() => MyInheritedWidgetDataState(); | |
// | |
// static MyInheritedWidgetDataState of(BuildContext context) { | |
// return (context.inheritFromWidgetOfExactType(MyInheritedWidget) | |
// as MyInheritedWidget) | |
// .state; | |
// } | |
static MyInheritedWidgetDataState of([BuildContext context, bool rebuild = true]){ | |
return (rebuild ? context.inheritFromWidgetOfExactType(MyInheritedWidget) as MyInheritedWidget | |
: context.ancestorWidgetOfExactType(MyInheritedWidget) as MyInheritedWidget).state; | |
} | |
} | |
class MyInheritedWidgetDataState extends State<MyInheritedWidgetData> { | |
String data; | |
@override | |
void initState() { | |
data = "First commit."; | |
super.initState(); | |
} | |
setData(String newData) { | |
setState(() { | |
data = newData; | |
}); | |
} | |
@override | |
Widget build(BuildContext context) { | |
return MyInheritedWidget(child: widget.child, state: this); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment