Skip to content
This repository was archived by the owner on Oct 3, 2024. It is now read-only.

Commit 0a55c71

Browse files
authored
Merge pull request #8 from 4inka/set_state_fix
Set state fix
2 parents d563aca + 8463d6e commit 0a55c71

File tree

5 files changed

+32
-32
lines changed

5 files changed

+32
-32
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
## 1.4.1 - [11-01-2022]
2+
3+
### Fixed
4+
* Correcting bug in which couldn't apply changes to TextField using setState
5+
16
## 1.4.0 - [07-01-2022]
27

38
### Changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ In the `pubspec.yaml` of your flutter project, add the following dependency:
3434
``` yaml
3535
dependencies:
3636
...
37-
easy_autocomplete: ^1.4.0
37+
easy_autocomplete: ^1.4.1
3838
```
3939
4040
## Basic example

example/pubspec.lock

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ packages:
5656
path: ".."
5757
relative: true
5858
source: path
59-
version: "1.4.0"
59+
version: "1.4.1"
6060
fake_async:
6161
dependency: transitive
6262
description:

lib/easy_autocomplete.dart

Lines changed: 24 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ class EasyAutocomplete extends StatefulWidget {
9595

9696
class _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
}

pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
name: easy_autocomplete
22
description: A simple but flexible autocomplete TextFormField to help you display suggestions to your users while typing
3-
version: 1.4.0
3+
version: 1.4.1
44
homepage: https://github.com/4inka/flutter_easy_autocomplete
55

66
environment:

0 commit comments

Comments
 (0)