如何在颤动中获得屏幕高度
How to get screen height in flutter
嗨,我只是想使用
MediaQuery.of(context).size.height
但得到 FlutterError (No MediaQuery widget ancestor found. MyApp widgets require a MediaQuery widget ancestor. The specific widget that could not find a MediaQuery ancestor was: MyApp The ownership chain for the affected widget is: "MyApp ← [root]" No MediaQuery ancestor could be found starting from the context that was passed to MediaQuery.of(). This can happen because you have not added a WidgetsApp, CupertinoApp, or MaterialApp widget (those widgets introduce a MediaQuery), or it can happen if the context you use comes from a widget above those widgets.)
class BaseLayout extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
body: Container(
height: MediaQuery.of(context).size.height,
color: Colors.red,
),
),
);
}
}
已编辑
你必须把它放在 MediaQuery 的上下文之上:
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
home: BaseLayout()
);
}
}
class BaseLayout extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
body: Container(
height: MediaQuery.of(context).size.height,
color: Colors.red,
),
),
);
}
}
创建一个自己的 class,您的错误应该消失了。
原回答
你的小部件树中有 MaterialApp 吗? MaterialApp 提供 MediaQuery.
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
body:WhateverWidget
);
}
}
顺便说一下...解决方案在您的错误代码中;)
This can happen because you have not added a WidgetsApp, CupertinoApp, or MaterialApp widget (those widgets introduce a MediaQuery), or it can happen if the context you use comes from a widget above those widgets.)
Hy tony 我找到了你问题的答案
您需要为其添加安全区域
class BaseLayout extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
home: SafeArea(
child: Scaffold(
body: Container(
height: MediaQuery.of(context).size.height,
color: Colors.red,
),
),
),
);
}
}
嗨,我只是想使用
MediaQuery.of(context).size.height
但得到 FlutterError (No MediaQuery widget ancestor found. MyApp widgets require a MediaQuery widget ancestor. The specific widget that could not find a MediaQuery ancestor was: MyApp The ownership chain for the affected widget is: "MyApp ← [root]" No MediaQuery ancestor could be found starting from the context that was passed to MediaQuery.of(). This can happen because you have not added a WidgetsApp, CupertinoApp, or MaterialApp widget (those widgets introduce a MediaQuery), or it can happen if the context you use comes from a widget above those widgets.)
class BaseLayout extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
body: Container(
height: MediaQuery.of(context).size.height,
color: Colors.red,
),
),
);
}
}
已编辑
你必须把它放在 MediaQuery 的上下文之上:
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
home: BaseLayout()
);
}
}
class BaseLayout extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
body: Container(
height: MediaQuery.of(context).size.height,
color: Colors.red,
),
),
);
}
}
创建一个自己的 class,您的错误应该消失了。
原回答
你的小部件树中有 MaterialApp 吗? MaterialApp 提供 MediaQuery.
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
body:WhateverWidget
);
}
}
顺便说一下...解决方案在您的错误代码中;)
This can happen because you have not added a WidgetsApp, CupertinoApp, or MaterialApp widget (those widgets introduce a MediaQuery), or it can happen if the context you use comes from a widget above those widgets.)
Hy tony 我找到了你问题的答案 您需要为其添加安全区域
class BaseLayout extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
home: SafeArea(
child: Scaffold(
body: Container(
height: MediaQuery.of(context).size.height,
color: Colors.red,
),
),
),
);
}
}