简体   繁体   English

根据条件是否导致 flutter 在屏幕之间导航

[英]navigate between screens based on if condition result in flutter

i have created a button to initiate a method after pressing this button, and this method applying if condition and i need to go to specific screen based on the result of this if statement but every time nothing happens, i don't even get an error!我创建了一个按钮以在按下此按钮后启动一个方法,并且此方法应用 if 条件,我需要根据此 if 语句的结果将 go 到特定屏幕,但每次什么都没有发生,我什至没有收到错误!

1- the button: 1-按钮:

RaisedButton(
                  child: Text('check'),
                  onPressed: _mobilestate,
                )

2 the method: 2 方法:

  _mobilestate() async {
    var connectivityResult = await (Connectivity().checkConnectivity());
    if (connectivityResult == ConnectivityResult.mobile) {
      return MaterialPageRoute(
        builder: (BuildContext context) => XDdetectingproblems19(),
      );



    } else if (connectivityResult == ConnectivityResult.none) {
      return MaterialPageRoute(
        builder: (BuildContext context) => XDdetectingproblems14(),
      );
    }
  }

i added the routes in main.我在 main.js 中添加了路线。 dart as below: dart 如下:

  routes: {
        '/XDdetectingproblems19' :(context) => XDdetectingproblems19(),
        '/XDdetectingproblems14' :(context) => XDdetectingproblems14(),

      },

Please advise, noting that the button and the method working fine, but the issue is with the navigation step.请告知,注意按钮和方法工作正常,但问题在于导航步骤。

You are returning a MaterialPageRoute in your _mobileState() method which doesn't push to any screen.您在_mobileState()方法中返回一个MaterialPageRoute ,它不会推送到任何屏幕。 You are meant to be pushing to any desired route.你应该被推向任何想要的路线。

I added a demo code of how your to help you get what you want done:我添加了一个演示代码,说明您如何帮助您完成您想做的事情:

     RaisedButton(
                  child: Text('check'),
                  onPressed: () async {
    var connectivityResult = await (Connectivity().checkConnectivity());
    if (connectivityResult == ConnectivityResult.mobile) {
      // navigate to the desired route
      Navigator.pushNamed(context, '/XDdetectingproblems19');
    } else if (connectivityResult == ConnectivityResult.none) {
      // navigate to the desired route
      Navigator.pushNamed(context, '/XDdetectingproblems14');
    }
  },
 )

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM