日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學(xué)無先后,達(dá)者為師

網(wǎng)站首頁 編程語言 正文

Flutter無context頁面跳轉(zhuǎn)及獲取全局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,
    );
  }
}

跳轉(zhuǎn):

navigatorKey.currentState.pushName('跳轉(zhuǎn)url')?

fluro跳轉(zhuǎn):

        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

欄目分類
最近更新