From 9536c418fc82da1b8f73e49f1e0cb430ca6588be Mon Sep 17 00:00:00 2001 From: Hans Muller Date: Wed, 9 Mar 2016 12:45:47 -0800 Subject: [PATCH] Scaffold saves and restores scrollable state, etc --- .../lib/demo/dialog_demo.dart | 232 ++++++++---------- .../lib/demo/page_selector_demo.dart | 31 +-- .../material_gallery/lib/demo/tabs_demo.dart | 11 +- .../flutter/lib/src/material/scaffold.dart | 7 + packages/flutter/lib/src/material/tabs.dart | 2 +- 5 files changed, 138 insertions(+), 145 deletions(-) diff --git a/examples/material_gallery/lib/demo/dialog_demo.dart b/examples/material_gallery/lib/demo/dialog_demo.dart index 79fec7c692..4c00efd0c3 100644 --- a/examples/material_gallery/lib/demo/dialog_demo.dart +++ b/examples/material_gallery/lib/demo/dialog_demo.dart @@ -14,11 +14,6 @@ enum DialogDemoAction { agree, } -const String _introText = - "Use dialogs sparingly because their sudden appearance forces users to stop their " - "current task and focus on the dialog's content. Alternatives to dialogs include " - "menus or inline expansion, both of which maintain the current context."; - const String _alertWithoutTitleText = "Discard draft?"; const String _alertWithTitleText = @@ -88,134 +83,115 @@ class DialogDemoState extends State { toolBar: new ToolBar( center: new Text('Dialogs') ), - body: new ButtonTheme( - color: ButtonColor.accent, - child: new Padding( - padding: const EdgeDims.all(24.0), - child: new ScrollableViewport( - child: new Column( - alignItems: FlexAlignItems.stretch, - children: [ - new Container( - child: new Text( - _introText, + body: new Block( + padding: const EdgeDims.symmetric(vertical: 24.0, horizontal: 72.0), + children: [ + new RaisedButton( + child: new Text('ALERT'), + onPressed: () { + showDemoDialog( + context: context, + dialog: new Dialog( + content: new Text( + _alertWithoutTitleText, style: dialogTextStyle ), - padding: const EdgeDims.only(top: 8.0, bottom: 24.0), - margin: const EdgeDims.only(bottom:16.0), - decoration: new BoxDecoration( - border: new Border(bottom: new BorderSide(color: theme.dividerColor)) - ) - ), - new FlatButton( - child: new Text('Alert without a title'), - onPressed: () { - showDemoDialog( - context: context, - dialog: new Dialog( - content: new Text( - _alertWithoutTitleText, - style: dialogTextStyle - ), - actions: [ - new FlatButton( - child: new Text('CANCEL'), - onPressed: () { Navigator.pop(context, DialogDemoAction.cancel); } - ), - new FlatButton( - child: new Text('DISCARD'), - onPressed: () { Navigator.pop(context, DialogDemoAction.discard); } - ) - ] - ) - ); - } - ), - new FlatButton( - child: new Text('Alert with a title'), - onPressed: () { - showDemoDialog( - context: context, - dialog: new Dialog( - title: new Text("Use Google's location service?"), - content: new Text( - _alertWithTitleText, - style: dialogTextStyle - ), - actions: [ - new FlatButton( - child: new Text('DISAGREE'), - onPressed: () { Navigator.pop(context, DialogDemoAction.disagree); } - ), - new FlatButton( - child: new Text('AGREE'), - onPressed: () { Navigator.pop(context, DialogDemoAction.agree); } - ) - ] - ) - ); - } - ), - new FlatButton( - child: new Text('Simple Dialog'), - onPressed: () { - showDemoDialog( - context: context, - dialog: new Dialog( - title: new Text('Set backup account'), - content: new Column( - children: [ - new DialogDemoItem( - icon: Icons.account_circle, - color: theme.primaryColor, - text: 'username@gmail.com', - onPressed: () { Navigator.pop(context, 'username@gmail.com'); } - ), - new DialogDemoItem( - icon: Icons.account_circle, - color: theme.primaryColor, - text: 'user02@gmail.com', - onPressed: () { Navigator.pop(context, 'user02@gmail.com'); } - ), - new DialogDemoItem( - icon: Icons.add_circle, - text: 'add account', - color: theme.disabledColor - ) - ] - ) - ) - ); - } - ), - new FlatButton( - child: new Text('Confirmation Dialog'), - onPressed: () { - showTimePicker( - context: context, - initialTime: const TimeOfDay(hour: 15, minute: 30) + actions: [ + new FlatButton( + child: new Text('CANCEL'), + onPressed: () { Navigator.pop(context, DialogDemoAction.cancel); } + ), + new FlatButton( + child: new Text('DISCARD'), + onPressed: () { Navigator.pop(context, DialogDemoAction.discard); } ) - .then((value) { // The value passed to Navigator.pop() or null. - if (value != null) { - scaffoldKey.currentState.showSnackBar(new SnackBar( - content: new Text('You selected: $value') - )); - } - }); - } - ), - new FlatButton( - child: new Text('Fullscreen Dialog'), - onPressed: () { - Navigator.push(context, new MaterialPageRoute( - builder: (BuildContext context) => new FullScreenDialogDemo() - )); - } + ] ) - ] - ) + ); + } + ), + new RaisedButton( + child: new Text('ALERT WITH TITLE'), + onPressed: () { + showDemoDialog( + context: context, + dialog: new Dialog( + title: new Text("Use Google's location service?"), + content: new Text( + _alertWithTitleText, + style: dialogTextStyle + ), + actions: [ + new FlatButton( + child: new Text('DISAGREE'), + onPressed: () { Navigator.pop(context, DialogDemoAction.disagree); } + ), + new FlatButton( + child: new Text('AGREE'), + onPressed: () { Navigator.pop(context, DialogDemoAction.agree); } + ) + ] + ) + ); + } + ), + new RaisedButton( + child: new Text('SIMPLE'), + onPressed: () { + showDemoDialog( + context: context, + dialog: new Dialog( + title: new Text('Set backup account'), + content: new Column( + children: [ + new DialogDemoItem( + icon: Icons.account_circle, + color: theme.primaryColor, + text: 'username@gmail.com', + onPressed: () { Navigator.pop(context, 'username@gmail.com'); } + ), + new DialogDemoItem( + icon: Icons.account_circle, + color: theme.primaryColor, + text: 'user02@gmail.com', + onPressed: () { Navigator.pop(context, 'user02@gmail.com'); } + ), + new DialogDemoItem( + icon: Icons.add_circle, + text: 'add account', + color: theme.disabledColor + ) + ] + ) + ) + ); + } + ), + new RaisedButton( + child: new Text('CONFIRMATION'), + onPressed: () { + showTimePicker( + context: context, + initialTime: const TimeOfDay(hour: 15, minute: 30) + ) + .then((value) { // The value passed to Navigator.pop() or null. + if (value != null) { + scaffoldKey.currentState.showSnackBar(new SnackBar( + content: new Text('You selected: $value') + )); + } + }); + } + ), + new RaisedButton( + child: new Text('FULLSCREEN'), + onPressed: () { + Navigator.push(context, new MaterialPageRoute( + builder: (BuildContext context) => new FullScreenDialogDemo() + )); + } ) - ) + ] ) ); } diff --git a/examples/material_gallery/lib/demo/page_selector_demo.dart b/examples/material_gallery/lib/demo/page_selector_demo.dart index 160d138019..3d8dcebbb9 100644 --- a/examples/material_gallery/lib/demo/page_selector_demo.dart +++ b/examples/material_gallery/lib/demo/page_selector_demo.dart @@ -5,17 +5,6 @@ import 'package:flutter/material.dart'; class PageSelectorDemo extends StatelessComponent { - Widget _buildTabView(IconData icon) { - return new Container( - key: new ObjectKey(icon), - padding: const EdgeDims.all(12.0), - child: new Card( - child: new Center( - child: new Icon(icon: icon, size: 48.0) - ) - ) - ); - } void _handleArrowButtonPress(BuildContext context, int delta) { final TabBarSelectionState selection = TabBarSelection.of(context); @@ -35,10 +24,11 @@ class PageSelectorDemo extends StatelessComponent { return new Scaffold( toolBar: new ToolBar(center: new Text('Page Selector')), - body: new TabBarSelection( + body: new TabBarSelection( values: icons, child: new Builder( builder: (BuildContext context) { + final Color color = Theme.of(context).accentColor; return new Column( children: [ new Container( @@ -47,12 +37,14 @@ class PageSelectorDemo extends StatelessComponent { children: [ new IconButton( icon: Icons.arrow_back, + color: color, onPressed: () { _handleArrowButtonPress(context, -1); }, tooltip: 'Back' ), - new TabPageSelector(), + new TabPageSelector(), new IconButton( icon: Icons.arrow_forward, + color: color, onPressed: () { _handleArrowButtonPress(context, 1); }, tooltip: 'Forward' ) @@ -62,7 +54,18 @@ class PageSelectorDemo extends StatelessComponent { ), new Flexible( child: new TabBarView( - children: icons.map(_buildTabView).toList() + children: icons.map((IconData icon) { + return new Container( + key: new ObjectKey(icon), + padding: const EdgeDims.all(12.0), + child: new Card( + child: new Center( + child: new Icon(icon: icon, size: 128.0, color: color) + ) + ) + ); + }) + .toList() ) ) ] diff --git a/examples/material_gallery/lib/demo/tabs_demo.dart b/examples/material_gallery/lib/demo/tabs_demo.dart index efcd9214df..7301455636 100644 --- a/examples/material_gallery/lib/demo/tabs_demo.dart +++ b/examples/material_gallery/lib/demo/tabs_demo.dart @@ -23,7 +23,8 @@ class TabsDemo extends StatelessComponent { Icons.language: 'LANGUAGE', }; - Widget build(_) { + Widget build(BuildContext context) { + final Color iconColor = Theme.of(context).accentColor; return new TabBarSelection( values: icons, child: new Scaffold( @@ -43,7 +44,13 @@ class TabsDemo extends StatelessComponent { key: new ObjectKey(icon), padding: const EdgeDims.all(12.0), child: new Card( - child: new Center(child: new Icon(icon: icon, size: 48.0)) + child: new Center( + child: new Icon( + icon: icon, + color: iconColor, + size: 128.0 + ) + ) ) ); }).toList() diff --git a/packages/flutter/lib/src/material/scaffold.dart b/packages/flutter/lib/src/material/scaffold.dart index df98fa2e74..3ad927d776 100644 --- a/packages/flutter/lib/src/material/scaffold.dart +++ b/packages/flutter/lib/src/material/scaffold.dart @@ -345,6 +345,12 @@ class ScaffoldState extends State { void initState() { super.initState(); _appBarController = new AnimationController(); + List scrollValues = PageStorage.of(context)?.readState(context); + if (scrollValues != null) { + assert(scrollValues.length == 2); + _scrollOffset = scrollValues[0]; + _scrollOffsetDelta = scrollValues[1]; + } } void dispose() { @@ -353,6 +359,7 @@ class ScaffoldState extends State { _snackBarController = null; _snackBarTimer?.cancel(); _snackBarTimer = null; + PageStorage.of(context)?.writeState(context, [_scrollOffset, _scrollOffsetDelta]); super.dispose(); } diff --git a/packages/flutter/lib/src/material/tabs.dart b/packages/flutter/lib/src/material/tabs.dart index 0c216fd45d..1e4760bbba 100644 --- a/packages/flutter/lib/src/material/tabs.dart +++ b/packages/flutter/lib/src/material/tabs.dart @@ -999,7 +999,7 @@ class TabPageSelector extends StatelessComponent { Widget build(BuildContext context) { final TabBarSelectionState selection = TabBarSelection.of(context); - final Color color = Theme.of(context).primaryColor; + final Color color = Theme.of(context).accentColor; final ColorTween selectedColor = new ColorTween(begin: Colors.transparent, end: color); final ColorTween previousColor = new ColorTween(begin: color, end: Colors.transparent); Animation animation = new CurvedAnimation(parent: selection.animation, curve: Curves.ease);