Expose long press in ``CupertinoButton`` (#154052)

Adds long press as per https://github.com/flutter/flutter/issues/153956 request

Fixes #153956
This commit is contained in:
Dimil Kalathiya 2024-08-31 00:26:00 +05:30 committed by GitHub
parent 9fc160b11c
commit 620d1ea729
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 40 additions and 3 deletions

View File

@ -80,6 +80,7 @@ class CupertinoButton extends StatefulWidget {
this.focusNode,
this.onFocusChange,
this.autofocus = false,
this.onLongPress,
required this.onPressed,
}) : assert(pressedOpacity == null || (pressedOpacity >= 0.0 && pressedOpacity <= 1.0)),
_style = _CupertinoButtonStyle.plain;
@ -108,6 +109,7 @@ class CupertinoButton extends StatefulWidget {
this.focusNode,
this.onFocusChange,
this.autofocus = false,
this.onLongPress,
required this.onPressed,
}) : _style = _CupertinoButtonStyle.tinted;
@ -131,6 +133,7 @@ class CupertinoButton extends StatefulWidget {
this.focusNode,
this.onFocusChange,
this.autofocus = false,
this.onLongPress,
required this.onPressed,
}) : assert(pressedOpacity == null || (pressedOpacity >= 0.0 && pressedOpacity <= 1.0)),
color = null,
@ -164,9 +167,12 @@ class CupertinoButton extends StatefulWidget {
/// The callback that is called when the button is tapped or otherwise activated.
///
/// If this is set to null, the button will be disabled.
/// If [onPressed] and [onLongPress] callbacks are null, then the button will be disabled.
final VoidCallback? onPressed;
/// If [onPressed] and [onLongPress] callbacks are null, then the button will be disabled.
final VoidCallback? onLongPress;
/// Minimum size of the button.
///
/// Defaults to kMinInteractiveDimensionCupertino which the iOS Human
@ -223,8 +229,8 @@ class CupertinoButton extends StatefulWidget {
final _CupertinoButtonStyle _style;
/// Whether the button is enabled or disabled. Buttons are disabled by default. To
/// enable a button, set its [onPressed] property to a non-null value.
bool get enabled => onPressed != null;
/// enable a button, set [onPressed] or [onLongPress] to a non-null value.
bool get enabled => onPressed != null || onLongPress != null;
@override
State<CupertinoButton> createState() => _CupertinoButtonState();
@ -392,6 +398,7 @@ class _CupertinoButtonState extends State<CupertinoButton> with SingleTickerProv
onTapUp: enabled ? _handleTapUp : null,
onTapCancel: enabled ? _handleTapCancel : null,
onTap: widget.onPressed,
onLongPress: widget.onLongPress,
child: Semantics(
button: true,
child: ConstrainedBox(

View File

@ -65,6 +65,36 @@ void main() {
);
});
testWidgets('OnLongPress works!', (WidgetTester tester) async {
bool value = false;
await tester.pumpWidget(
boilerplate(child: CupertinoButton(
onPressed: null,
onLongPress: () {
value = !value;
},
child: const Text('XXXX', style: testStyle),
)),
);
await tester.pump();
final Finder cupertinoBtn = find.byType(CupertinoButton);
await tester.longPress(cupertinoBtn);
expect(value, isTrue);
});
testWidgets('button is disabled if onLongPress and onPressed are both null', (WidgetTester tester) async {
await tester.pumpWidget(
boilerplate(child: const CupertinoButton(
onPressed: null,
child: Text('XXXX', style: testStyle),
)),
);
expect(find.byType(CupertinoButton), findsOneWidget);
final CupertinoButton button = tester.widget(find.byType(CupertinoButton));
expect(button.enabled, isFalse);
});
// TODO(LongCatIsLoong): Uncomment once https://github.com/flutter/flutter/issues/44115
// is fixed.
/*