From c96201a3c900707f39abea0a39bb045ef57bdbb5 Mon Sep 17 00:00:00 2001 From: Hans Muller Date: Thu, 13 Apr 2017 13:04:51 -0700 Subject: [PATCH] Scaffold appBar is-a PreferredSizeWidget, etc (#9380) * Scaffold appBar is-a PreferredSizeWidget, etc * Updated * Updated per review feedback --- .../flutter/lib/src/material/app_bar.dart | 61 +++++++-------- .../flutter/lib/src/material/scaffold.dart | 36 +++++---- packages/flutter/lib/src/material/tabs.dart | 11 ++- .../lib/src/widgets/preferred_size.dart | 58 ++++++++++++++ packages/flutter/lib/widgets.dart | 1 + .../flutter/test/material/app_bar_test.dart | 75 +++++++++++++++++++ .../material/floating_action_button_test.dart | 2 +- packages/flutter/test/material/page_test.dart | 12 +-- .../flutter/test/material/scaffold_test.dart | 8 +- .../flutter/test/material/theme_test.dart | 2 +- 10 files changed, 201 insertions(+), 65 deletions(-) create mode 100644 packages/flutter/lib/src/widgets/preferred_size.dart diff --git a/packages/flutter/lib/src/material/app_bar.dart b/packages/flutter/lib/src/material/app_bar.dart index 0ccf004ba4..fd88271afa 100644 --- a/packages/flutter/lib/src/material/app_bar.dart +++ b/packages/flutter/lib/src/material/app_bar.dart @@ -24,16 +24,6 @@ import 'tabs.dart'; import 'theme.dart'; import 'typography.dart'; -/// An interface for widgets that can appear at the bottom of an [AppBar] or -/// [SliverAppBar]. -/// -/// This interface exposes the height of the widget, so that the [Scaffold] and -/// [SliverAppBar] widgets can correctly size an [AppBar]. -abstract class AppBarBottomWidget extends Widget { - /// Defines the height of the app bar's optional bottom widget. - double get bottomHeight; -} - enum _ToolbarSlot { leading, title, @@ -156,7 +146,7 @@ class _ToolbarContainerLayout extends SingleChildLayoutDelegate { /// * [FlexibleSpaceBar], which is used with [flexibleSpace] when the app bar /// can expand and collapse. /// * -class AppBar extends StatefulWidget { +class AppBar extends StatefulWidget implements PreferredSizeWidget { /// Creates a material design app bar. /// /// Typically used in the [Scaffold.appBar] property. @@ -176,7 +166,7 @@ class AppBar extends StatefulWidget { this.centerTitle, this.toolbarOpacity: 1.0, this.bottomOpacity: 1.0, - }) : _bottomHeight = bottom?.bottomHeight ?? 0.0, + }) : preferredSize = new Size.fromHeight(kToolbarHeight + (bottom?.preferredSize?.height ?? 0.0)), super(key: key) { assert(elevation != null); assert(primary != null); @@ -229,17 +219,20 @@ class AppBar extends StatefulWidget { /// /// A flexible space isn't actually flexible unless the [AppBar]'s container /// changes the [AppBar]'s size. A [SliverAppBar] in a [CustomScrollView] - /// changes the [AppBar]'s height when scrolled. A [Scaffold] always sets the - /// [AppBar] to the [minExtent]. + /// changes the [AppBar]'s height when scrolled. /// /// Typically a [FlexibleSpaceBar]. See [FlexibleSpaceBar] for details. final Widget flexibleSpace; /// This widget appears across the bottom of the app bar. /// - /// Typically a [TabBar]. Only widgets that implement [AppBarBottomWidget] can + /// Typically a [TabBar]. Only widgets that implement [PreferredSizeWidget] can /// be used at the bottom of an app bar. - final AppBarBottomWidget bottom; + /// + /// See also: + /// + /// * [PreferredSize], which can be used to give an arbitrary widget a preferred size. + final PreferredSizeWidget bottom; /// The z-coordinate at which to place this app bar. /// @@ -274,8 +267,9 @@ class AppBar extends StatefulWidget { /// Whether this app bar is being displayed at the top of the screen. /// - /// If this is true, the top padding specified by the [MediaQuery] will be - /// added to the top of the toolbar. See also [minExtent]. + /// If true, the appbar's toolbar elements and [bottom] widget will be + /// padded on top by the height of the system status bar. The layout + /// of the [flexibleSpace] is not affected by the [primary] property. final bool primary; /// Whether the title should be centered. @@ -301,17 +295,12 @@ class AppBar extends StatefulWidget { /// bar is scrolled. final double bottomOpacity; - final double _bottomHeight; - - /// The height of the toolbar and the [bottom] widget. + /// A size whose height is the sum of [kToolbarHeight] and the [bottom] widget's + /// preferred height. /// - /// The parent widget should constrain the [AppBar] to a height between this - /// and whatever maximum size it wants the [AppBar] to have. - /// - /// If [primary] is true, the parent should increase this height by the height - /// of the top padding specified by the [MediaQuery] in scope for the - /// [AppBar]. - double get minExtent => kToolbarHeight + _bottomHeight; + /// [Scaffold] uses this this size to set its app bar's height. + @override + final Size preferredSize; bool _getEffectiveCenterTitle(ThemeData themeData) { if (centerTitle != null) @@ -566,7 +555,7 @@ class _SliverAppBarDelegate extends SliverPersistentHeaderDelegate { @required this.floating, @required this.pinned, @required this.snapConfiguration, - }) : _bottomHeight = bottom?.bottomHeight ?? 0.0 { + }) : _bottomHeight = bottom?.preferredSize?.height ?? 0.0 { assert(primary || topPadding == 0.0); } @@ -574,7 +563,7 @@ class _SliverAppBarDelegate extends SliverPersistentHeaderDelegate { final Widget title; final List actions; final Widget flexibleSpace; - final AppBarBottomWidget bottom; + final PreferredSizeWidget bottom; final int elevation; final Color backgroundColor; final Brightness brightness; @@ -769,9 +758,13 @@ class SliverAppBar extends StatefulWidget { /// This widget appears across the bottom of the appbar. /// - /// Typically a [TabBar]. This widget must be a widget that implements the - /// [AppBarBottomWidget] interface. - final AppBarBottomWidget bottom; + /// Typically a [TabBar]. Only widgets that implement [PreferredSizeWidget] can + /// be used at the bottom of an app bar. + /// + /// See also: + /// + /// * [PreferredSize], which can be used to give an arbitrary widget a preferred size. + final PreferredSizeWidget bottom; /// The z-coordinate at which to place this app bar. /// @@ -901,7 +894,7 @@ class _SliverAppBarState extends State with TickerProviderStateMix Widget build(BuildContext context) { final double topPadding = widget.primary ? MediaQuery.of(context).padding.top : 0.0; final double collapsedHeight = (widget.pinned && widget.floating && widget.bottom != null) - ? widget.bottom.bottomHeight + topPadding : null; + ? widget.bottom.preferredSize.height + topPadding : null; return new SliverPersistentHeader( floating: widget.floating, diff --git a/packages/flutter/lib/src/material/scaffold.dart b/packages/flutter/lib/src/material/scaffold.dart index 86b91251e3..91d44c3bf5 100644 --- a/packages/flutter/lib/src/material/scaffold.dart +++ b/packages/flutter/lib/src/material/scaffold.dart @@ -283,7 +283,7 @@ class _FloatingActionButtonTransitionState extends State<_FloatingActionButtonTr /// * class Scaffold extends StatefulWidget { /// Creates a visual scaffold for material design widgets. - Scaffold({ + const Scaffold({ Key key, this.appBar, this.body, @@ -292,11 +292,12 @@ class Scaffold extends StatefulWidget { this.drawer, this.bottomNavigationBar, this.backgroundColor, - this.resizeToAvoidBottomPadding: true - }) : super(key: key); + this.resizeToAvoidBottomPadding: true, + this.primary: true, + }) : assert(primary != null), super(key: key); /// An app bar to display at the top of the scaffold. - final AppBar appBar; + final PreferredSizeWidget appBar; /// The primary content of the scaffold. /// @@ -363,6 +364,15 @@ class Scaffold extends StatefulWidget { /// Defaults to true. final bool resizeToAvoidBottomPadding; + /// Whether this scaffold is being displayed at the top of the screen. + /// + /// If true then the height of the [appBar] will be extended by the height + /// of the screen's status bar, i.e. the top padding for [MediaQuery]. + /// + /// The default value of this property, like the default value of + /// [AppBar.primary], is true. + final bool primary; + /// The state from the closest instance of this class that encloses the given context. /// /// Typical usage is as follows: @@ -782,21 +792,17 @@ class ScaffoldState extends State with TickerProviderStateMixin { _addIfNonNull(children, widget.body, _ScaffoldSlot.body); if (widget.appBar != null) { - assert(widget.appBar.primary || padding.top == 0.0, 'A non-primary AppBar was passed to a Scaffold but the MediaQuery in scope has top padding.'); - final double topPadding = widget.appBar.primary ? padding.top : 0.0; - Widget appBar = widget.appBar; - final double extent = widget.appBar.minExtent + topPadding; - if (widget.appBar.flexibleSpace != null) { - appBar = FlexibleSpaceBar.createSettings( - currentExtent: extent, - child: appBar, - ); - } + final double topPadding = widget.primary ? padding.top : 0.0; + final double extent = widget.appBar.preferredSize.height + topPadding; + assert(extent >= 0.0 && extent.isFinite); _addIfNonNull( children, new ConstrainedBox( constraints: new BoxConstraints(maxHeight: extent), - child: appBar, + child: FlexibleSpaceBar.createSettings( + currentExtent: extent, + child: widget.appBar, + ), ), _ScaffoldSlot.appBar, ); diff --git a/packages/flutter/lib/src/material/tabs.dart b/packages/flutter/lib/src/material/tabs.dart index 525db630bf..922f92d335 100644 --- a/packages/flutter/lib/src/material/tabs.dart +++ b/packages/flutter/lib/src/material/tabs.dart @@ -349,7 +349,7 @@ class _DragAnimation extends Animation with AnimationWithParentMixin child; +} diff --git a/packages/flutter/lib/widgets.dart b/packages/flutter/lib/widgets.dart index 2fe1aee917..6989e54990 100644 --- a/packages/flutter/lib/widgets.dart +++ b/packages/flutter/lib/widgets.dart @@ -44,6 +44,7 @@ export 'src/widgets/page_view.dart'; export 'src/widgets/pages.dart'; export 'src/widgets/performance_overlay.dart'; export 'src/widgets/placeholder.dart'; +export 'src/widgets/preferred_size.dart'; export 'src/widgets/primary_scroll_controller.dart'; export 'src/widgets/raw_keyboard_listener.dart'; export 'src/widgets/routes.dart'; diff --git a/packages/flutter/test/material/app_bar_test.dart b/packages/flutter/test/material/app_bar_test.dart index 53b2cbd7b8..75f8af577d 100644 --- a/packages/flutter/test/material/app_bar_test.dart +++ b/packages/flutter/test/material/app_bar_test.dart @@ -637,4 +637,79 @@ void main() { expect(appBarTop(tester), lessThanOrEqualTo(0.0)); expect(appBarBottom(tester), kTextTabBarHeight); }); + + testWidgets('AppBar dimensions, with and without bottom, primary', (WidgetTester tester) async { + const MediaQueryData topPadding100 = const MediaQueryData(padding: const EdgeInsets.only(top: 100.0)); + + await tester.pumpWidget( + new MediaQuery( + data: topPadding100, + child: new Scaffold( + primary: false, + appBar: new AppBar(), + ), + ), + ); + expect(appBarTop(tester), 0.0); + expect(appBarHeight(tester), kToolbarHeight); + + await tester.pumpWidget( + new MediaQuery( + data: topPadding100, + child: new Scaffold( + primary: true, + appBar: new AppBar(title: const Text('title')) + ), + ), + ); + expect(appBarTop(tester), 0.0); + expect(tester.getTopLeft(find.text('title')).dy, greaterThan(100.0)); + expect(appBarHeight(tester), kToolbarHeight + 100.0); + + await tester.pumpWidget( + new MediaQuery( + data: topPadding100, + child: new Scaffold( + primary: false, + appBar: new AppBar( + bottom: new PreferredSize( + preferredSize: const Size.fromHeight(200.0), + child: new Container(), + ), + ), + ), + ), + ); + expect(appBarTop(tester), 0.0); + expect(appBarHeight(tester), kToolbarHeight + 200.0); + + await tester.pumpWidget( + new MediaQuery( + data: topPadding100, + child: new Scaffold( + primary: true, + appBar: new AppBar( + bottom: new PreferredSize( + preferredSize: const Size.fromHeight(200.0), + child: new Container(), + ), + ), + ), + ), + ); + expect(appBarTop(tester), 0.0); + expect(appBarHeight(tester), kToolbarHeight + 100.0 + 200.0); + + await tester.pumpWidget( + new MediaQuery( + data: topPadding100, + child: new AppBar( + primary: false, + title: const Text('title'), + ), + ), + ); + expect(appBarTop(tester), 0.0); + expect(tester.getTopLeft(find.text('title')).dy, lessThan(100.0)); + }); } diff --git a/packages/flutter/test/material/floating_action_button_test.dart b/packages/flutter/test/material/floating_action_button_test.dart index 5017271061..e4b2b46a73 100644 --- a/packages/flutter/test/material/floating_action_button_test.dart +++ b/packages/flutter/test/material/floating_action_button_test.dart @@ -28,7 +28,7 @@ void main() { testWidgets('Floating Action Button tooltip', (WidgetTester tester) async { await tester.pumpWidget( new MaterialApp( - home: new Scaffold( + home: const Scaffold( floatingActionButton: const FloatingActionButton( onPressed: null, tooltip: 'Add', diff --git a/packages/flutter/test/material/page_test.dart b/packages/flutter/test/material/page_test.dart index 055e85641d..21907f67e3 100644 --- a/packages/flutter/test/material/page_test.dart +++ b/packages/flutter/test/material/page_test.dart @@ -191,10 +191,10 @@ void main() { await tester.pumpWidget( new MaterialApp( theme: new ThemeData(platform: TargetPlatform.android), - home: new Scaffold(body: const Text('Page 1')), + home: const Scaffold(body: const Text('Page 1')), routes: { '/next': (BuildContext context) { - return new Scaffold(body: const Text('Page 2')); + return const Scaffold(body: const Text('Page 2')); }, }, ) @@ -222,10 +222,10 @@ void main() { await tester.pumpWidget( new MaterialApp( theme: new ThemeData(platform: TargetPlatform.iOS), - home: new Scaffold(body: const Text('Page 1')), + home: const Scaffold(body: const Text('Page 1')), routes: { '/next': (BuildContext context) { - return new Scaffold(body: const Text('Page 2')); + return const Scaffold(body: const Text('Page 2')); }, }, ) @@ -264,13 +264,13 @@ void main() { await tester.pumpWidget( new MaterialApp( theme: new ThemeData(platform: TargetPlatform.iOS), - home: new Scaffold(body: const Text('Page 1')), + home: const Scaffold(body: const Text('Page 1')), ) ); tester.state(find.byType(Navigator)).push(new MaterialPageRoute( builder: (BuildContext context) { - return new Scaffold(body: const Text('Page 2')); + return const Scaffold(body: const Text('Page 2')); }, fullscreenDialog: true, )); diff --git a/packages/flutter/test/material/scaffold_test.dart b/packages/flutter/test/material/scaffold_test.dart index de4b512ca3..f1394d9dc6 100644 --- a/packages/flutter/test/material/scaffold_test.dart +++ b/packages/flutter/test/material/scaffold_test.dart @@ -82,7 +82,7 @@ void main() { }); testWidgets('Floating action animation', (WidgetTester tester) async { - await tester.pumpWidget(new Scaffold( + await tester.pumpWidget(const Scaffold( floatingActionButton: const FloatingActionButton( key: const Key('one'), onPressed: null, @@ -92,7 +92,7 @@ void main() { expect(tester.binding.transientCallbackCount, 0); - await tester.pumpWidget(new Scaffold( + await tester.pumpWidget(const Scaffold( floatingActionButton: const FloatingActionButton( key: const Key('two'), onPressed: null, @@ -103,10 +103,10 @@ void main() { expect(tester.binding.transientCallbackCount, greaterThan(0)); await tester.pumpWidget(new Container()); expect(tester.binding.transientCallbackCount, 0); - await tester.pumpWidget(new Scaffold()); + await tester.pumpWidget(const Scaffold()); expect(tester.binding.transientCallbackCount, 0); - await tester.pumpWidget(new Scaffold( + await tester.pumpWidget(const Scaffold( floatingActionButton: const FloatingActionButton( key: const Key('one'), onPressed: null, diff --git a/packages/flutter/test/material/theme_test.dart b/packages/flutter/test/material/theme_test.dart index 5d9d5e088d..72bb05f4e3 100644 --- a/packages/flutter/test/material/theme_test.dart +++ b/packages/flutter/test/material/theme_test.dart @@ -206,7 +206,7 @@ void main() { onTap: () { showDialog( context: context, - child: new Scaffold( + child: const Scaffold( body: const SizedBox( width: 200.0, height: 200.0,