Skip to content

Instantly share code, notes, and snippets.

@benznest
Created February 7, 2019 02:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save benznest/d5debe790c5e65b7467f80debb4b8373 to your computer and use it in GitHub Desktop.
Save benznest/d5debe790c5e65b7467f80debb4b8373 to your computer and use it in GitHub Desktop.
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.');
}
}
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;
}
}
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