调整元素到屏幕宽度/高度的百分比 [英] Sizing elements to percentage of screen width/height

查看:66
本文介绍了调整元素到屏幕宽度/高度的百分比的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有一种简单的方法(非LayoutBuilder)来相对于屏幕大小(宽度/高度)来调整元素的大小?例如:如何将CardView的宽度设置为屏幕宽度的65%.

Is there a simple (non-LayoutBuilder) way to size an element relative to screen size (width/height)? For example: how do I set the width of a CardView to be 65% of the screen width.

(显然)不能在build方法内部完成,因此必须将其推迟到发布后再进行.有没有放置这样的逻辑的首选位置?

It can't be done inside the build method (obviously) so it would have to be deferred until post build. Is there a preferred place to put logic like this?

推荐答案

FractionallySizedBox也可能有用. 您也可以直接从MediaQuery.of(context).size中读取屏幕宽度,并根据该宽度创建一个尺寸框.

FractionallySizedBox may also be useful. You can also read the screen width directly out of MediaQuery.of(context).size and create a sized box based on that

MediaQuery.of(context).size.width * 0.65

如果您确实想将大小调整为屏幕的一小部分,而不管布局是什么.

if you really want to size as a fraction of the screen regardless of what the layout is.

这篇关于调整元素到屏幕宽度/高度的百分比的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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