flutter 为什么使用showDialog时对话框不会消失?

irlmq6kh  于 2023-04-07  发布在  Flutter
关注(0)|答案(1)|浏览(243)

我正在使用tabBar。如果我在第一个标签和第二个标签上点击了很多次,并且非常快,然后showDialog(加载)不会消失。它永远停留在屏幕上。我试图解决这个问题很长一段时间,但我找不到解决方案
这是第一个选项卡的代码(未在BlocConsumer中转换的小部件):

class FirstWidget extends StatefulWidget {
  const FirstWidget({super.key});

  @override
  State<FirstWidget> createState() => _FirstWidgetState();
}

class _FirstWidgetState extends State<FirstWidget> {
  @override
  Widget build(BuildContext context) {
    return TabBar(
  onTap: (index) async {
    if (index == 0) {

    final connectivityResult = await (Connectivity().checkConnectivity());
    if (connectivityResult == ConnectivityResult.wifi ||
      connectivityResult == ConnectivityResult.ethernet) {
        showWaiting(context); // Here is the loader, if I remove it then loader won't show up when I get data. So I just put it here and because of him my app is freezing
        BlocProvider.of<StatisticsCubit>(context).getData();
      }
    }
    },
    tabs: [
      Tab(
        icon: SizedBox(
        width: 60.w,
          child: Column(
            children: [
              Text(
                "First",
                style: TextStyle(
                fontSize: 14.sp,
                fontWeight: FontWeight.w500,
                height: 1,
                color: Colors.white
                ),
              ),
              ],
            ),
        ),
      ),
      Tab(
        icon: SizedBox(
        width: 60.w,
          child: Column(
            children: [
              Text(
                "Second",
                style: TextStyle(
                fontSize: 14.sp,
                fontWeight: FontWeight.w500,
                height: 1,
                color: Colors.white
                ),
              ),
              ],
            ),
        ),
      )
    ],
  );
  }
}

这是当我按秒选项卡时的小部件:

class SecondWidget extends StatefulWidget {
  const SecondWidget({super.key});

  @override
  State<SecondWidget> createState() => _SecondWidgetState();
}

class _SecondWidgetState extends State<SecondWidget> {
  @override
  Widget build(BuildContext context) {
    return BlocConsumer<StCubit, StState>(
      listener: (context, state) {
        if (state is Waiting) {
          showWaiting(context, false);
        }
        if (state is Success) {
          Navigator.of(context, rootNavigator: true).pop();
        }
      },
      builder: (context, state) {
        return Container();//content
      },
    );
  }
}

这是一个loader:

Future<void> showWaiting(BuildContext context) {
  return showDialog(
    context: context,
    builder: (BuildContext context) => Container(
      width: MediaQuery.of(context).size.width,
      height: MediaQuery.of(context).size.height,
      color: Colors.black.withOpacity(0.5),
      child: const Center(
        child: CircularProgressIndicator(
          strokeWidth: 1,
          color: Colors.black,
          backgroundColor: Colors.white,
        ),
      ),
    )
  );
}

当我点击第一个和第二个标签很多次时,加载器没有消失

z2acfund

z2acfund1#

这是因为有很大的可能性,对话框越来越堆叠起来,所以你必须首先弹出现有的对话框之前,显示新的。
尝试更新的代码。

Future<void> showWaiting(BuildContext context) async {
  // Dismiss any previously shown dialog
  Navigator.of(context, rootNavigator: true).pop(); // This should possibly fix the issue

  // Show the new dialog
  await showDialog(
    context: context,
    builder: (BuildContext context) => Container(
      width: MediaQuery.of(context).size.width,
      height: MediaQuery.of(context).size.height,
      color: Colors.black.withOpacity(0.5),
      child: const Center(
        child: CircularProgressIndicator(
          strokeWidth: 1,
          color: Colors.black,
          backgroundColor: Colors.white,
        ),
      ),
    ),
  );
}

相关问题