@@ -95,7 +95,7 @@ class EasyAutocomplete extends StatefulWidget {
9595
9696class _EasyAutocompleteState extends State <EasyAutocomplete > {
9797 final LayerLink _layerLink = LayerLink ();
98- late TextFormField _textFormField ;
98+ late TextEditingController _controller ;
9999 bool _hasOpenedOverlay = false ;
100100 bool _isLoading = false ;
101101 OverlayEntry ? _overlayEntry;
@@ -106,31 +106,8 @@ class _EasyAutocompleteState extends State<EasyAutocomplete> {
106106 @override
107107 void initState () {
108108 super .initState ();
109- _textFormField = TextFormField (
110- decoration: widget.decoration,
111- controller: widget.controller ?? TextEditingController (),
112- inputFormatters: widget.inputFormatter,
113- autofocus: widget.autofocus,
114- textCapitalization: widget.textCapitalization,
115- keyboardType: widget.keyboardType,
116- cursorColor: widget.cursorColor ?? Colors .blue,
117- style: widget.inputTextStyle,
118- onChanged: (value) {
119- openOverlay ();
120- widget.onChanged !(value);
121- },
122- onFieldSubmitted: (value) {
123- closeOverlay ();
124- widget.onChanged !(value);
125- },
126- onEditingComplete: () => closeOverlay (),
127- );
128- if (widget.controller == null ) {
129- _textFormField.controller! .text = widget.initialValue ?? '' ;
130- }
131- _textFormField.controller! .addListener (() {
132- updateSuggestions (_textFormField.controller! .text);
133- });
109+ _controller = widget.controller ?? TextEditingController (text: widget.initialValue ?? '' );
110+ _controller.addListener (() => updateSuggestions (_controller.text) );
134111 }
135112
136113 void openOverlay () {
@@ -155,7 +132,7 @@ class _EasyAutocompleteState extends State<EasyAutocomplete> {
155132 suggestionTextStyle: widget.suggestionTextStyle,
156133 suggestionBackgroundColor: widget.suggestionBackgroundColor,
157134 onItemTapped: (value) {
158- _textFormField.controller !
135+ _controller
159136 ..value = TextEditingValue (
160137 text: value,
161138 selection: TextSelection .collapsed (
@@ -223,7 +200,25 @@ class _EasyAutocompleteState extends State<EasyAutocomplete> {
223200 mainAxisAlignment: MainAxisAlignment .start,
224201 crossAxisAlignment: CrossAxisAlignment .start,
225202 children: [
226- _textFormField
203+ TextFormField (
204+ decoration: widget.decoration,
205+ controller: _controller,
206+ inputFormatters: widget.inputFormatter,
207+ autofocus: widget.autofocus,
208+ textCapitalization: widget.textCapitalization,
209+ keyboardType: widget.keyboardType,
210+ cursorColor: widget.cursorColor ?? Colors .blue,
211+ style: widget.inputTextStyle,
212+ onChanged: (value) {
213+ openOverlay ();
214+ widget.onChanged !(value);
215+ },
216+ onFieldSubmitted: (value) {
217+ closeOverlay ();
218+ widget.onChanged !(value);
219+ },
220+ onEditingComplete: () => closeOverlay ()
221+ )
227222 ]
228223 ),
229224 onFocusChange: (hasFocus) {
@@ -237,7 +232,7 @@ class _EasyAutocompleteState extends State<EasyAutocomplete> {
237232 @override
238233 void dispose () {
239234 if (_overlayEntry != null ) _overlayEntry! .dispose ();
240- if (widget.controller == null ) _textFormField.controller ! .dispose ();
235+ if (widget.controller == null ) _controller .dispose ();
241236 if (_debounce != null ) _debounce? .cancel ();
242237 super .dispose ();
243238 }
0 commit comments