zoukankan      html  css  js  c++  java
  • flutter dialog异常Another exception was thrown: No MaterialLocalizations found

    flutter dialog异常Another exception was thrown: No MaterialLocalizations found

    import 'package:flutter/material.dart';
    import 'package:scoped_model/scoped_model.dart';
    
    void main() {
      runApp(new RootLayout());
    }
    
    class RootLayout extends StatefulWidget {
      @override
      State<StatefulWidget> createState() {
        return new RootLayoutM();
      }
    }
    
    class RootLayoutM extends State<RootLayout> {
      _showMyMaterialDialog(BuildContext context) {
        print("_showMyMaterialDialog");
        showDialog(
            context: context,
            builder: (context) {
              return new AlertDialog(
                title: new Text("title"),
                content: new Text("内容内容内容内容内容内容内容内容内容内容内容"),
                actions: <Widget>[
                  new FlatButton(
                    onPressed: () {
                      Navigator.of(context).pop();
                    },
                    child: new Text("确认"),
                  ),
                  new FlatButton(
                    onPressed: () {
                      Navigator.of(context).pop();
                    },
                    child: new Text("取消"),
                  ),
                ],
              );
            });
      }
    
      @override
      Widget build(BuildContext context) {
        return new MaterialApp(
          home: new Scaffold(
            body: new Center(
              child: new Text("show simple dialog",
                  style: new TextStyle(color: Color(0xFF00FF00))),
            ),
            floatingActionButton: new FloatingActionButton(
                child: new Text("showDialog"),
                onPressed: () {
                  _showMyMaterialDialog(context);
                }),
          ),
        );
        ;
      }
    }

     这里顶层的context所在的Widget的顶层Widget属于StatefulWidget为什么还不能显示dialog呢

    这里发现

     @override
      Widget build(BuildContext context) {
        return new MaterialApp(
          home: new Scaffold(
            body: new Center(
              child: new Text("show simple dialog",
                  style: new TextStyle(color: Color(0xFF00FF00))),
            ),
            floatingActionButton: new FloatingActionButton(
                child: new Text("showDialog"),
                onPressed: () {
                  _showMyMaterialDialog(context);
                }),
          ),
        );
        ;
      }

    这个FloatingActionButton在外面包一层就可以了

    class MyFloat extends StatelessWidget{
      _showMyMaterialDialog(BuildContext context) {
        print("_showMyMaterialDialog");
        showDialog(
            context: context,
            builder: (context) {
              return new AlertDialog(
                title: new Text("title"),
                content: new Text("内容内容内容内容内容内容内容内容内容内容内容"),
                actions: <Widget>[
                  new FlatButton(
                    onPressed: () {
                      Navigator.of(context).pop();
                    },
                    child: new Text("确认"),
                  ),
                  new FlatButton(
                    onPressed: () {
                      Navigator.of(context).pop();
                    },
                    child: new Text("取消"),
                  ),
                ],
              );
            });
      }
      @override
      Widget build(BuildContext context) {
        // TODO: implement build
        return new FloatingActionButton(
            child: new Text("showDialog"),
            onPressed: () {
              _showMyMaterialDialog(context);
            });
      }

    完整代码如下

    import 'package:flutter/material.dart';
    import 'package:scoped_model/scoped_model.dart';
    
    void main() {
      runApp(new RootLayout());
    }
    
    class RootLayout extends StatefulWidget {
      @override
      State<StatefulWidget> createState() {
        return new RootLayoutM();
      }
    }
    
    class RootLayoutM extends State<RootLayout> {
    
      @override
      Widget build(BuildContext context) {
        return new MaterialApp(
          home: new Scaffold(
            body: new Center(
              child: new Text("show simple dialog",
                  style: new TextStyle(color: Color(0xFF00FF00))),
            ),
            floatingActionButton: new MyFloat(),
          )
        );
      }
     
    }
    
    class MyFloat extends StatelessWidget{
      _showMyMaterialDialog(BuildContext context) {
        print("_showMyMaterialDialog");
        showDialog(
            context: context,
            builder: (context) {
              return new AlertDialog(
                title: new Text("title"),
                content: new Text("内容内容内容内容内容内容内容内容内容内容内容"),
                actions: <Widget>[
                  new FlatButton(
                    onPressed: () {
                      Navigator.of(context).pop();
                    },
                    child: new Text("确认"),
                  ),
                  new FlatButton(
                    onPressed: () {
                      Navigator.of(context).pop();
                    },
                    child: new Text("取消"),
                  ),
                ],
              );
            });
      }
      @override
      Widget build(BuildContext context) {
        // TODO: implement build
        return new FloatingActionButton(
            child: new Text("showDialog"),
            onPressed: () {
              _showMyMaterialDialog(context);
            });
      }
    
    }
  • 相关阅读:
    八、JVM视角浅理解并发和锁
    七、JVM类加载机制
    六、JVM命令和工具
    五、jvm垃圾回收3(几种垃圾收集器)
    四、JVM垃圾回收2(垃圾收集算法)
    jvm引用类型
    三、JVM垃圾回收1(如何寻找垃圾?)
    【原创】Android 对话框的使用
    【原创】CMD常用命令:解决实际问题
    【原创】开机出现grub rescue,修复办法
  • 原文地址:https://www.cnblogs.com/mingfeng002/p/11585760.html
Copyright © 2011-2022 走看看