滑动选项卡后如何更改屏幕索引?颤动



我在我的应用程序中使用选项卡视图,该应用程序具有相同的浮动操作按钮:

floatingActionButton: FloatingActionButton(
onPressed: () async{
print("this is the current Screen :$currentScreen");
if(currentScreen==0)
{
await showInformationDialog(context);
}
else
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => AddNewVehical(1),
),
);
},

但是当我点击选项卡时,它工作得很好,currentscreen变量从0变为1,正如预期的那样,但问题出现了当我从一个选项卡滑到另一个选项卡时,currentscreenvariableprint总是为零,因此在另一个标签上,它也显示对话

有人能告诉我为什么会发生这种事吗?以及解决方案?提前感谢<3

您可以在initState()中添加TabController侦听器。

_tabController!.addListener(_handleTabSelection);

_handleTabSelection方法中,得到tabController索引。

currentScreen = _tabController!.index; 

您可以使用TabController。使用index属性可以获得currentIndex。参考:https://api.flutter.dev/flutter/material/TabController-class.html

最新更新