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

Commit

Permalink
Merge pull request #10 from 4inka/unfocus_fix
Browse files Browse the repository at this point in the history
Unfocus fix
  • Loading branch information
4inka authored Jan 13, 2022
2 parents 0a55c71 + acd4cf2 commit 4bd4f2f
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 35 deletions.
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
## 1.4.2 - [13-01-2022]

### Added
* Added `focusNode` parameter

### Fixed
* Correcting bug in which keyboard wouldn't close when pressing enter on keyboard

## 1.4.1 - [11-01-2022]

### Fixed
Expand Down
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ In the `pubspec.yaml` of your flutter project, add the following dependency:
``` yaml
dependencies:
...
easy_autocomplete: ^1.4.1
easy_autocomplete: ^1.4.2
```
## Basic example
Expand Down Expand Up @@ -218,6 +218,7 @@ The above example will generate something like below preview:
| initialValue | `String` | :x: | Can be used to set the textfield initial value | |
| textCapitalization | `TextCapitalization` | :x: | Can be used to set the text capitalization type | TextCapitalization.sentences |
| autofocus | `bool` | :x: | Determines if should gain focus on screen open | false |
| focusNode | `FocusNode` | :x: | Can be used to manage TextField focus | |
| keyboardType | `TextInputType` | :x: | Can be used to set different keyboardTypes to your field | TextInputType.text |
| cursorColor | `Color` | :x: | Can be used to set a custom color to the input cursor | Colors.blue |
| inputTextStyle | `TextStyle` | :x: | Can be used to set custom style to the suggestions textfield | |
Expand Down
2 changes: 1 addition & 1 deletion example/pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ packages:
path: ".."
relative: true
source: path
version: "1.4.1"
version: "1.4.2"
fake_async:
dependency: transitive
description:
Expand Down
77 changes: 45 additions & 32 deletions lib/easy_autocomplete.dart
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ class EasyAutocomplete extends StatefulWidget {
final bool autofocus;
/// Can be used to set different keyboardTypes to your field
final TextInputType keyboardType;
/// Can be used to manage TextField focus
final FocusNode? focusNode;
/// Can be used to set a custom color to the input cursor
final Color? cursorColor;
/// Can be used to set custom style to the suggestions textfield
Expand All @@ -80,6 +82,7 @@ class EasyAutocomplete extends StatefulWidget {
this.autofocus = false,
this.textCapitalization = TextCapitalization.sentences,
this.keyboardType = TextInputType.text,
this.focusNode,
this.cursorColor,
this.inputTextStyle = const TextStyle(),
this.suggestionTextStyle = const TextStyle(),
Expand All @@ -102,12 +105,18 @@ class _EasyAutocompleteState extends State<EasyAutocomplete> {
List<String> _suggestions = [];
Timer? _debounce;
String _previousAsyncSearchText = '';
late FocusNode _focusNode;

@override
void initState() {
super.initState();
_focusNode = widget.focusNode ?? FocusNode();
_controller = widget.controller ?? TextEditingController(text: widget.initialValue ?? '');
_controller.addListener(() => updateSuggestions(_controller.text) );
_focusNode.addListener(() {
if(_focusNode.hasFocus) openOverlay();
else closeOverlay();
});
}

void openOverlay() {
Expand Down Expand Up @@ -141,6 +150,7 @@ class _EasyAutocompleteState extends State<EasyAutocomplete> {
);
widget.onChanged!(value);
closeOverlay();
_focusNode.unfocus();
}
)
)
Expand Down Expand Up @@ -194,46 +204,49 @@ class _EasyAutocompleteState extends State<EasyAutocomplete> {
Widget build(BuildContext context) {
return CompositedTransformTarget(
link: _layerLink,
child: Focus(
child: Column(
mainAxisSize: MainAxisSize.min,
mainAxisAlignment: MainAxisAlignment.start,
crossAxisAlignment: CrossAxisAlignment.start,
children: [
TextFormField(
decoration: widget.decoration,
controller: _controller,
inputFormatters: widget.inputFormatter,
autofocus: widget.autofocus,
textCapitalization: widget.textCapitalization,
keyboardType: widget.keyboardType,
cursorColor: widget.cursorColor ?? Colors.blue,
style: widget.inputTextStyle,
onChanged: (value) {
openOverlay();
widget.onChanged!(value);
},
onFieldSubmitted: (value) {
closeOverlay();
widget.onChanged!(value);
},
onEditingComplete: () => closeOverlay()
)
]
),
onFocusChange: (hasFocus) {
if(hasFocus) openOverlay();
else closeOverlay();
}
child: Column(
mainAxisSize: MainAxisSize.min,
mainAxisAlignment: MainAxisAlignment.start,
crossAxisAlignment: CrossAxisAlignment.start,
children: [
TextFormField(
decoration: widget.decoration,
controller: _controller,
inputFormatters: widget.inputFormatter,
autofocus: widget.autofocus,
focusNode: _focusNode,
textCapitalization: widget.textCapitalization,
keyboardType: widget.keyboardType,
cursorColor: widget.cursorColor ?? Colors.blue,
style: widget.inputTextStyle,
onChanged: (value) => widget.onChanged!(value),
onFieldSubmitted: (value) {
closeOverlay();
widget.onChanged!(value);
_focusNode.unfocus();
},
onEditingComplete: () => closeOverlay()
)
]
)
);
}

@override
void dispose() {
if (_overlayEntry != null) _overlayEntry!.dispose();
if (widget.controller == null) _controller.dispose();
if (widget.controller == null) {
_controller.removeListener(() => updateSuggestions(_controller.text) );
_controller.dispose();
}
if (_debounce != null) _debounce?.cancel();
if (widget.focusNode == null) {
_focusNode.removeListener(() {
if(_focusNode.hasFocus) openOverlay();
else closeOverlay();
});
_focusNode.dispose();
}
super.dispose();
}
}
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: easy_autocomplete
description: A simple but flexible autocomplete TextFormField to help you display suggestions to your users while typing
version: 1.4.1
version: 1.4.2
homepage: https://github.com/4inka/flutter_easy_autocomplete

environment:
Expand Down

0 comments on commit 4bd4f2f

Please sign in to comment.