Commit ae847d97 authored by lisa-liao's avatar lisa-liao Committed by Anthony

Allow independent theming of Persistent and Modal bottom sheets (#38650)

BottomSheetThemeData has an additional field modalElevation which makes it possible to set different elevations between persistent and modal bottom sheets.
parent f0656ac3
...@@ -462,7 +462,7 @@ Future<T> showModalBottomSheet<T>({ ...@@ -462,7 +462,7 @@ Future<T> showModalBottomSheet<T>({
isScrollControlled: isScrollControlled, isScrollControlled: isScrollControlled,
barrierLabel: MaterialLocalizations.of(context).modalBarrierDismissLabel, barrierLabel: MaterialLocalizations.of(context).modalBarrierDismissLabel,
backgroundColor: backgroundColor, backgroundColor: backgroundColor,
elevation: elevation, elevation: elevation ?? Theme.of(context).bottomSheetTheme.modalElevation,
shape: shape, shape: shape,
clipBehavior: clipBehavior, clipBehavior: clipBehavior,
)); ));
......
...@@ -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.modalElevation,
this.shape, this.shape,
this.clipBehavior, this.clipBehavior,
}); });
...@@ -45,6 +46,12 @@ class BottomSheetThemeData extends Diagnosticable { ...@@ -45,6 +46,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.elevation] when the Bottom sheet is presented as a
/// modal bottom sheet.
///
/// If null, [BottomSheet.elevation] defaults to [elevation].
final double modalElevation;
/// Default value for [BottomSheet.shape]. /// Default value for [BottomSheet.shape].
/// ///
/// If null, no overriding shape is specified for [BottomSheet], so the /// If null, no overriding shape is specified for [BottomSheet], so the
...@@ -61,12 +68,14 @@ class BottomSheetThemeData extends Diagnosticable { ...@@ -61,12 +68,14 @@ class BottomSheetThemeData extends Diagnosticable {
BottomSheetThemeData copyWith({ BottomSheetThemeData copyWith({
Color backgroundColor, Color backgroundColor,
double elevation, double elevation,
double modalElevation,
ShapeBorder shape, ShapeBorder shape,
Clip clipBehavior, Clip clipBehavior,
}) { }) {
return BottomSheetThemeData( return BottomSheetThemeData(
backgroundColor: backgroundColor ?? this.backgroundColor, backgroundColor: backgroundColor ?? this.backgroundColor,
elevation: elevation ?? this.elevation, elevation: elevation ?? this.elevation,
modalElevation: modalElevation ?? this.modalElevation,
shape: shape ?? this.shape, shape: shape ?? this.shape,
clipBehavior: clipBehavior ?? this.clipBehavior, clipBehavior: clipBehavior ?? this.clipBehavior,
); );
...@@ -84,6 +93,7 @@ class BottomSheetThemeData extends Diagnosticable { ...@@ -84,6 +93,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),
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,
); );
...@@ -94,6 +104,7 @@ class BottomSheetThemeData extends Diagnosticable { ...@@ -94,6 +104,7 @@ class BottomSheetThemeData extends Diagnosticable {
return hashValues( return hashValues(
backgroundColor, backgroundColor,
elevation, elevation,
modalElevation,
shape, shape,
clipBehavior, clipBehavior,
); );
...@@ -108,6 +119,7 @@ class BottomSheetThemeData extends Diagnosticable { ...@@ -108,6 +119,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.modalElevation == modalElevation
&& typedOther.shape == shape && typedOther.shape == shape
&& typedOther.clipBehavior == clipBehavior; && typedOther.clipBehavior == clipBehavior;
} }
...@@ -117,6 +129,7 @@ class BottomSheetThemeData extends Diagnosticable { ...@@ -117,6 +129,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(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));
} }
......
...@@ -70,7 +70,7 @@ void main() { ...@@ -70,7 +70,7 @@ void main() {
find.descendant( find.descendant(
of: find.byType(BottomSheet), of: find.byType(BottomSheet),
matching: find.byType(Material), matching: find.byType(Material),
).first, ),
); );
expect(material.color, null); expect(material.color, null);
expect(material.elevation, 0.0); expect(material.elevation, 0.0);
...@@ -97,7 +97,7 @@ void main() { ...@@ -97,7 +97,7 @@ void main() {
find.descendant( find.descendant(
of: find.byType(BottomSheet), of: find.byType(BottomSheet),
matching: find.byType(Material), matching: find.byType(Material),
).first, ),
); );
expect(material.color, bottomSheetTheme.backgroundColor); expect(material.color, bottomSheetTheme.backgroundColor);
expect(material.elevation, bottomSheetTheme.elevation); expect(material.elevation, bottomSheetTheme.elevation);
...@@ -133,13 +133,120 @@ void main() { ...@@ -133,13 +133,120 @@ void main() {
find.descendant( find.descendant(
of: find.byType(BottomSheet), of: find.byType(BottomSheet),
matching: find.byType(Material), matching: find.byType(Material),
).first, ),
); );
expect(material.color, backgroundColor); expect(material.color, backgroundColor);
expect(material.elevation, elevation); expect(material.elevation, elevation);
expect(material.shape, shape); expect(material.shape, shape);
expect(material.clipBehavior, clipBehavior); expect(material.clipBehavior, clipBehavior);
}); });
testWidgets('BottomSheetThemeData.modalElevation takes priority over BottomSheetThemeData.elevation for modal bottom sheets', (WidgetTester tester) async {
const double modalElevation = 5.0;
const double persistentElevation = 7.0;
const BottomSheetThemeData bottomSheetTheme = BottomSheetThemeData(
elevation: persistentElevation,
modalElevation: modalElevation,
);
await tester.pumpWidget(bottomSheetWithElevations(bottomSheetTheme));
await tester.tap(find.text('Show Modal'));
await tester.pumpAndSettle();
final Material material = tester.widget<Material>(
find.descendant(
of: find.byType(BottomSheet),
matching: find.byType(Material),
),
);
expect(material.elevation, modalElevation);
});
testWidgets('BottomSheetThemeData.elevation takes priority over BottomSheetThemeData.modalElevation for peristent bottom sheets', (WidgetTester tester) async {
const double modalElevation = 5.0;
const double persistentElevation = 7.0;
const BottomSheetThemeData bottomSheetTheme = BottomSheetThemeData(
elevation: persistentElevation,
modalElevation: modalElevation,
);
await tester.pumpWidget(bottomSheetWithElevations(bottomSheetTheme));
await tester.tap(find.text('Show Persistent'));
await tester.pumpAndSettle();
final Material material = tester.widget<Material>(
find.descendant(
of: find.byType(BottomSheet),
matching: find.byType(Material),
),
);
expect(material.elevation, persistentElevation);
});
testWidgets('BottomSheetThemeData.modalElevation doesn\'t apply to persistent bottom sheets', (WidgetTester tester) async {
const double modalElevation = 5.0;
const BottomSheetThemeData bottomSheetTheme = BottomSheetThemeData(
modalElevation: modalElevation,
);
await tester.pumpWidget(bottomSheetWithElevations(bottomSheetTheme));
await tester.tap(find.text('Show Persistent'));
await tester.pumpAndSettle();
final Material material = tester.widget<Material>(
find.descendant(
of: find.byType(BottomSheet),
matching: find.byType(Material),
),
);
expect(material.elevation, 0);
});
}
Widget bottomSheetWithElevations(BottomSheetThemeData bottomSheetTheme) {
return MaterialApp(
theme: ThemeData(bottomSheetTheme: bottomSheetTheme),
home: Scaffold(
body: Builder(
builder: (BuildContext context) {
return Column(
children: <Widget>[
RawMaterialButton(
child: const Text('Show Modal'),
onPressed: () {
showModalBottomSheet<void>(
context: context,
builder: (BuildContext _) {
return Container(
child: const Text(
'This is a modal bottom sheet.',
),
);
},
);
},
),
RawMaterialButton(
child: const Text('Show Persistent'),
onPressed: () {
showBottomSheet<void>(
context: context,
builder: (BuildContext _) {
return Container(
child: const Text(
'This is a persistent bottom sheet.',
),
);
},
);
},
),
],
);
},
),
),
);
} }
BottomSheetThemeData _bottomSheetTheme() { BottomSheetThemeData _bottomSheetTheme() {
......
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