首先,先看下错误代码。
错误代码:
Error:
MediaQuery.of() called with a context that does not contain a MediaQuery.
没有在MediaQuery的上下文调用MediaQuery.of()
提示
No MediaQuery ancestor could be found starting from the context that was passed to MediaQuery.of().
This can happen because you do not have a WidgetsApp or MaterialApp widget (those widgets introduce
a MediaQuery), or it can happen if the context you use comes from a widget above those widgets.
提示上说出现这种错误会有两种情况:
- 没有使用WidgetsApp或MaterialApp组件,因为这些组件引用了MediaQuery。
可以从代码上找出:
- 使用的小组件在这些组件(WidgetsApp或MaterialApp组件)的层级之上。
修复Error:
我这里写了两种不同的方式:
第1种 main.dart
widget_test.dart
第2种 main.dart
widget_test.dart