|
| 1 | +import 'dart:ui' as ui; |
| 2 | + |
| 3 | +import 'package:flutter/gestures.dart'; |
1 | 4 | import 'package:flutter/material.dart'; |
2 | 5 | import 'package:languagetool_textfield/src/core/controllers/language_tool_controller.dart'; |
3 | 6 | import 'package:languagetool_textfield/src/utils/mistake_popup.dart'; |
4 | 7 | import 'package:languagetool_textfield/src/utils/popup_overlay_renderer.dart'; |
5 | 8 |
|
6 | 9 | /// A TextField widget that checks the grammar using the given |
7 | 10 | /// [LanguageToolController] |
8 | | -class LanguageToolTextField extends StatefulWidget { |
9 | | - /// A style to use for the text being edited. |
10 | | - final TextStyle? style; |
11 | | - |
12 | | - /// A decoration of this [TextField]. |
13 | | - final InputDecoration decoration; |
14 | | - |
15 | | - /// Color scheme to highlight mistakes |
16 | | - final LanguageToolController controller; |
| 11 | +class LanguageToolTextField extends TextField { |
| 12 | + /// LanguageToolController to highlight mistakes |
| 13 | + @override |
| 14 | + LanguageToolController get controller => |
| 15 | + super.controller as LanguageToolController? ?? LanguageToolController(); |
17 | 16 |
|
18 | 17 | /// Mistake popup window |
19 | 18 | final MistakePopup? mistakePopup; |
20 | 19 |
|
21 | | - /// The maximum number of lines to show at one time, wrapping if necessary. |
22 | | - final int? maxLines; |
23 | | - |
24 | | - /// The minimum number of lines to occupy when the content spans fewer lines. |
25 | | - final int? minLines; |
26 | | - |
27 | | - /// Whether this widget's height will be sized to fill its parent. |
28 | | - final bool expands; |
29 | | - |
30 | 20 | /// A language code like en-US, de-DE, fr, or auto to guess |
31 | 21 | /// the language automatically. |
32 | 22 | /// ```language``` = 'auto' by default. |
33 | 23 | final String language; |
34 | 24 |
|
35 | | - /// Determine text alignment |
36 | | - /// textAlign = [TextAlign.start] by default. |
37 | | - final TextAlign textAlign; |
38 | | - |
39 | | - /// Determine text Direction |
40 | | - final TextDirection? textDirection; |
41 | | - |
| 25 | + /// {@macro flutter.widgets.editableText.onChanged} |
42 | 26 | final ValueChanged<String>? onTextChange; |
| 27 | + |
| 28 | + /// {@macro flutter.widgets.editableText.onSubmitted} |
43 | 29 | final ValueChanged<String>? onTextSubmitted; |
44 | | - final VoidCallback? onTap; |
45 | | - final TapRegionCallback? onTapOutside; |
46 | | - final TextInputAction? textInputAction; |
47 | | - final TextInputType? keyboardType; |
48 | | - final Color? cursorColor; |
49 | | - final bool autoFocus; |
50 | | - final FocusNode? focusNode; |
51 | | - final Brightness? keyboardAppearance; |
52 | | - final bool autocorrect; |
53 | | - final bool readOnly; |
54 | | - final MouseCursor? mouseCursor; |
| 30 | + |
| 31 | + /// Whether to center align the text field widget. |
55 | 32 | final bool alignCenter; |
56 | 33 |
|
57 | 34 | /// Creates a widget that checks grammar errors. |
58 | 35 | const LanguageToolTextField({ |
59 | | - required this.controller, |
60 | | - this.style, |
61 | | - this.decoration = const InputDecoration(), |
62 | | - this.language = 'auto', |
| 36 | + required LanguageToolController super.controller, |
63 | 37 | this.mistakePopup, |
64 | | - this.maxLines = 1, |
65 | | - this.minLines, |
66 | | - this.expands = false, |
67 | | - this.textAlign = TextAlign.start, |
68 | | - this.textDirection, |
69 | | - this.cursorColor, |
70 | | - this.autocorrect = true, |
71 | | - this.autoFocus = false, |
72 | | - this.readOnly = false, |
73 | | - this.textInputAction, |
74 | | - this.keyboardType, |
75 | | - this.focusNode, |
76 | | - this.keyboardAppearance, |
77 | | - this.mouseCursor, |
78 | | - this.onTap, |
79 | | - this.onTapOutside, |
| 38 | + this.language = 'auto', |
80 | 39 | this.onTextChange, |
81 | 40 | this.onTextSubmitted, |
82 | 41 | this.alignCenter = true, |
| 42 | + super.focusNode, |
| 43 | + super.undoController, |
| 44 | + super.decoration = const InputDecoration(), |
| 45 | + super.keyboardType, |
| 46 | + super.textInputAction, |
| 47 | + super.textCapitalization = TextCapitalization.none, |
| 48 | + super.style, |
| 49 | + super.strutStyle, |
| 50 | + super.textAlign = TextAlign.start, |
| 51 | + super.textAlignVertical, |
| 52 | + super.textDirection, |
| 53 | + super.readOnly = false, |
| 54 | + super.showCursor, |
| 55 | + super.autofocus = false, |
| 56 | + super.statesController, |
| 57 | + super.obscuringCharacter = '•', |
| 58 | + super.obscureText = false, |
| 59 | + super.autocorrect = true, |
| 60 | + super.smartDashesType, |
| 61 | + super.smartQuotesType, |
| 62 | + super.enableSuggestions = true, |
| 63 | + super.maxLines = 1, |
| 64 | + super.minLines, |
| 65 | + super.expands = false, |
| 66 | + super.maxLength, |
| 67 | + super.maxLengthEnforcement, |
| 68 | + super.inputFormatters, |
| 69 | + super.enabled, |
| 70 | + super.ignorePointers, |
| 71 | + super.cursorWidth = 2.0, |
| 72 | + super.cursorHeight, |
| 73 | + super.cursorRadius, |
| 74 | + super.cursorOpacityAnimates, |
| 75 | + super.cursorColor, |
| 76 | + super.cursorErrorColor, |
| 77 | + super.selectionHeightStyle = ui.BoxHeightStyle.tight, |
| 78 | + super.selectionWidthStyle = ui.BoxWidthStyle.tight, |
| 79 | + super.keyboardAppearance, |
| 80 | + super.scrollPadding = const EdgeInsets.all(20.0), |
| 81 | + super.dragStartBehavior = DragStartBehavior.start, |
| 82 | + super.enableInteractiveSelection, |
| 83 | + super.selectionControls, |
| 84 | + super.onTap, |
| 85 | + super.onTapAlwaysCalled = false, |
| 86 | + super.onTapOutside, |
| 87 | + super.onTapUpOutside, |
| 88 | + super.mouseCursor, |
| 89 | + super.buildCounter, |
| 90 | + super.scrollController, |
| 91 | + super.scrollPhysics, |
| 92 | + super.autofillHints, |
| 93 | + super.contentInsertionConfiguration, |
| 94 | + super.clipBehavior = Clip.hardEdge, |
| 95 | + super.restorationId, |
| 96 | + super.stylusHandwritingEnabled = true, |
| 97 | + super.enableIMEPersonalizedLearning = true, |
| 98 | + super.contextMenuBuilder, |
| 99 | + super.canRequestFocus = true, |
| 100 | + super.spellCheckConfiguration, |
| 101 | + super.magnifierConfiguration, |
83 | 102 | super.key, |
84 | 103 | }); |
85 | 104 |
|
@@ -121,33 +140,76 @@ class _LanguageToolTextFieldState extends State<LanguageToolTextField> { |
121 | 140 | ), |
122 | 141 | ); |
123 | 142 |
|
124 | | - final inputDecoration = widget.decoration.copyWith( |
| 143 | + final inputDecoration = widget.decoration?.copyWith( |
125 | 144 | suffix: fetchError != null ? httpErrorText : null, |
126 | 145 | ); |
127 | 146 |
|
128 | 147 | Widget childWidget = TextField( |
129 | | - textAlign: widget.textAlign, |
130 | | - textDirection: widget.textDirection, |
131 | | - focusNode: _focusNode, |
132 | 148 | controller: widget.controller, |
| 149 | + focusNode: _focusNode, |
133 | 150 | scrollController: _scrollController, |
134 | 151 | decoration: inputDecoration, |
135 | | - minLines: widget.minLines, |
136 | | - maxLines: widget.maxLines, |
137 | | - expands: widget.expands, |
138 | | - style: widget.style, |
139 | | - cursorColor: widget.cursorColor, |
140 | | - autocorrect: widget.autocorrect, |
141 | | - textInputAction: widget.textInputAction, |
142 | | - keyboardAppearance: widget.keyboardAppearance, |
143 | 152 | keyboardType: widget.keyboardType, |
144 | | - autofocus: widget.autoFocus, |
| 153 | + textInputAction: widget.textInputAction, |
| 154 | + textCapitalization: widget.textCapitalization, |
| 155 | + style: widget.style, |
| 156 | + strutStyle: widget.strutStyle, |
| 157 | + textAlign: widget.textAlign, |
| 158 | + textAlignVertical: widget.textAlignVertical, |
| 159 | + textDirection: widget.textDirection, |
| 160 | + autofocus: widget.autofocus, |
| 161 | + statesController: widget.statesController, |
145 | 162 | readOnly: widget.readOnly, |
146 | | - mouseCursor: widget.mouseCursor, |
| 163 | + showCursor: widget.showCursor, |
| 164 | + obscuringCharacter: widget.obscuringCharacter, |
| 165 | + obscureText: widget.obscureText, |
| 166 | + autocorrect: widget.autocorrect, |
| 167 | + smartDashesType: widget.smartDashesType, |
| 168 | + smartQuotesType: widget.smartQuotesType, |
| 169 | + enableSuggestions: widget.enableSuggestions, |
| 170 | + maxLengthEnforcement: widget.maxLengthEnforcement, |
| 171 | + maxLines: widget.maxLines, |
| 172 | + minLines: widget.minLines, |
| 173 | + expands: widget.expands, |
| 174 | + maxLength: widget.maxLength, |
147 | 175 | onChanged: widget.onTextChange, |
148 | | - onSubmitted: widget.onTextSubmitted, |
149 | 176 | onTap: widget.onTap, |
| 177 | + onTapAlwaysCalled: widget.onTapAlwaysCalled, |
150 | 178 | onTapOutside: widget.onTapOutside, |
| 179 | + onTapUpOutside: widget.onTapUpOutside, |
| 180 | + onEditingComplete: widget.onEditingComplete, |
| 181 | + onSubmitted: widget.onTextSubmitted, |
| 182 | + inputFormatters: widget.inputFormatters, |
| 183 | + enabled: widget.enabled, |
| 184 | + ignorePointers: widget.ignorePointers, |
| 185 | + cursorWidth: widget.cursorWidth, |
| 186 | + cursorHeight: widget.cursorHeight, |
| 187 | + cursorRadius: widget.cursorRadius, |
| 188 | + cursorColor: widget.cursorColor, |
| 189 | + cursorErrorColor: widget.cursorErrorColor, |
| 190 | + scrollPadding: widget.scrollPadding, |
| 191 | + scrollPhysics: widget.scrollPhysics, |
| 192 | + keyboardAppearance: widget.keyboardAppearance, |
| 193 | + enableInteractiveSelection: widget.enableInteractiveSelection, |
| 194 | + selectionControls: widget.selectionControls, |
| 195 | + buildCounter: widget.buildCounter, |
| 196 | + autofillHints: widget.autofillHints, |
| 197 | + mouseCursor: widget.mouseCursor, |
| 198 | + contextMenuBuilder: widget.contextMenuBuilder, |
| 199 | + spellCheckConfiguration: widget.spellCheckConfiguration, |
| 200 | + magnifierConfiguration: widget.magnifierConfiguration, |
| 201 | + undoController: widget.undoController, |
| 202 | + onAppPrivateCommand: widget.onAppPrivateCommand, |
| 203 | + cursorOpacityAnimates: widget.cursorOpacityAnimates, |
| 204 | + selectionHeightStyle: widget.selectionHeightStyle, |
| 205 | + selectionWidthStyle: widget.selectionWidthStyle, |
| 206 | + dragStartBehavior: widget.dragStartBehavior, |
| 207 | + contentInsertionConfiguration: widget.contentInsertionConfiguration, |
| 208 | + clipBehavior: widget.clipBehavior, |
| 209 | + stylusHandwritingEnabled: widget.stylusHandwritingEnabled, |
| 210 | + canRequestFocus: widget.canRequestFocus, |
| 211 | + enableIMEPersonalizedLearning: widget.enableIMEPersonalizedLearning, |
| 212 | + restorationId: widget.restorationId, |
151 | 213 | ); |
152 | 214 |
|
153 | 215 | if (widget.alignCenter) { |
|
0 commit comments