diff --git a/examples/ui/actions_and_shortcuts/lib/copyable_text.dart b/examples/ui/actions_and_shortcuts/lib/copyable_text.dart index 73a05a7ac7c..b43613a2559 100644 --- a/examples/ui/actions_and_shortcuts/lib/copyable_text.dart +++ b/examples/ui/actions_and_shortcuts/lib/copyable_text.dart @@ -14,10 +14,12 @@ class CopyableTextField extends StatefulWidget { class _CopyableTextFieldState extends State { late final TextEditingController controller = TextEditingController(); + late final FocusNode focusNode = FocusNode(); @override void dispose() { controller.dispose(); + focusNode.dispose(); super.dispose(); } @@ -28,7 +30,7 @@ class _CopyableTextFieldState extends State { actions: >{ ClearIntent: ClearAction(controller), CopyIntent: CopyAction(controller), - SelectAllIntent: SelectAllAction(controller), + SelectAllIntent: SelectAllAction(controller, focusNode), }, child: Builder( builder: (context) { @@ -37,7 +39,12 @@ class _CopyableTextFieldState extends State { child: Row( children: [ const Spacer(), - Expanded(child: TextField(controller: controller)), + Expanded( + child: TextField( + controller: controller, + focusNode: focusNode, + ), + ), IconButton( icon: const Icon(Icons.copy), onPressed: Actions.handler( @@ -145,9 +152,10 @@ class SelectAllIntent extends Intent { /// An action that is bound to SelectAllAction that selects all text in its /// TextEditingController. class SelectAllAction extends Action { - SelectAllAction(this.controller); + SelectAllAction(this.controller, this.focusNode); final TextEditingController controller; + final FocusNode focusNode; @override Object? invoke(covariant SelectAllIntent intent) { @@ -157,6 +165,8 @@ class SelectAllAction extends Action { affinity: controller.selection.affinity, ); + focusNode.requestFocus(); + return null; } } diff --git a/src/content/ui/interactivity/actions-and-shortcuts.md b/src/content/ui/interactivity/actions-and-shortcuts.md index e31fea3549b..66e7e93f32a 100644 --- a/src/content/ui/interactivity/actions-and-shortcuts.md +++ b/src/content/ui/interactivity/actions-and-shortcuts.md @@ -446,10 +446,12 @@ class CopyableTextField extends StatefulWidget { class _CopyableTextFieldState extends State { late final TextEditingController controller = TextEditingController(); + late final FocusNode focusNode = FocusNode(); @override void dispose() { controller.dispose(); + focusNode.dispose(); super.dispose(); } @@ -460,7 +462,7 @@ class _CopyableTextFieldState extends State { actions: >{ ClearIntent: ClearAction(controller), CopyIntent: CopyAction(controller), - SelectAllIntent: SelectAllAction(controller), + SelectAllIntent: SelectAllAction(controller, focusNode), }, child: Builder( builder: (context) { @@ -469,7 +471,12 @@ class _CopyableTextFieldState extends State { child: Row( children: [ const Spacer(), - Expanded(child: TextField(controller: controller)), + Expanded( + child: TextField( + controller: controller, + focusNode: focusNode, + ), + ), IconButton( icon: const Icon(Icons.copy), onPressed: Actions.handler( @@ -577,9 +584,10 @@ class SelectAllIntent extends Intent { /// An action that is bound to SelectAllAction that selects all text in its /// TextEditingController. class SelectAllAction extends Action { - SelectAllAction(this.controller); + SelectAllAction(this.controller, this.focusNode); final TextEditingController controller; + final FocusNode focusNode; @override Object? invoke(covariant SelectAllIntent intent) { @@ -589,6 +597,8 @@ class SelectAllAction extends Action { affinity: controller.selection.affinity, ); + focusNode.requestFocus(); + return null; } }