@@ -9,42 +9,54 @@ class EnvironmentTriggerField extends StatefulWidget {
9
9
super .key,
10
10
required this .keyId,
11
11
this .initialValue,
12
+ this .controller,
13
+ this .focusNode,
12
14
this .onChanged,
13
15
this .onFieldSubmitted,
14
16
this .style,
15
17
this .decoration,
16
18
this .optionsWidthFactor,
17
- });
19
+ this .autocompleteNoTrigger,
20
+ }) : assert (
21
+ ! (controller != null && initialValue != null ),
22
+ 'controller and initialValue cannot be simultaneously defined.' ,
23
+ );
18
24
19
25
final String keyId;
20
26
final String ? initialValue;
27
+ final TextEditingController ? controller;
28
+ final FocusNode ? focusNode;
21
29
final void Function (String )? onChanged;
22
30
final void Function (String )? onFieldSubmitted;
23
31
final TextStyle ? style;
24
32
final InputDecoration ? decoration;
25
33
final double ? optionsWidthFactor;
34
+ final AutocompleteNoTrigger ? autocompleteNoTrigger;
26
35
27
36
@override
28
37
State <EnvironmentTriggerField > createState () =>
29
38
EnvironmentTriggerFieldState ();
30
39
}
31
40
32
41
class EnvironmentTriggerFieldState extends State <EnvironmentTriggerField > {
33
- final TextEditingController controller = TextEditingController () ;
34
- final FocusNode focusNode = FocusNode () ;
42
+ late TextEditingController controller;
43
+ late FocusNode _focusNode ;
35
44
36
45
@override
37
46
void initState () {
38
47
super .initState ();
39
- controller.text = widget.initialValue ?? '' ;
40
- controller.selection =
41
- TextSelection .collapsed (offset: controller.text.length);
48
+ controller = widget.controller ??
49
+ TextEditingController .fromValue (TextEditingValue (
50
+ text: widget.initialValue! ,
51
+ selection:
52
+ TextSelection .collapsed (offset: widget.initialValue! .length)));
53
+ _focusNode = widget.focusNode ?? FocusNode ();
42
54
}
43
55
44
56
@override
45
57
void dispose () {
46
58
controller.dispose ();
47
- focusNode .dispose ();
59
+ _focusNode .dispose ();
48
60
super .dispose ();
49
61
}
50
62
@@ -53,9 +65,11 @@ class EnvironmentTriggerFieldState extends State<EnvironmentTriggerField> {
53
65
super .didUpdateWidget (oldWidget);
54
66
if ((oldWidget.keyId != widget.keyId) ||
55
67
(oldWidget.initialValue != widget.initialValue)) {
56
- controller.text = widget.initialValue ?? "" ;
57
- controller.selection =
58
- TextSelection .collapsed (offset: controller.text.length);
68
+ controller = widget.controller ??
69
+ TextEditingController .fromValue (TextEditingValue (
70
+ text: widget.initialValue! ,
71
+ selection: TextSelection .collapsed (
72
+ offset: widget.initialValue! .length)));
59
73
}
60
74
}
61
75
@@ -64,9 +78,10 @@ class EnvironmentTriggerFieldState extends State<EnvironmentTriggerField> {
64
78
return MultiTriggerAutocomplete (
65
79
key: Key (widget.keyId),
66
80
textEditingController: controller,
67
- focusNode: focusNode ,
68
- optionsWidthFactor: widget.optionsWidthFactor,
81
+ focusNode: _focusNode ,
82
+ optionsWidthFactor: widget.optionsWidthFactor ?? 1 ,
69
83
autocompleteTriggers: [
84
+ if (widget.autocompleteNoTrigger != null ) widget.autocompleteNoTrigger! ,
70
85
AutocompleteTrigger (
71
86
trigger: '{' ,
72
87
triggerEnd: "}}" ,
@@ -108,7 +123,7 @@ class EnvironmentTriggerFieldState extends State<EnvironmentTriggerField> {
108
123
onSubmitted: widget.onFieldSubmitted,
109
124
specialTextSpanBuilder: EnvRegExpSpanBuilder (),
110
125
onTapOutside: (event) {
111
- focusNode .unfocus ();
126
+ _focusNode .unfocus ();
112
127
},
113
128
);
114
129
},
0 commit comments