Flutter初始化路由页面时自动弹出提示框

Flutter 在initState时自动弹出提示框

需求:刚一进入页面,检测用户是否认证了,如果没有认证则弹出提示框
问题原因:

由于Flutter的Alert提示框是需要页面内容Build完毕后,并且有了父级页面内容的时候,才可以进行正常的弹出,所以在一开始 initState的时候,就调用弹出方法,便会报错出现问题

解决办法一:

此办法是在有了上下文的context的情况下,也就是说在调用的时候context是存在的情况下可以使用

使用一个定时器去解决,在页面加载后延时调用,这个办法就解决了,我使用的是秒,延迟了1秒,用户体验上感觉不到什么,完美解决这个问题。附代码↓

:AlertMsg.alerDialog是我封装的方法,所以请替换为自己的。封装的方法也给附代码了

  var _isAuthentication = false;
  
  @override
  void initState() {
    super.initState();
    // 避免调用Alert时没有加载完父级页面,延迟1秒调用
    Timer(Duration(seconds: 1), () => _getUserIsAuthentication());
  }

  _getUserIsAuthentication(){
    if(!_isAuthentication){
      AlertMsg.alertDialog(context, '你还没有实名认证呦,赶快认证成为主播或专家吧', '立即认证', '先不认证', (){
        Navigator.pop(context);
        Navigator.pushNamed(context, '/myAuthentication');
      });
    }
  }
AlertMsg

:ScreenAdapter为封装的自适应大小,具体可参考我另外一篇Flutter 装修计划


import 'package:firetiger/utils/ScreenAdapter.dart';
import 'package:flutter/material.dart';


class AlertMsg {
  static alertDialog(context, text, confirmText, cancelText, confirmFn){
    showDialog(
      context: context,
      barrierDismissible: true,
      builder: (BuildContext context){
        return AlertDialog(
          content: Text('$text', style: TextStyle(fontWeight: FontWeight.bold, fontSize: ScreenAdapter.size(30))),
          backgroundColor: Colors.white,
          elevation: 24,
          shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(10)),
          actions: <Widget>[
            Container(
              width: ScreenAdapter.setWidth(200),
              child:OutlineButton(
                borderSide: BorderSide(color: Theme.of(context).primaryColor),
                highlightedBorderColor: Theme.of(context).primaryColor,
                shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(20)),
                child: Text('$cancelText', style: TextStyle(color: Theme.of(context).primaryColor),),
                onPressed: (){
                  Navigator.pop(context);
                },
              ),
            ),
            SizedBox(
              width:ScreenAdapter.setWidth(10)
            ),
            Container(
              width: ScreenAdapter.setWidth(200),
              child: RaisedButton(
                color: Theme.of(context).primaryColor,
                highlightColor:Theme.of(context).primaryColor,
                shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(20)),
                child: Text('$confirmText', style: TextStyle(color: Colors.white),),
                onPressed:confirmFn
              ),
            )
          ],
        );
      }
    );
  }
}
解决办法二:

当然还有一种原因就是在initState调用时候没有context上下文,那么我们可以使用Future.delayed去调用,这样也算解决了此问题

void initState() {
    // TODO: implement initState
    super.initState();
    Future.delayed(
      Duration.zero,
        AlertMsg.alertDialog(context, '你还没有实名认证呦,赶快认证成为主播或专家吧', '立即认证', '先不认证', (){
	      Navigator.pop(context);
	       Navigator.pushNamed(context, '/myAuthentication');
	     });
    );
 
  }`

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章