DefaultTextEditingShortcutsModifier class

A 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 the Action class for an example of remapping an Intent to a custom Action.

{@tool snippet}

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: const <ShortcutActivator, Intent>{
          SingleActivator(LogicalKeyboardKey.arrowDown, alt: true): NextFocusIntent(),
          SingleActivator(LogicalKeyboardKey.arrowUp, alt: true): 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.',
              ),
            ),
          ],
        ),
      ),
    ),
  );
}

{@end-tool}

{@tool snippet}

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({ super.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: <ShortcutActivator, Intent>{
                const SingleActivator(LogicalKeyboardKey.arrowUp): IncrementCounterIntent(),
                const SingleActivator(LogicalKeyboardKey.arrowDown): DecrementCounterIntent(),
              },
              child: Actions(
                actions: <Type, Action<Intent>>{
                  IncrementCounterIntent: CallbackAction<IncrementCounterIntent>(
                    onInvoke: (IncrementCounterIntent intent) {
                      setState(() {
                        _counter++;
                      });
                      return null;
                    },
                  ),
                  DecrementCounterIntent: CallbackAction<DecrementCounterIntent>(
                    onInvoke: (DecrementCounterIntent intent) {
                      setState(() {
                        _counter--;
                      });
                      return null;
                    },
                  ),
                },
                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.',
              ),
            ),
          ],
        ),
      ),
    );
  }
}

{@end-tool}

See also:

  • WidgetsApp, which creates a DefaultTextEditingShortcuts.
Inheritance
Available extensions

Constructors

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

Properties

hashCode int
The hash code for this object.
no setterinherited
key Key?
Controls how one widget replaces another widget in the tree.
finalinherited
modifierKey Key?
The actual key of the widget, which Modifier wrapped
finalinherited
runtimeType Type
A representation of the runtime type of the object.
no setterinherited

Methods

build(BuildContext context) Widget
Describes the part of the user interface represented by this widget.
inherited
buildWithChild(BuildContext context, Widget? child) Widget
A build method that receives an extra child parameter.
override
createElement() SingleChildStatelessElement
Create a SingleChildStatelessElement
inherited
debugDescribeChildren() List<DiagnosticsNode>
Returns a list of DiagnosticsNode objects describing this node's children.
inherited
debugFillProperties(DiagnosticPropertiesBuilder properties) → void
Add additional properties associated with the node.
inherited
modified() Modifier

Available on Widget, provided by the ModifierTransformer extension

Transform normal widget to Modifier
noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent 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, int wrapWidth = 65}) 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.
inherited