From d748186c51e1c5f017951f7e0797041f272c0592 Mon Sep 17 00:00:00 2001 From: Ian Hickson Date: Mon, 4 Apr 2016 15:25:06 -0700 Subject: [PATCH] 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. --- .../material_gallery/lib/gallery/app.dart | 20 ++++---- .../material_gallery/lib/gallery/drawer.dart | 47 ++++++++++--------- .../material_gallery/lib/gallery/home.dart | 24 +++++++++- 3 files changed, 55 insertions(+), 36 deletions(-) diff --git a/examples/material_gallery/lib/gallery/app.dart b/examples/material_gallery/lib/gallery/app.dart index f8a0c42fb5..8aac9dfbbe 100644 --- a/examples/material_gallery/lib/gallery/app.dart +++ b/examples/material_gallery/lib/gallery/app.dart @@ -3,34 +3,32 @@ // found in the LICENSE file. import 'package:flutter/material.dart'; +import 'package:flutter/scheduler.dart' show timeDilation; import 'home.dart'; class GalleryApp extends StatefulWidget { GalleryApp({ Key key }) : super(key: key); - static GalleryAppState of(BuildContext context) => context.ancestorStateOfType(const TypeMatcher()); - @override GalleryAppState createState() => new GalleryAppState(); } class GalleryAppState extends State { - bool _lightTheme = true; - bool get lightTheme => _lightTheme; - void set lightTheme(bool value) { - setState(() { - _lightTheme = value; - }); - } + bool _useLightTheme = true; @override Widget build(BuildContext context) { return new MaterialApp( title: 'Flutter Material Gallery', - theme: lightTheme ? _kGalleryLightTheme : _kGalleryDarkTheme, + theme: _useLightTheme ? _kGalleryLightTheme : _kGalleryDarkTheme, 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; }); } + ) } ); } diff --git a/examples/material_gallery/lib/gallery/drawer.dart b/examples/material_gallery/lib/gallery/drawer.dart index 9e484a4c3a..4dc6f24dd1 100644 --- a/examples/material_gallery/lib/gallery/drawer.dart +++ b/examples/material_gallery/lib/gallery/drawer.dart @@ -3,23 +3,24 @@ // found in the LICENSE file. import 'package:flutter/material.dart'; -import 'package:flutter/scheduler.dart' show timeDilation; -import 'package:flutter/widgets.dart'; - -import 'app.dart'; class GalleryDrawer extends StatelessWidget { - GalleryDrawer({ Key key }) : super(key: key); - - void _changeTheme(BuildContext context, bool value) { - GalleryApp.of(context).lightTheme = value; + GalleryDrawer({ + Key key, + this.theme, + this.onThemeChanged, + this.timeDilation, + this.onTimeDilationChanged + }) : super(key: key) { + assert(onThemeChanged != null); + assert(onTimeDilationChanged != null); } - void _toggleAnimationSpeed(BuildContext context) { - GalleryApp.of(context).setState(() { - timeDilation = (timeDilation != 1.0) ? 1.0 : 5.0; - }); - } + final bool theme; + final ValueChanged onThemeChanged; + + final double timeDilation; + final ValueChanged onTimeDilationChanged; @override Widget build(BuildContext context) { @@ -29,30 +30,30 @@ class GalleryDrawer extends StatelessWidget { new DrawerHeader(child: new Text('Flutter Gallery')), new DrawerItem( icon: Icons.brightness_5, - onPressed: () { _changeTheme(context, true); }, - selected: GalleryApp.of(context).lightTheme, + onPressed: () { onThemeChanged(true); }, + selected: theme, child: new Row( children: [ new Flexible(child: new Text('Light')), new Radio( value: true, - groupValue: GalleryApp.of(context).lightTheme, - onChanged: (bool value) { _changeTheme(context, value); } + groupValue: theme, + onChanged: onThemeChanged ) ] ) ), new DrawerItem( icon: Icons.brightness_7, - onPressed: () { _changeTheme(context, false); }, - selected: !GalleryApp.of(context).lightTheme, + onPressed: () { onThemeChanged(false); }, + selected: theme, child: new Row( children: [ new Flexible(child: new Text('Dark')), new Radio( value: false, - groupValue: GalleryApp.of(context).lightTheme, - onChanged: (bool value) { _changeTheme(context, value); } + groupValue: theme, + onChanged: onThemeChanged ) ] ) @@ -61,13 +62,13 @@ class GalleryDrawer extends StatelessWidget { new DrawerItem( icon: Icons.hourglass_empty, selected: timeDilation != 1.0, - onPressed: () { _toggleAnimationSpeed(context); }, + onPressed: () { onTimeDilationChanged(timeDilation != 1.0 ? 1.0 : 20.0); }, child: new Row( children: [ new Flexible(child: new Text('Animate Slowly')), new Checkbox( value: timeDilation != 1.0, - onChanged: (bool value) { _toggleAnimationSpeed(context); } + onChanged: (bool value) { onTimeDilationChanged(value ? 20.0 : 1.0); } ) ] ) diff --git a/examples/material_gallery/lib/gallery/home.dart b/examples/material_gallery/lib/gallery/home.dart index 5d2fff2588..6e66b655ee 100644 --- a/examples/material_gallery/lib/gallery/home.dart +++ b/examples/material_gallery/lib/gallery/home.dart @@ -44,7 +44,22 @@ import '../demo/weather_demo.dart'; const double _kFlexibleSpaceMaxHeight = 256.0; 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 onThemeChanged; + + final double timeDilation; + final ValueChanged onTimeDilationChanged; @override GalleryHomeState createState() => new GalleryHomeState(); @@ -60,7 +75,12 @@ class GalleryHomeState extends State { return new Scaffold( key: _homeKey, - drawer: new GalleryDrawer(), + drawer: new GalleryDrawer( + theme: config.theme, + onThemeChanged: config.onThemeChanged, + timeDilation: config.timeDilation, + onTimeDilationChanged: config.onTimeDilationChanged + ), appBar: new AppBar( expandedHeight: _kFlexibleSpaceMaxHeight, flexibleSpace: (BuildContext context) => new FlexibleSpaceBar(