Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add clipBehavior to DrawerThemeData #148061

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 4 additions & 1 deletion dev/tools/gen_defaults/lib/drawer_template.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,10 @@ class DrawerTemplate extends TokenTemplate {
String generate() => '''
class _${blockName}DefaultsM3 extends DrawerThemeData {
_${blockName}DefaultsM3(this.context)
: super(elevation: ${elevation("md.comp.navigation-drawer.modal.container")});
: super(
elevation: ${elevation("md.comp.navigation-drawer.modal.container")},
clipBehavior: Clip.hardEdge,
);

final BuildContext context;
late final TextDirection direction = Directionality.of(context);
Expand Down
12 changes: 9 additions & 3 deletions packages/flutter/lib/src/material/drawer.dart
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,7 @@ class Drawer extends StatelessWidget {
shadowColor: shadowColor ?? drawerTheme.shadowColor ?? defaults.shadowColor,
surfaceTintColor: surfaceTintColor ?? drawerTheme.surfaceTintColor ?? defaults.surfaceTintColor,
shape: effectiveShape,
clipBehavior: effectiveShape != null ? (clipBehavior ?? Clip.hardEdge) : Clip.none,
clipBehavior: effectiveShape != null ? (clipBehavior ?? drawerTheme.clipBehavior ?? defaults.clipBehavior!) : Clip.none,
child: child,
),
),
Expand Down Expand Up @@ -769,7 +769,10 @@ class DrawerControllerState extends State<DrawerController> with SingleTickerPro

class _DrawerDefaultsM2 extends DrawerThemeData {
const _DrawerDefaultsM2(this.context)
: super(elevation: 16.0);
: super(
elevation: 16.0,
clipBehavior: Clip.hardEdge,
);

final BuildContext context;

Expand All @@ -787,7 +790,10 @@ class _DrawerDefaultsM2 extends DrawerThemeData {

class _DrawerDefaultsM3 extends DrawerThemeData {
_DrawerDefaultsM3(this.context)
: super(elevation: 1.0);
: super(
elevation: 1.0,
clipBehavior: Clip.hardEdge,
);

final BuildContext context;
late final TextDirection direction = Directionality.of(context);
Expand Down
12 changes: 11 additions & 1 deletion packages/flutter/lib/src/material/drawer_theme.dart
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ class DrawerThemeData with Diagnosticable {
this.shape,
this.endShape,
this.width,
this.clipBehavior,
});

/// Overrides the default value of [Drawer.backgroundColor].
Expand All @@ -69,6 +70,9 @@ class DrawerThemeData with Diagnosticable {
/// Overrides the default value of [Drawer.width].
final double? width;

/// Overrides the default value of [Drawer.clipBehavior].
final Clip? clipBehavior;

/// Creates a copy of this object with the given fields replaced with the
/// new values.
DrawerThemeData copyWith({
Expand All @@ -80,6 +84,7 @@ class DrawerThemeData with Diagnosticable {
ShapeBorder? shape,
ShapeBorder? endShape,
double? width,
Clip? clipBehavior,
}) {
return DrawerThemeData(
backgroundColor: backgroundColor ?? this.backgroundColor,
Expand All @@ -90,6 +95,7 @@ class DrawerThemeData with Diagnosticable {
shape: shape ?? this.shape,
endShape: endShape ?? this.endShape,
width: width ?? this.width,
clipBehavior: clipBehavior ?? this.clipBehavior,
);
}

Expand All @@ -111,6 +117,7 @@ class DrawerThemeData with Diagnosticable {
shape: ShapeBorder.lerp(a?.shape, b?.shape, t),
endShape: ShapeBorder.lerp(a?.endShape, b?.endShape, t),
width: lerpDouble(a?.width, b?.width, t),
clipBehavior: t < 0.5 ? a?.clipBehavior : b?.clipBehavior,
);
}

Expand All @@ -124,6 +131,7 @@ class DrawerThemeData with Diagnosticable {
shape,
endShape,
width,
clipBehavior,
);

@override
Expand All @@ -142,7 +150,8 @@ class DrawerThemeData with Diagnosticable {
&& other.surfaceTintColor == surfaceTintColor
&& other.shape == shape
&& other.endShape == endShape
&& other.width == width;
&& other.width == width
&& other.clipBehavior == clipBehavior;
}

@override
Expand All @@ -156,6 +165,7 @@ class DrawerThemeData with Diagnosticable {
properties.add(DiagnosticsProperty<ShapeBorder>('shape', shape, defaultValue: null));
properties.add(DiagnosticsProperty<ShapeBorder>('endShape', endShape, defaultValue: null));
properties.add(DoubleProperty('width', width, defaultValue: null));
properties.add(DiagnosticsProperty<Clip>('clipBehavior', clipBehavior, defaultValue: null));
}
}

Expand Down
19 changes: 18 additions & 1 deletion packages/flutter/test/material/drawer_theme_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ void main() {
surfaceTintColor: Color(0x00000096),
shape: RoundedRectangleBorder(borderRadius: BorderRadius.all(Radius.circular(2.0))),
width: 200.0,
clipBehavior: Clip.hardEdge,
).debugFillProperties(builder);

final List<String> description = builder.properties
Expand All @@ -55,6 +56,7 @@ void main() {
'surfaceTintColor: Color(0x00000096)',
'shape: RoundedRectangleBorder(BorderSide(width: 0.0, style: none), BorderRadius.circular(2.0))',
'width: 200.0',
'clipBehavior: Clip.hardEdge',
]);
});

Expand All @@ -80,6 +82,7 @@ void main() {
expect(_drawerMaterial(tester).shape, null);
expect(_scrim(tester).color, Colors.black54);
expect(_drawerRenderBox(tester).size.width, 304.0);
expect(_drawerMaterial(tester).clipBehavior, Clip.none);
});

testWidgets('Material3 - Default values are used when no Drawer or DrawerThemeData properties are specified', (WidgetTester tester) async {
Expand Down Expand Up @@ -107,6 +110,7 @@ void main() {
);
expect(_scrim(tester).color, Colors.black54);
expect(_drawerRenderBox(tester).size.width, 304.0);
expect(_drawerMaterial(tester).clipBehavior, Clip.hardEdge);
});

testWidgets('Material2 - Default values are used when no Drawer or DrawerThemeData properties are specified in end drawer', (WidgetTester tester) async {
Expand All @@ -131,6 +135,7 @@ void main() {
expect(_drawerMaterial(tester).shape, null);
expect(_scrim(tester).color, Colors.black54);
expect(_drawerRenderBox(tester).size.width, 304.0);
expect(_drawerMaterial(tester).clipBehavior, Clip.none);
});

testWidgets('Material3 - Default values are used when no Drawer or DrawerThemeData properties are specified in end drawer', (WidgetTester tester) async {
Expand Down Expand Up @@ -158,6 +163,7 @@ void main() {
);
expect(_scrim(tester).color, Colors.black54);
expect(_drawerRenderBox(tester).size.width, 304.0);
expect(_drawerMaterial(tester).clipBehavior, Clip.hardEdge);
});

testWidgets('DrawerThemeData values are used when no Drawer properties are specified', (WidgetTester tester) async {
Expand All @@ -168,6 +174,7 @@ void main() {
const Color surfaceTintColor = Color(0x00000004);
const RoundedRectangleBorder shape = RoundedRectangleBorder(borderRadius: BorderRadius.all(Radius.circular(16.0)));
const double width = 200.0;
const Clip clipBehavior = Clip.antiAlias;

final GlobalKey<ScaffoldState> scaffoldKey = GlobalKey<ScaffoldState>();
await tester.pumpWidget(
Expand All @@ -181,6 +188,7 @@ void main() {
surfaceTintColor: surfaceTintColor,
shape: shape,
width: width,
clipBehavior: clipBehavior,
),
),
home: Scaffold(
Expand All @@ -199,6 +207,7 @@ void main() {
expect(_drawerMaterial(tester).shape, shape);
expect(_scrim(tester).color, scrimColor);
expect(_drawerRenderBox(tester).size.width, width);
expect(_drawerMaterial(tester).clipBehavior, clipBehavior);
});

testWidgets('Drawer values take priority over DrawerThemeData values when both properties are specified', (WidgetTester tester) async {
Expand All @@ -209,6 +218,7 @@ void main() {
const Color surfaceTintColor = Color(0x00000004);
const RoundedRectangleBorder shape = RoundedRectangleBorder(borderRadius: BorderRadius.all(Radius.circular(16.0)));
const double width = 200.0;
const Clip clipBehavior = Clip.antiAlias;

final GlobalKey<ScaffoldState> scaffoldKey = GlobalKey<ScaffoldState>();
await tester.pumpWidget(
Expand All @@ -220,6 +230,7 @@ void main() {
elevation: 13.0,
shape: RoundedRectangleBorder(borderRadius: BorderRadius.all(Radius.circular(29.0))),
width: 400.0,
clipBehavior: Clip.antiAliasWithSaveLayer,
),
),
home: Scaffold(
Expand All @@ -232,6 +243,7 @@ void main() {
surfaceTintColor: surfaceTintColor,
shape: shape,
width: width,
clipBehavior: clipBehavior,
),
),
),
Expand All @@ -246,6 +258,7 @@ void main() {
expect(_drawerMaterial(tester).shape, shape);
expect(_scrim(tester).color, scrimColor);
expect(_drawerRenderBox(tester).size.width, width);
expect(_drawerMaterial(tester).clipBehavior, clipBehavior);
});

testWidgets('DrawerTheme values take priority over ThemeData.drawerTheme values when both properties are specified', (WidgetTester tester) async {
Expand All @@ -256,6 +269,7 @@ void main() {
const Color surfaceTintColor = Color(0x00000004);
const RoundedRectangleBorder shape = RoundedRectangleBorder(borderRadius: BorderRadius.all(Radius.circular(16.0)));
const double width = 200.0;
const Clip clipBehavior = Clip.antiAlias;

final GlobalKey<ScaffoldState> scaffoldKey = GlobalKey<ScaffoldState>();
await tester.pumpWidget(
Expand All @@ -268,7 +282,8 @@ void main() {
shadowColor: Color(0x00000007),
surfaceTintColor: Color(0x00000007),
shape: RoundedRectangleBorder(borderRadius: BorderRadius.all(Radius.circular(29.0))),
width: 400.0
width: 400.0,
clipBehavior: Clip.antiAliasWithSaveLayer,
),
),
home: DrawerTheme(
Expand All @@ -280,6 +295,7 @@ void main() {
surfaceTintColor: surfaceTintColor,
shape: shape,
width: width,
clipBehavior: clipBehavior,
),
child: Scaffold(
key: scaffoldKey,
Expand All @@ -298,6 +314,7 @@ void main() {
expect(_drawerMaterial(tester).shape, shape);
expect(_scrim(tester).color, scrimColor);
expect(_drawerRenderBox(tester).size.width, width);
expect(_drawerMaterial(tester).clipBehavior, clipBehavior);
});
}

Expand Down