|
| 1 | +// Copyright 2014 The Flutter Authors. All rights reserved. |
| 2 | +// Use of this source code is governed by a BSD-style license that can be |
| 3 | +// found in the LICENSE file. |
| 4 | + |
| 5 | +import 'package:flutter/material.dart'; |
| 6 | +import 'package:flutter/services.dart'; |
| 7 | + |
| 8 | +/// Flutter code sample for [TextField]. |
| 9 | +
|
| 10 | +void main() { |
| 11 | + runApp(const TextFieldExampleApp()); |
| 12 | +} |
| 13 | + |
| 14 | +class TextFieldExampleApp extends StatelessWidget { |
| 15 | + const TextFieldExampleApp({super.key}); |
| 16 | + |
| 17 | + @override |
| 18 | + Widget build(BuildContext context) { |
| 19 | + return MaterialApp( |
| 20 | + home: Scaffold( |
| 21 | + appBar: AppBar(title: const Text('TextField Shift+Enter Example')), |
| 22 | + body: const TextFieldShiftEnterExample(), |
| 23 | + ), |
| 24 | + ); |
| 25 | + } |
| 26 | +} |
| 27 | + |
| 28 | +class TextFieldShiftEnterExample extends StatefulWidget { |
| 29 | + const TextFieldShiftEnterExample({super.key}); |
| 30 | + |
| 31 | + @override |
| 32 | + State<TextFieldShiftEnterExample> createState() => _TextFieldShiftEnterExampleState(); |
| 33 | +} |
| 34 | + |
| 35 | +class _TextFieldShiftEnterExampleState extends State<TextFieldShiftEnterExample> { |
| 36 | + final FocusNode _focusNode = FocusNode(); |
| 37 | + |
| 38 | + final TextEditingController _controller = TextEditingController(); |
| 39 | + |
| 40 | + String? _submittedText; |
| 41 | + |
| 42 | + @override |
| 43 | + void dispose() { |
| 44 | + _focusNode.dispose(); |
| 45 | + _controller.dispose(); |
| 46 | + super.dispose(); |
| 47 | + } |
| 48 | + |
| 49 | + @override |
| 50 | + Widget build(BuildContext context) { |
| 51 | + return Column( |
| 52 | + children: <Widget>[ |
| 53 | + Expanded( |
| 54 | + child: Center( |
| 55 | + child: Text( |
| 56 | + _submittedText == null |
| 57 | + ? 'Please submit some text\n\n' |
| 58 | + 'Press Shift+Enter for a new line\n' |
| 59 | + 'Press Enter to submit' |
| 60 | + : 'Submitted text:\n\n${_submittedText!}', |
| 61 | + textAlign: TextAlign.center, |
| 62 | + ), |
| 63 | + ), |
| 64 | + ), |
| 65 | + Shortcuts( |
| 66 | + shortcuts: <ShortcutActivator, Intent>{ |
| 67 | + // Map the `Shift+Enter` combination to our custom intent. |
| 68 | + const SingleActivator(LogicalKeyboardKey.enter, shift: true): |
| 69 | + _InsertNewLineTextIntent(), |
| 70 | + }, |
| 71 | + child: Actions( |
| 72 | + actions: <Type, Action<Intent>>{ |
| 73 | + // When the _InsertNewLineTextIntent is invoked, CallbackAction's |
| 74 | + // onInvoke callback is executed. |
| 75 | + _InsertNewLineTextIntent: CallbackAction<_InsertNewLineTextIntent>( |
| 76 | + onInvoke: (_InsertNewLineTextIntent intent) { |
| 77 | + final TextEditingValue value = _controller.value; |
| 78 | + final String newText = value.text.replaceRange( |
| 79 | + value.selection.start, |
| 80 | + value.selection.end, |
| 81 | + '\n', |
| 82 | + ); |
| 83 | + _controller.value = value.copyWith( |
| 84 | + text: newText, |
| 85 | + selection: TextSelection.collapsed(offset: value.selection.start + 1), |
| 86 | + ); |
| 87 | + |
| 88 | + return null; |
| 89 | + }, |
| 90 | + ), |
| 91 | + }, |
| 92 | + child: Padding( |
| 93 | + padding: const EdgeInsets.all(12), |
| 94 | + child: TextField( |
| 95 | + focusNode: _focusNode, |
| 96 | + autofocus: true, |
| 97 | + controller: _controller, |
| 98 | + decoration: const InputDecoration(border: OutlineInputBorder(), labelText: 'Text'), |
| 99 | + maxLines: null, |
| 100 | + textInputAction: TextInputAction.done, |
| 101 | + onSubmitted: (String? text) { |
| 102 | + setState(() { |
| 103 | + _submittedText = text; |
| 104 | + _controller.clear(); |
| 105 | + _focusNode.requestFocus(); |
| 106 | + }); |
| 107 | + }, |
| 108 | + ), |
| 109 | + ), |
| 110 | + ), |
| 111 | + ), |
| 112 | + ], |
| 113 | + ); |
| 114 | + } |
| 115 | +} |
| 116 | + |
| 117 | +/// A custom [Intent] to represent the action of inserting a newline. |
| 118 | +class _InsertNewLineTextIntent extends Intent {} |
0 commit comments