Make CupertinoButton interactive by keyboard shortcuts (#153126)
This should have been added with https://github.com/flutter/flutter/pull/150721.
This commit is contained in:
@@ -6,6 +6,7 @@
|
||||
library;
|
||||
|
||||
import 'package:flutter/foundation.dart';
|
||||
import 'package:flutter/semantics.dart';
|
||||
import 'package:flutter/widgets.dart';
|
||||
|
||||
import 'colors.dart';
|
||||
@@ -300,6 +301,13 @@ class _CupertinoButtonState extends State<CupertinoButton> with SingleTickerProv
|
||||
}
|
||||
}
|
||||
|
||||
void _handleTap([Intent? _]) {
|
||||
if (widget.onPressed != null) {
|
||||
widget.onPressed!();
|
||||
context.findRenderObject()!.sendSemanticsEvent(const TapSemanticEvent());
|
||||
}
|
||||
}
|
||||
|
||||
void _animate() {
|
||||
if (_animationController.isAnimating) {
|
||||
return;
|
||||
@@ -321,6 +329,10 @@ class _CupertinoButtonState extends State<CupertinoButton> with SingleTickerProv
|
||||
});
|
||||
}
|
||||
|
||||
late final Map<Type, Action<Intent>> _actionMap = <Type, Action<Intent>>{
|
||||
ActivateIntent: CallbackAction<ActivateIntent>(onInvoke: _handleTap),
|
||||
};
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final bool enabled = widget.enabled;
|
||||
@@ -368,6 +380,7 @@ class _CupertinoButtonState extends State<CupertinoButton> with SingleTickerProv
|
||||
return MouseRegion(
|
||||
cursor: enabled && kIsWeb ? SystemMouseCursors.click : MouseCursor.defer,
|
||||
child: FocusableActionDetector(
|
||||
actions: _actionMap,
|
||||
focusNode: widget.focusNode,
|
||||
autofocus: widget.autofocus,
|
||||
onFocusChange: widget.onFocusChange,
|
||||
|
||||
@@ -7,6 +7,7 @@ import 'package:flutter/foundation.dart';
|
||||
import 'package:flutter/gestures.dart';
|
||||
import 'package:flutter/rendering.dart';
|
||||
import 'package:flutter/scheduler.dart';
|
||||
import 'package:flutter/services.dart';
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
|
||||
import '../widgets/semantics_tester.dart';
|
||||
@@ -731,6 +732,41 @@ void main() {
|
||||
),
|
||||
);
|
||||
});
|
||||
|
||||
testWidgets('Button can be activated by keyboard shortcuts', (WidgetTester tester) async {
|
||||
tester.binding.focusManager.highlightStrategy = FocusHighlightStrategy.alwaysTraditional;
|
||||
bool value = true;
|
||||
await tester.pumpWidget(CupertinoApp(
|
||||
home: Center(
|
||||
child: StatefulBuilder(builder: (BuildContext context, StateSetter setState) {
|
||||
return CupertinoButton(
|
||||
onPressed: () {
|
||||
setState(() {
|
||||
value = !value;
|
||||
});
|
||||
},
|
||||
autofocus: true,
|
||||
child: const Text('Tap me'),
|
||||
);
|
||||
}),
|
||||
),
|
||||
),
|
||||
);
|
||||
await tester.pump();
|
||||
await tester.sendKeyEvent(LogicalKeyboardKey.enter);
|
||||
await tester.pump();
|
||||
// On web, buttons don't respond to the enter key.
|
||||
expect(value, kIsWeb ? isTrue : isFalse);
|
||||
await tester.sendKeyEvent(LogicalKeyboardKey.enter);
|
||||
await tester.pump();
|
||||
expect(value, isTrue);
|
||||
await tester.sendKeyEvent(LogicalKeyboardKey.space);
|
||||
await tester.pump();
|
||||
expect(value, isFalse);
|
||||
await tester.sendKeyEvent(LogicalKeyboardKey.space);
|
||||
await tester.pump();
|
||||
expect(value, isTrue);
|
||||
});
|
||||
}
|
||||
|
||||
Widget boilerplate({ required Widget child }) {
|
||||
|
||||
Reference in New Issue
Block a user