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);
            });
      }
    
    }
  • 相关阅读:
    抽象工厂模式
    python 工厂方法
    采用__call__ 实现装饰器模式
    策略模式
    采集15个代理IP网站,打造免费代理IP池
    grid网格布局——色子布局
    观察者模式
    搭建免费代理池---采集代理(1)
    python 爬虫 user-agent 生成
    多进程 + 多线程抓取博客园信息
  • 原文地址:https://www.cnblogs.com/mingfeng002/p/11585760.html
Copyright © 2011-2022 走看看