Fix clipBehavior for Drawer with shape and add clipBehavior property. (#124104)

This commit is contained in:
Taha Tesser
2023-04-05 02:23:32 +03:00
committed by GitHub
parent 9e4b5fb7d9
commit 38805a4392
2 changed files with 117 additions and 3 deletions

View File

@@ -153,6 +153,7 @@ class Drawer extends StatelessWidget {
this.width,
this.child,
this.semanticLabel,
this.clipBehavior,
}) : assert(elevation == null || elevation >= 0.0);
/// Sets the color of the [Material] that holds all of the [Drawer]'s
@@ -237,6 +238,14 @@ class Drawer extends StatelessWidget {
/// value is used.
final String? semanticLabel;
/// {@macro flutter.material.Material.clipBehavior}
///
/// The [clipBehavior] argument specifies how to clip the drawer's [shape].
///
/// If the drawer has a [shape], it defaults to [Clip.hardEdge]. Otherwise,
/// defaults to [Clip.none].
final Clip? clipBehavior;
@override
Widget build(BuildContext context) {
assert(debugCheckHasMaterialLocalizations(context));
@@ -255,6 +264,9 @@ class Drawer extends StatelessWidget {
final bool useMaterial3 = Theme.of(context).useMaterial3;
final bool isDrawerStart = DrawerController.maybeOf(context)?.alignment != DrawerAlignment.end;
final DrawerThemeData defaults= useMaterial3 ? _DrawerDefaultsM3(context): _DrawerDefaultsM2(context);
final ShapeBorder? effectiveShape = shape ?? (isDrawerStart
? (drawerTheme.shape ?? defaults.shape)
: (drawerTheme.endShape ?? defaults.endShape));
return Semantics(
scopesRoute: true,
namesRoute: true,
@@ -267,9 +279,8 @@ class Drawer extends StatelessWidget {
elevation: elevation ?? drawerTheme.elevation ?? defaults.elevation!,
shadowColor: shadowColor ?? drawerTheme.shadowColor ?? defaults.shadowColor,
surfaceTintColor: surfaceTintColor ?? drawerTheme.surfaceTintColor ?? defaults.surfaceTintColor,
shape: shape ?? (isDrawerStart
? (drawerTheme.shape ?? defaults.shape)
: (drawerTheme.endShape ?? defaults.endShape)),
shape: effectiveShape,
clipBehavior: effectiveShape != null ? (clipBehavior ?? Clip.hardEdge) : Clip.none,
child: child,
),
),

View File

@@ -688,6 +688,57 @@ void main() {
);
});
testWidgets('Drawer clip behavior', (WidgetTester tester) async {
await tester.pumpWidget(
MaterialApp(
theme: ThemeData(useMaterial3: true),
home: const Scaffold(
drawer: Drawer(),
),
),
);
final Finder drawerMaterial = find.descendant(
of: find.byType(Drawer),
matching: find.byType(Material),
);
final ScaffoldState state = tester.firstState(find.byType(Scaffold));
// Open the drawer.
state.openDrawer();
await tester.pump();
await tester.pump(const Duration(seconds: 1));
// Test default clip behavior.
Material material = tester.widget<Material>(drawerMaterial);
expect(material.clipBehavior, Clip.hardEdge);
state.closeDrawer();
await tester.pumpAndSettle();
// Provide a custom clip behavior.
await tester.pumpWidget(
MaterialApp(
theme: ThemeData(useMaterial3: true),
home: const Scaffold(
drawer: Drawer(
clipBehavior: Clip.antiAlias,
),
),
),
);
// Open the drawer again.
state.openDrawer();
await tester.pump();
await tester.pump(const Duration(seconds: 1));
// Clip behavior is now updated.
material = tester.widget<Material>(drawerMaterial);
expect(material.clipBehavior, Clip.antiAlias);
});
group('Material 2', () {
// Tests that are only relevant for Material 2. Once ThemeData.useMaterial3
// is turned on by default, these tests can be removed.
@@ -732,5 +783,57 @@ void main() {
material = tester.widget<Material>(drawerMaterial);
expect(material.shape, null);
});
testWidgets('Drawer clip behavior', (WidgetTester tester) async {
await tester.pumpWidget(
MaterialApp(
theme: ThemeData(useMaterial3: false),
home: const Scaffold(
drawer: Drawer(),
),
),
);
final Finder drawerMaterial = find.descendant(
of: find.byType(Drawer),
matching: find.byType(Material),
);
final ScaffoldState state = tester.firstState(find.byType(Scaffold));
// Open the drawer.
state.openDrawer();
await tester.pump();
await tester.pump(const Duration(seconds: 1));
// Test default clip behavior.
Material material = tester.widget<Material>(drawerMaterial);
expect(material.clipBehavior, Clip.none);
state.closeDrawer();
await tester.pumpAndSettle();
// Provide a shape and custom clip behavior.
await tester.pumpWidget(
MaterialApp(
theme: ThemeData(useMaterial3: false),
home: const Scaffold(
drawer: Drawer(
clipBehavior: Clip.hardEdge,
shape: RoundedRectangleBorder(),
),
),
),
);
// Open the drawer again.
state.openDrawer();
await tester.pump();
await tester.pump(const Duration(seconds: 1));
// Clip behavior is now updated.
material = tester.widget<Material>(drawerMaterial);
expect(material.clipBehavior, Clip.hardEdge);
});
});
}