text stringlengths 1 474 |
|---|
onPressed: () { |
// Some code to undo the change. |
}, |
), |
);<code_end> |
<topic_end> |
<topic_start> |
Interactive example |
info Note |
In this example, the SnackBar displays when a user taps a button. |
For more information on working with user input, |
see the Gestures section of the cookbook. |
<code_start>import 'package:flutter/material.dart'; |
void main() => runApp(const SnackBarDemo()); |
class SnackBarDemo extends StatelessWidget { |
const SnackBarDemo({super.key}); |
@override |
Widget build(BuildContext context) { |
return MaterialApp( |
title: 'SnackBar Demo', |
home: Scaffold( |
appBar: AppBar( |
title: const Text('SnackBar Demo'), |
), |
body: const SnackBarPage(), |
), |
); |
} |
} |
class SnackBarPage extends StatelessWidget { |
const SnackBarPage({super.key}); |
@override |
Widget build(BuildContext context) { |
return Center( |
child: ElevatedButton( |
onPressed: () { |
final snackBar = SnackBar( |
content: const Text('Yay! A SnackBar!'), |
action: SnackBarAction( |
label: 'Undo', |
onPressed: () { |
// Some code to undo the change. |
}, |
), |
); |
// Find the ScaffoldMessenger in the widget tree |
// and use it to show a SnackBar. |
ScaffoldMessenger.of(context).showSnackBar(snackBar); |
}, |
child: const Text('Show SnackBar'), |
), |
); |
} |
}<code_end> |
<topic_end> |
<topic_start>Using Actions and Shortcuts |
This page describes how to bind physical keyboard events to actions in the user |
interface. For instance, to define keyboard shortcuts in your application, this |
page is for you.<topic_end> |
<topic_start> |
Overview |
For a GUI application to do anything, it has to have actions: users want to tell |
the application to do something. Actions are often simple functions that |
directly perform the action (such as set a value or save a file). In a larger |
application, however, things are more complex: the code for invoking the action, |
and the code for the action itself might need to be in different places. |
Shortcuts (key bindings) might need definition at a level that knows nothing |
about the actions they invoke.That’s where Flutter’s actions and shortcuts system comes in. It allows |
developers to define actions that fulfill intents bound to them. In this |
context, an intent is a generic action that the user wishes to perform, and an |
Intent class instance represents these user intents in Flutter. An |
Intent can be general purpose, fulfilled by different actions in different |
contexts. An Action can be a simple callback (as in the case of |
the CallbackAction) or something more complex that integrates with entire |
undo/redo architectures (for example) or other logic.Shortcuts are key bindings that activate by pressing a key or combination |
of keys. The key combinations reside in a table with their bound intent. When |
the Shortcuts widget invokes them, it sends their matching intent to the |
actions subsystem for fulfillment.To illustrate the concepts in actions and shortcuts, this article creates a |
simple app that allows a user to select and copy text in a text field using both |
buttons and shortcuts.<topic_end> |
<topic_start> |
Why separate Actions from Intents? |
You might wonder: why not just map a key combination directly to an action? Why |
have intents at all? This is because it is useful to have a separation of |
concerns between where the key mapping definitions are (often at a high level), |
and where the action definitions are (often at a low level), and because it is |
important to be able to have a single key combination map to an intended |
operation in an app, and have it adapt automatically to whichever action |
fulfills that intended operation for the focused context.For instance, Flutter has an ActivateIntent widget that maps each type of |
control to its corresponding version of an ActivateAction (and that executes |
the code that activates the control). This code often needs fairly private |
access to do its work. If the extra layer of indirection that Intents provide |
didn’t exist, it would be necessary to elevate the definition of the actions to |
where the defining instance of the Shortcuts widget could see them, causing |
the shortcuts to have more knowledge than necessary about which action to |
invoke, and to have access to or provide state that it wouldn’t necessarily have |
or need otherwise. This allows your code to separate the two concerns to be more |
independent.Intents configure an action so that the same action can serve multiple uses. An |
example of this is DirectionalFocusIntent, which takes a direction to move |
the focus in, allowing the DirectionalFocusAction to know which direction to |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.