Did you know?
The CallbackShortcuts
widget makes it easy to add keyboard shortcuts to your Flutter app.
To use it:
- Add one or more key bindings using
SingleActivator
- Add a
Focus
widget to capture the desired events - Use callbacks to perform your desired actions
Note: the activators will only be triggered when there's a focused child widget.
The easiest way to do this is to wrap your child widget in a Focus
widget with autofocus: true
.
If other widgets also rely on active focus, you can explicitly request focus with a FocusNode
.
For more details about CallbackShortcuts
, check this video:
To dive deeper and learn what to do when things don't work as you expect, watch this three-part series. 👇
- Part 1: Focus (Widget of the Week)
- Part 2: Shortcuts (Widget of the Week)
- Part 3: Actions (Widget of the Week)
If you want to see this technique in action, check my Flutter Tips app (web version):
This supports three different keyboard shortcuts:
- Left arrow: go to the previous tip
- Space: favorite/unfavorite a tip
- Right arrow: go to the next tip
Previous | Next |
---|---|
GitHub Self-Hosted Runners | Wildcard Variables in Dart 3.7 |