No MaterialLocalizations found.和No MediaQuery widget found的解决方法
【摘要】 在使用showDialog和showModalBottomSheet时,发现它们不能直接写在 MaterialApp的home属性下,会报如下错误: Another exception was thrown: No MaterialLocalizations found. 或 Another exception was thrown: No MediaQuery wid...
在使用showDialog
和showModalBottomSheet
时,发现它们不能直接写在 MaterialApp
的home属性下,会报如下错误:
Another exception was thrown: No MaterialLocalizations found.
或
Another exception was thrown: No MediaQuery widget found.
解决的办法就是将代码提取出来:
class MyApp extends StatelessWidget { @override
Widget build(BuildContext context) { return MaterialApp( /// [home]这个地方,不能直接写dialog的代码,否则你会得到 "Another exception was thrown: No MaterialLocalizations found." /// 也不能写showModalBottomSheet的代码,否则你会得到 Another exception was thrown: No MediaQuery widget found. /// 正确的做法就是把所有的内容都提取出去写。如下面这样把内容都提出来写到[HomePage] home: HomePage(),// 不能直接写dialog的代码 );
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
提取出来的代码放到HomePage下:
class HomePage extends StatefulWidget {
@override
_HomePageState createState() => _HomePageState();
}
class _HomePageState extends State<HomePage> { @override
Widget build(BuildContext context) { return Scaffold( appBar: AppBar( title: const Text('分享'), actions: <Widget>[ IconButton( icon: Icon(Icons.share), onPressed: () { showModalBottomSheet( /** * showModalBottomSheet常用属性 * shape 设置形状 * isScrollControlled:全屏还是半屏 * isDismissible:外部是否可以点击,false不可以点击,true可以点击,点击后消失 * backgroundColor : 设置背景色 */ backgroundColor: Colors.transparent, context: context, builder: (BuildContext context) { return RaisedButton( child: Text("Click"), onPressed: () {}, ); }, ); }, ), ], ), body: Center(), );
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
文章来源: blog.csdn.net,作者:WongKyunban,版权归原作者所有,如需转载,请联系作者。
原文链接:blog.csdn.net/weixin_40763897/article/details/109412386
【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱:
cloudbbs@huaweicloud.com
- 点赞
- 收藏
- 关注作者
评论(0)