Skip to content

keep state inside SearchDelegate  #43582

Closed

Description

@mohamed6996

I have FutureBuilder>>ListView widgets Inside @OverRide buildResults(BuildContext context) method. I navigate to a new screen on list view item tap. but when i pop from the new screen the future is called again. Also i have pagination in the main list view, so when the user tap on item 100 for ex. after popping from the new screen, the main listview it will rebuild from the beginning

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

  @override
  ThemeData appBarTheme(BuildContext context) {
    final ThemeData theme = Theme.of(context);
    return theme.copyWith(
        inputDecorationTheme: InputDecorationTheme(
            hintStyle: TextStyle(color: theme.primaryTextTheme.title.color)),
        primaryColor: theme.primaryColor,
        primaryIconTheme: theme.primaryIconTheme,
        primaryColorBrightness: theme.primaryColorBrightness,
        primaryTextTheme: theme.primaryTextTheme,
        textTheme: theme.textTheme.copyWith(
            title: theme.textTheme.title
                .copyWith(color: theme.primaryTextTheme.title.color)));
  }

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

  @override
  Widget buildResults(BuildContext context) {
     SearchProvider searchProvider =
        Provider.of<SearchProvider>(context, listen: false);

    return query.isEmpty == true ? Container() : FutureBuilder(
        future:  searchProvider.fetchData(query),
        builder: (context, snapshot) {
          switch (snapshot.connectionState) {
            case ConnectionState.waiting:
              return Center(
                  child: SpinKitHourGlass(
                color: Colors.greenAccent,
              ));
              break;
            default:
              if (snapshot.hasError) {
                return Center(child: Text("Error: ${snapshot.error}"));
              } else {
                return Consumer<SearchProvider>(
                  builder: (context, provider, child) => ImageList(
                    models: snapshot.data,
                    isSearch: true,
                  ),
                );
              }
          }
        });
  }

  @override
  Widget buildSuggestions(BuildContext context) {
    return Container();
  }
}

Activity

  1. mohamed6996 commented on Oct 27, 2019

    @mohamed6996
    Author

    I have tried to wrap the Futurebuilder widget inside a statefull widget and use AutomaticKeepAliveClientMixin but in vain

    class SearchWidget extends StatefulWidget {
      final String query;
      SearchWidget(this.query);
      @override
      _SearchWidgetState createState() => _SearchWidgetState();
    }
    
    class _SearchWidgetState extends State<SearchWidget>
        with AutomaticKeepAliveClientMixin {
      Future _future;
      @override
      void initState() {
        super.initState();
        SearchProvider searchProvider =
            Provider.of<SearchProvider>(context, listen: false);
        _future = searchProvider.fetchData(widget.query);
      }
    
      @override
      Widget build(BuildContext context) {
        super.build(context);
        return FutureBuilder(
            future: _future,
            builder: (context, snapshot) {
              switch (snapshot.connectionState) {
                case ConnectionState.waiting:
                  return Center(
                      child: SpinKitHourGlass(
                    color: Colors.greenAccent,
                  ));
                  break;
                default:
                  if (snapshot.hasError) {
                    return Center(child: Text("Error: ${snapshot.error}"));
                  } else {
                    return Consumer<SearchProvider>(
                      builder: (context, provider, child) => ImageList(
                        models: snapshot.data,
                        isSearch: true,
                      ),
                    );
                  }
              }
            });
      }
    
      @override
      bool get wantKeepAlive => true;
    }
    
  2. added
    frameworkflutter/packages/flutter repository. See also f: labels.
    c: new featureNothing broken; request for a new capability
    on Oct 28, 2019
  3. gopalkriagg commented on Dec 19, 2019

    @gopalkriagg

    Would like to see this feature get implemented! Thanks!

  4. SalehHub commented on Jan 15, 2020

    @SalehHub

    This is really important to us
    I hope it gets implemented

  5. JigneshWorld commented on Apr 13, 2020

    @JigneshWorld

    Any possible workaround for this?

  6. ixsans commented on Aug 18, 2020

    @ixsans

    I also facing this issue. Hopefully this can be implemented.

  7. pankaj-nayak commented on Aug 21, 2020

    @pankaj-nayak

    I am also facing this issue. I am passing a list to search delegate , and if query does not have the item in the list, I need to add this to the list and reload the list again . Do we have any kind of "state" in search delegate?

  8. DJ-Raven commented on Sep 25, 2020

    @DJ-Raven

    ctrl click on SearchDelegate it will open class material\search.dart
    change bool get maintainState => false;
    to bool get maintainState => true;

  9. yaroaxito commented on Oct 15, 2020

    @yaroaxito

    I am also facing this issue. Please implement this.

  10. zhanghsgithub commented on Oct 30, 2020

    @zhanghsgithub

    我一直以为是我学艺不精,原来是源码有问题^_^!

  11. ivanchaukn commented on Jan 27, 2021

    @ivanchaukn

    I'm having the same issue too!

  12. HendricManenga commented on Feb 4, 2021

    @HendricManenga

    I have the same issue

  13. AmG2000 commented on Feb 10, 2021

    @AmG2000

    I also have the same issue. Is there an alternative to copying search.dart?

  14. 7 remaining items

  15. added 8 commits that reference this issue on Jul 27, 2024
    119550e
    1b007d0
    09bf103
    099fafe
    31db7b8
    8d0c6a5
    73a753f
    d39b982
  16. added a commit that references this issue on Aug 6, 2024
    6f98b48
  17. added a commit that references this issue on Aug 7, 2024
    b90741b
  18. github-actions commented on Aug 20, 2024

    @github-actions

    This thread has been automatically locked since there has not been any recent activity after it was closed. If you are still experiencing a similar issue, please open a new bug, including the output of flutter doctor -v and a minimal reproduction of the issue.

  19. locked as resolved and limited conversation to collaborators on Aug 20, 2024
  20. added a commit that references this issue on Sep 2, 2024
    d2cffc6
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    c: new featureNothing broken; request for a new capabilityframeworkflutter/packages/flutter repository. See also f: labels.p: material_uimaterial_ui package in flutter/packagesteam-designOwned by Design Languages teamtriaged-designTriaged by Design Languages team

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions