From 440753bae8f324b7a9a2dd03516d9a12f1d9c761 Mon Sep 17 00:00:00 2001 From: Darren Austin Date: Tue, 8 Oct 2019 21:33:17 -0700 Subject: [PATCH] Fix regression with ModalBottomSheets not responding to changes in theme (#42189) Fix regression with ModalBottomSheets not responding to changes in theme after they are shown. --- .../lib/src/material/bottom_sheet.dart | 11 ++- .../material/bottom_sheet_theme_test.dart | 70 +++++++++++++++++++ 2 files changed, 75 insertions(+), 6 deletions(-) diff --git a/packages/flutter/lib/src/material/bottom_sheet.dart b/packages/flutter/lib/src/material/bottom_sheet.dart index f81f064d62..91a41aeb67 100644 --- a/packages/flutter/lib/src/material/bottom_sheet.dart +++ b/packages/flutter/lib/src/material/bottom_sheet.dart @@ -380,6 +380,7 @@ class _ModalBottomSheetRoute extends PopupRoute { @override Widget buildPage(BuildContext context, Animation animation, Animation secondaryAnimation) { + final BottomSheetThemeData sheetTheme = theme?.bottomSheetTheme ?? Theme.of(context).bottomSheetTheme; // By definition, the bottom sheet is aligned to the bottom of the page // and isn't exposed to the top padding of the MediaQuery. Widget bottomSheet = MediaQuery.removePadding( @@ -387,8 +388,8 @@ class _ModalBottomSheetRoute extends PopupRoute { removeTop: true, child: _ModalBottomSheet( route: this, - backgroundColor: backgroundColor, - elevation: elevation, + backgroundColor: backgroundColor ?? sheetTheme?.modalBackgroundColor ?? sheetTheme?.backgroundColor, + elevation: elevation ?? sheetTheme?.modalElevation ?? sheetTheme?.elevation, shape: shape, clipBehavior: clipBehavior, isScrollControlled: isScrollControlled, @@ -460,15 +461,13 @@ Future showModalBottomSheet({ assert(debugCheckHasMediaQuery(context)); assert(debugCheckHasMaterialLocalizations(context)); - final BottomSheetThemeData theme = Theme.of(context).bottomSheetTheme; - return Navigator.of(context, rootNavigator: useRootNavigator).push(_ModalBottomSheetRoute( builder: builder, theme: Theme.of(context, shadowThemeOnly: true), isScrollControlled: isScrollControlled, barrierLabel: MaterialLocalizations.of(context).modalBarrierDismissLabel, - backgroundColor: backgroundColor ?? theme?.modalBackgroundColor ?? theme?.backgroundColor, - elevation: elevation ?? theme?.modalElevation ?? theme?.elevation, + backgroundColor: backgroundColor, + elevation: elevation, shape: shape, clipBehavior: clipBehavior, )); diff --git a/packages/flutter/test/material/bottom_sheet_theme_test.dart b/packages/flutter/test/material/bottom_sheet_theme_test.dart index e3566ce2ac..b9007a203a 100644 --- a/packages/flutter/test/material/bottom_sheet_theme_test.dart +++ b/packages/flutter/test/material/bottom_sheet_theme_test.dart @@ -4,6 +4,7 @@ import 'package:flutter/material.dart'; import 'package:flutter/rendering.dart'; +import 'package:flutter/services.dart'; import 'package:flutter_test/flutter_test.dart'; void main() { @@ -214,6 +215,75 @@ void main() { expect(material.elevation, 0); expect(material.color, null); }); + + testWidgets('Modal bottom sheets respond to theme changes', (WidgetTester tester) async { + const double lightElevation = 5.0; + const double darkElevation = 3.0; + const Color lightBackgroundColor = Colors.green; + const Color darkBackgroundColor = Colors.grey; + + await tester.pumpWidget(MaterialApp( + theme: ThemeData.light().copyWith( + bottomSheetTheme: const BottomSheetThemeData( + elevation: lightElevation, + backgroundColor: lightBackgroundColor, + ), + ), + darkTheme: ThemeData.dark().copyWith( + bottomSheetTheme: const BottomSheetThemeData( + elevation: darkElevation, + backgroundColor: darkBackgroundColor, + ), + ), + home: Scaffold( + body: Builder( + builder: (BuildContext context) { + return Column( + children: [ + RawMaterialButton( + child: const Text('Show Modal'), + onPressed: () { + showModalBottomSheet( + context: context, + builder: (BuildContext context) { + return Container( + child: const Text('This is a modal bottom sheet.'), + ); + }, + ); + }, + ), + ], + ); + }, + ), + ), + )); + await tester.tap(find.text('Show Modal')); + await tester.pumpAndSettle(); + + final Material lightMaterial = tester.widget( + find.descendant( + of: find.byType(BottomSheet), + matching: find.byType(Material), + ), + ); + expect(lightMaterial.elevation, lightElevation); + expect(lightMaterial.color, lightBackgroundColor); + + // Simulate the user changing to dark theme + tester.binding.window.platformBrightnessTestValue = Brightness.dark; + await tester.pumpAndSettle(); + + final Material darkMaterial = tester.widget( + find.descendant( + of: find.byType(BottomSheet), + matching: find.byType(Material), + ), + ); + expect(darkMaterial.elevation, darkElevation); + expect(darkMaterial.color, darkBackgroundColor); + }); } Widget bottomSheetWithElevations(BottomSheetThemeData bottomSheetTheme) {