Skip to content

Commit 01b7ada

Browse files
authored
SliverAppBar Default Elevation Patch (flutter#73526)
1 parent 17c5c4c commit 01b7ada

File tree

2 files changed

+59
-10
lines changed

2 files changed

+59
-10
lines changed

packages/flutter/lib/src/material/app_bar.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1154,7 +1154,7 @@ class _SliverAppBarDelegate extends SliverPersistentHeaderDelegate {
11541154
? Semantics(child: flexibleSpace, header: true)
11551155
: flexibleSpace,
11561156
bottom: bottom,
1157-
elevation: forceElevated || overlapsContent || (pinned && shrinkOffset > maxExtent - minExtent) ? elevation ?? 4.0 : 0.0,
1157+
elevation: forceElevated || overlapsContent || (pinned && shrinkOffset > maxExtent - minExtent) ? elevation : 0.0,
11581158
shadowColor: shadowColor,
11591159
backgroundColor: backgroundColor,
11601160
foregroundColor: foregroundColor,

packages/flutter/test/material/app_bar_test.dart

Lines changed: 58 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -903,30 +903,79 @@ void main() {
903903
expect(tabBarHeight(tester), initialTabBarHeight);
904904
});
905905

906-
testWidgets('SliverAppBar rebuilds when forceElevated changes', (WidgetTester tester) async {
907-
// Regression test for https://github.com/flutter/flutter/issues/59158.
908-
Widget buildSliverAppBar(bool forceElevated) {
906+
testWidgets('AppBar uses the specified elevation or defaults to 4.0', (WidgetTester tester) async {
907+
Widget buildAppBar([double? elevation]) {
909908
return MaterialApp(
909+
home: Scaffold(
910+
appBar: AppBar(title: const Text('Title'), elevation: elevation),
911+
),
912+
);
913+
}
914+
915+
Material getMaterial() => tester.widget<Material>(find.descendant(
916+
of: find.byType(AppBar),
917+
matching: find.byType(Material),
918+
));
919+
920+
// Default elevation should be _AppBarState._defaultElevation = 4.0
921+
await tester.pumpWidget(buildAppBar());
922+
expect(getMaterial().elevation, 4.0);
923+
924+
// AppBar should use the specified elevation.
925+
await tester.pumpWidget(buildAppBar(8.0));
926+
expect(getMaterial().elevation, 8.0);
927+
});
928+
929+
group('SliverAppBar elevation', () {
930+
Widget buildSliverAppBar(bool forceElevated, {double? elevation, double? themeElevation}) {
931+
return MaterialApp(
932+
theme: ThemeData(appBarTheme: AppBarTheme(elevation: themeElevation)),
910933
home: CustomScrollView(
911934
slivers: <Widget>[
912935
SliverAppBar(
913936
backwardsCompatibility: false,
914937
title: const Text('Title'),
915938
forceElevated: forceElevated,
939+
elevation: elevation,
916940
),
917941
],
918942
),
919943
);
920944
}
921945

922-
final Finder appBarFinder = find.byType(AppBar);
923-
AppBar getAppBarWidget(Finder finder) => tester.widget<AppBar>(finder);
946+
testWidgets('Respects forceElevated parameter', (WidgetTester tester) async {
947+
// Regression test for https://github.com/flutter/flutter/issues/59158.
948+
AppBar getAppBar() => tester.widget<AppBar>(find.byType(AppBar));
949+
Material getMaterial() => tester.widget<Material>(find.byType(Material));
950+
951+
// When forceElevated is off, SliverAppBar should not be elevated.
952+
await tester.pumpWidget(buildSliverAppBar(false));
953+
expect(getMaterial().elevation, 0.0);
954+
955+
// Default elevation should be _AppBarState._defaultElevation = 4.0, and
956+
// the AppBar's elevation should not be specified by SliverAppBar.
957+
await tester.pumpWidget(buildSliverAppBar(true));
958+
expect(getMaterial().elevation, 4.0);
959+
expect(getAppBar().elevation, null);
924960

925-
await tester.pumpWidget(buildSliverAppBar(false));
926-
expect(getAppBarWidget(appBarFinder).elevation, 0.0);
961+
// SliverAppBar should use the specified elevation.
962+
await tester.pumpWidget(buildSliverAppBar(true, elevation: 8.0));
963+
expect(getMaterial().elevation, 8.0);
964+
});
965+
966+
testWidgets('Uses elevation of AppBarTheme by default', (WidgetTester tester) async {
967+
// Regression test for https://github.com/flutter/flutter/issues/73525.
968+
Material getMaterial() => tester.widget<Material>(find.byType(Material));
969+
970+
await tester.pumpWidget(buildSliverAppBar(false, themeElevation: 12.0));
971+
expect(getMaterial().elevation, 0.0);
927972

928-
await tester.pumpWidget(buildSliverAppBar(true));
929-
expect(getAppBarWidget(appBarFinder).elevation, 4.0);
973+
await tester.pumpWidget(buildSliverAppBar(true, themeElevation: 12.0));
974+
expect(getMaterial().elevation, 12.0);
975+
976+
await tester.pumpWidget(buildSliverAppBar(true, elevation: 8.0, themeElevation: 12.0));
977+
expect(getMaterial().elevation, 8.0);
978+
});
930979
});
931980

932981
testWidgets('AppBar dimensions, with and without bottom, primary', (WidgetTester tester) async {

0 commit comments

Comments
 (0)