Shortcuts widget

To apply a set of keyboard shortcuts to a large section of the tree, use the Shortcuts widget:

// Define a class for each type of shortcut action you want

class CreateNewItemIntent extends Intent {

 const CreateNewItemIntent();

}

Widget build(BuildContext context) {

 return Shortcuts(

  // Bind intents to key combinations

  shortcuts: const <ShortcutActivator, Intent>{

   SingleActivator(LogicalKeyboardKey.keyN, control: true):

     CreateNewItemIntent(),

  },

  child: Actions(

   // Bind intents to an actual method in your code

   actions: <Type, Action<Intent>>{

    CreateNewItemIntent: CallbackAction<CreateNewItemIntent>(

     onInvoke: (intent) => _createNewItem(),

    ),

   },

   // Your sub-tree must be wrapped in a focusNode, so it can take focus.

   child: Focus(

    autofocus: true,

    child: Container(),

   ),

  ),

 );

}

The Shortcuts widget is useful because it only allows shortcuts to be fired when this widget tree or one of its children has focus and is visible.

Leave a comment

Your email address will not be published. Required fields are marked *