……

Flutter 碎知识(五)路由及跳转

Posted on 2022-08-13 17:31  WALL*E  阅读(120)  评论(0编辑  收藏  举报

打开App如有登录进入内页,没有则进入登录页面。 main.dart配置

routes: { // 注册路由表
  "/": (context) => const Welcome(),
  "home": (context){
    bool firstSetNet = true;
    ModalRoute? modalRoute = ModalRoute.of(context);
    if(modalRoute != null ){
      firstSetNet = !((modalRoute.settings.arguments ?? '') == 'next');
    }
    return HomePage(firstSetNet: firstSetNet);
  },//登录页
  "services": (context) => const Services()
}

在Welcome.dart 配置。注意:此页面不用写html只需做逻辑处理(中间页),在initState初始化中调用即可。

_getFirstApp() async {
    FlutterNativeSplash.remove();

    SharedPreferences prefs = await SharedPreferences.getInstance();
    bool first = prefs.getBool('firstApp') ?? true;//判断是否是未登录(第一次)进入

    if(first){
      OneContext().pushReplacementNamed('home');//true进入登录
    }else{
      OneContext().pushReplacementNamed('services');//false进入内页
    }
  }

在登录之后需要将firstApp判断字段改变状态


个人使用,仅供参考。