Flutter获取全局context
通过navigatorKey的方式void main() {runApp(MyApp());}final GlobalKey<NavigatorState> navigatorKey = new GlobalKey<NavigatorState>();class MyApp extends StatelessWidget {MyApp() {}// This widget
·
通过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,
);
}
}
获取context:
BuildContext context = navigatorKey.currentState.overlay.context
注意:通过这种方式获取的context在某些情况下需要放在Future.delayed(Duration(seconds: 0)).then((onValue) { });
更多推荐



所有评论(0)