Skip to content

Commit 8e53ad9

Browse files
Add clipBehavior to DialogTheme (#147635)
Fixes flutter/flutter#147634
1 parent a8a9b9b commit 8e53ad9

File tree

4 files changed

+91
-16
lines changed

4 files changed

+91
-16
lines changed

dev/tools/gen_defaults/lib/dialog_template.dart

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ class _${blockName}DefaultsM3 extends DialogTheme {
1818
alignment: Alignment.center,
1919
elevation: ${elevation("md.comp.dialog.container")},
2020
shape: ${shape("md.comp.dialog.container")},
21+
clipBehavior: Clip.none,
2122
);
2223
2324
final BuildContext context;
@@ -54,7 +55,7 @@ class DialogFullscreenTemplate extends TokenTemplate {
5455
@override
5556
String generate() => '''
5657
class _${blockName}DefaultsM3 extends DialogTheme {
57-
const _${blockName}DefaultsM3(this.context);
58+
const _${blockName}DefaultsM3(this.context): super(clipBehavior: Clip.none);
5859
5960
final BuildContext context;
6061

packages/flutter/lib/src/material/dialog.dart

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ class Dialog extends StatelessWidget {
5555
this.insetAnimationDuration = const Duration(milliseconds: 100),
5656
this.insetAnimationCurve = Curves.decelerate,
5757
this.insetPadding,
58-
this.clipBehavior = Clip.none,
58+
this.clipBehavior,
5959
this.shape,
6060
this.alignment,
6161
this.child,
@@ -186,9 +186,10 @@ class Dialog extends StatelessWidget {
186186
/// See the enum [Clip] for details of all possible options and their common
187187
/// use cases.
188188
///
189-
/// Defaults to [Clip.none].
189+
/// If null, then [DialogTheme.clipBehavior] is used. If that is also null,
190+
/// defaults to [Clip.none].
190191
/// {@endtemplate}
191-
final Clip clipBehavior;
192+
final Clip? clipBehavior;
192193

193194
/// {@template flutter.material.dialog.shape}
194195
/// The shape of this dialog's border.
@@ -244,7 +245,7 @@ class Dialog extends StatelessWidget {
244245
surfaceTintColor: surfaceTintColor ?? dialogTheme.surfaceTintColor ?? defaults.surfaceTintColor,
245246
shape: shape ?? dialogTheme.shape ?? defaults.shape!,
246247
type: MaterialType.card,
247-
clipBehavior: clipBehavior,
248+
clipBehavior: clipBehavior ?? dialogTheme.clipBehavior ?? defaults.clipBehavior!,
248249
child: child,
249250
),
250251
),
@@ -393,7 +394,7 @@ class AlertDialog extends StatelessWidget {
393394
this.surfaceTintColor,
394395
this.semanticLabel,
395396
this.insetPadding,
396-
this.clipBehavior = Clip.none,
397+
this.clipBehavior,
397398
this.shape,
398399
this.alignment,
399400
this.scrollable = false,
@@ -686,7 +687,7 @@ class AlertDialog extends StatelessWidget {
686687
final EdgeInsets? insetPadding;
687688

688689
/// {@macro flutter.material.dialog.clipBehavior}
689-
final Clip clipBehavior;
690+
final Clip? clipBehavior;
690691

691692
/// {@macro flutter.material.dialog.shape}
692693
final ShapeBorder? shape;
@@ -920,7 +921,7 @@ class _AdaptiveAlertDialog extends AlertDialog {
920921
super.surfaceTintColor,
921922
super.semanticLabel,
922923
super.insetPadding,
923-
super.clipBehavior = Clip.none,
924+
super.clipBehavior,
924925
super.shape,
925926
super.alignment,
926927
super.scrollable = false,
@@ -1118,7 +1119,7 @@ class SimpleDialog extends StatelessWidget {
11181119
this.surfaceTintColor,
11191120
this.semanticLabel,
11201121
this.insetPadding,
1121-
this.clipBehavior = Clip.none,
1122+
this.clipBehavior,
11221123
this.shape,
11231124
this.alignment,
11241125
});
@@ -1195,7 +1196,7 @@ class SimpleDialog extends StatelessWidget {
11951196
final EdgeInsets? insetPadding;
11961197

11971198
/// {@macro flutter.material.dialog.clipBehavior}
1198-
final Clip clipBehavior;
1199+
final Clip? clipBehavior;
11991200

12001201
/// {@macro flutter.material.dialog.shape}
12011202
final ShapeBorder? shape;
@@ -1606,6 +1607,7 @@ class _DialogDefaultsM2 extends DialogTheme {
16061607
alignment: Alignment.center,
16071608
elevation: 24.0,
16081609
shape: const RoundedRectangleBorder(borderRadius: BorderRadius.all(Radius.circular(4.0))),
1610+
clipBehavior: Clip.none,
16091611
);
16101612

16111613
final BuildContext context;
@@ -1644,6 +1646,7 @@ class _DialogDefaultsM3 extends DialogTheme {
16441646
alignment: Alignment.center,
16451647
elevation: 6.0,
16461648
shape: const RoundedRectangleBorder(borderRadius: BorderRadius.all(Radius.circular(28.0))),
1649+
clipBehavior: Clip.none,
16471650
);
16481651

16491652
final BuildContext context;
@@ -1682,7 +1685,7 @@ class _DialogDefaultsM3 extends DialogTheme {
16821685
// dev/tools/gen_defaults/bin/gen_defaults.dart.
16831686

16841687
class _DialogFullscreenDefaultsM3 extends DialogTheme {
1685-
const _DialogFullscreenDefaultsM3(this.context);
1688+
const _DialogFullscreenDefaultsM3(this.context): super(clipBehavior: Clip.none);
16861689

16871690
final BuildContext context;
16881691

packages/flutter/lib/src/material/dialog_theme.dart

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ class DialogTheme with Diagnosticable {
4040
this.actionsPadding,
4141
this.barrierColor,
4242
this.insetPadding,
43+
this.clipBehavior,
4344
});
4445

4546
/// Overrides the default value for [Dialog.backgroundColor].
@@ -80,6 +81,9 @@ class DialogTheme with Diagnosticable {
8081
/// Overrides the default value for [Dialog.insetPadding].
8182
final EdgeInsets? insetPadding;
8283

84+
/// Overrides the default value of [Dialog.clipBehavior].
85+
final Clip? clipBehavior;
86+
8387
/// Creates a copy of this object but with the given fields replaced with the
8488
/// new values.
8589
DialogTheme copyWith({
@@ -95,6 +99,7 @@ class DialogTheme with Diagnosticable {
9599
EdgeInsetsGeometry? actionsPadding,
96100
Color? barrierColor,
97101
EdgeInsets? insetPadding,
102+
Clip? clipBehavior,
98103
}) {
99104
return DialogTheme(
100105
backgroundColor: backgroundColor ?? this.backgroundColor,
@@ -109,6 +114,7 @@ class DialogTheme with Diagnosticable {
109114
actionsPadding: actionsPadding ?? this.actionsPadding,
110115
barrierColor: barrierColor ?? this.barrierColor,
111116
insetPadding: insetPadding ?? this.insetPadding,
117+
clipBehavior: clipBehavior ?? this.clipBehavior,
112118
);
113119
}
114120

@@ -137,6 +143,7 @@ class DialogTheme with Diagnosticable {
137143
actionsPadding: EdgeInsetsGeometry.lerp(a?.actionsPadding, b?.actionsPadding, t),
138144
barrierColor: Color.lerp(a?.barrierColor, b?.barrierColor, t),
139145
insetPadding: EdgeInsets.lerp(a?.insetPadding, b?.insetPadding, t),
146+
clipBehavior: t < 0.5 ? a?.clipBehavior : b?.clipBehavior,
140147
);
141148
}
142149

@@ -154,6 +161,7 @@ class DialogTheme with Diagnosticable {
154161
actionsPadding,
155162
barrierColor,
156163
insetPadding,
164+
clipBehavior
157165
]);
158166

159167
@override
@@ -176,7 +184,8 @@ class DialogTheme with Diagnosticable {
176184
&& other.contentTextStyle == contentTextStyle
177185
&& other.actionsPadding == actionsPadding
178186
&& other.barrierColor == barrierColor
179-
&& other.insetPadding == insetPadding;
187+
&& other.insetPadding == insetPadding
188+
&& other.clipBehavior == clipBehavior;
180189
}
181190

182191
@override
@@ -194,5 +203,6 @@ class DialogTheme with Diagnosticable {
194203
properties.add(DiagnosticsProperty<EdgeInsetsGeometry>('actionsPadding', actionsPadding, defaultValue: null));
195204
properties.add(ColorProperty('barrierColor', barrierColor));
196205
properties.add(DiagnosticsProperty<EdgeInsets>('insetPadding', insetPadding, defaultValue: null));
206+
properties.add(DiagnosticsProperty<Clip>('clipBehavior', clipBehavior, defaultValue: null));
197207
}
198208
}

packages/flutter/test/material/dialog_theme_test.dart

Lines changed: 65 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,14 @@ MaterialApp _appWithDialog(WidgetTester tester, Widget dialog, { ThemeData? them
3939

4040
final Key _painterKey = UniqueKey();
4141

42-
Material _getMaterialFromDialog(WidgetTester tester) {
42+
Material _getMaterialAlertDialog(WidgetTester tester) {
4343
return tester.widget<Material>(find.descendant(of: find.byType(AlertDialog), matching: find.byType(Material)));
4444
}
4545

46+
Material _getMaterialDialog(WidgetTester tester) {
47+
return tester.widget<Material>(find.descendant(of: find.byType(Dialog), matching: find.byType(Material)));
48+
}
49+
4650
RenderParagraph _getTextRenderObject(WidgetTester tester, String text) {
4751
return tester.element<StatelessElement>(find.text(text)).renderObject! as RenderParagraph;
4852
}
@@ -73,6 +77,7 @@ void main() {
7377
actionsPadding: EdgeInsets.all(8.0),
7478
barrierColor: Color(0xff000005),
7579
insetPadding: EdgeInsets.all(20.0),
80+
clipBehavior: Clip.antiAlias,
7681
).debugFillProperties(builder);
7782
final List<String> description = builder.properties
7883
.where((DiagnosticsNode n) => !n.isFiltered(DiagnosticLevel.info))
@@ -89,6 +94,7 @@ void main() {
8994
'actionsPadding: EdgeInsets.all(8.0)',
9095
'barrierColor: Color(0xff000005)',
9196
'insetPadding: EdgeInsets.all(20.0)',
97+
'clipBehavior: Clip.antiAlias'
9298
]);
9399
});
94100

@@ -104,7 +110,7 @@ void main() {
104110
await tester.tap(find.text('X'));
105111
await tester.pumpAndSettle();
106112

107-
final Material materialWidget = _getMaterialFromDialog(tester);
113+
final Material materialWidget = _getMaterialAlertDialog(tester);
108114
expect(materialWidget.color, customColor);
109115
});
110116

@@ -130,7 +136,7 @@ void main() {
130136
await tester.tap(find.text('X'));
131137
await tester.pumpAndSettle();
132138

133-
final Material materialWidget = _getMaterialFromDialog(tester);
139+
final Material materialWidget = _getMaterialAlertDialog(tester);
134140
expect(materialWidget.elevation, customElevation);
135141
expect(materialWidget.shadowColor, shadowColor);
136142
expect(materialWidget.surfaceTintColor, surfaceTintColor);
@@ -151,7 +157,7 @@ void main() {
151157
await tester.tap(find.text('X'));
152158
await tester.pumpAndSettle();
153159

154-
final Material materialWidget = _getMaterialFromDialog(tester);
160+
final Material materialWidget = _getMaterialAlertDialog(tester);
155161
expect(materialWidget.shape, customBorder);
156162
});
157163

@@ -548,4 +554,59 @@ void main() {
548554
),
549555
);
550556
});
557+
558+
testWidgets('DialogTheme.clipBehavior updates the dialogs clip behavior', (WidgetTester tester) async {
559+
const DialogTheme dialogTheme = DialogTheme(clipBehavior: Clip.hardEdge);
560+
const Dialog dialog = Dialog(child: Placeholder());
561+
562+
await tester.pumpWidget(_appWithDialog(
563+
tester,
564+
dialog,
565+
theme: ThemeData(dialogTheme: dialogTheme),
566+
));
567+
await tester.tap(find.text('X'));
568+
await tester.pumpAndSettle();
569+
570+
final Material materialWidget = _getMaterialDialog(tester);
571+
expect(materialWidget.clipBehavior, dialogTheme.clipBehavior);
572+
});
573+
574+
testWidgets('Dialog.clipBehavior takes priority over theme', (WidgetTester tester) async {
575+
const Dialog dialog = Dialog(
576+
clipBehavior: Clip.antiAlias,
577+
child: Placeholder(),
578+
);
579+
final ThemeData theme = ThemeData(
580+
dialogTheme: const DialogTheme(clipBehavior: Clip.hardEdge),
581+
);
582+
583+
await tester.pumpWidget(
584+
_appWithDialog(tester, dialog, theme: theme),
585+
);
586+
await tester.tap(find.text('X'));
587+
await tester.pumpAndSettle();
588+
589+
final Material materialWidget = _getMaterialDialog(tester);
590+
expect(materialWidget.clipBehavior, Clip.antiAlias);
591+
});
592+
593+
testWidgets('Material2 - Dialog.clipBehavior takes priority over theme', (WidgetTester tester) async {
594+
const Dialog dialog = Dialog(
595+
clipBehavior: Clip.antiAlias,
596+
child: Placeholder(),
597+
);
598+
final ThemeData theme = ThemeData(
599+
useMaterial3: false,
600+
dialogTheme: const DialogTheme(clipBehavior: Clip.hardEdge),
601+
);
602+
603+
await tester.pumpWidget(
604+
_appWithDialog(tester, dialog, theme: theme),
605+
);
606+
await tester.tap(find.text('X'));
607+
await tester.pumpAndSettle();
608+
609+
final Material materialWidget = _getMaterialDialog(tester);
610+
expect(materialWidget.clipBehavior, Clip.antiAlias);
611+
});
551612
}

0 commit comments

Comments
 (0)