From 620d1ea7295ea7244c8fcc5b57ac4e4c7252c647 Mon Sep 17 00:00:00 2001 From: Dimil Kalathiya Date: Sat, 31 Aug 2024 00:26:00 +0530 Subject: [PATCH] Expose long press in ```CupertinoButton``` (#154052) Adds long press as per https://github.com/flutter/flutter/issues/153956 request Fixes #153956 --- .../flutter/lib/src/cupertino/button.dart | 13 ++++++-- .../flutter/test/cupertino/button_test.dart | 30 +++++++++++++++++++ 2 files changed, 40 insertions(+), 3 deletions(-) diff --git a/packages/flutter/lib/src/cupertino/button.dart b/packages/flutter/lib/src/cupertino/button.dart index 637588045b8..172b6e0fdb3 100644 --- a/packages/flutter/lib/src/cupertino/button.dart +++ b/packages/flutter/lib/src/cupertino/button.dart @@ -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 createState() => _CupertinoButtonState(); @@ -392,6 +398,7 @@ class _CupertinoButtonState extends State with SingleTickerProv onTapUp: enabled ? _handleTapUp : null, onTapCancel: enabled ? _handleTapCancel : null, onTap: widget.onPressed, + onLongPress: widget.onLongPress, child: Semantics( button: true, child: ConstrainedBox( diff --git a/packages/flutter/test/cupertino/button_test.dart b/packages/flutter/test/cupertino/button_test.dart index feb436fae82..6420b6f87ab 100644 --- a/packages/flutter/test/cupertino/button_test.dart +++ b/packages/flutter/test/cupertino/button_test.dart @@ -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. /*