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);
        });
  }

}

 

转载于:https://www.cnblogs.com/mingfeng002/p/11585760.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值