text
stringlengths 1
474
|
|---|
border: OutlineInputBorder(),
|
),
|
),
|
),
|
);
|
}
|
}<code_end>
|
If you’d like to apply a set of keyboard shortcuts to a
|
large section of the tree, you can use the Shortcuts widget:
|
<code_start>// 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(),
|
),
|
),
|
);
|
}<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();
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.