简体   繁体   中英

MediaQuery.of() called with a context that does not contain a MediaQuery

Why do am I getting this Error???

Isn't the Mediaquery provided by the MaterialApp? I don't quite understand.

class Test extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      theme: themeBuilder(context),
      home: Scaffold(
        appBar: AppBar(
          title: Text('Title'),
        ),
        body: Container(
          alignment: Alignment.center,
          child: Column(
            mainAxisAlignment: MainAxisAlignment.center,
            children: <Widget>[
              Text(
                'All their equipment and instruments are alive.',
              ),
              Text(
                'All their equipment and instruments are alive.',
                style: TextStyle(
                  fontSize: 34,
                  fontWeight: FontWeight.w700,
                ),
              ),
              GradientButton(
                child: Text('click me!'),
                onPressed: () {},
                width: MediaQuery.of(context).size.width,
              ),
              TextField()
            ],
          ),
        ),
      ),
    );
  }
}

I don't know what is it I am doing wrong here. It worked just fine before.

class Test extends StatelessWidget {
 @override
 Widget build(BuildContext context) {
  return MaterialApp(
   title: Text('Title'),
   theme: themeBuilder(context),
   home: HomePage(),
  );
 }
}

class HomePage extends StatelessWidget {
 @override
 Widget build(BuildContext context) {
   final size = MediaQuery.of(context).size;

   return Container(
     child: Column(
        mainAxisAlignment: MainAxisAlignment.center,
        children: <Widget>[

         ...

         GradientButton(
            child: Text('click me!'),
            onPressed: () {},
            width: MediaQuery.of(context).size.width,
          ),
         ]
     ),
   );
 }
}

Try this and lemme know how it goes

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM