flutter - 使用不包含 MediaQuery 的上下文调用 MediaQuery.of()。(紧急援助)
问题描述
我删除了“MaterialApp”代码块,因为我一开始无法编写我想要的代码。
现在它给出了一个错误,我该如何解决这个问题?我必须在很短的时间内处理这个问题,我必须把我写的代码放在“MaterialApp”块中,但我做不到。
你能帮助我吗 ?
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return CupertinoTabScaffold(
tabBar: CupertinoTabBar(
items: < BottomNavigationBarItem > [
new BottomNavigationBarItem(
icon: new Icon(Icons.home),
title: Text('Enes'),
),
new BottomNavigationBarItem(
icon: new Icon(Icons.bluetooth),
title: Text('Mehmet'),
),
],
),
tabBuilder: (BuildContext context, int index) {
return CupertinoTabView(
builder: (BuildContext context) {
return CupertinoPageScaffold(
navigationBar: CupertinoNavigationBar(
middle: Text('Page 1 of tab $index'),
),
child: Center(
child: CupertinoButton(
child: const Text('Next Page'),
onPressed: () {
Navigator.of(context).push(
CupertinoPageRoute < void > (
builder: (BuildContext context) {
return CupertinoPageScaffold(
navigationBar: CupertinoNavigationBar(
middle: Text('Page 2 of tab $index'),
),
child: Center(
child: CupertinoButton(
child: const Text('Back'),
onPressed: () {
Navigator.of(context).pop();
},
),
),
);
},
),
);
},
),
),
);
},
);
},
);
}
}
解决方案
不知道为什么它必须在 MaterialApp 中。这个解决方案是我过去部署的:
在您的 MaterialApp 中,SetupStuff(可以命名任何东西)被设置为您的 home 或 initialRoute。
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
color: Colors.yellow[100],
debugShowCheckedModeBanner: false,
title: 'MyApp',
theme: currentTheme,
home: SetupStuff(),
);
}
}
class SetupStuff extends StatelessWidget {
@override
Widget build(BuildContext context) {
// This is the first 'context' with a MediaQuery, therefore,
// this is the first opportunity to set MediaQuery based values
//Set values / do things here.
WidgetsBinding.instance.addPostFrameCallback((_) {
Navigator.pushReplacement(context,
MaterialPageRoute(builder: (BuildContext context) => AlsoMyApp()));
});
return SafeArea(child: Material(color: Colors.yellow[300]));
}
}
推荐阅读
- reactjs - Reactjs - 以正确的方式独立切换类
- javascript - 如何使用我的代码查找 `script` 元素?
- html - QML 文本不显示 Unicode 字符
- python - 是否有一个 python 库可以让我自定义根?
- c# - 更改控制台应用程序图标(Visual Studio 代码)
- sql - 如何根据当前日期以及该周的日期查找周数
- python - PySpark 在终端中执行时“非法反射访问操作”
- javascript - 等待被认为是一种不好的编码习惯吗?
- javascript - 如何在 JSX 中使用 Map 迭代嵌套的 JSON
- mongodb - EC2实例中的MongoDB数据被自动删除