Change some usage of RawKeyEvent to KeyEvent in preparation for deprecation (#136420)

## Description

This converts some usages of `RawKeyEvent` to `KeyEvent` to prepare the repo for deprecation of `RawKeyEvent`, and swaps out the `raw_keyboard.dart` manual test for `hardware_keyboard.dart`.

## Related Issues
 - https://github.com/flutter/flutter/issues/136419

## Tests
 - Just refactoring code, no semantic changes.
This commit is contained in:
Greg Spencer
2023-10-13 17:25:18 -07:00
committed by GitHub
parent 33db5d7a87
commit 3ef32f0407
7 changed files with 110 additions and 167 deletions

View File

@@ -46,7 +46,7 @@ class _MyKeyExampleState extends State<MyKeyExample> {
// Handles the key events from the Focus widget and updates the
// _message.
KeyEventResult _handleKeyEvent(FocusNode node, RawKeyEvent event) {
KeyEventResult _handleKeyEvent(FocusNode node, KeyEvent event) {
setState(() {
if (event.logicalKey == LogicalKeyboardKey.keyQ) {
_message = 'Pressed the "Q" key!';
@@ -73,7 +73,7 @@ class _MyKeyExampleState extends State<MyKeyExample> {
style: textTheme.headlineMedium!,
child: Focus(
focusNode: _focusNode,
onKey: _handleKeyEvent,
onKeyEvent: _handleKeyEvent,
child: ListenableBuilder(
listenable: _focusNode,
builder: (BuildContext context, Widget? child) {

View File

@@ -46,7 +46,7 @@ class _MyPhysicalKeyExampleState extends State<MyPhysicalKeyExample> {
// Handles the key events from the RawKeyboardListener and update the
// _message.
KeyEventResult _handleKeyEvent(FocusNode node, RawKeyEvent event) {
KeyEventResult _handleKeyEvent(FocusNode node, KeyEvent event) {
setState(() {
if (event.physicalKey == PhysicalKeyboardKey.keyA) {
_message = 'Pressed the key next to CAPS LOCK!';
@@ -73,7 +73,7 @@ class _MyPhysicalKeyExampleState extends State<MyPhysicalKeyExample> {
style: textTheme.headlineMedium!,
child: Focus(
focusNode: _focusNode,
onKey: _handleKeyEvent,
onKeyEvent: _handleKeyEvent,
child: ListenableBuilder(
listenable: _focusNode,
builder: (BuildContext context, Widget? child) {

View File

@@ -41,7 +41,7 @@ class _ColorfulButtonState extends State<ColorfulButton> {
super.initState();
_node = FocusNode(debugLabel: 'Button');
_node.addListener(_handleFocusChange);
_nodeAttachment = _node.attach(context, onKey: _handleKeyPress);
_nodeAttachment = _node.attach(context, onKeyEvent: _handleKeyPress);
}
void _handleFocusChange() {
@@ -52,8 +52,8 @@ class _ColorfulButtonState extends State<ColorfulButton> {
}
}
KeyEventResult _handleKeyPress(FocusNode node, RawKeyEvent event) {
if (event is RawKeyDownEvent) {
KeyEventResult _handleKeyPress(FocusNode node, KeyEvent event) {
if (event is KeyDownEvent) {
debugPrint('Focus node ${node.debugLabel} got key event: ${event.logicalKey}');
if (event.logicalKey == LogicalKeyboardKey.keyR) {
debugPrint('Changing color to red.');

View File

@@ -33,8 +33,8 @@ class FocusExample extends StatefulWidget {
class _FocusExampleState extends State<FocusExample> {
Color _color = Colors.white;
KeyEventResult _handleKeyPress(FocusNode node, RawKeyEvent event) {
if (event is RawKeyDownEvent) {
KeyEventResult _handleKeyPress(FocusNode node, KeyEvent event) {
if (event is KeyDownEvent) {
debugPrint('Focus node ${node.debugLabel} got key event: ${event.logicalKey}');
if (event.logicalKey == LogicalKeyboardKey.keyR) {
debugPrint('Changing color to red.');
@@ -68,7 +68,7 @@ class _FocusExampleState extends State<FocusExample> {
child: DefaultTextStyle(
style: textTheme.headlineMedium!,
child: Focus(
onKey: _handleKeyPress,
onKeyEvent: _handleKeyPress,
debugLabel: 'Button',
child: Builder(
builder: (BuildContext context) {