Commit 8b90d9d0 authored by lisa-liao's avatar lisa-liao Committed by rami-a

Allow independent theming of Persistent and Modal bottom sheets Background Color (#39333)

parent 8661dc40
...@@ -427,6 +427,10 @@ class _ModalBottomSheetRoute<T> extends PopupRoute<T> { ...@@ -427,6 +427,10 @@ class _ModalBottomSheetRoute<T> extends PopupRoute<T> {
/// that a modal [BottomSheet] needs to be displayed above all other content /// that a modal [BottomSheet] needs to be displayed above all other content
/// but the caller is inside another [Navigator]. /// but the caller is inside another [Navigator].
/// ///
/// The optional [backgroundColor], [elevation], [shape], and [clipBehavior]
/// parameters can be passed in to customize the appearance and behavior of
/// modal bottom sheets.
///
/// Returns a `Future` that resolves to the value (if any) that was passed to /// Returns a `Future` that resolves to the value (if any) that was passed to
/// [Navigator.pop] when the modal bottom sheet was closed. /// [Navigator.pop] when the modal bottom sheet was closed.
/// ///
...@@ -456,13 +460,15 @@ Future<T> showModalBottomSheet<T>({ ...@@ -456,13 +460,15 @@ Future<T> showModalBottomSheet<T>({
assert(debugCheckHasMediaQuery(context)); assert(debugCheckHasMediaQuery(context));
assert(debugCheckHasMaterialLocalizations(context)); assert(debugCheckHasMaterialLocalizations(context));
final BottomSheetThemeData theme = Theme.of(context).bottomSheetTheme;
return Navigator.of(context, rootNavigator: useRootNavigator).push(_ModalBottomSheetRoute<T>( return Navigator.of(context, rootNavigator: useRootNavigator).push(_ModalBottomSheetRoute<T>(
builder: builder, builder: builder,
theme: Theme.of(context, shadowThemeOnly: true), theme: Theme.of(context, shadowThemeOnly: true),
isScrollControlled: isScrollControlled, isScrollControlled: isScrollControlled,
barrierLabel: MaterialLocalizations.of(context).modalBarrierDismissLabel, barrierLabel: MaterialLocalizations.of(context).modalBarrierDismissLabel,
backgroundColor: backgroundColor, backgroundColor: backgroundColor ?? theme?.modalBackgroundColor ?? theme?.backgroundColor,
elevation: elevation ?? Theme.of(context).bottomSheetTheme.modalElevation, elevation: elevation ?? theme?.modalElevation ?? theme?.elevation,
shape: shape, shape: shape,
clipBehavior: clipBehavior, clipBehavior: clipBehavior,
)); ));
...@@ -474,6 +480,10 @@ Future<T> showModalBottomSheet<T>({ ...@@ -474,6 +480,10 @@ Future<T> showModalBottomSheet<T>({
/// Returns a controller that can be used to close and otherwise manipulate the /// Returns a controller that can be used to close and otherwise manipulate the
/// bottom sheet. /// bottom sheet.
/// ///
/// The optional [backgroundColor], [elevation], [shape], and [clipBehavior]
/// parameters can be passed in to customize the appearance and behavior of
/// persistent bottom sheets.
///
/// To rebuild the bottom sheet (e.g. if it is stateful), call /// To rebuild the bottom sheet (e.g. if it is stateful), call
/// [PersistentBottomSheetController.setState] on the controller returned by /// [PersistentBottomSheetController.setState] on the controller returned by
/// this method. /// this method.
......
...@@ -29,6 +29,7 @@ class BottomSheetThemeData extends Diagnosticable { ...@@ -29,6 +29,7 @@ class BottomSheetThemeData extends Diagnosticable {
const BottomSheetThemeData({ const BottomSheetThemeData({
this.backgroundColor, this.backgroundColor,
this.elevation, this.elevation,
this.modalBackgroundColor,
this.modalElevation, this.modalElevation,
this.shape, this.shape,
this.clipBehavior, this.clipBehavior,
...@@ -46,10 +47,12 @@ class BottomSheetThemeData extends Diagnosticable { ...@@ -46,10 +47,12 @@ class BottomSheetThemeData extends Diagnosticable {
/// If null, [BottomSheet] defaults to 0.0. /// If null, [BottomSheet] defaults to 0.0.
final double elevation; final double elevation;
/// Value for [BottomSheet.backgroundColor] when the Bottom sheet is presented
/// as a modal bottom sheet.
final Color modalBackgroundColor;
/// Value for [BottomSheet.elevation] when the Bottom sheet is presented as a /// Value for [BottomSheet.elevation] when the Bottom sheet is presented as a
/// modal bottom sheet. /// modal bottom sheet.
///
/// If null, [BottomSheet.elevation] defaults to [elevation].
final double modalElevation; final double modalElevation;
/// Default value for [BottomSheet.shape]. /// Default value for [BottomSheet.shape].
...@@ -68,6 +71,7 @@ class BottomSheetThemeData extends Diagnosticable { ...@@ -68,6 +71,7 @@ class BottomSheetThemeData extends Diagnosticable {
BottomSheetThemeData copyWith({ BottomSheetThemeData copyWith({
Color backgroundColor, Color backgroundColor,
double elevation, double elevation,
Color modalBackgroundColor,
double modalElevation, double modalElevation,
ShapeBorder shape, ShapeBorder shape,
Clip clipBehavior, Clip clipBehavior,
...@@ -75,6 +79,7 @@ class BottomSheetThemeData extends Diagnosticable { ...@@ -75,6 +79,7 @@ class BottomSheetThemeData extends Diagnosticable {
return BottomSheetThemeData( return BottomSheetThemeData(
backgroundColor: backgroundColor ?? this.backgroundColor, backgroundColor: backgroundColor ?? this.backgroundColor,
elevation: elevation ?? this.elevation, elevation: elevation ?? this.elevation,
modalBackgroundColor: modalBackgroundColor ?? this.modalBackgroundColor,
modalElevation: modalElevation ?? this.modalElevation, modalElevation: modalElevation ?? this.modalElevation,
shape: shape ?? this.shape, shape: shape ?? this.shape,
clipBehavior: clipBehavior ?? this.clipBehavior, clipBehavior: clipBehavior ?? this.clipBehavior,
...@@ -93,6 +98,7 @@ class BottomSheetThemeData extends Diagnosticable { ...@@ -93,6 +98,7 @@ class BottomSheetThemeData extends Diagnosticable {
return BottomSheetThemeData( return BottomSheetThemeData(
backgroundColor: Color.lerp(a?.backgroundColor, b?.backgroundColor, t), backgroundColor: Color.lerp(a?.backgroundColor, b?.backgroundColor, t),
elevation: lerpDouble(a?.elevation, b?.elevation, t), elevation: lerpDouble(a?.elevation, b?.elevation, t),
modalBackgroundColor: Color.lerp(a?.modalBackgroundColor, b?.modalBackgroundColor, t),
modalElevation: lerpDouble(a?.modalElevation, b?.modalElevation, t), modalElevation: lerpDouble(a?.modalElevation, b?.modalElevation, t),
shape: ShapeBorder.lerp(a?.shape, b?.shape, t), shape: ShapeBorder.lerp(a?.shape, b?.shape, t),
clipBehavior: t < 0.5 ? a?.clipBehavior : b?.clipBehavior, clipBehavior: t < 0.5 ? a?.clipBehavior : b?.clipBehavior,
...@@ -104,6 +110,7 @@ class BottomSheetThemeData extends Diagnosticable { ...@@ -104,6 +110,7 @@ class BottomSheetThemeData extends Diagnosticable {
return hashValues( return hashValues(
backgroundColor, backgroundColor,
elevation, elevation,
modalBackgroundColor,
modalElevation, modalElevation,
shape, shape,
clipBehavior, clipBehavior,
...@@ -119,6 +126,7 @@ class BottomSheetThemeData extends Diagnosticable { ...@@ -119,6 +126,7 @@ class BottomSheetThemeData extends Diagnosticable {
final BottomSheetThemeData typedOther = other; final BottomSheetThemeData typedOther = other;
return typedOther.backgroundColor == backgroundColor return typedOther.backgroundColor == backgroundColor
&& typedOther.elevation == elevation && typedOther.elevation == elevation
&& typedOther.modalBackgroundColor == modalBackgroundColor
&& typedOther.modalElevation == modalElevation && typedOther.modalElevation == modalElevation
&& typedOther.shape == shape && typedOther.shape == shape
&& typedOther.clipBehavior == clipBehavior; && typedOther.clipBehavior == clipBehavior;
...@@ -129,6 +137,7 @@ class BottomSheetThemeData extends Diagnosticable { ...@@ -129,6 +137,7 @@ class BottomSheetThemeData extends Diagnosticable {
super.debugFillProperties(properties); super.debugFillProperties(properties);
properties.add(ColorProperty('backgroundColor', backgroundColor, defaultValue: null)); properties.add(ColorProperty('backgroundColor', backgroundColor, defaultValue: null));
properties.add(DoubleProperty('elevation', elevation, defaultValue: null)); properties.add(DoubleProperty('elevation', elevation, defaultValue: null));
properties.add(ColorProperty('modalBackgroundColor', modalBackgroundColor, defaultValue: null));
properties.add(DoubleProperty('modalElevation', modalElevation, defaultValue: null)); properties.add(DoubleProperty('modalElevation', modalElevation, defaultValue: null));
properties.add(DiagnosticsProperty<ShapeBorder>('shape', shape, defaultValue: null)); properties.add(DiagnosticsProperty<ShapeBorder>('shape', shape, defaultValue: null));
properties.add(DiagnosticsProperty<Clip>('clipBehavior', clipBehavior, defaultValue: null)); properties.add(DiagnosticsProperty<Clip>('clipBehavior', clipBehavior, defaultValue: null));
......
...@@ -141,12 +141,16 @@ void main() { ...@@ -141,12 +141,16 @@ void main() {
expect(material.clipBehavior, clipBehavior); expect(material.clipBehavior, clipBehavior);
}); });
testWidgets('BottomSheetThemeData.modalElevation takes priority over BottomSheetThemeData.elevation for modal bottom sheets', (WidgetTester tester) async { testWidgets('Modal bottom sheet-specific parameters are used for modal bottom sheets', (WidgetTester tester) async {
const double modalElevation = 5.0; const double modalElevation = 5.0;
const double persistentElevation = 7.0; const double persistentElevation = 7.0;
const Color modalBackgroundColor = Colors.yellow;
const Color persistentBackgroundColor = Colors.red;
const BottomSheetThemeData bottomSheetTheme = BottomSheetThemeData( const BottomSheetThemeData bottomSheetTheme = BottomSheetThemeData(
elevation: persistentElevation, elevation: persistentElevation,
modalElevation: modalElevation, modalElevation: modalElevation,
backgroundColor: persistentBackgroundColor,
modalBackgroundColor: modalBackgroundColor,
); );
await tester.pumpWidget(bottomSheetWithElevations(bottomSheetTheme)); await tester.pumpWidget(bottomSheetWithElevations(bottomSheetTheme));
...@@ -160,14 +164,19 @@ void main() { ...@@ -160,14 +164,19 @@ void main() {
), ),
); );
expect(material.elevation, modalElevation); expect(material.elevation, modalElevation);
expect(material.color, modalBackgroundColor);
}); });
testWidgets('BottomSheetThemeData.elevation takes priority over BottomSheetThemeData.modalElevation for peristent bottom sheets', (WidgetTester tester) async { testWidgets('General bottom sheet parameters take priority over modal bottom sheet-specific parameters for peristent bottom sheets', (WidgetTester tester) async {
const double modalElevation = 5.0; const double modalElevation = 5.0;
const double persistentElevation = 7.0; const double persistentElevation = 7.0;
const Color modalBackgroundColor = Colors.yellow;
const Color persistentBackgroundColor = Colors.red;
const BottomSheetThemeData bottomSheetTheme = BottomSheetThemeData( const BottomSheetThemeData bottomSheetTheme = BottomSheetThemeData(
elevation: persistentElevation, elevation: persistentElevation,
modalElevation: modalElevation, modalElevation: modalElevation,
backgroundColor: persistentBackgroundColor,
modalBackgroundColor: modalBackgroundColor,
); );
await tester.pumpWidget(bottomSheetWithElevations(bottomSheetTheme)); await tester.pumpWidget(bottomSheetWithElevations(bottomSheetTheme));
...@@ -181,12 +190,15 @@ void main() { ...@@ -181,12 +190,15 @@ void main() {
), ),
); );
expect(material.elevation, persistentElevation); expect(material.elevation, persistentElevation);
expect(material.color, persistentBackgroundColor);
}); });
testWidgets('BottomSheetThemeData.modalElevation doesn\'t apply to persistent bottom sheets', (WidgetTester tester) async { testWidgets('Modal bottom sheet-specific parameters don\'t apply to persistent bottom sheets', (WidgetTester tester) async {
const double modalElevation = 5.0; const double modalElevation = 5.0;
const Color modalBackgroundColor = Colors.yellow;
const BottomSheetThemeData bottomSheetTheme = BottomSheetThemeData( const BottomSheetThemeData bottomSheetTheme = BottomSheetThemeData(
modalElevation: modalElevation, modalElevation: modalElevation,
modalBackgroundColor: modalBackgroundColor,
); );
await tester.pumpWidget(bottomSheetWithElevations(bottomSheetTheme)); await tester.pumpWidget(bottomSheetWithElevations(bottomSheetTheme));
...@@ -200,6 +212,7 @@ void main() { ...@@ -200,6 +212,7 @@ void main() {
), ),
); );
expect(material.elevation, 0); expect(material.elevation, 0);
expect(material.color, null);
}); });
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment