按下异步按钮后

时间:2020-02-27 18:30:55

标签: asynchronous flutter flutter-onpressed

Flutter的新手。我如何按按钮按下调用的条件来推送新的路由? (异步登录呼叫)。

这种事情:如果signIn(async)= success-> Navigator.push

欢呼

Widget buildButtons() {
    return new Container(
      child: new Column(
        children: <Widget>[
          new MaterialButton(
            minWidth: MediaQuery.of(context).size.width - 30, //FULL WIDTH - 30
            color: Style.palette3,
            padding: EdgeInsets.all(Style.padding1),
            child: new Text('Sign in',
                style: Style.signInBtn
            ),
            onPressed: () {
              if (LoginControl.signIn()) Navigator.push(context, MaterialPageRoute(builder: (context) => ArmDisarm()));
            },
          ),
          new FlatButton(
            child: Padding(
              padding: Style.paddingCreateAcc,
              child: new Text(
                  'Create an account',
                  style: Style.fontSize1
              ),
            ),
          ),
        ],
      ),
    );
  }

1 个答案:

答案 0 :(得分:1)

最好向我们展示LoginControl.signIn()的代码。

无论如何,如果signIn()Future,则您要使用async await

...
onPressed: () async {
  if (await LoginControl.signIn()) {
    Navigator.push(context, MaterialPageRoute(builder: (context) => ArmDisarm()));
  }
},
...

如果用户成功登录,请确保signIn()方法返回true