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

时间:2020-07-18 01:52:29

标签: flutter if-statement navigation

我创建了一个按钮来按下此按钮后启动一个方法,该方法适用if条件,并且我需要根据此if语句的结果转到特定的屏幕,但是每次什么都没有发生,我什至不出现错误!

1-按钮:

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

2方法:

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

      },

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

private val communicationViewModel by lazy {
        ViewModelProvider(this).get(
            MyProfileEditSharedViewModel::class.java
        )
    }

请注意,该按钮和方法可以正常工作,但问题在于导航步骤。

1 个答案:

答案 0 :(得分:1)

您将以MaterialPageRoute方法返回一个_mobileState(),该方法不会推送到任何屏幕。您注定要推到任何想要的路线。

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

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