text stringlengths 1 372 |
|---|
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(), |
), |
), |
); |
} |
<code_end> |
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. |
the final option is a global listener. this listener |
can be used for always-on, app-wide shortcuts or for |
panels that can accept shortcuts whenever they’re visible |
(regardless of their focus state). adding global listeners |
is easy with HardwareKeyboard: |
<code_start> |
@override |
void initState() { |
super.initState(); |
HardwareKeyboard.instance.addHandler(_handleKey); |
} |
@override |
void dispose() { |
HardwareKeyboard.instance.removeHandler(_handleKey); |
super.dispose(); |
} |
<code_end> |
to check key combinations with the global listener, |
you can use the HardwareKeyboard.instance.logicalKeysPressed set. |
for example, a method like the following can check whether any |
of the provided keys are being held down: |
<code_start> |
static bool isKeyDown(Set<LogicalKeyboardKey> keys) { |
return keys |
.intersection(hardwarekeyboard.instance.logicalkeyspressed) |
.isnotempty; |
} |
<code_end> |
putting these two things together, |
you can fire an action when Shift+N is pressed: |
<code_start> |
bool _handleKey(KeyEvent event) { |
bool isShiftDown = isKeyDown({ |
LogicalKeyboardKey.shiftLeft, |
LogicalKeyboardKey.shiftRight, |
}); |
if (isshiftdown && event.logicalKey == LogicalKeyboardKey.keyN) { |
_createNewItem(); |
return true; |
} |
return false; |
} |
<code_end> |
one note of caution when using the static listener, |
is that you often need to disable it when the user |
is typing in a field or when the widget it’s associated with |
is hidden from view. |
unlike with shortcuts or KeyboardListener, |
this is your responsibility to manage. this can be especially |
important when you’re binding a Delete/Backspace accelerator for |
delete, but then have child TextFields that the user |
might be typing in. |
<topic_end> |
<topic_start> |
mouse enter, exit, and hover |
on desktop, it’s common to change the mouse cursor |
to indicate the functionality about the content the |
mouse is hovering over. for example, you usually see |
a hand cursor when you hover over a button, |
or an i cursor when you hover over text. |
the material component set has built-in support |
for your standard button and text cursors. |
to change the cursor from within your own widgets, |
use MouseRegion: |
<code_start> |
// show hand cursor |
return MouseRegion( |
cursor: SystemMouseCursors.click, |
// request focus when clicked |
child: GestureDetector( |
onTap: () { |
Focus.of(context).requestFocus(); |
_submit(); |
}, |
child: Logo(showBorder: hasFocus), |
), |
); |
<code_end> |
MouseRegion is also useful for creating custom |
rollover and hover effects: |
<code_start> |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.