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(