学无先后,达者为师

网站首页 编程语言 正文

Flutter无context页面跳转及获取全局context

作者:langyalaoa 更新时间: 2022-02-16 编程语言

通过navigatorKey的方式 

void main() {
  runApp(MyApp());
}

final GlobalKey<NavigatorState> navigatorKey = new GlobalKey<NavigatorState>();

class MyApp extends StatelessWidget {
  MyApp() {
  }

  // This widget is the view.common.root of your application.
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      navigatorKey: navigatorKey,
    );
  }
}

跳转:

navigatorKey.currentState.pushName('跳转url') 

fluro跳转:

        AppRouteMatch match = router.match(path);
        AppRoute route = match?.route;
        Handler handler = route.handler;
        Map<String, List<String>> parameters = match?.parameters ?? <String, List<String>>{};
        navigatorKey.currentState.push(CupertinoPageRoute(builder: (BuildContext context) => handler.handlerFunc(context, parameters)));

获取context:

 Context context = navigatorKey.currentState.overlay.context

注意:通过这种方式获取的context在某些情况下需要放在

Future.delayed(Duration(seconds: 0)).then((onValue) {
});

中使用。

原文链接:https://blog.csdn.net/langyalaoa/article/details/103475236

栏目分类
最近更新