Flutter-如何在没有用户交互的情况下(例如onTap)推送到其他路线? [英] Flutter - How to push to other Route without user interaction (like onTap)?

查看:147
本文介绍了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()
            )
        );
      });
    }
  }

这篇关于Flutter-如何在没有用户交互的情况下(例如onTap)推送到其他路线?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆