diff --git a/packages/flutter/lib/src/cupertino/button.dart b/packages/flutter/lib/src/cupertino/button.dart index 337cb8b7fd..c4ffc8141d 100644 --- a/packages/flutter/lib/src/cupertino/button.dart +++ b/packages/flutter/lib/src/cupertino/button.dart @@ -40,9 +40,11 @@ class CupertinoButton extends StatefulWidget { this.minSize = kMinInteractiveDimensionCupertino, this.pressedOpacity = 0.4, this.borderRadius = const BorderRadius.all(Radius.circular(8.0)), + this.alignment = Alignment.center, required this.onPressed, }) : assert(pressedOpacity == null || (pressedOpacity >= 0.0 && pressedOpacity <= 1.0)), assert(disabledColor != null), + assert(alignment != null), _filled = false, super(key: key); @@ -60,9 +62,11 @@ class CupertinoButton extends StatefulWidget { this.minSize = kMinInteractiveDimensionCupertino, this.pressedOpacity = 0.4, this.borderRadius = const BorderRadius.all(Radius.circular(8.0)), + this.alignment = Alignment.center, required this.onPressed, }) : assert(pressedOpacity == null || (pressedOpacity >= 0.0 && pressedOpacity <= 1.0)), assert(disabledColor != null), + assert(alignment != null), color = null, _filled = true, super(key: key); @@ -116,6 +120,16 @@ class CupertinoButton extends StatefulWidget { /// Defaults to round corners of 8 logical pixels. final BorderRadius? borderRadius; + /// The alignment of the button's [child]. + /// + /// Typically buttons are sized to be just big enough to contain the child and its + /// [padding]. If the button's size is constrained to a fixed size, for example by + /// enclosing it with a [SizedBox], this property defines how the child is aligned + /// within the available space. + /// + /// Always defaults to [Alignment.center]. + final AlignmentGeometry alignment; + final bool _filled; /// Whether the button is enabled or disabled. Buttons are disabled by default. To @@ -252,7 +266,8 @@ class _CupertinoButtonState extends State with SingleTickerProv padding: widget.padding ?? (backgroundColor != null ? _kBackgroundButtonPadding : _kButtonPadding), - child: Center( + child: Align( + alignment: widget.alignment, widthFactor: 1.0, heightFactor: 1.0, child: DefaultTextStyle( diff --git a/packages/flutter/test/cupertino/button_test.dart b/packages/flutter/test/cupertino/button_test.dart index fa7516d486..4cc6af9a42 100644 --- a/packages/flutter/test/cupertino/button_test.dart +++ b/packages/flutter/test/cupertino/button_test.dart @@ -101,6 +101,33 @@ void main() { }); */ + testWidgets('Button child alignment', (WidgetTester tester) async { + await tester.pumpWidget( + CupertinoApp( + home: CupertinoButton( + onPressed: () { }, + child: const Text('button'), + ), + ), + ); + + Align align = tester.firstWidget(find.ancestor(of: find.text('button'), matching: find.byType(Align))); + expect(align.alignment, Alignment.center); // default + + await tester.pumpWidget( + CupertinoApp( + home: CupertinoButton( + alignment: Alignment.centerLeft, + onPressed: () { }, + child: const Text('button'), + ), + ), + ); + + align = tester.firstWidget(find.ancestor(of: find.text('button'), matching: find.byType(Align))); + expect(align.alignment, Alignment.centerLeft); + }); + testWidgets('Button with background is wider', (WidgetTester tester) async { await tester.pumpWidget(boilerplate(child: const CupertinoButton( child: Text('X', style: testStyle),