From 52b4f725f74d092cda1daec10c2cf068830c9829 Mon Sep 17 00:00:00 2001 From: Arpan Patel <47818179+arps18@users.noreply.github.com> Date: Thu, 5 Nov 2020 05:51:02 +0530 Subject: [PATCH] AdoptAWidget: Shortcut (#69568) * Added Interactive Sample --- .../flutter/lib/src/widgets/shortcuts.dart | 51 +++++++++++++++++++ 1 file changed, 51 insertions(+) diff --git a/packages/flutter/lib/src/widgets/shortcuts.dart b/packages/flutter/lib/src/widgets/shortcuts.dart index 979870b2a9..1ce5ae3918 100644 --- a/packages/flutter/lib/src/widgets/shortcuts.dart +++ b/packages/flutter/lib/src/widgets/shortcuts.dart @@ -386,6 +386,57 @@ class ShortcutManager extends ChangeNotifier with Diagnosticable { /// when invoking an [Action] via a keyboard key combination that maps to an /// [Intent]. /// +/// {@tool dartpad --template=stateful_widget_scaffold_center} +/// +/// Here, we will use a [Shortcuts] and [Actions] widget to add and remove from a counter. +/// This can be done by creating a child widget that is focused and pressing the logical key +/// sets that have been defined in [Shortcuts] and defining the actions that each key set +/// performs. +/// +/// ```dart imports +/// import 'package:flutter/services.dart'; +/// ``` +/// +/// ```dart preamble +/// class Increment extends Intent {} +/// +/// class Decrement extends Intent {} +/// ``` +/// +/// ```dart +/// int count = 0; +/// +/// Widget build(BuildContext context) { +/// return Shortcuts( +/// shortcuts: { +/// LogicalKeySet(LogicalKeyboardKey.shift, LogicalKeyboardKey.keyK): Increment(), +/// LogicalKeySet(LogicalKeyboardKey.shift, LogicalKeyboardKey.keyL): Decrement(), +/// }, +/// child: Actions( +/// actions: > { +/// Increment: CallbackAction( +/// onInvoke: (Increment intent) => setState(() { count = count + 1; }), +/// ), +/// Decrement: CallbackAction( +/// onInvoke: (Decrement intent) => setState(() { count = count - 1; }), +/// ), +/// }, +/// child: Focus( +/// autofocus:true, +/// child: Column( +/// children: [ +/// Text('Add to the counter by pressing keyboard Shift and keyboard "K"'), +/// Text('Subtract from the counter by pressing keyboard Shift and keyboard "L"'), +/// Text('count: $count'), +/// ], +/// ), +/// ), +/// ), +/// ); +/// } +/// ``` +/// {@end-tool} +/// /// See also: /// /// * [Intent], a class for containing a description of a user action to be