From 3265e15925eaf12db9ff5c6fdb26b9eee31dac4e Mon Sep 17 00:00:00 2001 From: Shi-Hao Hong Date: Tue, 21 May 2019 08:30:58 -0700 Subject: [PATCH] SliverAppBar shape property (#33073) --- .../flutter/lib/src/material/app_bar.dart | 11 +++ .../flutter/test/material/app_bar_test.dart | 94 ++++++++++++++++--- 2 files changed, 91 insertions(+), 14 deletions(-) diff --git a/packages/flutter/lib/src/material/app_bar.dart b/packages/flutter/lib/src/material/app_bar.dart index 95d2d3aa957..28d072dd1e1 100644 --- a/packages/flutter/lib/src/material/app_bar.dart +++ b/packages/flutter/lib/src/material/app_bar.dart @@ -687,6 +687,7 @@ class _SliverAppBarDelegate extends SliverPersistentHeaderDelegate { @required this.floating, @required this.pinned, @required this.snapConfiguration, + @required this.shape, }) : assert(primary || topPadding == 0.0), _bottomHeight = bottom?.preferredSize?.height ?? 0.0; @@ -711,6 +712,7 @@ class _SliverAppBarDelegate extends SliverPersistentHeaderDelegate { final double topPadding; final bool floating; final bool pinned; + final ShapeBorder shape; final double _bottomHeight; @@ -765,6 +767,7 @@ class _SliverAppBarDelegate extends SliverPersistentHeaderDelegate { primary: primary, centerTitle: centerTitle, titleSpacing: titleSpacing, + shape: shape, toolbarOpacity: toolbarOpacity, bottomOpacity: pinned ? 1.0 : (visibleMainHeight / _bottomHeight).clamp(0.0, 1.0), ), @@ -908,6 +911,7 @@ class SliverAppBar extends StatefulWidget { this.floating = false, this.pinned = false, this.snap = false, + this.shape, }) : assert(automaticallyImplyLeading != null), assert(forceElevated != null), assert(primary != null), @@ -1125,6 +1129,12 @@ class SliverAppBar extends StatefulWidget { /// behavior of the app bar in combination with [floating]. final bool pinned; + /// The material's shape as well its shadow. + /// + /// A shadow is only displayed if the [elevation] is greater than + /// zero. + final ShapeBorder shape; + /// If [snap] and [floating] are true then the floating app bar will "snap" /// into view. /// @@ -1221,6 +1231,7 @@ class _SliverAppBarState extends State with TickerProviderStateMix topPadding: topPadding, floating: widget.floating, pinned: widget.pinned, + shape: widget.shape, snapConfiguration: _snapConfiguration, ), ), diff --git a/packages/flutter/test/material/app_bar_test.dart b/packages/flutter/test/material/app_bar_test.dart index c153562ece8..1cc013f990e 100644 --- a/packages/flutter/test/material/app_bar_test.dart +++ b/packages/flutter/test/material/app_bar_test.dart @@ -1425,7 +1425,6 @@ void main() { testWidgets('Changing SliverAppBar snap from true to false', (WidgetTester tester) async { // Regression test for https://github.com/flutter/flutter/issues/17598 - const double appBarHeight = 256.0; bool snap = true; @@ -1486,32 +1485,99 @@ void main() { await tester.pump(); }); - testWidgets('AppBar with shape', (WidgetTester tester) async { - const RoundedRectangleBorder roundedRectangleBorder = RoundedRectangleBorder(borderRadius: BorderRadius.all(Radius.circular(15.0))); + testWidgets('AppBar shape default', (WidgetTester tester) async { await tester.pumpWidget( MaterialApp( home: AppBar( leading: const Text('L'), title: const Text('No Scaffold'), - shape: roundedRectangleBorder, actions: const [Text('A1'), Text('A2')], ), ), ); final Finder appBarFinder = find.byType(AppBar); - - AppBar getAppBarWidget() { - return tester.widget(appBarFinder); - } - - expect(getAppBarWidget().shape, roundedRectangleBorder); + AppBar getAppBarWidget(Finder finder) => tester.widget(finder); + expect(getAppBarWidget(appBarFinder).shape, null); final Finder materialFinder = find.byType(Material); - Material getMaterialWidget() { - return tester.widget(materialFinder); - } + Material getMaterialWidget(Finder finder) => tester.widget(finder); + expect(getMaterialWidget(materialFinder).shape, null); + }); - expect(getMaterialWidget().shape, roundedRectangleBorder); + testWidgets('AppBar with shape', (WidgetTester tester) async { + const RoundedRectangleBorder roundedRectangleBorder = RoundedRectangleBorder( + borderRadius: BorderRadius.all(Radius.circular(15.0)) + ); + await tester.pumpWidget( + MaterialApp( + home: AppBar( + leading: const Text('L'), + title: const Text('No Scaffold'), + actions: const [Text('A1'), Text('A2')], + shape: roundedRectangleBorder, + ), + ), + ); + + final Finder appBarFinder = find.byType(AppBar); + AppBar getAppBarWidget(Finder finder) => tester.widget(finder); + expect(getAppBarWidget(appBarFinder).shape, roundedRectangleBorder); + + final Finder materialFinder = find.byType(Material); + Material getMaterialWidget(Finder finder) => tester.widget(finder); + expect(getMaterialWidget(materialFinder).shape, roundedRectangleBorder); + }); + + testWidgets('SliverAppBar shape default', (WidgetTester tester) async { + await tester.pumpWidget( + const MaterialApp( + home: CustomScrollView( + slivers: [ + SliverAppBar( + leading: Text('L'), + title: Text('No Scaffold'), + actions: [Text('A1'), Text('A2')], + ), + ], + ), + ), + ); + + final Finder sliverAppBarFinder = find.byType(SliverAppBar); + SliverAppBar getSliverAppBarWidget(Finder finder) => tester.widget(finder); + expect(getSliverAppBarWidget(sliverAppBarFinder).shape, null); + + final Finder materialFinder = find.byType(Material); + Material getMaterialWidget(Finder finder) => tester.widget(finder); + expect(getMaterialWidget(materialFinder).shape, null); + }); + + testWidgets('SliverAppBar with shape', (WidgetTester tester) async { + const RoundedRectangleBorder roundedRectangleBorder = RoundedRectangleBorder( + borderRadius: BorderRadius.all(Radius.circular(15.0)), + ); + await tester.pumpWidget( + const MaterialApp( + home: CustomScrollView( + slivers: [ + SliverAppBar( + leading: Text('L'), + title: Text('No Scaffold'), + actions: [Text('A1'), Text('A2')], + shape: roundedRectangleBorder, + ), + ], + ), + ), + ); + + final Finder sliverAppBarFinder = find.byType(SliverAppBar); + SliverAppBar getSliverAppBarWidget(Finder finder) => tester.widget(finder); + expect(getSliverAppBarWidget(sliverAppBarFinder).shape, roundedRectangleBorder); + + final Finder materialFinder = find.byType(Material); + Material getMaterialWidget(Finder finder) => tester.widget(finder); + expect(getMaterialWidget(materialFinder).shape, roundedRectangleBorder); }); }