Skip to content

Instantly share code, notes, and snippets.

@letsar
Created January 10, 2018 19:26
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save letsar/77307e6699d38643da230da15b0f95cf to your computer and use it in GitHub Desktop.
Save letsar/77307e6699d38643da230da15b0f95cf to your computer and use it in GitHub Desktop.
Flutter InheritedWidget test
import 'dart:math';
import 'package:flutter/material.dart';
import 'package:meta/meta.dart';
void main() {
runApp(new AppContainer());
}
class AppContextData {
final String appName;
final int value;
final ThemeData themeData;
const AppContextData({this.appName, this.value, this.themeData});
AppContextData withName(String newAppName) {
return new AppContextData(
appName: newAppName, value: value, themeData: themeData);
}
AppContextData increment() {
return new AppContextData(
appName: appName, value: value + 1, themeData: themeData);
}
AppContextData withTheme(ThemeData themeData) {
return new AppContextData(
appName: appName, value: value, themeData: themeData);
}
}
class AppContext extends InheritedWidget {
final AppContextData appContextData;
final Function(String title) updateTitle;
final Function() increment;
final Function() changeTheme;
AppContext({
Key key,
@required this.appContextData,
@required this.updateTitle,
@required this.increment,
@required this.changeTheme,
@required Widget child,
})
: super(key: key, child: child);
static AppContext of(BuildContext context) {
return context.inheritFromWidgetOfExactType(AppContext);
}
@override
bool updateShouldNotify(AppContext old) =>
appContextData != old.appContextData;
}
class AppContainer extends StatefulWidget {
@override
_AppContainerState createState() => new _AppContainerState();
}
class _AppContainerState extends State<AppContainer> {
AppContextData appContextData;
@override
void initState() {
appContextData = new AppContextData(
appName: "mon titre",
value: 0,
themeData: new ThemeData(
primarySwatch: Colors.blue,
));
super.initState();
}
@override
Widget build(BuildContext context) {
return new AppContext(
appContextData: appContextData,
updateTitle: _updateTitle,
increment: _increment,
changeTheme: _changeTheme,
child: new MyApp());
}
void _updateTitle(String title) {
setState(() {
appContextData = appContextData.withName(title);
});
}
void _increment() {
setState(() {
appContextData = appContextData.increment();
});
}
void _changeTheme() {
setState(() {
Random rnd = new Random();
var index = rnd.nextInt(Colors.primaries.length);
appContextData = appContextData.withTheme(new ThemeData(
primarySwatch: Colors.primaries[index],
));
});
}
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
final appContext = AppContext.of(context);
final appContextData = appContext.appContextData;
return new MaterialApp(
title: 'Flutter Demo',
theme: appContextData.themeData,
home: new MyHomePage(),
);
}
}
class MyHomePage extends StatelessWidget {
@override
Widget build(BuildContext context) {
final appContext = AppContext.of(context);
final appContextData = appContext.appContextData;
return new Scaffold(
appBar: new AppBar(
title: new Text(appContextData.appName),
),
body: new Center(
child: new Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
new Text(
'You have pushed the button this many times:',
),
new Text(
'${appContextData.value}',
style: Theme.of(context).textTheme.display1,
),
new Text("Set new title below:"),
new TextField(onChanged: appContext.updateTitle),
new RaisedButton(
onPressed: appContext.changeTheme,
child: new Text("change theme"),
)
],
),
),
floatingActionButton: new FloatingActionButton(
onPressed: appContext.increment,
tooltip: 'Increment',
child: new Icon(Icons.add),
),
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment