【Flutter】关于对话框showDialog传入context无效问题

本文讲述了在处理异步任务时,如何修正showLoadingAlert方法以解决因无效context导致的加载对话框显示问题。通过使用stateKey和currentContext,确保在未附加到UI时仍能正确展示加载提示。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

  1. 当执行异步操作,或者从下一个页面返回结果时,需要调用类似如下加载对话框代码,有时就会出现不能展示的问题,调式发现传入的context是无效的
	/**
	* showLoading()
	* 加载对话框
	*/
  Future<T?> showLoadingAlert<T>(BuildContext context, Function task) async {
    return await showDialog<T>(context: context, builder: (context) {
      (() async {
        Navigator.of(context).pop(await task(context));
      })();
      return UnconstrainedBox(
        child: SizedBox(
          width: 200,
          child: AlertDialog(
            content: Column(
              mainAxisSize: MainAxisSize.min,
              children: const [
                CircularProgressIndicator(),
                Padding(
                  padding: EdgeInsets.only(top: 20),
                  child: Text("加载中..."),
                ),
              ],
            ),
          ),
        ),
      );
    }, barrierDismissible: false);
  }
  1. 如果想通过这样添加判断mounted解决,就会觉得实在不妥,因为这样不能保证顺利执行下去
 if (!mounted) {
   print('$c not mounted!');
   //...
 } else {
 	var result = await showLoadingAlert(context, () async {
		//handler task
	});
   //...;
 }
  1. 查过网上的资料,最后整理出来,改成如下这样试试,注意到MyPage页面代码中有定义了stateKey,最后赋值给key了,
class MyPage extends StatelessWidget with MixWidget {
	final stateKey = GlobalKey<ScaffoldState>();

	@override
	Widget build(BuildContext context) {
		return Scaffold(
      		key: stateKey,
      		//...
      	}
	}
	
	//...
  Future<T?> showLoadingAlert<T>(Function(BuildContext context) task) async {
    return await showDialog<T>(context: stateKey.currentContext!, builder: (context) {
      (() async {
        Navigator.of(context).pop(await task(context));
      })();
      return UnconstrainedBox(
        child: SizedBox(
          width: 200,
          child: AlertDialog(
            content: Column(
              mainAxisSize: MainAxisSize.min,
              children: const [
                CircularProgressIndicator(),
                Padding(
                  padding: EdgeInsets.only(top: 20),
                  child: Text("加载中..."),
                ),
              ],
            ),
          ),
        ),
      );
    }, barrierDismissible: false);
  }
  //...
}
  1. 运行以后,代码如下,只要调用showLoadingAlert()方法,不再传入context,就能展示加载对话框了
onClick() async {
 	var result = await showLoadingAlert(() async {
		//执行耗时请求
		//...
	});
   //...;
}
  1. 最后,附上运行结果截图
    在这里插入图片描述
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

TA远方

谢谢!收到你的爱╮(╯▽╰)╭

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值