feat(CupertinoButton): Add minWidth and minHeight to replace minSize. (#161295)

Add minWidth and minHeight to CupertinoButton to facilitate control over
the minimum dimensions.

fix: https://github.com/flutter/flutter/issues/161294

## Pre-launch Checklist

- [x] I read the [Contributor Guide] and followed the process outlined
there for submitting PRs.
- [x] I read the [Tree Hygiene] wiki page, which explains my
responsibilities.
- [x] I read and followed the [Flutter Style Guide], including [Features
we expect every widget to implement].
- [x] I signed the [CLA].
- [x] I listed at least one issue that this PR fixes in the description
above.
- [x] I updated/added relevant documentation (doc comments with `///`).
- [x] I added new tests to check the change I am making, or this PR is
[test-exempt].
- [x] I followed the [breaking change policy] and added [Data Driven
Fixes] where supported.
- [x] All existing and new tests are passing.
This commit is contained in:
StanleyCocos 2025-02-04 07:55:01 +08:00 committed by GitHub
parent 53e7e99c52
commit 3315ad2e27
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 79 additions and 3 deletions

View File

@ -404,4 +404,28 @@ transforms:
fillColor:
kind: fragment
value: 'arguments[fillColor]'
# Changes made in https://github.com/flutter/flutter/pull/161295
- title: "Migrate 'minSize' to 'minimumSize'"
date: 2025-01-09
element:
uris: [ 'cupertino.dart' ]
constructor: ''
inClass: 'CupertinoButton'
oneOf:
- if: "minSize != ''"
changes:
- kind: 'addParameter'
index: 7
name: 'minimumSize'
style: optional_named
argumentValue:
expression: 'Size({% minSize %}, {% minSize %})'
requiredIf: "minSize != ''"
- kind: 'removeParameter'
name: 'minSize'
variables:
minSize:
kind: fragment
value: 'arguments[minSize]'
# Before adding a new fix: read instructions at the top of this file.

View File

@ -76,7 +76,12 @@ class CupertinoButton extends StatefulWidget {
this.padding,
this.color,
this.disabledColor = CupertinoColors.quaternarySystemFill,
@Deprecated(
'Use minimumSize instead. '
'This feature was deprecated after v3.28.0-3.0.pre.',
)
this.minSize,
this.minimumSize,
this.pressedOpacity = 0.4,
this.borderRadius,
this.alignment = Alignment.center,
@ -87,6 +92,7 @@ class CupertinoButton extends StatefulWidget {
this.onLongPress,
required this.onPressed,
}) : assert(pressedOpacity == null || (pressedOpacity >= 0.0 && pressedOpacity <= 1.0)),
assert(minimumSize == null || minSize == null),
_style = _CupertinoButtonStyle.plain;
/// Creates an iOS-style button with a tinted background.
@ -105,7 +111,12 @@ class CupertinoButton extends StatefulWidget {
this.padding,
this.color,
this.disabledColor = CupertinoColors.tertiarySystemFill,
@Deprecated(
'Use minimumSize instead. '
'This feature was deprecated after v3.28.0-3.0.pre.',
)
this.minSize,
this.minimumSize,
this.pressedOpacity = 0.4,
this.borderRadius,
this.alignment = Alignment.center,
@ -115,7 +126,8 @@ class CupertinoButton extends StatefulWidget {
this.autofocus = false,
this.onLongPress,
required this.onPressed,
}) : _style = _CupertinoButtonStyle.tinted;
}) : assert(minimumSize == null || minSize == null),
_style = _CupertinoButtonStyle.tinted;
/// Creates an iOS-style button with a filled background.
///
@ -129,7 +141,12 @@ class CupertinoButton extends StatefulWidget {
this.sizeStyle = CupertinoButtonSize.large,
this.padding,
this.disabledColor = CupertinoColors.tertiarySystemFill,
@Deprecated(
'Use minimumSize instead. '
'This feature was deprecated after v3.28.0-3.0.pre.',
)
this.minSize,
this.minimumSize,
this.pressedOpacity = 0.4,
this.borderRadius,
this.alignment = Alignment.center,
@ -140,6 +157,7 @@ class CupertinoButton extends StatefulWidget {
this.onLongPress,
required this.onPressed,
}) : assert(pressedOpacity == null || (pressedOpacity >= 0.0 && pressedOpacity <= 1.0)),
assert(minimumSize == null || minSize == null),
color = null,
_style = _CupertinoButtonStyle.filled;
@ -181,8 +199,19 @@ class CupertinoButton extends StatefulWidget {
///
/// Defaults to kMinInteractiveDimensionCupertino which the iOS Human
/// Interface Guidelines recommends as the minimum tappable area.
@Deprecated(
'Use minimumSize instead. '
'This feature was deprecated after v3.28.0-3.0.pre.',
)
final double? minSize;
/// The minimum size of the button.
///
/// Defaults to a button with a height and a width of
/// [kMinInteractiveDimensionCupertino], which the iOS Human
/// Interface Guidelines recommends as the minimum tappable area.
final Size? minimumSize;
/// The opacity that the button will fade to when it is pressed.
/// The button will have an opacity of 1.0 when it is not pressed.
///
@ -355,6 +384,12 @@ class _CupertinoButtonState extends State<CupertinoButton> with SingleTickerProv
@override
Widget build(BuildContext context) {
final bool enabled = widget.enabled;
final Size? minimumSize =
widget.minimumSize == null
? widget.minSize == null
? null
: Size(widget.minSize!, widget.minSize!)
: widget.minimumSize!;
final CupertinoThemeData themeData = CupertinoTheme.of(context);
final Color primaryColor = themeData.primaryColor;
final Color? backgroundColor = (widget.color == null
@ -418,11 +453,11 @@ class _CupertinoButtonState extends State<CupertinoButton> with SingleTickerProv
child: ConstrainedBox(
constraints: BoxConstraints(
minWidth:
widget.minSize ??
minimumSize?.width ??
kCupertinoButtonMinSize[widget.sizeStyle] ??
kMinInteractiveDimensionCupertino,
minHeight:
widget.minSize ??
minimumSize?.height ??
kCupertinoButtonMinSize[widget.sizeStyle] ??
kMinInteractiveDimensionCupertino,
),

View File

@ -48,6 +48,17 @@ void main() {
);
});
testWidgets('Minimum size minimumSize parameter', (WidgetTester tester) async {
const Size size = Size(60.0, 100.0);
await tester.pumpWidget(
boilerplate(
child: const CupertinoButton(onPressed: null, minimumSize: size, child: SizedBox.shrink()),
),
);
final RenderBox buttonBox = tester.renderObject(find.byType(CupertinoButton));
expect(buttonBox.size, size);
});
testWidgets('Size grows with text', (WidgetTester tester) async {
await tester.pumpWidget(
boilerplate(

View File

@ -286,4 +286,7 @@ void main() {
inactiveColor: Colors.red,
fillColor: WidgetStatePropertyAll(CupertinoColors.white),
);
// https://github.com/flutter/flutter/pull/161295
CupertinoButton(minSize: 60.0);
}

View File

@ -304,4 +304,7 @@ void main() {
CupertinoCheckbox(
fillColor: WidgetStatePropertyAll(CupertinoColors.white),
);
// https://github.com/flutter/flutter/pull/161295
CupertinoButton(minimumSize: Size(60.0, 60.0));
}