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

flutter dialog异常Another exception was thrown: No MaterialLocalizations found的更多相关文章

  1. flutter dialog异常Another exception was thrown: Navigator operation requested with a context that does not include a Navigator

    我在使用flutter里的对话框控件的时候遇到了一个奇怪的错误 Another exception was thrown: Navigator operation requested with a c ...

  2. flutter SnackBar异常Another exception was thrown: Scaffold.of() called with a context that does not contain a Scaffold

    代码如下: import 'package:flutter/material.dart'; void main() { runApp(MaterialApp( title: 'Returning Da ...

  3. Could not create the view: An unexpected exception was thrown的解决方法

    MyEclipse下面的server窗口突然不能正常显示了,而且还显示Could not create the view: An unexpected exception was thrown(无法创 ...

  4. Python中的异常(Exception)处理

    异常 当你的程序出现例外情况时就会发生异常(Exception).例如,当你想要读取一个文件时,而那个文件却不存在,怎么办?又或者你在程序执行时不小心把它删除了,怎么办?这些通过使用异常来进行处理. ...

  5. [C#] C# 知识回顾 - 你真的懂异常(Exception)吗?

    你真的懂异常(Exception)吗? 目录 异常介绍 异常的特点 怎样使用异常 处理异常的 try-catch-finally 捕获异常的 Catch 块 释放资源的 Finally 块 一.异常介 ...

  6. myEclipse Could not create the view: An unexpected exception was thrown.

    myEclipse 非正常关闭,打开后 service Explorer or Package Explorer 视图显示不出来.报“Could not create the view: An une ...

  7. poco json 中文字符,抛异常JSON Exception -->iconv 转换 备忘录。

    起因 最近linux服务器通信需要用到json. jsoncpp比较出名,但poco 1.5版本以后已经带有json库,所以决定使用poco::json(linux 上已经用到了poco这一套框架). ...

  8. Python中获取异常(Exception)信息

    异常信息的获取对于程序的调试非常重要,可以有助于快速定位有错误程序语句的位置.下面介绍几种python中获取异常信息的方法,这里获取异常(Exception)信息采用try...except...程序 ...

  9. (转)Could not create the view: An unexpected exception was thrown. 电脑突然断电,myeclipse非正常关闭,出现错误

    问题:电脑突然断电,myeclipse非正常关闭,“Package Explorer”非正常显示,出现错误“Could not create the view: An unexpected excep ...

随机推荐

  1. scala学习笔记(9)

    一.高阶函数 --------------------------------------------------- 1.作为值的函数:在Scala中,函数是头等公民,就和数字一样. import s ...

  2. Centos安装 Apache2.4提示 APR not found的解决办法

    在安装apache2.2.22版本的时候没有任何问题,可直接使用命令编译安装. 但是,在apache 2.4.12版本,./configure 进行配置时, 提示 configure: error: ...

  3. 028-实现阿里云ESC多FLAT网络

    实现类似于阿里云ECS的网络结构,其效果为云主机拥有两块和两个不同的网络,一个网络是用于用于和外网连接,一个用于内网通信,但宿主机上至少有两个网卡,整体配置如下:1.在wmare里给宿主机添加一块网卡 ...

  4. 瞎JB逆

    P为质 ; long long quickpow(long long a, long long b) { ) ; ; a %= mod; while(b) { ) ret = (ret * a) % ...

  5. 给定两个大小为 m 和 n 的有序数组 nums1 和 nums2。 请你找出这两个有序数组的中位数,并且要求算法的时间复杂度为 O(log(m + n))。 你可以假设 nums1 和 nums2 不会同时为空。

    class Solution { public double findMedianSortedArrays(int[] A, int[] B) { int m = A.length; int n = ...

  6. 利用雅虎ycsb对cassandra做性能测试

    准备: 环境: 两台虚拟机:ip:192.168.138.128/129;配置:2核4G: 版本:apache-cassandra-3.10    ycsb-cassandra-binding-0.1 ...

  7. MySql的导入导出

    可以参看之前的博客:https://www.cnblogs.com/shijinglu2018/p/8672699.html 可以参看视频:https://i.cnblogs.com/EditPost ...

  8. Day_02-Python的循环结构

    循环结构 应用场景 如果在程序中我们需要重复的执行某条或某些指令,例如用程序控制机器人踢足球,如果机器人持球而且还没有进入射门范围,那么我们就要一直发出让机器人向球门方向奔跑的指令.当然你可能已经注意 ...

  9. 2017年cocoaPods 1.2.1升级

    还在用老版本的ccoaPods,安装三方库时,会报错 : [!] Invalid `Podfile` file: [!] The specification of `link_with` in the ...

  10. layer.confirm

    layer.confirm('确定不选择花车?', { title: false, btn: ['确定','取消'] //按钮 }, function(ind){ layer.close(ind); ...