Unverified Commit f5578257 authored by Hans Muller's avatar Hans Muller Committed by GitHub

Added CupertinoButton alignment property (#73900)

parent 277c4d22
...@@ -40,9 +40,11 @@ class CupertinoButton extends StatefulWidget { ...@@ -40,9 +40,11 @@ class CupertinoButton extends StatefulWidget {
this.minSize = kMinInteractiveDimensionCupertino, this.minSize = kMinInteractiveDimensionCupertino,
this.pressedOpacity = 0.4, this.pressedOpacity = 0.4,
this.borderRadius = const BorderRadius.all(Radius.circular(8.0)), this.borderRadius = const BorderRadius.all(Radius.circular(8.0)),
this.alignment = Alignment.center,
required this.onPressed, required this.onPressed,
}) : assert(pressedOpacity == null || (pressedOpacity >= 0.0 && pressedOpacity <= 1.0)), }) : assert(pressedOpacity == null || (pressedOpacity >= 0.0 && pressedOpacity <= 1.0)),
assert(disabledColor != null), assert(disabledColor != null),
assert(alignment != null),
_filled = false, _filled = false,
super(key: key); super(key: key);
...@@ -60,9 +62,11 @@ class CupertinoButton extends StatefulWidget { ...@@ -60,9 +62,11 @@ class CupertinoButton extends StatefulWidget {
this.minSize = kMinInteractiveDimensionCupertino, this.minSize = kMinInteractiveDimensionCupertino,
this.pressedOpacity = 0.4, this.pressedOpacity = 0.4,
this.borderRadius = const BorderRadius.all(Radius.circular(8.0)), this.borderRadius = const BorderRadius.all(Radius.circular(8.0)),
this.alignment = Alignment.center,
required this.onPressed, required this.onPressed,
}) : assert(pressedOpacity == null || (pressedOpacity >= 0.0 && pressedOpacity <= 1.0)), }) : assert(pressedOpacity == null || (pressedOpacity >= 0.0 && pressedOpacity <= 1.0)),
assert(disabledColor != null), assert(disabledColor != null),
assert(alignment != null),
color = null, color = null,
_filled = true, _filled = true,
super(key: key); super(key: key);
...@@ -116,6 +120,16 @@ class CupertinoButton extends StatefulWidget { ...@@ -116,6 +120,16 @@ class CupertinoButton extends StatefulWidget {
/// Defaults to round corners of 8 logical pixels. /// Defaults to round corners of 8 logical pixels.
final BorderRadius? borderRadius; 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; final bool _filled;
/// Whether the button is enabled or disabled. Buttons are disabled by default. To /// Whether the button is enabled or disabled. Buttons are disabled by default. To
...@@ -252,7 +266,8 @@ class _CupertinoButtonState extends State<CupertinoButton> with SingleTickerProv ...@@ -252,7 +266,8 @@ class _CupertinoButtonState extends State<CupertinoButton> with SingleTickerProv
padding: widget.padding ?? (backgroundColor != null padding: widget.padding ?? (backgroundColor != null
? _kBackgroundButtonPadding ? _kBackgroundButtonPadding
: _kButtonPadding), : _kButtonPadding),
child: Center( child: Align(
alignment: widget.alignment,
widthFactor: 1.0, widthFactor: 1.0,
heightFactor: 1.0, heightFactor: 1.0,
child: DefaultTextStyle( child: DefaultTextStyle(
......
...@@ -101,6 +101,33 @@ void main() { ...@@ -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<Align>(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<Align>(find.ancestor(of: find.text('button'), matching: find.byType(Align)));
expect(align.alignment, Alignment.centerLeft);
});
testWidgets('Button with background is wider', (WidgetTester tester) async { testWidgets('Button with background is wider', (WidgetTester tester) async {
await tester.pumpWidget(boilerplate(child: const CupertinoButton( await tester.pumpWidget(boilerplate(child: const CupertinoButton(
child: Text('X', style: testStyle), child: Text('X', style: testStyle),
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment