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

后端 未结 14 1137
借酒劲吻你
借酒劲吻你 2020-11-28 14:05

I have been trying to get the size of the whole context view in Flutter. But every time I try I\'m getting the above mentioned error. Here\'s my code:

impor         


        
14条回答
  •  刺人心
    刺人心 (楼主)
    2020-11-28 14:45

    import 'package:flutter/material.dart';
    
    void main() => runApp(App());
    
    class App extends StatelessWidget {
    
      @override
     
     Widget build(BuildContext context) {
        
       return MaterialApp(
        
          home: Scaffold(
          body:HomePage(),
          ),
        );
       }
    
    }
    
    
    class HomePage extends StatelessWidget {
    
      @override
    
      Widget build(BuildContext context) {
    
        var size = MediaQuery.of(context).size.height;
    
        return Container(
          height:size/2,
          color:Colors.lightBlueAccent,
            
        );
      }
    
    }
    

    YOU SHOULD TRY THIS I HAVE DONE IT.

提交回复
热议问题