Flutter 调用显示弹框Dialog 异常报错点击弹框没有反应 Another exception was thrown: No MaterialLocalizations found.

Flutter 调用显示弹框Dialog 异常报错点击弹框没有反应 Another exception was thrown: No MaterialLocalizations found. 

自己记录下,Flutter 中调用showDialog的时候会日志报 Another exception was thrown: No MaterialLocalizations found

是因为showDialog的时候传递了一个上下文context,我传的这个context顶层是Stateless的布局的context,而实际上该dialog需要传入一个MaterialApp的context

class MyApp extends StatelessWidget {
  final Widget child;
  MyApp({Key key, this.child}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: Manage(),
    );
  }
}
class Manage extends StatefulWidget {
  final Widget child;
  Manage({Key key, this.child}) : super(key: key);

  _ManageState createState() => _ManageState();
}

class _ManageState extends State {
  
  _ManageState();
  @override
  Widget build(BuildContext context) {
  }
}

Manage()对象下的context传递给showDialog是有效的,如果传递的是MyApp下的context是无效的

 

 

你可能感兴趣的:(Flutter)