简体   繁体   English

如何使用 MediaQuery 计算高度和宽度?

[英]how to use calculate height and width using MediaQuery?

Suppose I have a container with height 60. How this 60 can be written in mediaquery?假设我有一个高度为 60 的容器。这个 60 怎么可以写在 mediaquery 中?

example code:示例代码:

Container(height:60,)

I want this container looks like having 60 as height but in responsive way.我希望这个容器看起来像高度为 60,但以响应方式。

Size size = MediaQuert.of(context).size

Container(
   //width: size.width * 0.1 //10% width of Device
   height: size.height * 0.1 //10% height of Device
),

You can Try this:你可以试试这个:

var size = MediaQuery.of(context).size;

Container(height:size.height * 60/580,);

here 60 is the height you prefer and 580 is the height of your device in ui design.这里60是您喜欢的高度,而580是您的设备在 ui 设计中的高度。

this will help var size = MediaQuery.of(context).size.height ;这将有助于var size = MediaQuery.of(context).size.height

and also some plugins there which can help Pubdev Plugins还有一些可以帮助Pubdev 插件的插件

You can use like this你可以这样使用

void main() => runApp(App());

class App extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'App',
      theme: ThemeData(
        primaryColor: Colors.blue,
      ),
      home: HomePage(),
    );
  }
}

class HomePage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    final height = MediaQuery.of(context).size.height;
    final width = MediaQuery.of(context).size.width;
    return Container(
      height: height*0.6,
      width: width*0.5,
      child: ...,
    );
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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