Flutter简单加载框显示和隐藏

  • • 发表于2019-12-18 00:27:40.0
  • • 作者 Mercury1z
  • • 2078 次浏览
  • • 0 条评论
  • • 最后编辑时间 2019-12-18 00:27:40.0
  • • 来自 [Android]

原创声明:本文为作者原创,未经允许不得转载,经授权转载需注明作者和出处

加载框的代码

class LoadingDialog {
  static BuildContext _context;

  static void show(BuildContext context) {
    _context = context;
    Navigator.push(_context, DialogRouter(_LoadingDialog()));
  }

  static void hiden() {
    if (_context != null) {
      if (Navigator.canPop(_context)) {
        Navigator.pop(_context);
      }
      _context = null;
    }
  }
}

class DialogRouter extends PageRouteBuilder {
  final Widget page;

  DialogRouter(this.page)
      : super(
    opaque: false,
    pageBuilder: (context, animation, secondaryAnimation) => page,
  );
}

class _LoadingDialog extends Dialog {
  @override
  Widget build(BuildContext context) {
    return Center(
      child: new Material(

        ///背景透明
        color: Colors.black54,

        ///保证控件居中效果
        child: new Center(

          ///弹框大小
          child: new SizedBox(
            width: 120.0,
            height: 120.0,
            child: new Container(

              ///弹框背景和圆角
              decoration: ShapeDecoration(
                color: Color(0xffffffff),
                shape: RoundedRectangleBorder(
                  borderRadius: BorderRadius.all(
                    Radius.circular(8.0),
                  ),
                ),
              ),
              child: new Column(
                mainAxisAlignment: MainAxisAlignment.center,
                crossAxisAlignment: CrossAxisAlignment.center,
                children: <Widget>[
                  new CircularProgressIndicator(),
                  new Padding(
                    padding: const EdgeInsets.only(
                      top: 20.0,
                    ),
                    child: new Text(
                      "加载中",
                      style: new TextStyle(fontSize: 16.0),
                    ),
                  ),
                ],
              ),
            ),
          ),
        ),
      ),
    );
  }
}

使用方法

显示:LoadingDialog.show(_context);
隐藏:LoadingDialog.hiden();

评论区(共0条评论)
0条评论
Ctrl+Enter
作者

Michael

Mercury1z

帖子:9 回复:0

简简单单的开发攻城师

作者详情》
Top