mirror of
https://github.com/flutter/flutter.git
synced 2026-02-20 02:29:02 +08:00
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:
parent
9fc160b11c
commit
620d1ea729
@ -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(
|
||||
|
||||
@ -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.
|
||||
/*
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user