[英]Flutter Error: MediaQuery.of() called with a context that does not contain a MediaQuery
[英]Getting Error in flutter MediaQuery.of() called with a context that does not contain a MediaQuery
我不明白错误是什么。 我正在返回一个 Scaffold 小部件。 Visual Studio 代码未检测到语法错误。 运行应用程序时出现错误。
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: Text("here is my text")),
body: Text(
'No, we need bold strokes. We need this plan.',
style: TextStyle(
fontWeight: FontWeight.bold,
color: Colors.blue.withOpacity(0.7),
height: 1,
fontSize: 20,
decoration: TextDecoration.underline,
decorationColor: Colors.green,
decorationStyle: TextDecorationStyle.wavy,
backgroundColor: Colors.red),
),
);
}
}
如果我将脚手架小部件包装在材料应用程序中,它就可以工作。 谁能给我解释一下原因?
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(title: Text("here is my text")),
body: Text(
'No, we need bold strokes. We need this plan.',
style: TextStyle(
fontWeight: FontWeight.bold,
color: Colors.blue.withOpacity(0.7),
height: 1,
fontSize: 20,
decoration: TextDecoration.underline,
decorationColor: Colors.green,
decorationStyle: TextDecorationStyle.wavy,
backgroundColor: Colors.red),
),
),
);
}
}
最顶部的小部件必须包装在 MaterialApp 小部件中。 这就是生成它正在寻找的上下文链的原因。
Media Query 是 MaterialApp 的一个属性,所以如果您不使用 MaterialApp,您将无法访问媒体查询。 正确的方法是在你的主 class 中使用它。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.