Flutter 颤振TabBar/TabBarView显示错误的子对象

Flutter 颤振TabBar/TabBarView显示错误的子对象,flutter,tabbar,Flutter,Tabbar,我在一个颤振应用程序中工作。我有一个有六个孩子的酒吧。其中两个是TabBar屏幕,但我有一个问题。每当我在TabBar/TabBarView中更改所选选项卡,并移动到底部栏导航中的其他屏幕,并返回上一个带有选项卡的屏幕时,TabBarView将显示最后选择的选项卡 代码为: return SafeArea( child: DefaultTabController( length: 4, initialIndex: 0, key: widget.key,

我在一个颤振应用程序中工作。我有一个有六个孩子的酒吧。其中两个是TabBar屏幕,但我有一个问题。每当我在TabBar/TabBarView中更改所选选项卡,并移动到底部栏导航中的其他屏幕,并返回上一个带有选项卡的屏幕时,TabBarView将显示最后选择的选项卡

代码为:

return SafeArea(

  child: DefaultTabController(
      length: 4,
      initialIndex: 0,
      key: widget.key,
      child: Scaffold(
        //backgroundColor: Colors.white,
        appBar: ColoredTabBar(
          color: Theme.of(context).primaryColorLight,
          tabBar: TabBar(
            unselectedLabelStyle: TextStyle(fontSize: textSize*0.8),
            indicator: BoxDecoration(
              borderRadius: BorderRadius.only(topLeft: Radius.circular(00),topRight: Radius.circular(00)),
              color: Theme.of(context).primaryColor,
              //border: Border.all(),
            ),
            //isScrollable: true,
            labelStyle: TextStyle(fontSize: textSize),
            unselectedLabelColor: Theme.of(context).textTheme.body1.color,
            labelColor: Theme.of(context).unselectedWidgetColor,




            tabs: [
              Tab(text: "Paquetes"),
              Tab(text: "Micro"),
              Tab(text: "Recargas"),
              Tab(text: "A la medida"),
            ],
          ),
          mywalletResponse: widget.wallet,
          numero: widget.defaultNumber,
          onPressed: widget.onPressed,
          onTap: widget.onTap,


        ),
        body:  TabBarView(
          //controller: _tabController2,
          key: widget.key,
          children: [
            CombosScreen(),
            MicroScreen(),
            RecargasScreen(),
            CustomScreen(), // tercera pagina: notificaciones
          ],
        ),
      )))     
;
ColoredTabBar小部件是:

    class ColoredTabBar extends Container implements PreferredSizeWidget{
  ColoredTabBar({@required this.color, @required this.tabBar, @required this.mywalletResponse, @required this.numero, @required this.onTap, @required this.onPressed});

  final Color color;
  final TabBar tabBar;
  final WalletResponse mywalletResponse;
  final String numero;
  final VoidCallback onTap;
  final VoidCallback onPressed;





  @override
  Size get preferredSize => Size.fromHeight(103.0);

  @override
  Widget build(BuildContext context) => Container(

      color: color,
      child: Column(
        children: <Widget>[
          Header(number: numero, walletModel: mywalletResponse, onTap: onTap, onPressed: onPressed,),
          tabBar,
        ],
      )

  );
}
类ColoredTabBar扩展容器实现PreferredSizeWidget{
ColoredTabBar({@required this.color、@required this.tabBar、@required this.mywalletResponse、@required this.numero、@required this.onTap、@required this.onPressed});
最终颜色;
最终TabBar TabBar;
最终WalletResponse mywalletResponse;
最后的字符串数字;
最终确认为onTap;
最后一次按下按钮;
@凌驾
Size get preferredSize=>Size.fromHeight(103.0);
@凌驾
小部件构建(构建上下文)=>容器(
颜色:颜色,
子:列(
儿童:[
标题(编号:numero,walletModel:mywalletResponse,onTap:onTap,onPressed:onPressed,),
塔巴,
],
)
);
}
例如,我使用defaultTabController转到小部件并选择索引2处的小部件,然后移动到底部导航栏中的另一个小部件,然后使用defaultTabController返回到上一个小部件。所选选项卡是索引0处的选项卡,但TabBarView显示索引2处的小部件


您知道如何以选项卡和小部件相同的方式初始化索引吗?

您必须保存上一个选项卡索引,一旦重定向到选项卡视图屏幕,然后通过选项卡控制器重新设置选项卡索引的值

让我们使用
TabBarView
来代替
DefaultTabController
,它与
TabController

class\u页面{
_页面({this.widget});
最终小部件;
}
列出所有页面;
类Home扩展了StatefulWidget{
@凌驾
_HomeState createState()=>new_HomeState();
changeTabIndex(整数索引){
_HomeState()。_controller.animateTo(索引);
}
}
类_HomeState使用TickerProviderStateMixin扩展状态{
TabController\u控制器;
_家园();
@凌驾
void initState(){
super.initState();
_所有页面=[
_页面(小部件:CombosScreen()),
_页面(小部件:MicroScreen()),
_页面(小部件:RecargasScreen()),
_页面(小部件:CustomScreen()),
];
_controller=TabController(vsync:this,长度:4);
_controller.addListener(_handleabselection);
}

_handleTabSelection(){//Header()和WalletResponse类是什么?发布一个最小的可复制代码,以便进行测试。Read我尝试了此操作,但遇到一个异常,即使有可用的索引,方法animateTo也会在null上调用。initState()有效吗?在未创建状态时可能有效。
initState()
它可以工作,但在我的例子中,它显示了@Carlos Sandoval的上述问题的错误选项卡。如果我返回底部导航栏中的onTap。选择了
0
索引选项卡栏,它表示
1
索引选项卡栏的数据。