Flutter獲取全局context

經過navigatorKey的方式app

 

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,
    );
  }
}

獲取context:less

BuildContext context = navigatorKey.currentState.overlay.contextide

注意:經過這種方式獲取的context在某些狀況下須要放在
Future.delayed(Duration(seconds: 0)).then((onValue) { });
ui



做者:小學生課表明
連接:https://www.jianshu.com/p/897222522abb
來源:簡書
著做權歸做者全部。商業轉載請聯繫做者得到受權,非商業轉載請註明出處。




code

相關文章
相關標籤/搜索