Custom Keyboard Shortcuts in Flutter for returning an independent widget

25 Views Asked by At

In my Flutter application I was trying to add custom Keyboard shortcuts like I used File Picker for attaching images . Now, I want to do that using Keyboard shortcut like "Ctrl + I" which returns that file picker widget and then runs that widget in background which asks to add the image.

I tried using various in built widgets like Focus, Keyboard Listener , HardwareKeyboard and FocusableActionDetector class but nothing worked in the way I wanted it to. As I am a beginner I want to know if I am missing out on some stuff.

1

There are 1 best solutions below

0
Maciej Bastian On

Checkout CallbackShortcuts class (https://api.flutter.dev/flutter/widgets/CallbackShortcuts-class.html). Wrap it around your widget tree, use Focus a and pass commands you want to implement. For example for Ctrl + I it would be:

CallbackShortcuts(
  bindings: <ShortcutActivator, VoidCallback>{
    const SingleActivator(LogicalKeyboardKey.keyI, control: true): () {
      // callback
    },
  }
  child: Focus(
    autofocus: true,
    child: ...
  ),
);

It’s a simpler widget than Shortcuts (https://api.flutter.dev/flutter/widgets/Shortcuts-class.html) but it does the work so if you’re a beginner CallbackShortcuts is a good choice

Hope that helps