首页 > 解决方案 > 使用不包含 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();
                                                                    },
                                                            ),
                                                        ),
                                                    );
                                                },
                                            ),
                                        );
                                    },
                            ),
                        ),
                    );
                },
            );
        },
    );
}

}

标签: flutterdart

解决方案


不知道为什么它必须在 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]));
  }
}

推荐阅读