Convert some usage of `RawKeyEvent`, et al to `KeyEvent` (#139329)
## Description
This converts some uses of `RawKeyEvent` to `KeyEvent` in preparation for deprecating `RawKeyEvent`.
## Related Issues
- https://github.com/flutter/flutter/issues/136419
diff --git a/packages/flutter/lib/src/material/app.dart b/packages/flutter/lib/src/material/app.dart
index 31561bc..9bcb717 100644
--- a/packages/flutter/lib/src/material/app.dart
+++ b/packages/flutter/lib/src/material/app.dart
@@ -1079,8 +1079,9 @@
Widget result = _buildWidgetApp(context);
result = Focus(
canRequestFocus: false,
- onKey: (FocusNode node, RawKeyEvent event) {
- if (event is! RawKeyDownEvent || event.logicalKey != LogicalKeyboardKey.escape) {
+ onKeyEvent: (FocusNode node, KeyEvent event) {
+ if ((event is! KeyDownEvent && event is! KeyRepeatEvent) ||
+ event.logicalKey != LogicalKeyboardKey.escape) {
return KeyEventResult.ignored;
}
return Tooltip.dismissAllToolTips() ? KeyEventResult.handled : KeyEventResult.ignored;
diff --git a/packages/flutter/test/material/text_field_test.dart b/packages/flutter/test/material/text_field_test.dart
index 121941a..7ef7f7c 100644
--- a/packages/flutter/test/material/text_field_test.dart
+++ b/packages/flutter/test/material/text_field_test.dart
@@ -31,143 +31,6 @@
import '../widgets/semantics_tester.dart';
import 'feedback_tester.dart';
-typedef FormatEditUpdateCallback = void Function(TextEditingValue, TextEditingValue);
-
-// On web, key events in text fields are handled by the browser.
-const bool areKeyEventsHandledByPlatform = isBrowser;
-
-class CupertinoLocalizationsDelegate extends LocalizationsDelegate<CupertinoLocalizations> {
- @override
- bool isSupported(Locale locale) => true;
-
- @override
- Future<CupertinoLocalizations> load(Locale locale) =>
- DefaultCupertinoLocalizations.load(locale);
-
- @override
- bool shouldReload(CupertinoLocalizationsDelegate old) => false;
-}
-
-class MaterialLocalizationsDelegate extends LocalizationsDelegate<MaterialLocalizations> {
- @override
- bool isSupported(Locale locale) => true;
-
- @override
- Future<MaterialLocalizations> load(Locale locale) => DefaultMaterialLocalizations.load(locale);
-
- @override
- bool shouldReload(MaterialLocalizationsDelegate old) => false;
-}
-
-class WidgetsLocalizationsDelegate extends LocalizationsDelegate<WidgetsLocalizations> {
- @override
- bool isSupported(Locale locale) => true;
-
- @override
- Future<WidgetsLocalizations> load(Locale locale) => DefaultWidgetsLocalizations.load(locale);
-
- @override
- bool shouldReload(WidgetsLocalizationsDelegate old) => false;
-}
-
-Widget overlay({ required Widget child }) {
- final OverlayEntry entry = OverlayEntry(
- builder: (BuildContext context) {
- return Center(
- child: Material(
- child: child,
- ),
- );
- },
- );
- addTearDown(() => entry..remove()..dispose());
- return overlayWithEntry(entry);
-}
-
-Widget overlayWithEntry(OverlayEntry entry) {
- return Localizations(
- locale: const Locale('en', 'US'),
- delegates: <LocalizationsDelegate<dynamic>>[
- WidgetsLocalizationsDelegate(),
- MaterialLocalizationsDelegate(),
- CupertinoLocalizationsDelegate(),
- ],
- child: DefaultTextEditingShortcuts(
- child: Directionality(
- textDirection: TextDirection.ltr,
- child: MediaQuery(
- data: const MediaQueryData(size: Size(800.0, 600.0)),
- child: Overlay(
- initialEntries: <OverlayEntry>[
- entry,
- ],
- ),
- ),
- ),
- ),
- );
-}
-
-Widget boilerplate({ required Widget child, ThemeData? theme }) {
- return MaterialApp(
- theme: theme,
- home: Localizations(
- locale: const Locale('en', 'US'),
- delegates: <LocalizationsDelegate<dynamic>>[
- WidgetsLocalizationsDelegate(),
- MaterialLocalizationsDelegate(),
- ],
- child: Directionality(
- textDirection: TextDirection.ltr,
- child: MediaQuery(
- data: const MediaQueryData(size: Size(800.0, 600.0)),
- child: Center(
- child: Material(
- child: child,
- ),
- ),
- ),
- ),
- ),
- );
-}
-
-Future<void> skipPastScrollingAnimation(WidgetTester tester) async {
- await tester.pump();
- await tester.pump(const Duration(milliseconds: 200));
-}
-
-double getOpacity(WidgetTester tester, Finder finder) {
- return tester.widget<FadeTransition>(
- find.ancestor(
- of: finder,
- matching: find.byType(FadeTransition),
- ),
- ).opacity.value;
-}
-
-class TestFormatter extends TextInputFormatter {
- TestFormatter(this.onFormatEditUpdate);
- FormatEditUpdateCallback onFormatEditUpdate;
- @override
- TextEditingValue formatEditUpdate(TextEditingValue oldValue, TextEditingValue newValue) {
- onFormatEditUpdate(oldValue, newValue);
- return newValue;
- }
-}
-
-FocusNode _focusNode() {
- final FocusNode result = FocusNode();
- addTearDown(result.dispose);
- return result;
-}
-
-TextEditingController _textEditingController({String text = ''}) {
- final TextEditingController result = TextEditingController(text: text);
- addTearDown(result.dispose);
- return result;
-}
-
void main() {
TestWidgetsFlutterBinding.ensureInitialized();
final MockClipboard mockClipboard = MockClipboard();
@@ -7127,7 +6990,7 @@
await tester.pumpWidget(
MaterialApp(
home: Material(
- child: RawKeyboardListener(
+ child: KeyboardListener(
focusNode: focusNode,
child: TextField(
controller: controller,
@@ -7320,7 +7183,7 @@
await tester.pumpWidget(
MaterialApp(
home: Material(
- child: RawKeyboardListener(
+ child: KeyboardListener(
focusNode: focusNode,
child: textField,
),
@@ -7394,7 +7257,7 @@
await tester.pumpWidget(
MaterialApp(
home: Material(
- child: RawKeyboardListener(
+ child: KeyboardListener(
focusNode: focusNode,
child: textField,
),
@@ -7449,7 +7312,7 @@
await tester.pumpWidget(
MaterialApp(
home: Material(
- child: RawKeyboardListener(
+ child: KeyboardListener(
focusNode: focusNode,
child: textField,
),
@@ -7517,7 +7380,7 @@
await tester.pumpWidget(
MaterialApp(
home: Material(
- child: RawKeyboardListener(
+ child: KeyboardListener(
focusNode: focusNode,
child: textField,
),
@@ -7568,7 +7431,7 @@
await tester.pumpWidget(
MaterialApp(
home: Material(
- child: RawKeyboardListener(
+ child: KeyboardListener(
focusNode: focusNode,
child: textField,
),
@@ -7613,7 +7476,7 @@
testWidgetsWithLeakTracking('Changing positions of text fields', (WidgetTester tester) async {
final FocusNode focusNode = _focusNode();
- final List<RawKeyEvent> events = <RawKeyEvent>[];
+ final List<KeyEvent> events = <KeyEvent>[];
final TextEditingController c1 = _textEditingController();
final TextEditingController c2 = _textEditingController();
@@ -7624,9 +7487,9 @@
MaterialApp(
home:
Material(
- child: RawKeyboardListener(
+ child: KeyboardListener(
focusNode: focusNode,
- onKey: events.add,
+ onKeyEvent: events.add,
child: Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: <Widget>[
@@ -7669,9 +7532,9 @@
MaterialApp(
home:
Material(
- child: RawKeyboardListener(
+ child: KeyboardListener(
focusNode: focusNode,
- onKey: events.add,
+ onKeyEvent: events.add,
child: Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: <Widget>[
@@ -7707,7 +7570,7 @@
testWidgetsWithLeakTracking('Changing focus test', (WidgetTester tester) async {
final FocusNode focusNode = _focusNode();
- final List<RawKeyEvent> events = <RawKeyEvent>[];
+ final List<KeyEvent> events = <KeyEvent>[];
final TextEditingController c1 = _textEditingController();
final TextEditingController c2 = _textEditingController();
@@ -7718,9 +7581,9 @@
MaterialApp(
home:
Material(
- child: RawKeyboardListener(
+ child: KeyboardListener(
focusNode: focusNode,
- onKey: events.add,
+ onKeyEvent: events.add,
child: Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: <Widget>[
@@ -17238,3 +17101,141 @@
);
}
}
+
+
+typedef FormatEditUpdateCallback = void Function(TextEditingValue, TextEditingValue);
+
+// On web, key events in text fields are handled by the browser.
+const bool areKeyEventsHandledByPlatform = isBrowser;
+
+class CupertinoLocalizationsDelegate extends LocalizationsDelegate<CupertinoLocalizations> {
+ @override
+ bool isSupported(Locale locale) => true;
+
+ @override
+ Future<CupertinoLocalizations> load(Locale locale) =>
+ DefaultCupertinoLocalizations.load(locale);
+
+ @override
+ bool shouldReload(CupertinoLocalizationsDelegate old) => false;
+}
+
+class MaterialLocalizationsDelegate extends LocalizationsDelegate<MaterialLocalizations> {
+ @override
+ bool isSupported(Locale locale) => true;
+
+ @override
+ Future<MaterialLocalizations> load(Locale locale) => DefaultMaterialLocalizations.load(locale);
+
+ @override
+ bool shouldReload(MaterialLocalizationsDelegate old) => false;
+}
+
+class WidgetsLocalizationsDelegate extends LocalizationsDelegate<WidgetsLocalizations> {
+ @override
+ bool isSupported(Locale locale) => true;
+
+ @override
+ Future<WidgetsLocalizations> load(Locale locale) => DefaultWidgetsLocalizations.load(locale);
+
+ @override
+ bool shouldReload(WidgetsLocalizationsDelegate old) => false;
+}
+
+Widget overlay({ required Widget child }) {
+ final OverlayEntry entry = OverlayEntry(
+ builder: (BuildContext context) {
+ return Center(
+ child: Material(
+ child: child,
+ ),
+ );
+ },
+ );
+ addTearDown(() => entry..remove()..dispose());
+ return overlayWithEntry(entry);
+}
+
+Widget overlayWithEntry(OverlayEntry entry) {
+ return Localizations(
+ locale: const Locale('en', 'US'),
+ delegates: <LocalizationsDelegate<dynamic>>[
+ WidgetsLocalizationsDelegate(),
+ MaterialLocalizationsDelegate(),
+ CupertinoLocalizationsDelegate(),
+ ],
+ child: DefaultTextEditingShortcuts(
+ child: Directionality(
+ textDirection: TextDirection.ltr,
+ child: MediaQuery(
+ data: const MediaQueryData(size: Size(800.0, 600.0)),
+ child: Overlay(
+ initialEntries: <OverlayEntry>[
+ entry,
+ ],
+ ),
+ ),
+ ),
+ ),
+ );
+}
+
+Widget boilerplate({ required Widget child, ThemeData? theme }) {
+ return MaterialApp(
+ theme: theme,
+ home: Localizations(
+ locale: const Locale('en', 'US'),
+ delegates: <LocalizationsDelegate<dynamic>>[
+ WidgetsLocalizationsDelegate(),
+ MaterialLocalizationsDelegate(),
+ ],
+ child: Directionality(
+ textDirection: TextDirection.ltr,
+ child: MediaQuery(
+ data: const MediaQueryData(size: Size(800.0, 600.0)),
+ child: Center(
+ child: Material(
+ child: child,
+ ),
+ ),
+ ),
+ ),
+ ),
+ );
+}
+
+Future<void> skipPastScrollingAnimation(WidgetTester tester) async {
+ await tester.pump();
+ await tester.pump(const Duration(milliseconds: 200));
+}
+
+double getOpacity(WidgetTester tester, Finder finder) {
+ return tester.widget<FadeTransition>(
+ find.ancestor(
+ of: finder,
+ matching: find.byType(FadeTransition),
+ ),
+ ).opacity.value;
+}
+
+class TestFormatter extends TextInputFormatter {
+ TestFormatter(this.onFormatEditUpdate);
+ FormatEditUpdateCallback onFormatEditUpdate;
+ @override
+ TextEditingValue formatEditUpdate(TextEditingValue oldValue, TextEditingValue newValue) {
+ onFormatEditUpdate(oldValue, newValue);
+ return newValue;
+ }
+}
+
+FocusNode _focusNode() {
+ final FocusNode result = FocusNode();
+ addTearDown(result.dispose);
+ return result;
+}
+
+TextEditingController _textEditingController({String text = ''}) {
+ final TextEditingController result = TextEditingController(text: text);
+ addTearDown(result.dispose);
+ return result;
+}
diff --git a/packages/flutter/test/widgets/focus_traversal_test.dart b/packages/flutter/test/widgets/focus_traversal_test.dart
index 0270cae..9a4f17f 100644
--- a/packages/flutter/test/widgets/focus_traversal_test.dart
+++ b/packages/flutter/test/widgets/focus_traversal_test.dart
@@ -2809,8 +2809,9 @@
await tester.pumpWidget(MaterialApp(home: Container()));
- RawKeyboard.instance.addListener((RawKeyEvent event) {
+ HardwareKeyboard.instance.addHandler((KeyEvent event) {
events.add(event);
+ return true;
});
await tester.idle();
@@ -2829,7 +2830,7 @@
});
testWidgetsWithLeakTracking('Focus traversal does not break when no focusable is available on a WidgetsApp', (WidgetTester tester) async {
- final List<RawKeyEvent> events = <RawKeyEvent>[];
+ final List<KeyEvent> events = <KeyEvent>[];
await tester.pumpWidget(
WidgetsApp(
@@ -2843,8 +2844,9 @@
),
);
- RawKeyboard.instance.addListener((RawKeyEvent event) {
+ HardwareKeyboard.instance.addHandler((KeyEvent event) {
events.add(event);
+ return true;
});
await tester.idle();
diff --git a/packages/flutter/test/widgets/selectable_text_test.dart b/packages/flutter/test/widgets/selectable_text_test.dart
index 5a03e38..3b736c1 100644
--- a/packages/flutter/test/widgets/selectable_text_test.dart
+++ b/packages/flutter/test/widgets/selectable_text_test.dart
@@ -1895,7 +1895,7 @@
testWidgetsWithLeakTracking('Changing positions of selectable text', (WidgetTester tester) async {
final FocusNode focusNode = FocusNode();
addTearDown(focusNode.dispose);
- final List<RawKeyEvent> events = <RawKeyEvent>[];
+ final List<KeyEvent> events = <KeyEvent>[];
final Key key1 = UniqueKey();
final Key key2 = UniqueKey();
@@ -1904,9 +1904,9 @@
MaterialApp(
home:
Material(
- child: RawKeyboardListener(
+ child: KeyboardListener(
focusNode: focusNode,
- onKey: events.add,
+ onKeyEvent: events.add,
child: Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: <Widget>[
@@ -1947,9 +1947,9 @@
MaterialApp(
home:
Material(
- child: RawKeyboardListener(
+ child: KeyboardListener(
focusNode: focusNode,
- onKey: events.add,
+ onKeyEvent: events.add,
child: Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: <Widget>[
@@ -1986,7 +1986,7 @@
testWidgetsWithLeakTracking('Changing focus test', (WidgetTester tester) async {
final FocusNode focusNode = FocusNode();
addTearDown(focusNode.dispose);
- final List<RawKeyEvent> events = <RawKeyEvent>[];
+ final List<KeyEvent> events = <KeyEvent>[];
final Key key1 = UniqueKey();
final Key key2 = UniqueKey();
@@ -1995,9 +1995,9 @@
MaterialApp(
home:
Material(
- child: RawKeyboardListener(
+ child: KeyboardListener(
focusNode: focusNode,
- onKey: events.add,
+ onKeyEvent: events.add,
child: Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: <Widget>[
diff --git a/packages/flutter/test/widgets/shortcuts_test.dart b/packages/flutter/test/widgets/shortcuts_test.dart
index 4e49f20..e18e8bd 100644
--- a/packages/flutter/test/widgets/shortcuts_test.dart
+++ b/packages/flutter/test/widgets/shortcuts_test.dart
@@ -175,7 +175,7 @@
expect(invoked, 0);
invoked = 0;
- expect(RawKeyboard.instance.keysPressed, isEmpty);
+ expect(HardwareKeyboard.instance.logicalKeysPressed, isEmpty);
});
test('LogicalKeySet.hashCode is stable', () {
@@ -352,7 +352,7 @@
expect(invoked, 1);
invoked = 0;
- expect(RawKeyboard.instance.keysPressed, isEmpty);
+ expect(HardwareKeyboard.instance.logicalKeysPressed, isEmpty);
}, variant: KeySimulatorTransitModeVariant.all());
testWidgetsWithLeakTracking('handles repeated events', (WidgetTester tester) async {
@@ -378,7 +378,7 @@
expect(invoked, 2);
invoked = 0;
- expect(RawKeyboard.instance.keysPressed, isEmpty);
+ expect(HardwareKeyboard.instance.logicalKeysPressed, isEmpty);
}, variant: KeySimulatorTransitModeVariant.all());
testWidgetsWithLeakTracking('rejects repeated events if requested', (WidgetTester tester) async {
@@ -405,7 +405,7 @@
expect(invoked, 1);
invoked = 0;
- expect(RawKeyboard.instance.keysPressed, isEmpty);
+ expect(HardwareKeyboard.instance.logicalKeysPressed, isEmpty);
}, variant: KeySimulatorTransitModeVariant.all());
testWidgetsWithLeakTracking('handles Shift-Ctrl-C', (WidgetTester tester) async {
@@ -455,7 +455,7 @@
expect(invoked, 0);
invoked = 0;
- expect(RawKeyboard.instance.keysPressed, isEmpty);
+ expect(HardwareKeyboard.instance.logicalKeysPressed, isEmpty);
});
testWidgetsWithLeakTracking('isActivatedBy works as expected', (WidgetTester tester) async {