如何获得响应式Flutter布局? [英] How to get responsive Flutter layouts?

查看:240
本文介绍了如何获得响应式Flutter布局?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

创建自适应布局的最佳实践是什么?我知道您可以使用媒体查询来获取大小调整信息,以决定要做什么.例如台式机,平板电脑,手机的不同屏幕.

What is best practice for creating a responsive layout? I know that you can use media queries to get sizing information to dictate what to do; e.g. different screens for desktop, tablet, phone.

但是,通常使用ExpandedFlex属性来确保小部件增长或填充适当的屏幕尺寸吗?作为Flutter的新开发人员,请尝试了解如何平衡典型用例.

However, is it common practice to use Expanded or Flex properties to ensure widgets grow or fill the appropriate screen sizes? As a new Flutter developer, trying to understand how the balance is struck on typical use cases.

推荐答案

有多种方法可以使您的UI在Flutter中响应,但是仅列举一些经验法则即可完成工作:

There are multiple ways to make your UI responsive in Flutter, but just to name a few rules of thumb that will mostly get the job done:

您可以根据屏幕的一部分设置一些小部件heightwidth,例如,创建一个SizedBox,它将沿垂直和水平方向填充屏幕的50%:

You can set some widget height or width based on a portion of the screen, for example, creating a SizedBox that will fill 50% of the screen both vertically and horizontally:

SizedBox(
  height: MediaQuery.of(context).size.height * 0.5,
  width: MediaQuery.of(context).size.width * 0.5,
)

您可能会对MediaQuery中的其他属性感兴趣,例如安全区域视口中的padding等.您也可以找到关于它的很好的文章此处.

There are other properties that might interest you in the MediaQuery such as the padding from the safe area viewport and so on. You can also find a good article about it here.

构建布局时最有趣的小部件之一.它将为您提供父级约束,因此您可以使用它来动态调整用户界面.

One of the most interesting widgets when it comes to build layouts. It will provide you with the parent constraints so you can use it to dynamically adapt your UI.

例如,这将使您的孩子(SizedBox)小部件使用父级的maxWidth.

For example, this will make your child (SizedBox) widget take the parent's maxWidth.

LayoutBuilder(
  builder: (BuildContext context, BoxConstraints constraints){
   return SizedBox(
              width: constraints.maxWidth
          );
     }
)

关于LayoutBuilder的一些有趣文章可以在此处.

Some interesting article about LayoutBuilder can be found here.

通过使用Flex小部件(例如ExpandedFlexible).在RowColumn中使用时,它们将根据它们施加的约束条件进行动态调整,并且与ColumnRow对齐方式/大小一起使用,它们的功能非常强大,足以使您的UI响应.

By using Flex widgets such as Expanded and Flexible. When used in a Row or Column they'll dynamically adapt based on the constraints imposed by them and along with Column and Row alignments/size they are quite powerful enough to make your UI responsive.

例如,您可以在一个Row中有两个Container,其中一个使用视图的1/4,另一个使用视图的3/4.

For example, you can have a two Containers in one Row where one uses 1/4 of the view and the other takes 3/4 of the space available.

Row(
  children: <Widget>[
         Expanded(
           flex: 1,
           child: Container(),
         ),
         Expanded(
           flex: 3,
           child: Container(),
         ),
      ]
)

可以在此处找到另一篇很棒的文章

Another great article about it can be found here.

此外,您始终可以使用Platform类获取器来查找基础平台以做出一些决定.

Also, you can always lookup for the underlying platform to make some decisions by using the Platform class getters.

import 'dart:io';

if(Platform.isAndroid) {
   print('Running on Android');
}

TL; DR::有很多选项可以一起使用,您应该始终为每种情况寻找最佳方法.

TL;DR: There are a lot of options that can be played together, you should always look for the best approach for each scenario.

这篇关于如何获得响应式Flutter布局?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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