DefaultTextEditingShortcuts class Null safety

A Shortcuts widget with the shortcuts used for the default text editing behavior.

This default behavior can be overridden by placing a Shortcuts widget lower in the widget tree than this. See DefaultTextEditingActions for an example of remapping a text editing Intent to a custom Action.

This example shows how to use an additional Shortcuts widget to override some default text editing keyboard shortcuts to have new behavior. Instead of moving the cursor, alt + up/down will change the focused widget.
@override
Widget build(BuildContext context) {
  // If using WidgetsApp or its descendents MaterialApp or CupertinoApp,
  // then DefaultTextEditingShortcuts is already being inserted into the
  // widget tree.
  return DefaultTextEditingShortcuts(
    child: Center(
      child: Shortcuts(
        shortcuts: <LogicalKeySet, Intent>{
          LogicalKeySet(LogicalKeyboardKey.alt, LogicalKeyboardKey.arrowDown): const NextFocusIntent(),
          LogicalKeySet(LogicalKeyboardKey.alt, LogicalKeyboardKey.arrowUp): const PreviousFocusIntent(),
        },
        child: Column(
          children: const <Widget>[
            TextField(
              decoration: InputDecoration(
                hintText: 'alt + down moves to the next field.',
              ),
            ),
            TextField(
              decoration: InputDecoration(
                hintText: 'And alt + up moves to the previous.',
              ),
            ),
          ],
        ),
      ),
    ),
  );
}

This example shows how to use an additional Shortcuts widget to override default text editing shortcuts to have completely custom behavior defined by a custom Intent and Action. Here, the up/down arrow keys increment/decrement a counter instead of moving the cursor.
class IncrementCounterIntent extends Intent {}
class DecrementCounterIntent extends Intent {}

class MyWidget extends StatefulWidget {
  const MyWidget({ Key? key }) : super(key: key);

  @override
  MyWidgetState createState() => MyWidgetState();
}

class MyWidgetState extends State<MyWidget> {

  int _counter = 0;

  @override
  Widget build(BuildContext context) {
    // If using WidgetsApp or its descendents MaterialApp or CupertinoApp,
    // then DefaultTextEditingShortcuts is already being inserted into the
    // widget tree.
    return DefaultTextEditingShortcuts(
      child: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            const Text(
              'You have pushed the button this many times:',
            ),
            Text(
              '$_counter',
              style: Theme.of(context).textTheme.headline4,
            ),
            Shortcuts(
              shortcuts: <LogicalKeySet, Intent>{
                LogicalKeySet(LogicalKeyboardKey.arrowUp): IncrementCounterIntent(),
                LogicalKeySet(LogicalKeyboardKey.arrowDown): DecrementCounterIntent(),
              },
              child: Actions(
                actions: <Type, Action<Intent>>{
                  IncrementCounterIntent: CallbackAction<IncrementCounterIntent>(
                    onInvoke: (IncrementCounterIntent intent) {
                      setState(() {
                        _counter++;
                      });
                    },
                  ),
                  DecrementCounterIntent: CallbackAction<DecrementCounterIntent>(
                    onInvoke: (DecrementCounterIntent intent) {
                      setState(() {
                        _counter--;
                      });
                    },
                  ),
                },
                child: const TextField(
                  maxLines: 2,
                  decoration: InputDecoration(
                    hintText: 'Up/down increment/decrement here.',
                  ),
                ),
              ),
            ),
            const TextField(
              maxLines: 2,
              decoration: InputDecoration(
                hintText: 'Up/down behave normally here.',
              ),
            ),
          ],
        ),
      ),
    );
  }
}

See also:

Inheritance

Constructors

DefaultTextEditingShortcuts({Key? key, required Widget child})
Creates a Shortcuts widget that provides the default text editing shortcuts on the current platform.

Properties

child Widget
The child widget for this Shortcuts widget. [...]
final, inherited
debugLabel String?
The debug label that is printed for this node when logged. [...]
final, inherited
hashCode int
The hash code for this object. [...]
@nonVirtual, read-only, inherited
key Key?
Controls how one widget replaces another widget in the tree. [...]
final, inherited
manager ShortcutManager?
The ShortcutManager that will manage the mapping between key combinations and Actions. [...]
final, inherited
runtimeType Type
A representation of the runtime type of the object.
read-only, inherited
shortcuts Map<LogicalKeySet, Intent>
The map of shortcuts that the ShortcutManager will be given to manage. [...]
final, inherited

Methods

createElement() StatefulElement
Creates a StatefulElement to manage this widget's location in the tree. [...]
inherited
createState() → _ShortcutsState
Creates the mutable state for this widget at a given location in the tree. [...]
inherited
debugDescribeChildren() List<DiagnosticsNode>
Returns a list of DiagnosticsNode objects describing this node's children. [...]
@protected, inherited
debugFillProperties(DiagnosticPropertiesBuilder properties) → void
Add additional properties associated with the node. [...]
inherited
noSuchMethod(Invocation invocation) → dynamic
Invoked when a non-existent method or property is accessed. [...]
inherited
toDiagnosticsNode({String? name, DiagnosticsTreeStyle? style}) DiagnosticsNode
Returns a debug representation of the object that is used by debugging tools and by DiagnosticsNode.toStringDeep. [...]
inherited
toString({DiagnosticLevel minLevel = DiagnosticLevel.info}) String
A string representation of this object. [...]
inherited
toStringDeep({String prefixLineOne = '', String? prefixOtherLines, DiagnosticLevel minLevel = DiagnosticLevel.debug}) String
Returns a string representation of this node and its descendants. [...]
inherited
toStringShallow({String joiner = ', ', DiagnosticLevel minLevel = DiagnosticLevel.debug}) String
Returns a one-line detailed description of the object. [...]
inherited
toStringShort() String
A short, textual description of this widget.
inherited

Operators

operator ==(Object other) bool
The equality operator. [...]
@nonVirtual, inherited