如何防止IndexedStack中的子项被重建? [英] How to prevent child in IndexedStack get rebuilt?

查看:97
本文介绍了如何防止IndexedStack中的子项被重建?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有4个子项的 IndexedStack ,它是4个可以更改的选项卡.其他所有3个简单的子程序都运行良好,但是如果切换到第4个子程序,则正在对其进行重建.如何预防呢?

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?

该子对象是一个自定义的 StatefulWidget (我们称其为A),它构建了一个 FutureBuilder .如果成功从联机获取数据,则它将返回另一个自定义的 StatfulWidget (我们将其称为B).B也有一个 FutureBuilder ,并且最终返回的内容基本上是 Scaffold 中的一个 NestedScrollView ,并且具有自己的选项卡视图.

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.

我希望它的滚动位置保持在选项卡之间切换时的位置.可能是什么原因?顺便说一句,我尝试将 AutomaticKeepAliveClientMixin 添加到自定义的 StatefulWidget 中,但仍然相同.

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.

可能有用的代码:窗口小部件A:

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();
      },
    );
  }

推荐答案

  //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


这篇关于如何防止IndexedStack中的子项被重建?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆