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

news/2024/7/5 18:55:17

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布局中的单位


http://www.niftyadmin.cn/n/3649752.html

相关文章

Signing a midlet suite的讨论稿[J2ME]

[J2ME] Signing a midlet suite的讨论稿发起者&#xff1a;郑昀(zhengyun_ustc)[问题]如何对一个MIDlet jar签名呢&#xff1f;很多人都想知道怎么让自己的MIDlet访问网络啦、发送短信啦都能够不弹出烦人的警告框。单纯回答说“你必须去Versign购买一个证书&#xff0c;或者找厂…

在JavaScript中使用Trim String方法

It’s always helpful to have an easy method method on strings to remove trailing or leading whitespace and the trim() method available on JavaScript strings is here to help us with doing exactly that. 在字符串上使用简单的方法方法来删除尾部或前导空格总是有帮…

[Nutch]如何利用HTML页面中meta元素?

[Nutch]如何利用HTML页面中meta元素&#xff1f;[郑昀]1&#xff1a;假如说你的站点页面中有这么一句&#xff1a;你利用Nutch抓取并分析时&#xff0c;如何得到这个meta数值并处理呢&#xff1f;2&#xff1a;你可以从HtmlParseFilter扩展出一个RobotsParserFilter&#xff0c…

链表中倒数最后k个结点

描述 输入一个长度为 n 的链表&#xff0c;设链表中的元素的值为 ai &#xff0c;返回该链表中倒数第k个节点。 如果该链表长度小于k&#xff0c;请返回一个长度为 0 的链表。 数据范围&#xff1a;0 \leq n \leq 10^50≤n≤105&#xff0c;0 \leq a_i \leq 10^90≤ai​≤109…

flutter获取地理位置_Flutter:使用Geolocator插件获取用户位置

flutter获取地理位置Ever wanted to get a user’s location within your Flutter application? We’re going to be building an application that does exactly that by taking advantage of the Geolocator plugin. 是否曾经想在Flutter应用程序中获取用户的位置&#xff1…

用户界面和多媒体版面问题[二][j2medev][0406更新]

第一部分在 "用户界面和多媒体"版面问题整理[j2medev.com][不断更新中]。下面是后续更新的部分。全部目录为&#xff1a;1 全屏问题2 Image和ByteArray转换问题3 getRGB问题4 字符串换行问题5 字体问题6 重新播放音乐问题7 MediaException问题8 混音问题9 CustomItem…

手机用户界面和多媒体版面有价值问题整理[j2medev.com][0406更新]

预告&#xff1a;j2medev.com将开始整理各个版面的有价值问题&#xff0c;并以PDF格式集结&#xff0c;敬请期待。出品商产品名称产品版本J2medev.com版面有价值的问题合集0.1j2medev.com之“用户界面和多媒体”版面有价值问题的整理历史VersionDateCreatorDescription1.0.0.12…

golang init_了解Go中的init

golang init介绍 (Introduction) In Go, the predefined init() function sets off a piece of code to run before any other part of your package. This code will execute as soon as the package is imported, and can be used when you need your application to initial…