ArtS
ArtS

Reputation: 2012

How to prevent child in IndexedStack get rebuilt?

I have a IndexedStack with 4 children, as 4 tabs that can be changed. All other 3 simple ones are working well, however the 4th child is getting rebuilt if switching to it. How to prevent this?

The child is a custom StatefulWidget (let's call it A) that builds a FutureBuilder. If the data is fetched from online successfully then it returns another customized StatfulWidget (let's call it B). B also had a FutureBuilder, and ultimately returns something that basically is a NestedScrollView within a Scaffold, and has it's own tabviews.

I want the scroll position of it keeping where it is when switching between the tabs. What could be the potential reason? BTW I've tried adding AutomaticKeepAliveClientMixin to my customized StatefulWidget, still the same.

Codes that might helps: Widget A:

@override
  Widget build(BuildContext context) {
    return FutureBuilder<bool>(
      future: MainMgr.instance.isLoggedIn(),
      builder: (context, snapshot) {
        if (snapshot.connectionState == ConnectionState.waiting)
          return Center(child: CircularProgressIndicator());
        else if (snapshot.connectionState == ConnectionState.done) {
          if (snapshot.hasError)
            return Text(snapshot.error);
          else {
            if (snapshot.data)
              return Stack(
                children: [
                  UserProfile(
                    MainMgr.instance.selfCOUserID,
                    null,
                    showBackButton: false,
                  ),
                  Positioned(
                    top: 60,
                    child: RaisedButton(
                      child: Text('Log out'),
                      color: Color.fromARGB(255, 29, 161, 242),
                      onPressed: () async {
                        await MainMgr.instance.logout();
                        setState(() {});
                      },
                    ),
                  ),
                ],
              );
            else
              return LoginSignupPage();
          }
        } else
          return Container();
      },
    );
  }

Upvotes: 1

Views: 1086

Answers (1)

Henok
Henok

Reputation: 3393

  //below your class's state
  Future future; 


  //inside your init state add
  future = MainMgr.instance.isLoggedIn();


  //inside your FutureBuilder 
  return FutureBuilder<bool>(
      future: future,//here is whats important
      builder: (context, snapshot) {
        if (snapshot.connectionState == ConnectionState.waiting)
          return Center(child: Cir
      //rest of the code


Upvotes: 2

Related Questions