![](/img/trans.png)
[英]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.