Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Autocomplete on restore form #606

Closed
wants to merge 3 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 21 additions & 9 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,15 @@ 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();
return suggestionList.isNotEmpty ? suggestionList : List.empty();
if (suggestionList.isNotEmpty && suggestionList.length == 1) {
widget.textEditingControllers[itemIndex].text = suggestionList.first;
if (itemIndex + 1 < focusNodes.length) {
focusNodes[itemIndex + 1].requestFocus();
}
return List.empty();
}
return suggestionList;
}
}
Loading