double.infinity和MediaQuery有什么区别? [英] Whats the difference between double.infinity and MediaQuery?

查看:563
本文介绍了double.infinity和MediaQuery有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

要占用整个宽度或高度,我使用 double.infinity ,但是在进行一些抖动示例时,我注意到许多人使用 MediaQuery.of(context).size.width height 。我浏览了文档,但在这两个文档之间以及何时使用哪一个都具有相同功能方面并没有提及任何区别。它们是特定于特定的小部件吗?

To occupy the whole width or height , I use double.infinity, but going through some of the flutter samples, I have noticed many people use MediaQuery.of(context).size.width or height. I went through the docs but there are no differences mentioned between this two or when to use which one as both allow same feature. Are they specific to particular widgets?

推荐答案

区别可以总结为:


  • 我想和父母允许的一样大(double.infinity)

  • 我想和屏幕一样大(MediaQuery)。

通常,您将要使用 double.infinity ,但这是并非总是可能。

Usually, you'll want to use double.infinity, but it's not always possible.

某些小部件允许其子级达到想要的大小( Column ListView OverflowBox ...)。在这种情况下,使用 double.infinity 会产生一个悖论:

Some Widgets allow their children to be as big as they want to be (Column, ListView, OverflowBox...). In that situation using double.infinity creates a paradox:


  • 父级允许任何大小

  • 孩子想要父母允许的最大大小

在这种情况下使用 MediaQuery 是不好的。除非您要创建类似于 Scaffold 的小部件,否则您几乎不需要这样做。

Using MediaQuery in these situations is bad though. You will rarely want to do that unless you're creating a widget similar to Scaffold.

在这里方便使用 IntrinsincHeight 之类的小部件。这些小部件能够解决这一矛盾,因此具有有效的布局。

That's where widgets such as IntrinsincHeight comes in handy. These widgets are able to solve the paradox, and therefore have a valid layout.

这篇关于double.infinity和MediaQuery有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆