簡體   English   中英

MediaQuery.of()使用不包含MediaQuery的上下文調用

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

為什么我得到這個錯誤???

不是MaterialApp提供的Mediaquery嗎? 我不太明白。

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

我不知道這是什么我在這里做錯了。 它之前工作得很好。

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

試試這個,然后知道它是怎么回事

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM