-
-
Notifications
You must be signed in to change notification settings - Fork 64
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
Preserve Search Text After Tapping Suggestions #172
Comments
Hi @EluvK, Thanks for filing the issue.
This would by design go against what this package is intended to do, However you can achieve that by implicitly keeping track of the searched text and updating the field value using a controller. With this approach you will also need to add your Search logic in import 'dart:developer' as d;
import 'package:flutter/material.dart';
import 'package:searchfield/searchfield.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple),
useMaterial3: true,
),
home: Code());
}
}
class Code extends StatefulWidget {
const Code({super.key});
@override
State<Code> createState() => _CodeState();
}
class _CodeState extends State<Code> {
final TextEditingController controller = TextEditingController();
String lastSearch = '';
@override
Widget build(BuildContext context) {
return Scaffold(
body: Center(
child: SearchField(
controller: controller,
suggestions: ['ABC', 'DEF', 'GHI', 'JKL']
.map((e) => SearchFieldListItem(e, child: Text(e)))
.toList(),
suggestionAction: SuggestionAction.unfocus,
onSearchTextChanged: (query) {
lastSearch = query;
List<String> filter;
query = query.trim();
if (query.isEmpty) {
filter = ['ABC', 'DEF', 'GHI', 'JKL'];
} else {
filter = ['ABC', 'DEF', 'GHI', 'JKL']
.where((element) =>
element.toLowerCase().contains(query.toLowerCase()))
.toList();
}
return filter
.map((e) => SearchFieldListItem<String>(e, child: Text(e)))
.toList();
},
onSuggestionTap: (x) {
controller.text = lastSearch;
},
onSubmit: (v) => print(v),
),
),
);
}
} |
@maheshj01 Thank you so much for these detailed explanations. I tried to resolve my issue by making the However, your explanation is also worth trying, and I will give it a shot. It might be more suitable, especially since I have some ideas to add commands and operators to the search text.
I totally agree with you. However, in case anyone else comes across the same problem and find this, my simple but crude solution might also help them, so I've shared it here. EluvK@6d916fb Since it's no longer an issue for me, I'm closing this issue. Thanks again for your help. 😊 |
@EluvK When you say command based search, I think #116 might be of your interest. FYI a new version of searchfield has just been released v1.1.1. Thank you for using this package. |
Is your feature request related to a problem? Please describe.
I'm building a log search navigation bar using this library. My goal is to jump to a specific log after tapping any suggestion provided by the search field. To achieve this, I set the logs as suggestion item and implemented some logic in
onSuggestionTap
to handle my requirements. The functionality works as expected, but the TextField containing the search keywords is replaced by theitem.searchKey
.Upon investigation, I found the following code:
searchfield/lib/src/searchfield.dart
Lines 682 to 687 in 63c9f11
This code sets the text first and then calls my
widget.onSuggestionTap
.Describe the solution you'd like
Is it possible to keep the
searchController.text
unchanged after tapping any suggestions?Describe alternatives you've considered
I attempted to retrieve the text before tapping and refill it using a custom
TextController
, but implementing my ownonSearchTextChanged
prevented the library from providing any suggestions.Additional context
I conducted a brief search of existing issues but did not find any relevant solutions.
Thank you for your great work on this library! It has been very helpful in my project.
The text was updated successfully, but these errors were encountered: