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

  1. import 'package:flutter/material.dart';
  2. import 'package:scoped_model/scoped_model.dart';
  3.  
  4. void main() {
  5. runApp(new RootLayout());
  6. }
  7.  
  8. class RootLayout extends StatefulWidget {
  9. @override
  10. State<StatefulWidget> createState() {
  11. return new RootLayoutM();
  12. }
  13. }
  14.  
  15. class RootLayoutM extends State<RootLayout> {
  16. _showMyMaterialDialog(BuildContext context) {
  17. print("_showMyMaterialDialog");
  18. showDialog(
  19. context: context,
  20. builder: (context) {
  21. return new AlertDialog(
  22. title: new Text("title"),
  23. content: new Text("内容内容内容内容内容内容内容内容内容内容内容"),
  24. actions: <Widget>[
  25. new FlatButton(
  26. onPressed: () {
  27. Navigator.of(context).pop();
  28. },
  29. child: new Text("确认"),
  30. ),
  31. new FlatButton(
  32. onPressed: () {
  33. Navigator.of(context).pop();
  34. },
  35. child: new Text("取消"),
  36. ),
  37. ],
  38. );
  39. });
  40. }
  41.  
  42. @override
  43. Widget build(BuildContext context) {
  44. return new MaterialApp(
  45. home: new Scaffold(
  46. body: new Center(
  47. child: new Text("show simple dialog",
  48. style: new TextStyle(color: Color(0xFF00FF00))),
  49. ),
  50. floatingActionButton: new FloatingActionButton(
  51. child: new Text("showDialog"),
  52. onPressed: () {
  53. _showMyMaterialDialog(context);
  54. }),
  55. ),
  56. );
  57. ;
  58. }
  59. }

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

这里发现

  1. @override
  2. Widget build(BuildContext context) {
  3. return new MaterialApp(
  4. home: new Scaffold(
  5. body: new Center(
  6. child: new Text("show simple dialog",
  7. style: new TextStyle(color: Color(0xFF00FF00))),
  8. ),
  9. floatingActionButton: new FloatingActionButton(
  10. child: new Text("showDialog"),
  11. onPressed: () {
  12. _showMyMaterialDialog(context);
  13. }),
  14. ),
  15. );
  16. ;
  17. }

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

  1. class MyFloat extends StatelessWidget{
  2. _showMyMaterialDialog(BuildContext context) {
  3. print("_showMyMaterialDialog");
  4. showDialog(
  5. context: context,
  6. builder: (context) {
  7. return new AlertDialog(
  8. title: new Text("title"),
  9. content: new Text("内容内容内容内容内容内容内容内容内容内容内容"),
  10. actions: <Widget>[
  11. new FlatButton(
  12. onPressed: () {
  13. Navigator.of(context).pop();
  14. },
  15. child: new Text("确认"),
  16. ),
  17. new FlatButton(
  18. onPressed: () {
  19. Navigator.of(context).pop();
  20. },
  21. child: new Text("取消"),
  22. ),
  23. ],
  24. );
  25. });
  26. }
  27. @override
  28. Widget build(BuildContext context) {
  29. // TODO: implement build
  30. return new FloatingActionButton(
  31. child: new Text("showDialog"),
  32. onPressed: () {
  33. _showMyMaterialDialog(context);
  34. });
  35. }

完整代码如下

  1. import 'package:flutter/material.dart';
  2. import 'package:scoped_model/scoped_model.dart';
  3.  
  4. void main() {
  5. runApp(new RootLayout());
  6. }
  7.  
  8. class RootLayout extends StatefulWidget {
  9. @override
  10. State<StatefulWidget> createState() {
  11. return new RootLayoutM();
  12. }
  13. }
  14.  
  15. class RootLayoutM extends State<RootLayout> {
  16.  
  17. @override
  18. Widget build(BuildContext context) {
  19. return new MaterialApp(
  20. home: new Scaffold(
  21. body: new Center(
  22. child: new Text("show simple dialog",
  23. style: new TextStyle(color: Color(0xFF00FF00))),
  24. ),
  25. floatingActionButton: new MyFloat(),
  26. )
  27. );
  28. }
  29.  
  30. }
  31.  
  32. class MyFloat extends StatelessWidget{
  33. _showMyMaterialDialog(BuildContext context) {
  34. print("_showMyMaterialDialog");
  35. showDialog(
  36. context: context,
  37. builder: (context) {
  38. return new AlertDialog(
  39. title: new Text("title"),
  40. content: new Text("内容内容内容内容内容内容内容内容内容内容内容"),
  41. actions: <Widget>[
  42. new FlatButton(
  43. onPressed: () {
  44. Navigator.of(context).pop();
  45. },
  46. child: new Text("确认"),
  47. ),
  48. new FlatButton(
  49. onPressed: () {
  50. Navigator.of(context).pop();
  51. },
  52. child: new Text("取消"),
  53. ),
  54. ],
  55. );
  56. });
  57. }
  58. @override
  59. Widget build(BuildContext context) {
  60. // TODO: implement build
  61. return new FloatingActionButton(
  62. child: new Text("showDialog"),
  63. onPressed: () {
  64. _showMyMaterialDialog(context);
  65. });
  66. }
  67.  
  68. }

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. react找到对象数组中指定的值

    找到对象数组中指定的值var array = [            { label: "Custom", value: "0" },            ...

  2. mailaddr - 关于邮件地址的描述

    DESCRIPTION 描述 本手册给出的是 Internet 使用的 SMTP 邮件地址的简要描述.这些地址的通常的格式是 user@domain 这里的域 (domain) 是分级的子域的列表,子 ...

  3. PHP7搭建项目遇到的坑

    报错一:该网页无法正常运作情况原因记录 code 500 原因:由于php.ini配置文件中错误显示关闭导致 解决方法: 修改php.ini配置文件 display_errors = On displ ...

  4. Linux服务器安装系统之1-LSI阵列卡raid5配置方法

  5. Codeforces 955 LR询问 多次幂处理

    A 模拟题 #include <bits/stdc++.h> #define PI acos(-1.0) #define mem(a,b) memset((a),b,sizeof(a)) ...

  6. 1130. Infix Expression (25)

    Given a syntax tree (binary), you are supposed to output the corresponding infix expression, with pa ...

  7. redis安装篇

    Redis简介 Redis是一个开源的使用ANSI C语言编写.支持网络.可基于内存亦可持久化的日志型.Key-Value数据库,并提供多种语言的API.从2010年3月15日起,Redis的开发工作 ...

  8. ipsec概念理解

    互联网安全协议(英语:Internet Protocol Security,缩写:IPsec): 本质上一个协议包,透过对IP协议的分组进行加密和认证来保护IP协议的网络传输协议族(一些相互关联的协议 ...

  9. AGC007题解

    发现自己思维能力又跟不上了...做题有点吃力...所以回归AGC刷题计划... AGC040506都写了一部分题然后懒得补全了,所以从07开始做吧.大概是从C开始. C 这也太人类智慧了吧... 我先 ...

  10. Oracle12c RAC RMAN异机恢复

    ######################################################## #编辑pfile文件initspdb.ora vi /oracle/app/oracl ...