Flutter search delegate doesn't work when navigate back from a route

Viewed 4546

I'm using Flutter SearchDelegate in my app and here's the code:

class NameSearch extends SearchDelegate<String> {
    @override
    List<Widget> buildActions(BuildContext context) {
    return [
      IconButton(
        icon: Icon(Icons.clear),
        onPressed: () {
          query = "";
        },
      )
    ];
    }

    @override
    Widget buildLeading(BuildContext context) {
    return IconButton(
      icon: AnimatedIcon(
          icon: AnimatedIcons.menu_arrow, progress: transitionAnimation),
      onPressed: () {
        close(context, null);
      },
    );
    }

    @override
    Widget buildResults(BuildContext context) {
     return null;
    }

    @override
    Widget buildSuggestions(BuildContext context) {
    suggestionList = query.isEmpty ? [] : List.generate(nameList.length,
     (i) => nameList[i]).where((p) => p.name.startsWith(query)).toList();

    return ListView.builder(
      itemBuilder: (context, index) => ListTile(
            onTap: () {
              Navigator.push(
                  context,
                  MaterialPageRoute(
                      builder: (context) => DetailScreen(
                            detail: suggestionList[index],
                          )));
            },
            leading: Icon(Icons.book),
            title: RichText(
              text: TextSpan(
                  text: suggestionList[index].name.substring(0, query.length),
                  style: TextStyle(
                      color: Colors.black, fontWeight: FontWeight.bold),
                  children: [
                    TextSpan(
                        text: suggestionList[index].name.substring(query.length),
                        style: TextStyle(color: Colors.grey))
                  ]),
            ),
          ),
      itemCount: suggestionList.length,
    );
    }
}

When I clicked on an item in a suggestion list, It gets me to the new detail screen and works properly. But when I want to back to the search screen, the text input become like that: search screen

And I can't insert any text anymore, till restart the app or go to another page and after that go back to search page again!

And here is my DetailScreen code:

class DetailScreen extends StatelessWidget {
    final BookDetail detail;
    DetailScreen({Key key, @required this.detail}) : super(key: key);

    @override
    Widget build(BuildContext context) {
      return Scaffold(
        appBar: AppBar(
        title: Text(detail.name),
      ),
      body: Padding(
        padding: EdgeInsets.all(16.0),
        child: Text(detail.description),
      ),
    );
    }
}
2 Answers

In the line 83 -85 of the flutter search source code :

Once the user has selected a search result, [SearchDelegate.close] should be called to remove the search page from the top of the navigation stack and to notify the caller of [showSearch] about the selected search result.

So the the showSearch is structed for single-use only. And you have to call it again when you navigate back from your DetailScreen if you intend to use it for another search query.

I was facing same issue so what i did is i simply copy the search.dart from material library and replace

bool get maintainState => false;

to

bool get maintainState => true;

on line 294 it worked for me.

Related