loadingDialog加载进度条

  // 加载进度条
  Container loadingDialog;

  // 显示加载进度条
  showLoadingDialog() {
    setState(() {
      loadingDialog = new Container(
          constraints: BoxConstraints.expand(),
          color: Color(0x80000000),
          child: new Center(
            child: new CircularProgressIndicator(),
          ));
    });
  }

  // 隐藏加载进度条
  hideLoadingDialog() {
    setState(() {
      loadingDialog = new Container();
    });
  }

然后在Stack上面叠加loadingDialog就行了,需要显示的时候就调用showLoadingDialog();,需要隐藏就调用hideLoadingDialog();

  void login() async {
    if (phoneController.text.length != 11) {
      ToastUtil.toast("请输入11位手机号码");
    } else if (passController.text.length == 0) {
      ToastUtil.toast("密码不能为空");
    } else {
      showLoadingDialog();
      Dio dio = new Dio();
      dio.options.baseUrl = ServerUrl.base;
      FormData formData = new FormData.from({
        "mobile": phoneController.text,
        "pwd": passController.text,
      });
      try {
        Response response = await dio.post(ServerUrl.token, data: formData);
        print(response.data.toString());
        if (response.data["status"] == 0) {
          String token = response.data["data"]["token"];
          print(token);
          if (token != null && token.trim() != "") {
            SharedPreferences sp = await SharedPreferences.getInstance();
//      sp.setString("token", token);
            Navigator.of(context).pushReplacementNamed('/main');
          } else {
            ToastUtil.toast("登录失败)");
          }
        } else {
          ToastUtil.toast("账号或密码错误");
        }
      } catch (e) {
        // The request was made and the server responded with a status code
        // that falls out of the range of 2xx and is also not 304.
        ToastUtil.toast("网络连接错误");
      } finally {
        hideLoadingDialog();
      }
//      phoneController.clear();
//      passController.clear();
    }
  }

你可能感兴趣的:(loadingDialog加载进度条)