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

Preserve Search Text After Tapping Suggestions #172

Closed
EluvK opened this issue Sep 14, 2024 · 3 comments
Closed

Preserve Search Text After Tapping Suggestions #172

EluvK opened this issue Sep 14, 2024 · 3 comments
Labels
in triage Issue is currently being triaged question Question on using a certain api of the package solved A solution has been proposed waiting for author waiting for author to respond back with more info

Comments

@EluvK
Copy link

EluvK commented Sep 14, 2024

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 the item.searchKey.

Upon investigation, I found the following code:

void onSuggestionTapped(SearchFieldListItem<T> item, int index) {
{
selected = index;
searchController!.text = item.searchKey;
searchController!.selection = TextSelection.fromPosition(
TextPosition(

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 own onSearchTextChanged 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.

@maheshj01 maheshj01 added the in triage Issue is currently being triaged label Sep 14, 2024
@maheshj01
Copy link
Owner

Hi @EluvK, Thanks for filing the issue.

Is it possible to keep the searchController.text unchanged after tapping any suggestions?

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 onSearchTextChanged Please see the below code sample

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 maheshj01 added waiting for author waiting for author to respond back with more info question Question on using a certain api of the package labels Sep 14, 2024
@EluvK
Copy link
Author

EluvK commented Sep 14, 2024

@maheshj01 Thank you so much for these detailed explanations. I tried to resolve my issue by making the searchController!.text = item.searchKey; optional in my forked version, and it's working for me for now.

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.

would by design go against what this package is intended to do

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 EluvK closed this as completed Sep 14, 2024
@maheshj01
Copy link
Owner

@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.

@maheshj01 maheshj01 added the solved A solution has been proposed label Sep 14, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
in triage Issue is currently being triaged question Question on using a certain api of the package solved A solution has been proposed waiting for author waiting for author to respond back with more info
Projects
None yet
Development

No branches or pull requests

2 participants