Skip to content

Commit 51b2c48

Browse files
guidezplTahaTesser
andauthored
Fix actions padding for M3 alert dialogs (#105008)
* update docs * use correct actions padding for dialogs * remove trailing spaces * Update dialog_theme.dart * Update packages/flutter/lib/src/material/dialog.dart Co-authored-by: Taha Tesser <[email protected]> Co-authored-by: Taha Tesser <[email protected]>
1 parent 3d62732 commit 51b2c48

File tree

3 files changed

+26
-44
lines changed

3 files changed

+26
-44
lines changed

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

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -268,7 +268,7 @@ class AlertDialog extends StatelessWidget {
268268
this.contentPadding = const EdgeInsets.fromLTRB(24.0, 20.0, 24.0, 24.0),
269269
this.contentTextStyle,
270270
this.actions,
271-
this.actionsPadding = EdgeInsets.zero,
271+
this.actionsPadding,
272272
this.actionsAlignment,
273273
this.actionsOverflowAlignment,
274274
this.actionsOverflowDirection,
@@ -350,10 +350,9 @@ class AlertDialog extends StatelessWidget {
350350
/// Typically used to provide padding to the button bar between the button bar
351351
/// and the edges of the dialog.
352352
///
353-
/// If are no [actions], then no padding will be included. The padding around
354-
/// the button bar defaults to zero. It is also important to note that
355-
/// [buttonPadding] may contribute to the padding on the edges of [actions] as
356-
/// well.
353+
/// If there are no [actions], then no padding will be included. It is also
354+
/// important to note that [buttonPadding] may contribute to the padding on
355+
/// the edges of [actions] as well.
357356
///
358357
/// {@tool snippet}
359358
/// This is an example of a set of actions aligned with the content widget.
@@ -373,7 +372,7 @@ class AlertDialog extends StatelessWidget {
373372
/// See also:
374373
///
375374
/// * [OverflowBar], which [actions] configures to lay itself out.
376-
final EdgeInsetsGeometry actionsPadding;
375+
final EdgeInsetsGeometry? actionsPadding;
377376

378377
/// Defines the horizontal layout of the [actions] according to the same
379378
/// rules as for [Row.mainAxisAlignment].
@@ -507,8 +506,6 @@ class AlertDialog extends StatelessWidget {
507506
// children.
508507
final double paddingScaleFactor = _paddingScaleFactor(MediaQuery.of(context).textScaleFactor);
509508
final TextDirection? textDirection = Directionality.maybeOf(context);
510-
const double m3ActionEndPadding = 18.0;
511-
const double m3ActionBottomPadding = 12.0;
512509

513510
Widget? titleWidget;
514511
Widget? contentWidget;
@@ -557,14 +554,11 @@ class AlertDialog extends StatelessWidget {
557554

558555
if (actions != null) {
559556
final double spacing = (buttonPadding?.horizontal ?? 16) / 2;
557+
final EdgeInsetsGeometry effetiveActionsPadding = (actionsPadding ?? EdgeInsets.zero).add(
558+
theme.useMaterial3 ? const EdgeInsets.only(left: 24.0, right: 24.0, bottom: 24.0) : EdgeInsets.all(spacing),
559+
);
560560
actionsWidget = Padding(
561-
padding: theme.useMaterial3
562-
? actionsPadding.add(EdgeInsets.all(spacing))
563-
.add(const EdgeInsets.only(
564-
right: m3ActionEndPadding,
565-
bottom: m3ActionBottomPadding,
566-
))
567-
: actionsPadding.add(EdgeInsets.all(spacing)),
561+
padding: effetiveActionsPadding,
568562
child: OverflowBar(
569563
alignment: actionsAlignment ?? MainAxisAlignment.end,
570564
spacing: spacing,

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

Lines changed: 12 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -15,16 +15,13 @@ import 'theme.dart';
1515
/// `DialogTheme.of(context)`. Instances of [DialogTheme] can be customized with
1616
/// [DialogTheme.copyWith].
1717
///
18-
/// When Shape is `null`, the dialog defaults to a [RoundedRectangleBorder] with
19-
/// a border radius of 2.0 on all corners.
20-
///
21-
/// [titleTextStyle] and [contentTextStyle] are used in [AlertDialog]s.
22-
/// If null, they default to [TextTheme.headline6] and [TextTheme.subtitle1],
23-
/// respectively.
18+
/// [titleTextStyle] and [contentTextStyle] are used in [AlertDialog]s and [SimpleDialog]s.
2419
///
2520
/// See also:
2621
///
27-
/// * [Dialog], a Material Design dialog that can be customized using this [DialogTheme].
22+
/// * [Dialog], a dialog that can be customized using this [DialogTheme].
23+
/// * [AlertDialog], a dialog that can be customized using this [DialogTheme].
24+
/// * [SimpleDialog], a dialog that can be customized using this [DialogTheme].
2825
/// * [ThemeData], which describes the overall theme information for the
2926
/// application.
3027
@immutable
@@ -39,33 +36,24 @@ class DialogTheme with Diagnosticable {
3936
this.contentTextStyle,
4037
});
4138

42-
/// Default value for [Dialog.backgroundColor].
43-
///
44-
/// If null, [ThemeData.dialogBackgroundColor] is used, if that's null,
45-
/// defaults to [Colors.white].
39+
/// Overrides the default value for [Dialog.backgroundColor].
4640
final Color? backgroundColor;
4741

48-
/// Default value for [Dialog.elevation].
49-
///
50-
/// If null, the [Dialog] elevation defaults to `24.0`.
42+
/// Overrides the default value for [Dialog.elevation].
5143
final double? elevation;
5244

53-
/// Default value for [Dialog.shape].
45+
/// Overrides the default value for [Dialog.shape].
5446
final ShapeBorder? shape;
5547

56-
/// Default value for [Dialog.alignment].
57-
///
58-
/// If null, the [Dialog] alignment defaults to [Alignment.center].
48+
/// Overrides the default value for [Dialog.alignment].
5949
final AlignmentGeometry? alignment;
6050

61-
/// Used to configure the [DefaultTextStyle] for the [AlertDialog.title] widget.
62-
///
63-
/// If null, defaults to [TextTheme.headline6] of [ThemeData.textTheme].
51+
/// Overrides the default value for [DefaultTextStyle] for [SimpleDialog.title] and
52+
/// [AlertDialog.title].
6453
final TextStyle? titleTextStyle;
6554

66-
/// Used to configure the [DefaultTextStyle] for the [AlertDialog.content] widget.
67-
///
68-
/// If null, defaults to [TextTheme.subtitle1] of [ThemeData.textTheme].
55+
/// Overrides the default value for [DefaultTextStyle] for [SimpleDialog.children] and
56+
/// [AlertDialog.content].
6957
final TextStyle? contentTextStyle;
7058

7159
/// Creates a copy of this object but with the given fields replaced with the

packages/flutter/test/material/dialog_test.dart

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -658,25 +658,25 @@ void main() {
658658
// First button
659659
expect(
660660
tester.getTopRight(find.byKey(key1)).dy,
661-
tester.getTopRight(_findButtonBar()).dy + 8.0,
661+
tester.getTopRight(_findButtonBar()).dy,
662662
); // top
663663
expect(
664664
tester.getBottomRight(find.byKey(key1)).dy,
665-
tester.getBottomRight(_findButtonBar()).dy - 20.0,
665+
tester.getBottomRight(_findButtonBar()).dy - 24.0,
666666
); // bottom
667667

668668
// // Second button
669669
expect(
670670
tester.getTopRight(find.byKey(key2)).dy,
671-
tester.getTopRight(_findButtonBar()).dy + 8.0,
671+
tester.getTopRight(_findButtonBar()).dy,
672672
); // top
673673
expect(
674674
tester.getBottomRight(find.byKey(key2)).dy,
675-
tester.getBottomRight(_findButtonBar()).dy - 20.0,
675+
tester.getBottomRight(_findButtonBar()).dy - 24.0,
676676
); // bottom
677677
expect(
678678
tester.getBottomRight(find.byKey(key2)).dx,
679-
tester.getBottomRight(_findButtonBar()).dx - 26.0,
679+
tester.getBottomRight(_findButtonBar()).dx - 24.0,
680680
); // right
681681
});
682682

0 commit comments

Comments
 (0)