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:
@@ -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) {
|
||||
|
||||
@@ -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) {
|
||||
|
||||
@@ -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.');
|
||||
|
||||
@@ -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) {
|
||||
|
||||
Reference in New Issue
Block a user