Flutter-如何在没有用户交互的情况下(例如onTap)推送到其他路线?

Flutter-如何在没有用户交互的情况下(例如onTap)推送到其他路线?

问题描述:

我是一个iOS开发人员,现在正在尝试Flutter。

I am an iOS developer and trying Flutter now.

作为标题,我想要实现的是使一个类像 manager一样进行控制我在应用程序上的页面路线开始。

As the title, what I want to achieve is to make a class act like "manager", to control my page route on the app start.

例如,在iOS中,我可以在AppDelegate(didFinishedLaunchingWithOptions)/ ViewController(ViewDidLoad / Appear)中检查布尔并更改rootViewController或推送至所需的ViewController取决于在布尔上,就像 isLogin一样,将其推入LoginViewController / LoggedViewController。

For example in iOS, I can check a 'Bool' inside AppDelegate (didFinishedLaunchingWithOptions) / ViewController (ViewDidLoad/Appear) and change the rootViewController or push to a desire ViewController depends on the 'Bool', like something 'isLogin' to rather push to LoginViewController / LoggedViewController.

我知道我可以在用户交互上进行推送,例如监听IconButton的onTap。但是我不知道如何自动

I know I can do Push on user interaction, like listening onTap of IconButton. But I have no idea how to do it 'Automatically'.

据我所知,Flutter中的 build(context)类似于iOS中的 viewDidLoad,但它专门用于UI,因此我可以在哪里放置

In my knowledge, 'build(context)' in Flutter is something similar to 'viewDidLoad' in iOS, but it is specifically for UI, so where can I put the logic?

在InitState()中调用Navigator.push的一种方法:

One way of Calling Navigator.push in InitState():

@override
  void initState() {
    super.initState();
    if(condition){
      WidgetsBinding.instance.addPostFrameCallback((_) async {
        Navigator.push(
            context,
            MaterialPageRoute(
                builder: (BuildContext context) => NextPage()
            )
        );
      });
    }
  }