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

navigate between screens based on if condition result in flutter

我已经创建了一个按钮来在按下这个按钮后启动一个方法,这个方法应用 if 条件,我需要根据这个 if 语句的结果转到特定的屏幕,但每次什么都没有发生,我不知道甚至不会出现错误!

1- 按钮:

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

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(),
      );
    }
  }

我在 main 中添加了路由。飞镖如下:

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

      },

请告知,请注意按钮和方法工作正常,但问题出在导航步骤上。

您正在 _mobileState() 方法中返回一个 MaterialPageRoute,它不会推送到任何屏幕。你注定要推到任何想要的路线。

我添加了一个演示代码,说明您如何帮助您完成想要的工作:

     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');
    }
  },
 )