• 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);
            });
      }
    
    }
  • 相关阅读:
    Tarjan 的一些板子
    对 SAM 和 PAM 的一点理解
    一些敲可爱的数论板子
    异常
    面向对象编程
    JAVA数组
    JAVA方法
    JAVA流程控制
    JAVA基础
    JAVA入门
  • 原文地址:https://www.cnblogs.com/mingfeng002/p/11585760.html
Copyright © 2020-2023  润新知