Skip to content

Commit

Permalink
Autocomplete on restore form
Browse files Browse the repository at this point in the history
closes #505
  • Loading branch information
erdemyerebasmaz committed Jul 17, 2023
1 parent 4d0fbfe commit 0d2c93e
Showing 1 changed file with 17 additions and 8 deletions.
25 changes: 17 additions & 8 deletions lib/routes/initial_walkthrough/mnemonics/widgets/restore_form.dart
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,9 @@ class RestoreFormPageState extends State<RestoreForm> {
),
autovalidateMode: _autoValidateMode,
validator: (text) => _onValidate(context, text!),
suggestionsCallback: _getSuggestions,
suggestionsCallback: (pattern) => _getSuggestions(pattern, itemIndex),
hideOnEmpty: true,
hideOnLoading: true,
autoFlipDirection: true,
suggestionsBoxDecoration: const SuggestionsBoxDecoration(
color: Colors.white,
Expand Down Expand Up @@ -95,19 +96,23 @@ class RestoreFormPageState extends State<RestoreForm> {
),
);
},
onSuggestionSelected: <String>(suggestion) {
widget.textEditingControllers[itemIndex].text = suggestion;
if (itemIndex + 1 < focusNodes.length) {
focusNodes[itemIndex + 1].requestFocus();
}
},
onSuggestionSelected: (suggestion) => _selectSuggestion(suggestion, itemIndex),
);
}),
),
),
);
}

Function _selectSuggestion(String suggestion, int itemIndex) {
return <String>(suggestion) {
widget.textEditingControllers[itemIndex].text = suggestion;
if (itemIndex + 1 < focusNodes.length) {
focusNodes[itemIndex + 1].requestFocus();
}
};
}

String? _onValidate(BuildContext context, String text) {
final texts = context.texts();
if (text.isEmpty) {
Expand All @@ -119,8 +124,12 @@ class RestoreFormPageState extends State<RestoreForm> {
return null;
}

FutureOr<List<String>> _getSuggestions(pattern) {
FutureOr<List<String>> _getSuggestions(String pattern, int itemIndex) {
var suggestionList = WORDLIST.where((item) => item.startsWith(pattern)).toList();
if (pattern.length == 4 && suggestionList.isNotEmpty) {
_selectSuggestion(suggestionList.first, itemIndex);
return List.empty();
}
return suggestionList.isNotEmpty ? suggestionList : List.empty();
}
}

0 comments on commit 0d2c93e

Please sign in to comment.