flutter布局中的单位_在Flutter中创建基本布局

flutter布局中的单位

As Flutter quickly becomes one of the leading technologies in app development, it is important for even someone in the web dev environment to check out. Coming from a background of HTML, CSS and JavaScript, the way Flutter handled everything seemed extremely foreign to me. So we’re going to be try and simplify it to just the essentials of structuring the elements in our app to create a simple layout using the Material Components.

随着FlutterSwift成为应用程序开发中的领先技术之一,即使对于Web开发环境中的某人来说,签出也是非常重要的。 Flutter具有HTML,CSS和JavaScript的背景,它处理所有事情的方式对我来说似乎非常陌生。 因此,我们将尝试将其简化为仅结构化应用程序中的元素以使用Material Components创建简单布局的要素。

先决条件 (Prerequisites)

Already having Flutter installed and an emulator set up is necessary, we covered getting started in Flutter here. Becoming familiar with the official docs is also always a plus.

已经安装了Flutter并已设置仿真器是必要的,我们在这里介绍了Flutter的入门 。 熟悉官方文档也总是有加的。

脚手架 (Scaffold)

The Scaffold instantiates our main structure, usually whatever is consistent across our app like our appbar or navigation, then we’ll set our body to the more interesting part of our app, abstracting it out of runApp will also allow us to use hot reload.

Scaffold实例化我们的主要结构,通常是整个应用程序中一致的东西,例如我们的应用程序栏或导航,然后将我们的body设置为应用程序中更有趣的部分,从runApp抽象出来也将允许我们使用热重载。

main.dart
main.dart
import 'package:flutter/material.dart';

void main() {
  runApp(
    MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          centerTitle: true,
          title: Text('I\'m an App'),
          backgroundColor: Colors.red[600],
        ),
        body: App(),
      ),
    ),
  );
}

class App extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Container();
  }
}

And here’s what this looks like:

这是这样的:

货柜 (Containers)

Like with HTML div’s, we can wrap our containers to give us more control over our elements when we can’t manipulate them themselves, since not every widget has properties like width or padding. Containers have some of the same properties from CSS like height, width, padding, and margin. By default, they will take up the maximum space of their children, empty containers try to take up the maximum amount of space of their parent.

像HTML div一样,我们可以包装容器,以便在我们无法操纵元素时对元素进行更多控制,因为并非每个小部件都具有width或padding之类的属性。 容器具有CSS的一些相同属性,例如heightwidthpaddingmargin 。 默认情况下,它们将占用其子级的最大空间,空容器会尝试占用其父级的最大空间。

保证金和填充 (Margin and Padding)

Controlling the spacing can be a bit weird, instead of directly setting padding or margin to a number of pixels, we need to set it to a property on EdgeInsets and pass in our value in pixels. There is also the Padding widget that you can wrap your elements in, but you still need to pass-in padding the same as before so it’s really just to be explicit.

控制间距可能有点怪异,而不是直接将padding或margin设置为多个像素,我们需要将其设置为EdgeInsets上的一个属性, EdgeInsets以像素为单位传递值。 还可以使用Padding小部件来包装元素,但是您仍然需要像以前一样传递padding,因此实际上只是为了明确起见。

  • EdgeInsets.all()

    EdgeInsets.all()

  • EdgeInsets.only(left: 0, top: 0, right: 0, bottom: 0)

    EdgeInsets.only(left: 0, top: 0, right: 0, bottom: 0)

  • EdgeInsets.symmetric(vertical: 0, horizontal: 0)

    EdgeInsets.symmetric(vertical: 0, horizontal: 0)

  • EdgeInsets.fromLTRB(left, top, right, bottom) Just takes the values without them be explicitly assigned.

    EdgeInsets.fromLTRB(left, top, right, bottom)只接受没有显式分配的值。

main.dart
main.dart
class App extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Container(
      color: Colors.blue,
      margin: EdgeInsets.only(top: 20, right: 50),
      child: Container(
        color: Colors.green,
        // Add 200px on top and bottom
        margin: EdgeInsets.symmetric(vertical: 200),
        child: Container(
          color: Colors.yellow,
          margin: EdgeInsets.fromLTRB(0, 20, 200, 20),
        ),
      ),
    );
  }
}

And now we have a bunch of containers:

现在我们有一堆容器:

列和行 (Columns and Rows)

The problem with containers is that we can only have one child in each. The column and row widgets let us use a more flexible version of containers while controlling the direction of our elements. We can’t just pass child to them for each element, we need to set children to an array of the type <Widget>, then pass in each of our elements.

容器的问题是每个容器只能有一个孩子。 列和行小部件使我们可以使用更灵活的容器版本,同时控制元素的方向。 我们不能只是将child元素传递给每个元素,我们需要将children元素设置为<Widget>类型的数组,然后传入每个元素。

If we put a container inside of an Expanded widget, it will then take up the maximum amount of space of its column or row.

如果我们将容器放在Expanded小部件内,则它将占用其列或行的最大空间。

Similar to how flexbox works on the web, we can use mainAxisAlignment and crossAxisAlignment to do things the same as justify-content and align-items. We even have the same options of start, center, end, spaceEvenly, spaceAround, spaceBetween, and stretch.

与flexbox在网络上的工作方式类似,我们可以使用mainAxisAlignmentcrossAxisAlignment来完成与justify-contentalign-items 。 我们甚至有相同的startcenterendspaceEvenlyspaceAroundspaceBetweenstretch

main.dart
main.dart
class App extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Column(
        mainAxisAlignment: MainAxisAlignment.spaceAround,
        children: <Widget>[
          // Row 1
          Row(
            children: <Widget>[
              Container(
                color: Colors.blue, height: 40, width: 40, child: Text('1')),
              Container(
                color: Colors.blue, height: 40, width: 40, child: Text('2')),
              Container(
                color: Colors.blue, height: 40, width: 40, child: Text('3')),
            ],
          ),
          // Row 2
          Row(
            children: <Widget>[
              Container(
                color: Colors.blue, height: 40, width: 40, child: Text('1')),
              //Will expand to fill all remaining space
              Expanded(
                  child: Container(
                    color: Colors.green,
                    height: 40,
                    width: 40,
                    child: Text('2'))),
              Container(
                color: Colors.blue, height: 40, width: 40, child: Text('3')),
            ],
          ),
          //Row 3
          Container(
              height: 100,
              child: Row(
                //Stretches to vertically fill its parent container
                crossAxisAlignment: CrossAxisAlignment.stretch,
                children: <Widget>[
                  Container(
                    color: Colors.blue,
                    height: 40,
                    width: 40,
                    child: Text('1')),
                  Expanded(
                      child: Container(
                        color: Colors.green,
                        height: 40,
                        width: 40,
                        child: Text('2'))),
                  Container(
                    color: Colors.blue,
                    height: 40,
                    width: 40,
                    child: Text('3')),
                ],
              )),
          // Row 4
          Row(
            //Creates even space between each item and their parent container
            mainAxisAlignment: MainAxisAlignment.spaceAround,
            children: <Widget>[
              Container(
                color: Colors.blue, height: 40, width: 40, child: Text('1')),
              Container(
                color: Colors.blue, height: 40, width: 40, child: Text('1')),
              Container(
                color: Colors.blue, height: 40, width: 40, child: Text('3')),
            ],
          )
        ]);
  }
}

And here’s a screenshot of our rows and columns:

这是我们的行和列的屏幕截图:

结论 (Conclusion)

While working with layouts in Flutter may seem a bit more clunky and verbose that on the web at first glance, it’s good to keep in mind that we also don’t have to deal with the complexity of dynamic grids or bloated media queries for every screen size. At times, layouts in Flutter may seem less powerful than the CSS you’re used to, because it really doesn’t have to be. With just a few containers, columns, rows, and spacing you can get most of the structures than you’ll ever need.

乍一看,在Flutter中使用布局可能看起来有些笨拙和冗长,但请记住,我们也不必处理每个屏幕的动态网格或庞大的媒体查询的复杂性尺寸。 有时,Flutter中的布局似乎不如您惯用CSS强大,因为它不一定必须如此。 仅需几个容器,列,行和间距,就可以得到所需的大部分结构。

翻译自: https://www.digitalocean.com/community/tutorials/flutter-basic-layout

flutter布局中的单位

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值