Commit d748186c authored by Ian Hickson's avatar Ian Hickson

Use callbacks for sending messages up the app

Using .of() to call mutating setters is not great practice since it ties

parts of the app together in ways that prevent reuse and composition.



Since people are now using the gallery app to copy from, let's use the

better practice of using callbacks.
parent 08bb332c
...@@ -3,34 +3,32 @@ ...@@ -3,34 +3,32 @@
// found in the LICENSE file. // found in the LICENSE file.
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:flutter/scheduler.dart' show timeDilation;
import 'home.dart'; import 'home.dart';
class GalleryApp extends StatefulWidget { class GalleryApp extends StatefulWidget {
GalleryApp({ Key key }) : super(key: key); GalleryApp({ Key key }) : super(key: key);
static GalleryAppState of(BuildContext context) => context.ancestorStateOfType(const TypeMatcher<GalleryAppState>());
@override @override
GalleryAppState createState() => new GalleryAppState(); GalleryAppState createState() => new GalleryAppState();
} }
class GalleryAppState extends State<GalleryApp> { class GalleryAppState extends State<GalleryApp> {
bool _lightTheme = true; bool _useLightTheme = true;
bool get lightTheme => _lightTheme;
void set lightTheme(bool value) {
setState(() {
_lightTheme = value;
});
}
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
return new MaterialApp( return new MaterialApp(
title: 'Flutter Material Gallery', title: 'Flutter Material Gallery',
theme: lightTheme ? _kGalleryLightTheme : _kGalleryDarkTheme, theme: _useLightTheme ? _kGalleryLightTheme : _kGalleryDarkTheme,
routes: { routes: {
'/': (BuildContext context) => new GalleryHome() '/': (BuildContext context) => new GalleryHome(
theme: _useLightTheme,
onThemeChanged: (bool value) { setState(() { _useLightTheme = value; }); },
timeDilation: timeDilation,
onTimeDilationChanged: (double value) { setState(() { timeDilation = value; }); }
)
} }
); );
} }
......
...@@ -3,23 +3,24 @@ ...@@ -3,23 +3,24 @@
// found in the LICENSE file. // found in the LICENSE file.
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:flutter/scheduler.dart' show timeDilation;
import 'package:flutter/widgets.dart';
import 'app.dart';
class GalleryDrawer extends StatelessWidget { class GalleryDrawer extends StatelessWidget {
GalleryDrawer({ Key key }) : super(key: key); GalleryDrawer({
Key key,
void _changeTheme(BuildContext context, bool value) { this.theme,
GalleryApp.of(context).lightTheme = value; this.onThemeChanged,
this.timeDilation,
this.onTimeDilationChanged
}) : super(key: key) {
assert(onThemeChanged != null);
assert(onTimeDilationChanged != null);
} }
void _toggleAnimationSpeed(BuildContext context) { final bool theme;
GalleryApp.of(context).setState(() { final ValueChanged<bool> onThemeChanged;
timeDilation = (timeDilation != 1.0) ? 1.0 : 5.0;
}); final double timeDilation;
} final ValueChanged<double> onTimeDilationChanged;
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
...@@ -29,30 +30,30 @@ class GalleryDrawer extends StatelessWidget { ...@@ -29,30 +30,30 @@ class GalleryDrawer extends StatelessWidget {
new DrawerHeader(child: new Text('Flutter Gallery')), new DrawerHeader(child: new Text('Flutter Gallery')),
new DrawerItem( new DrawerItem(
icon: Icons.brightness_5, icon: Icons.brightness_5,
onPressed: () { _changeTheme(context, true); }, onPressed: () { onThemeChanged(true); },
selected: GalleryApp.of(context).lightTheme, selected: theme,
child: new Row( child: new Row(
children: <Widget>[ children: <Widget>[
new Flexible(child: new Text('Light')), new Flexible(child: new Text('Light')),
new Radio<bool>( new Radio<bool>(
value: true, value: true,
groupValue: GalleryApp.of(context).lightTheme, groupValue: theme,
onChanged: (bool value) { _changeTheme(context, value); } onChanged: onThemeChanged
) )
] ]
) )
), ),
new DrawerItem( new DrawerItem(
icon: Icons.brightness_7, icon: Icons.brightness_7,
onPressed: () { _changeTheme(context, false); }, onPressed: () { onThemeChanged(false); },
selected: !GalleryApp.of(context).lightTheme, selected: theme,
child: new Row( child: new Row(
children: <Widget>[ children: <Widget>[
new Flexible(child: new Text('Dark')), new Flexible(child: new Text('Dark')),
new Radio<bool>( new Radio<bool>(
value: false, value: false,
groupValue: GalleryApp.of(context).lightTheme, groupValue: theme,
onChanged: (bool value) { _changeTheme(context, value); } onChanged: onThemeChanged
) )
] ]
) )
...@@ -61,13 +62,13 @@ class GalleryDrawer extends StatelessWidget { ...@@ -61,13 +62,13 @@ class GalleryDrawer extends StatelessWidget {
new DrawerItem( new DrawerItem(
icon: Icons.hourglass_empty, icon: Icons.hourglass_empty,
selected: timeDilation != 1.0, selected: timeDilation != 1.0,
onPressed: () { _toggleAnimationSpeed(context); }, onPressed: () { onTimeDilationChanged(timeDilation != 1.0 ? 1.0 : 20.0); },
child: new Row( child: new Row(
children: <Widget>[ children: <Widget>[
new Flexible(child: new Text('Animate Slowly')), new Flexible(child: new Text('Animate Slowly')),
new Checkbox( new Checkbox(
value: timeDilation != 1.0, value: timeDilation != 1.0,
onChanged: (bool value) { _toggleAnimationSpeed(context); } onChanged: (bool value) { onTimeDilationChanged(value ? 20.0 : 1.0); }
) )
] ]
) )
......
...@@ -44,7 +44,22 @@ import '../demo/weather_demo.dart'; ...@@ -44,7 +44,22 @@ import '../demo/weather_demo.dart';
const double _kFlexibleSpaceMaxHeight = 256.0; const double _kFlexibleSpaceMaxHeight = 256.0;
class GalleryHome extends StatefulWidget { class GalleryHome extends StatefulWidget {
GalleryHome({ Key key }) : super(key: key); GalleryHome({
Key key,
this.theme,
this.onThemeChanged,
this.timeDilation,
this.onTimeDilationChanged
}) : super(key: key) {
assert(onThemeChanged != null);
assert(onTimeDilationChanged != null);
}
final bool theme;
final ValueChanged<bool> onThemeChanged;
final double timeDilation;
final ValueChanged<double> onTimeDilationChanged;
@override @override
GalleryHomeState createState() => new GalleryHomeState(); GalleryHomeState createState() => new GalleryHomeState();
...@@ -60,7 +75,12 @@ class GalleryHomeState extends State<GalleryHome> { ...@@ -60,7 +75,12 @@ class GalleryHomeState extends State<GalleryHome> {
return new Scaffold( return new Scaffold(
key: _homeKey, key: _homeKey,
drawer: new GalleryDrawer(), drawer: new GalleryDrawer(
theme: config.theme,
onThemeChanged: config.onThemeChanged,
timeDilation: config.timeDilation,
onTimeDilationChanged: config.onTimeDilationChanged
),
appBar: new AppBar( appBar: new AppBar(
expandedHeight: _kFlexibleSpaceMaxHeight, expandedHeight: _kFlexibleSpaceMaxHeight,
flexibleSpace: (BuildContext context) => new FlexibleSpaceBar( flexibleSpace: (BuildContext context) => new FlexibleSpaceBar(
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment