text stringlengths 1 372 |
|---|
object? invokeAction( |
covariant Action<Intent> action, |
covariant intent intent, [ |
BuildContext? context, |
]) { |
print('Action invoked: $action($intent) from $context'); |
super.invokeAction(action, intent, context); |
return null; |
} |
@override |
(bool, object?) invokeActionIfEnabled( |
covariant Action<Intent> action, |
covariant intent intent, [ |
BuildContext? context, |
]) { |
print('Action invoked: $action($intent) from $context'); |
return super.invokeActionIfEnabled(action, intent, context); |
} |
} |
<code_end> |
then you pass that to your top-level actions widget: |
<code_start> |
@override |
widget build(BuildContext context) { |
return actions( |
dispatcher: LoggingActionDispatcher(), |
actions: <type, Action<Intent>>{ |
SelectAllIntent: SelectAllAction(model), |
}, |
child: builder( |
builder: (context) => TextButton( |
onPressed: Actions.handler<SelectAllIntent>( |
context, |
const SelectAllIntent(), |
), |
child: const Text('SELECT ALL'), |
), |
), |
); |
} |
<code_end> |
this logs every action as it executes, like so: |
<topic_end> |
<topic_start> |
putting it together |
the combination of actions and shortcuts is powerful: you can define generic |
intents that map to specific actions at the widget level. here’s a simple app |
that illustrates the concepts described above. the app creates a text field that |
also has “select all” and “copy to clipboard” buttons next to it. the buttons |
invoke actions to accomplish their work. all the invoked actions and |
shortcuts are logged. |
<code_start> |
import 'package:flutter/material.dart'; |
import 'package:flutter/services.dart'; |
/// a text field that also has buttons to select all the text and copy the |
/// selected text to the clipboard. |
class CopyableTextField extends StatefulWidget { |
const CopyableTextField({super.key, required this.title}); |
final string title; |
@override |
State<CopyableTextField> createState() => _CopyableTextFieldState(); |
} |
class _CopyableTextFieldState extends State<CopyableTextField> { |
late final TextEditingController controller = TextEditingController(); |
@override |
void dispose() { |
controller.dispose(); |
super.dispose(); |
} |
@override |
widget build(BuildContext context) { |
return actions( |
dispatcher: LoggingActionDispatcher(), |
actions: <type, Action<Intent>>{ |
ClearIntent: ClearAction(controller), |
CopyIntent: CopyAction(controller), |
SelectAllIntent: SelectAllAction(controller), |
}, |
child: builder(builder: (context) { |
return scaffold( |
body: center( |
child: row( |
children: <widget>[ |
const spacer(), |
expanded( |
child: TextField(controller: controller), |
), |
IconButton( |
icon: const Icon(Icons.copy), |
onPressed: |
Actions.handler<CopyIntent>(context, const CopyIntent()), |
), |
IconButton( |
icon: const Icon(Icons.select_all), |
onPressed: Actions.handler<SelectAllIntent>( |
context, const SelectAllIntent()), |
), |
const spacer(), |
], |
), |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.