Created
September 10, 2021 04:45
-
-
Save doyle-flutter/1c4c4626f3addcf7dc92d187638b03e6 to your computer and use it in GitHub Desktop.
Dart 2.14 & Flutter 2.5 : Keyboard Mapping
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /// Flutter code sample for Shortcuts | |
| /// - https://api.flutter.dev/flutter/widgets/Shortcuts-class.html | |
| import 'package:flutter/material.dart'; | |
| import 'package:flutter/services.dart'; | |
| void main() => runApp(const MyApp()); | |
| class MyApp extends StatelessWidget { | |
| const MyApp({Key? key}) : super(key: key); | |
| static const String _title = 'Flutter Code Sample'; | |
| @override | |
| Widget build(BuildContext context) { | |
| return MaterialApp( | |
| title: _title, | |
| home: Scaffold( | |
| appBar: AppBar(title: const Text(_title)), | |
| body: const Center( | |
| child: MyStatefulWidget(), | |
| ), | |
| ), | |
| ); | |
| } | |
| } | |
| class Model with ChangeNotifier { | |
| int count = 0; | |
| void incrementBy(int amount) { | |
| count += amount; | |
| notifyListeners(); | |
| } | |
| void decrementBy(int amount) { | |
| count -= amount; | |
| notifyListeners(); | |
| } | |
| } | |
| class IncrementIntent extends Intent { | |
| const IncrementIntent(this.amount); | |
| final int amount; | |
| } | |
| class DecrementIntent extends Intent { | |
| const DecrementIntent(this.amount); | |
| final int amount; | |
| } | |
| class IncrementAction extends Action<IncrementIntent> { | |
| IncrementAction(this.model); | |
| final Model model; | |
| /// covariant | |
| /// - https://dart.dev/guides/language/sound-problems#appendix | |
| @override | |
| void invoke(covariant IncrementIntent intent) { | |
| model.incrementBy(intent.amount); | |
| } | |
| } | |
| class DecrementAction extends Action<DecrementIntent> { | |
| DecrementAction(this.model); | |
| final Model model; | |
| @override | |
| void invoke(covariant DecrementIntent intent) { | |
| model.decrementBy(intent.amount); | |
| } | |
| } | |
| class MyStatefulWidget extends StatefulWidget { | |
| const MyStatefulWidget({Key? key}) : super(key: key); | |
| @override | |
| State<MyStatefulWidget> createState() => _MyStatefulWidgetState(); | |
| } | |
| class _MyStatefulWidgetState extends State<MyStatefulWidget> { | |
| Model model = Model(); | |
| @override | |
| Widget build(BuildContext context) { | |
| return Shortcuts( | |
| shortcuts: <ShortcutActivator, Intent>{ | |
| LogicalKeySet(LogicalKeyboardKey.arrowUp): const IncrementIntent(2), | |
| LogicalKeySet(LogicalKeyboardKey.arrowDown): const DecrementIntent(2), | |
| LogicalKeySet(LogicalKeyboardKey.keyD): const IncrementIntent(10), | |
| LogicalKeySet(LogicalKeyboardKey.keyJ): const DecrementIntent(10), | |
| }, | |
| child: Actions( | |
| actions: <Type, Action<Intent>>{ | |
| IncrementIntent: IncrementAction(model), | |
| DecrementIntent: DecrementAction(model), | |
| }, | |
| child: Focus( | |
| autofocus: true, | |
| child: Column( | |
| children: <Widget>[ | |
| const Text('Add to the counter by pressing the up arrow key'), | |
| const Text('Subtract from the counter by pressing the down arrow key'), | |
| AnimatedBuilder( | |
| animation: model, | |
| builder: (BuildContext context, Widget? child) { | |
| return Text('count: ${model.count}'); | |
| }, | |
| ), | |
| ], | |
| ), | |
| ), | |
| ), | |
| ); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment