Update key examples to use Focus widgets instead of RawKeyboardListener (#101537)
This updates the examples for PhysicalKeyboardKey and LogicalKeyboardKey to use Focus widgets that handle the keys instead of using RawKeyboardListener, since that usually leads people down the wrong path. Updated the See Also and added tests as well. Also exposed the `physicalKey` attribute for `tester.sendKeyEvent`.
This commit is contained in:
@@ -0,0 +1,27 @@
|
||||
// Copyright 2014 The Flutter Authors. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
import 'package:flutter/services.dart';
|
||||
import 'package:flutter_api_samples/services/keyboard_key/logical_keyboard_key.0.dart' as example;
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
|
||||
void main() {
|
||||
testWidgets('Responds to key', (WidgetTester tester) async {
|
||||
await tester.pumpWidget(
|
||||
const example.KeyExampleApp(),
|
||||
);
|
||||
|
||||
await tester.tap(find.text('Click to focus'));
|
||||
await tester.pumpAndSettle();
|
||||
expect(find.text('Press a key'), findsOneWidget);
|
||||
|
||||
await tester.sendKeyEvent(LogicalKeyboardKey.keyQ);
|
||||
await tester.pumpAndSettle();
|
||||
expect(find.text('Pressed the "Q" key!'), findsOneWidget);
|
||||
|
||||
await tester.sendKeyEvent(LogicalKeyboardKey.keyB);
|
||||
await tester.pumpAndSettle();
|
||||
expect(find.text('Not a Q: Pressed Key B'), findsOneWidget);
|
||||
});
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
// Copyright 2014 The Flutter Authors. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
import 'package:flutter/services.dart';
|
||||
import 'package:flutter_api_samples/services/keyboard_key/physical_keyboard_key.0.dart' as example;
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
|
||||
void main() {
|
||||
testWidgets('Responds to key', (WidgetTester tester) async {
|
||||
await tester.pumpWidget(
|
||||
const example.KeyExampleApp(),
|
||||
);
|
||||
|
||||
await tester.tap(find.text('Click to focus'));
|
||||
await tester.pumpAndSettle();
|
||||
expect(find.text('Press a key'), findsOneWidget);
|
||||
await tester.sendKeyEvent(LogicalKeyboardKey.keyQ, physicalKey: PhysicalKeyboardKey.keyA);
|
||||
await tester.pumpAndSettle();
|
||||
expect(find.text('Pressed the key next to CAPS LOCK!'), findsOneWidget);
|
||||
|
||||
await tester.sendKeyEvent(LogicalKeyboardKey.keyB, physicalKey: PhysicalKeyboardKey.keyB);
|
||||
await tester.pumpAndSettle();
|
||||
expect(find.text('Not the key next to CAPS LOCK: Pressed Key B'), findsOneWidget);
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user