如何修复showDialog关闭屏幕而不是对话框窗口?



我有一个问题,当我试图从api加载数据…

当我调用emit(Waiting)时,我有cubit=比;这个显示的是showDialog黑色全屏,中间有加载指示灯,当api响应为200时,发出(Success)关闭showDialog() =>导航器。

这就是问题所在,当我登录到应用程序并转到主部件时,然后我点击主部件内部的选项卡(TabBar)并调用BlocProvider.of(context).callApi()在initState()中,然后当响应是200时,我返回登录页面,显示对话框消失,我的主要小部件也是。如何解决这个问题?

这是主要的小部件代码:

class _MainWidgetState extends State<MainWidget> with WindowListener {
onTap: (index) { // This problem happens too if I add this, I return to login screen when go to HomeWidget
if(index == 0) {
BlocProvider.of<StatisticsCubit>(context).callApi();
}
}
return TabBar(
tabs: [
Tab(
child: "Home",
),
Tab(
child: "Test",
),
Tab(
child: "Statistics",
)
]
)
}

当我按Home选项卡时,我进入HomeWidget,这是Home widget:

class _HomeWidgetState extends State<HomeWidget> with WindowListener {
@override
void initState() {
BlocProvider.of<StatisticsCubit>(context).callApi();
super.initState();
}

@override
Widget build(BuildContext context) {
return BlocConsumer<StatisticsCubit, StatisticsState>(
listener: (context,state) {
if (state is WaitingNfb) {
showDialog(
context: context,
builder: (context) => Container(
width: MediaQuery.of(context).size.width,
height: MediaQuery.of(context).size.height,
color: Colors.black.withOpacity(0.7),
child: const Center(
child: CircularProgressIndicator(
strokeWidth: 1,
color: Colors.black,
backgroundColor: Colors.white,
),
),
)
);
}
if (state is Success) {
Navigator.of(context, rootNavigator: true).pop(); // And here I return to login screen, not showing content of HomeWidget
}
},
builder: (context, state) {
return Container(
child: ,
)
}),
)
}
}

如何只关闭showDialog,而不是所有屏幕?

尝试将listener作为async,并在showDialog前面使用await

listener: () async {
if (someState) {
await showDialog()
}
}

最新更新