0

I deleted the 'MaterialApp' code block because I couldn't write the codes I wanted at the beginning.

Now it gives an error, how can I fix this? I have to handle this in a very short time, I have to put the codes I wrote in the "MaterialApp" block, but I can't.

Can you help me ?

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();
                                                                    },
                                                            ),
                                                        ),
                                                    );
                                                },
                                            ),
                                        );
                                    },
                            ),
                        ),
                    );
                },
            );
        },
    );
}

}

Saeed All Gharaee
  • 1,546
  • 1
  • 14
  • 27
EnesBudak
  • 13
  • 2
  • 1
    Does this answer your question? [Flutter Error: MediaQuery.of() called with a context that does not contain a MediaQuery](https://stackoverflow.com/questions/50214338/flutter-error-mediaquery-of-called-with-a-context-that-does-not-contain-a-med) – Nae Jun 02 '20 at 11:36

2 Answers2

0

Not sure why it must be in the MaterialApp. This solution is what I have deployed in the past:

In your MaterialApp, SetupStuff (could be named anything) is set as your home or 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]));
  }
}
JonnyH
  • 358
  • 2
  • 8
  • Sorry I'm new to this program. When I write the code you have discarded, I get an error in "Router", "splashRoute" and "currentTheme" codes. Could you please apply it on the codes I wrote and share it? – EnesBudak Jun 02 '20 at 11:50
  • Rename your 'MyApp' to 'AlsoMyApp'. And try the code above. More details about this issue can be found at the link provided by Nae above. – JonnyH Jun 02 '20 at 12:05
0

It seems like you want to develop an app with iOS elements. If so, you need to replace MaterialApp with CupertinoApp. If not, you MUST NOT delete MaterialApp because it is necessary to be there.

class CupertinoApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
   return CupertinoApp(
     home: CupertinoHomePage(),
    );
  }
}

I wish it helps.

Saeed All Gharaee
  • 1,546
  • 1
  • 14
  • 27