From dd9169ec712ca4c1789e571934b6342496153280 Mon Sep 17 00:00:00 2001 From: Rami <2364772+rami-a@users.noreply.github.com> Date: Mon, 30 Aug 2021 13:54:54 -0400 Subject: [PATCH] Allow for custom alignment for Dialogs (#88984) --- packages/flutter/lib/src/material/dialog.dart | 22 +++++++++- .../lib/src/material/dialog_theme.dart | 13 +++++- .../flutter/test/material/dialog_test.dart | 22 ++++++++++ .../test/material/dialog_theme_test.dart | 43 +++++++++++++++++++ 4 files changed, 98 insertions(+), 2 deletions(-) diff --git a/packages/flutter/lib/src/material/dialog.dart b/packages/flutter/lib/src/material/dialog.dart index 14b18684f2..c95f60a586 100644 --- a/packages/flutter/lib/src/material/dialog.dart +++ b/packages/flutter/lib/src/material/dialog.dart @@ -47,6 +47,7 @@ class Dialog extends StatelessWidget { this.insetPadding = _defaultInsetPadding, this.clipBehavior = Clip.none, this.shape, + this.alignment, this.child, }) : assert(clipBehavior != null), super(key: key); @@ -114,6 +115,14 @@ class Dialog extends StatelessWidget { /// {@endtemplate} final ShapeBorder? shape; + /// {@template flutter.material.dialog.alignment} + /// How to align the [Dialog]. + /// + /// If null, then [DialogTheme.alignment] is used. If that is also null, the + /// default is [Alignment.center]. + /// {@endtemplate} + final AlignmentGeometry? alignment; + /// The widget below this widget in the tree. /// /// {@macro flutter.widgets.ProxyWidget.child} @@ -137,7 +146,8 @@ class Dialog extends StatelessWidget { removeRight: true, removeBottom: true, context: context, - child: Center( + child: Align( + alignment: alignment ?? dialogTheme.alignment ?? Alignment.center, child: ConstrainedBox( constraints: const BoxConstraints(minWidth: 280.0), child: Material( @@ -260,6 +270,7 @@ class AlertDialog extends StatelessWidget { this.insetPadding = _defaultInsetPadding, this.clipBehavior = Clip.none, this.shape, + this.alignment, this.scrollable = false, }) : assert(contentPadding != null), assert(clipBehavior != null), @@ -437,6 +448,9 @@ class AlertDialog extends StatelessWidget { /// {@macro flutter.material.dialog.shape} final ShapeBorder? shape; + /// {@macro flutter.material.dialog.shape} + final AlignmentGeometry? alignment; + /// Determines whether the [title] and [content] widgets are wrapped in a /// scrollable. /// @@ -577,6 +591,7 @@ class AlertDialog extends StatelessWidget { insetPadding: insetPadding, clipBehavior: clipBehavior, shape: shape, + alignment: alignment, child: dialogChild, ); } @@ -742,6 +757,7 @@ class SimpleDialog extends StatelessWidget { this.insetPadding = _defaultInsetPadding, this.clipBehavior = Clip.none, this.shape, + this.alignment, }) : assert(titlePadding != null), assert(contentPadding != null), super(key: key); @@ -818,6 +834,9 @@ class SimpleDialog extends StatelessWidget { /// {@macro flutter.material.dialog.shape} final ShapeBorder? shape; + /// {@macro flutter.material.dialog.shape} + final AlignmentGeometry? alignment; + @override Widget build(BuildContext context) { assert(debugCheckHasMaterialLocalizations(context)); @@ -908,6 +927,7 @@ class SimpleDialog extends StatelessWidget { insetPadding: insetPadding, clipBehavior: clipBehavior, shape: shape, + alignment: alignment, child: dialogChild, ); } diff --git a/packages/flutter/lib/src/material/dialog_theme.dart b/packages/flutter/lib/src/material/dialog_theme.dart index dc7c9a5254..f3718fdd2a 100644 --- a/packages/flutter/lib/src/material/dialog_theme.dart +++ b/packages/flutter/lib/src/material/dialog_theme.dart @@ -34,6 +34,7 @@ class DialogTheme with Diagnosticable { this.backgroundColor, this.elevation, this.shape, + this.alignment, this.titleTextStyle, this.contentTextStyle, }); @@ -52,6 +53,11 @@ class DialogTheme with Diagnosticable { /// Default value for [Dialog.shape]. final ShapeBorder? shape; + /// Default value for [Dialog.alignment]. + /// + /// If null, the [Dialog] alignment defaults to [Alignment.center]. + final AlignmentGeometry? alignment; + /// Used to configure the [DefaultTextStyle] for the [AlertDialog.title] widget. /// /// If null, defaults to [TextTheme.headline6] of [ThemeData.textTheme]. @@ -68,6 +74,7 @@ class DialogTheme with Diagnosticable { Color? backgroundColor, double? elevation, ShapeBorder? shape, + AlignmentGeometry? alignment, TextStyle? titleTextStyle, TextStyle? contentTextStyle, }) { @@ -75,6 +82,7 @@ class DialogTheme with Diagnosticable { backgroundColor: backgroundColor ?? this.backgroundColor, elevation: elevation ?? this.elevation, shape: shape ?? this.shape, + alignment: alignment ?? this.alignment, titleTextStyle: titleTextStyle ?? this.titleTextStyle, contentTextStyle: contentTextStyle ?? this.contentTextStyle, ); @@ -96,6 +104,7 @@ class DialogTheme with Diagnosticable { backgroundColor: Color.lerp(a?.backgroundColor, b?.backgroundColor, t), elevation: lerpDouble(a?.elevation, b?.elevation, t), shape: ShapeBorder.lerp(a?.shape, b?.shape, t), + alignment: AlignmentGeometry.lerp(a?.alignment, b?.alignment, t), titleTextStyle: TextStyle.lerp(a?.titleTextStyle, b?.titleTextStyle, t), contentTextStyle: TextStyle.lerp(a?.contentTextStyle, b?.contentTextStyle, t), ); @@ -114,6 +123,7 @@ class DialogTheme with Diagnosticable { && other.backgroundColor == backgroundColor && other.elevation == elevation && other.shape == shape + && other.alignment == alignment && other.titleTextStyle == titleTextStyle && other.contentTextStyle == contentTextStyle; } @@ -122,8 +132,9 @@ class DialogTheme with Diagnosticable { void debugFillProperties(DiagnosticPropertiesBuilder properties) { super.debugFillProperties(properties); properties.add(ColorProperty('backgroundColor', backgroundColor)); - properties.add(DiagnosticsProperty('shape', shape, defaultValue: null)); properties.add(DoubleProperty('elevation', elevation)); + properties.add(DiagnosticsProperty('shape', shape, defaultValue: null)); + properties.add(DiagnosticsProperty('alignment', alignment, defaultValue: null)); properties.add(DiagnosticsProperty('titleTextStyle', titleTextStyle, defaultValue: null)); properties.add(DiagnosticsProperty('contentTextStyle', contentTextStyle, defaultValue: null)); } diff --git a/packages/flutter/test/material/dialog_test.dart b/packages/flutter/test/material/dialog_test.dart index f18300beb8..411c3c95a3 100644 --- a/packages/flutter/test/material/dialog_test.dart +++ b/packages/flutter/test/material/dialog_test.dart @@ -113,6 +113,11 @@ void main() { expect(materialWidget.color, Colors.grey[800]); expect(materialWidget.shape, _defaultDialogShape); expect(materialWidget.elevation, 24.0); + + final Offset bottomLeft = tester.getBottomLeft( + find.descendant(of: find.byType(Dialog), matching: find.byType(Material)), + ); + expect(bottomLeft.dy, 360.0); }); testWidgets('Custom dialog elevation', (WidgetTester tester) async { @@ -237,6 +242,23 @@ void main() { expect(materialWidget.shape, customBorder); }); + testWidgets('Custom dialog alignment', (WidgetTester tester) async { + const AlertDialog dialog = AlertDialog( + actions: [ ], + alignment: Alignment.bottomLeft, + ); + await tester.pumpWidget(_buildAppWithDialog(dialog)); + + await tester.tap(find.text('X')); + await tester.pumpAndSettle(); + + final Offset bottomLeft = tester.getBottomLeft( + find.descendant(of: find.byType(Dialog), matching: find.byType(Material)), + ); + expect(bottomLeft.dx, 40.0); + expect(bottomLeft.dy, 576.0); + }); + testWidgets('Simple dialog control test', (WidgetTester tester) async { await tester.pumpWidget( const MaterialApp( diff --git a/packages/flutter/test/material/dialog_theme_test.dart b/packages/flutter/test/material/dialog_theme_test.dart index a82c887571..2826884dd9 100644 --- a/packages/flutter/test/material/dialog_theme_test.dart +++ b/packages/flutter/test/material/dialog_theme_test.dart @@ -48,6 +48,7 @@ void main() { backgroundColor: Color(0xff123456), elevation: 8.0, shape: null, + alignment: Alignment.bottomLeft, titleTextStyle: TextStyle(color: Color(0xffffffff)), contentTextStyle: TextStyle(color: Color(0xff000000)), ).debugFillProperties(builder); @@ -57,6 +58,7 @@ void main() { expect(description, [ 'backgroundColor: Color(0xff123456)', 'elevation: 8.0', + 'alignment: Alignment.bottomLeft', 'titleTextStyle: TextStyle(inherit: true, color: Color(0xffffffff))', 'contentTextStyle: TextStyle(inherit: true, color: Color(0xff000000))', ]); @@ -115,6 +117,47 @@ void main() { expect(materialWidget.shape, customBorder); }); + testWidgets('Custom dialog alignment', (WidgetTester tester) async { + const AlertDialog dialog = AlertDialog( + title: Text('Title'), + actions: [ ], + ); + final ThemeData theme = ThemeData(dialogTheme: const DialogTheme(alignment: Alignment.bottomLeft)); + + await tester.pumpWidget( + _appWithDialog(tester, dialog, theme: theme), + ); + await tester.tap(find.text('X')); + await tester.pumpAndSettle(); + + final Offset bottomLeft = tester.getBottomLeft( + find.descendant(of: find.byType(Dialog), matching: find.byType(Material)), + ); + expect(bottomLeft.dx, 40.0); + expect(bottomLeft.dy, 576.0); + }); + + testWidgets('Dialog alignment takes priority over theme', (WidgetTester tester) async { + const AlertDialog dialog = AlertDialog( + title: Text('Title'), + actions: [ ], + alignment: Alignment.topRight, + ); + final ThemeData theme = ThemeData(dialogTheme: const DialogTheme(alignment: Alignment.bottomLeft)); + + await tester.pumpWidget( + _appWithDialog(tester, dialog, theme: theme), + ); + await tester.tap(find.text('X')); + await tester.pumpAndSettle(); + + final Offset bottomLeft = tester.getBottomLeft( + find.descendant(of: find.byType(Dialog), matching: find.byType(Material)), + ); + expect(bottomLeft.dx, 480.0); + expect(bottomLeft.dy, 104.0); + }); + testWidgets('Custom dialog shape matches golden', (WidgetTester tester) async { const RoundedRectangleBorder customBorder = RoundedRectangleBorder(borderRadius: BorderRadius.all(Radius.circular(16.0)));