本页面向熟悉 HTML 和 CSS 语法来排列应用程序 UI 组件的用户。它将 HTML/CSS 代码片段映射到其 Flutter/Dart 代码等效项。

Flutter 是一个用于构建跨平台应用程序的框架,它使用 Dart 编程语言。要了解 Dart 编程和 Javascript 编程之间的一些差异,请参阅作为 JavaScript 开发者学习 Dart

设计 Web 布局和 Flutter 布局之间的一个根本区别是学习约束的工作方式以及小部件的尺寸和定位方式。要了解更多信息,请参阅理解约束

这些示例假设

  • HTML 文档以 <!DOCTYPE html> 开头,并且所有 HTML 元素的 CSS 盒模型都设置为border-box,以与 Flutter 模型保持一致。

    css
    {
        box-sizing: border-box;
    }
  • 在 Flutter 中,为了保持语法简洁,“Lorem ipsum”文本的默认样式由 bold24Roboto 变量定义,如下所示:

    dart
    TextStyle bold24Roboto = const TextStyle(
      color: Colors.white,
      fontSize: 24,
      fontWeight: FontWeight.bold,
    );

执行基本布局操作

#

以下示例展示了如何执行最常见的 UI 布局任务。

文本样式和对齐

#

CSS 通过 fontcolor 属性处理的字体样式、大小和其他文本属性是 Text 组件的 TextStyle 子项的独立属性。

对于 CSS 中用于对齐文本的 text-align 属性,Text 组件有一个 textAlign 属性。

在 HTML 和 Flutter 中,子元素或小部件默认都锚定在左上角。

css
<div class="grey-box">
  Lorem ipsum
</div>

.grey-box {
    background-color: #e0e0e0; /* grey 300 */
    width: 320px;
    height: 240px;
    font: 900 24px Georgia;
}
dart
final container = Container(
  // grey box
  width: 320,
  height: 240,
  color: Colors.grey[300],
  child: const Text(
    'Lorem ipsum',
    style: TextStyle(
      fontFamily: 'Georgia',
      fontSize: 24,
      fontWeight: FontWeight.bold,
    ),
    textAlign: TextAlign.center,
  ),
);

设置背景颜色

#

在 Flutter 中,您可以使用 Containercolor 属性或 decoration 属性设置背景颜色。但是,您不能同时提供两者,因为它可能会导致装饰覆盖背景颜色。当背景是简单的颜色时,应首选 color 属性。对于其他情况,例如渐变或图像,请使用 decoration 属性。

CSS 示例使用与 Material 调色板等效的十六进制颜色。

css
<div class="grey-box">
  Lorem ipsum
</div>

.grey-box {
    background-color: #e0e0e0; /* grey 300 */
    width: 320px;
    height: 240px;
    font: 900 24px Roboto;
}
dart
final container = Container(
  // grey box
  width: 320,
  height: 240,
  color: Colors.grey[300],
  child: Text(
    'Lorem ipsum',
    style: bold24Roboto,
  ),
);
dart
final container = Container(
  // grey box
  width: 320,
  height: 240,
  decoration: BoxDecoration(
    color: Colors.grey[300],
  ),
  child: Text(
    'Lorem ipsum',
    style: bold24Roboto,
  ),
);

组件居中

#

一个 Center 小部件会将其子项水平和垂直居中。

要在 CSS 中实现类似的效果,父元素使用 flex 或 table-cell 显示行为。本页上的示例显示了 flex 行为。

css
<div class="grey-box">
  Lorem ipsum
</div>

.grey-box {
    background-color: #e0e0e0; /* grey 300 */
    width: 320px;
    height: 240px;
    font: 900 24px Roboto;
    display: flex;
    align-items: center;
    justify-content: center;
}
dart
final container = Container(
  // grey box
  width: 320,
  height: 240,
  color: Colors.grey[300],
  child: Center(
    child: Text(
      'Lorem ipsum',
      style: bold24Roboto,
    ),
  ),
);

设置容器宽度

#

要指定 Container 组件的宽度,请使用其 width 属性。这是一个固定宽度,不像 CSS 的 max-width 属性会根据最大值调整容器宽度。要在 Flutter 中模仿这种效果,请使用 Container 的 constraints 属性。创建一个新的 BoxConstraints 组件,并设置 minWidthmaxWidth

对于嵌套的 Container,如果父容器的宽度小于子容器的宽度,子容器会调整自身大小以匹配父容器。

css
<div class="grey-box">
  <div class="red-box">
    Lorem ipsum
  </div>
</div>

.grey-box {
    background-color: #e0e0e0; /* grey 300 */
    width: 320px;
    height: 240px;
    font: 900 24px Roboto;
    display: flex;
    align-items: center;
    justify-content: center;
}
.red-box {
    background-color: #ef5350; /* red 400 */
    padding: 16px;
    color: #ffffff;
    width: 100%;
    max-width: 240px;
}
dart
final container = Container(
  // grey box
  width: 320,
  height: 240,
  color: Colors.grey[300],
  child: Center(
    child: Container(
      // red box
      width: 240, // max-width is 240
      padding: const EdgeInsets.all(16),
      decoration: BoxDecoration(
        color: Colors.red[400],
      ),
      child: Text(
        'Lorem ipsum',
        style: bold24Roboto,
      ),
    ),
  ),
);

操作位置和大小

#

以下示例展示了如何对小部件的位置、大小和背景执行更复杂的操作。

设置绝对位置

#

默认情况下,小部件相对于其父级定位。

要为小部件指定 x-y 坐标的绝对位置,请将其嵌套在 Positioned 小部件中,该小部件又嵌套在 Stack 小部件中。

css
<div class="grey-box">
  <div class="red-box">
    Lorem ipsum
  </div>
</div>

.grey-box {
    position: relative;
    background-color: #e0e0e0; /* grey 300 */
    width: 320px;
    height: 240px;
    font: 900 24px Roboto;
}
.red-box {
    background-color: #ef5350; /* red 400 */
    padding: 16px;
    color: #ffffff;
    position: absolute;
    top: 24px;
    left: 24px;
}
dart
final container = Container(
  // grey box
  width: 320,
  height: 240,
  color: Colors.grey[300],
  child: Stack(
    children: [
      Positioned(
        // red box
        left: 24,
        top: 24,
        child: Container(
          padding: const EdgeInsets.all(16),
          decoration: BoxDecoration(
            color: Colors.red[400],
          ),
          child: Text(
            'Lorem ipsum',
            style: bold24Roboto,
          ),
        ),
      ),
    ],
  ),
);

旋转组件

#

要旋转小部件,请将其嵌套在 Transform 小部件中。使用 Transform 小部件的 alignmentorigin 属性分别以相对和绝对术语指定变换原点(支点)。

对于简单的 2D 旋转,其中小部件在 Z 轴上旋转,请创建一个新的 Matrix4 恒等对象,并使用其 rotateZ() 方法以弧度(度数 × π / 180)指定旋转因子。

css
<div class="grey-box">
  <div class="red-box">
    Lorem ipsum
  </div>
</div>

.grey-box {
    background-color: #e0e0e0; /* grey 300 */
    width: 320px;
    height: 240px;
    font: 900 24px Roboto;
    display: flex;
    align-items: center;
    justify-content: center;
}
.red-box {
    background-color: #ef5350; /* red 400 */
    padding: 16px;
    color: #ffffff;
    transform: rotate(15deg);
}
dart
final container = Container(
  // grey box
  width: 320,
  height: 240,
  color: Colors.grey[300],
  child: Center(
    child: Transform(
      alignment: Alignment.center,
      transform: Matrix4.identity()..rotateZ(15 * 3.1415927 / 180),
      child: Container(
        // red box
        padding: const EdgeInsets.all(16),
        decoration: BoxDecoration(
          color: Colors.red[400],
        ),
        child: Text(
          'Lorem ipsum',
          style: bold24Roboto,
          textAlign: TextAlign.center,
        ),
      ),
    ),
  ),
);

缩放组件

#

要放大或缩小小部件,请将其嵌套在 Transform 小部件中。使用 Transform 小部件的 alignmentorigin 属性分别以相对或绝对术语指定变换原点(支点)。

对于沿 x 轴的简单缩放操作,请创建一个新的 Matrix4 恒等对象,并使用其 scale() 方法指定缩放因子。

当您缩放父小部件时,其子小部件也会相应地缩放。

css
<div class="grey-box">
  <div class="red-box">
    Lorem ipsum
  </div>
</div>

.grey-box {
    background-color: #e0e0e0; /* grey 300 */
    width: 320px;
    height: 240px;
    font: 900 24px Roboto;
    display: flex;
    align-items: center;
    justify-content: center;
}
.red-box {
    background-color: #ef5350; /* red 400 */
    padding: 16px;
    color: #ffffff;
    transform: scale(1.5);
}
dart
final container = Container(
  // grey box
  width: 320,
  height: 240,
  color: Colors.grey[300],
  child: Center(
    child: Transform(
      alignment: Alignment.center,
      transform: Matrix4.identity()..scaleByDouble(1.5, 1.5, 1.5, 1.5),
      child: Container(
        // red box
        padding: const EdgeInsets.all(16),
        decoration: BoxDecoration(
          color: Colors.red[400],
        ),
        child: Text(
          'Lorem ipsum',
          style: bold24Roboto,
          textAlign: TextAlign.center,
        ),
      ),
    ),
  ),
);

应用线性渐变

#

要将线性渐变应用于小部件的背景,请将其嵌套在 Container 小部件中。然后使用 Container 小部件的 decoration 属性创建 BoxDecoration 对象,并使用 BoxDecorationgradient 属性转换背景填充。

渐变“角度”基于对齐 (x, y) 值

  • 如果起始和结束 x 值相等,则渐变为垂直 (0° | 180°)。
  • 如果起始和结束 y 值相等,则渐变为水平 (90° | 270°)。

垂直渐变

#
css
<div class="grey-box">
  <div class="red-box">
    Lorem ipsum
  </div>
</div>

.grey-box {
    background-color: #e0e0e0; /* grey 300 */
    width: 320px;
    height: 240px;
    font: 900 24px Roboto;
    display: flex;
    align-items: center;
    justify-content: center;
}
.red-box {
    padding: 16px;
    color: #ffffff;
    background: linear-gradient(180deg, #ef5350, rgba(0, 0, 0, 0) 80%);
}
dart
final container = Container(
  // grey box
  width: 320,
  height: 240,
  color: Colors.grey[300],
  child: Center(
    child: Container(
      // red box
      decoration: const BoxDecoration(
        gradient: LinearGradient(
          begin: Alignment.topCenter,
          end: Alignment(0.0, 0.6),
          colors: <Color>[
            Color(0xffef5350),
            Color(0x00ef5350),
          ],
        ),
      ),
      padding: const EdgeInsets.all(16),
      child: Text(
        'Lorem ipsum',
        style: bold24Roboto,
      ),
    ),
  ),
);

水平渐变

#
css
<div class="grey-box">
  <div class="red-box">
    Lorem ipsum
  </div>
</div>

.grey-box {
    background-color: #e0e0e0; /* grey 300 */
    width: 320px;
    height: 240px;
    font: 900 24px Roboto;
    display: flex;
    align-items: center;
    justify-content: center;
}
.red-box {
    padding: 16px;
    color: #ffffff;
    background: linear-gradient(90deg, #ef5350, rgba(0, 0, 0, 0) 80%);
}
dart
final container = Container(
  // grey box
  width: 320,
  height: 240,
  color: Colors.grey[300],
  child: Center(
    child: Container(
      // red box
      padding: const EdgeInsets.all(16),
      decoration: const BoxDecoration(
        gradient: LinearGradient(
          begin: Alignment(-1.0, 0.0),
          end: Alignment(0.6, 0.0),
          colors: <Color>[
            Color(0xffef5350),
            Color(0x00ef5350),
          ],
        ),
      ),
      child: Text(
        'Lorem ipsum',
        style: bold24Roboto,
      ),
    ),
  ),
);

操作形状

#

以下示例展示了如何制作和自定义形状。

圆角

#

要使矩形形状的角变圆,请使用 BoxDecoration 对象的 borderRadius 属性。创建一个新的 BorderRadius 对象,指定每个角的圆角半径。

css
<div class="grey-box">
  <div class="red-box">
    Lorem ipsum
  </div>
</div>

.grey-box {
    background-color: #e0e0e0; /* grey 300 */
    width: 320px;
    height: 240px;
    font: 900 24px Roboto;
    display: flex;
    align-items: center;
    justify-content: center;
}
.red-box {
    background-color: #ef5350; /* red 400 */
    padding: 16px;
    color: #ffffff;
    border-radius: 8px;
}
dart
final container = Container(
  // grey box
  width: 320,
  height: 240,
  color: Colors.grey[300],
  child: Center(
    child: Container(
      // red circle
      padding: const EdgeInsets.all(16),
      decoration: BoxDecoration(
        color: Colors.red[400],
        borderRadius: const BorderRadius.all(
          Radius.circular(8),
        ),
      ),
      child: Text(
        'Lorem ipsum',
        style: bold24Roboto,
      ),
    ),
  ),
);

添加盒阴影

#

在 CSS 中,您可以使用 box-shadow 属性以简写方式指定阴影偏移和模糊。此示例显示了两个盒阴影,其属性为

  • xOffset: 0px, yOffset: 2px, blur: 4px, color: 黑色 @80% 透明度
  • xOffset: 0px, yOffset: 06px, blur: 20px, color: 黑色 @50% 透明度

在 Flutter 中,每个属性和值都是单独指定的。使用 BoxDecorationboxShadow 属性创建 BoxShadow 小部件列表。您可以定义一个或多个 BoxShadow 小部件,它们可以堆叠以自定义阴影深度、颜色等。

css
<div class="grey-box">
  <div class="red-box">
    Lorem ipsum
  </div>
</div>

.grey-box {
    background-color: #e0e0e0; /* grey 300 */
    width: 320px;
    height: 240px;
    font: 900 24px Roboto;
    display: flex;
    align-items: center;
    justify-content: center;
}
.red-box {
    background-color: #ef5350; /* red 400 */
    padding: 16px;
    color: #ffffff;
    box-shadow: 0 2px 4px rgba(0, 0, 0, 0.8),
              0 6px 20px rgba(0, 0, 0, 0.5);
}
dart
final container = Container(
  // grey box
  width: 320,
  height: 240,
  margin: const EdgeInsets.only(bottom: 16),
  decoration: BoxDecoration(
    color: Colors.grey[300],
  ),
  child: Center(
    child: Container(
      // red box
      padding: const EdgeInsets.all(16),
      decoration: BoxDecoration(
        color: Colors.red[400],
        boxShadow: const <BoxShadow>[
          BoxShadow(
            color: Color(0xcc000000),
            offset: Offset(0, 2),
            blurRadius: 4,
          ),
          BoxShadow(
            color: Color(0x80000000),
            offset: Offset(0, 6),
            blurRadius: 20,
          ),
        ],
      ),
      child: Text(
        'Lorem ipsum',
        style: bold24Roboto,
      ),
    ),
  ),
);

制作圆形和椭圆形

#

在 CSS 中制作圆形需要一种变通方法,即对矩形的所有四个边应用 50% 的 border-radius,尽管也有基本形状

虽然这种方法在 BoxDecorationborderRadius 属性中受支持,但 Flutter 为此目的提供了带有 BoxShape 枚举shape 属性。

css
<div class="grey-box">
  <div class="red-circle">
    Lorem ipsum
  </div>
</div>

.grey-box {
    background-color: #e0e0e0; /* grey 300 */
    width: 320px;
    height: 240px;
    font: 900 24px Roboto;
    display: flex;
    align-items: center;
    justify-content: center;
}
.red-circle {
    background-color: #ef5350; /* red 400 */
    padding: 16px;
    color: #ffffff;
    text-align: center;
    width: 160px;
    height: 160px;
    border-radius: 50%;
}
dart
final container = Container(
  // grey box
  width: 320,
  height: 240,
  color: Colors.grey[300],
  child: Center(
    child: Container(
      // red circle
      decoration: BoxDecoration(
        color: Colors.red[400],
        shape: BoxShape.circle,
      ),
      padding: const EdgeInsets.all(16),
      width: 160,
      height: 160,
      child: Text(
        'Lorem ipsum',
        style: bold24Roboto,
        textAlign: TextAlign.center,
      ),
    ),
  ),
);

操作文本

#

以下示例展示了如何指定字体和其他文本属性。它们还展示了如何转换文本字符串、自定义间距和创建摘录。

调整文本间距

#

在 CSS 中,您可以通过为 letter-spacingword-spacing 属性分别给定长度值来指定每个字母或单词之间的空白量。空白量可以是 px、pt、cm、em 等。

在 Flutter 中,您将空白指定为逻辑像素(允许负值),用于 Text 组件的 TextStyle 子项的 letterSpacingwordSpacing 属性。

css
<div class="grey-box">
  <div class="red-box">
    Lorem ipsum
  </div>
</div>

.grey-box {
    background-color: #e0e0e0; /* grey 300 */
    width: 320px;
    height: 240px;
    font: 900 24px Roboto;
    display: flex;
    align-items: center;
    justify-content: center;
}
.red-box {
    background-color: #ef5350; /* red 400 */
    padding: 16px;
    color: #ffffff;
    letter-spacing: 4px;
}
dart
final container = Container(
  // grey box
  width: 320,
  height: 240,
  color: Colors.grey[300],
  child: Center(
    child: Container(
      // red box
      padding: const EdgeInsets.all(16),
      decoration: BoxDecoration(
        color: Colors.red[400],
      ),
      child: const Text(
        'Lorem ipsum',
        style: TextStyle(
          color: Colors.white,
          fontSize: 24,
          fontWeight: FontWeight.w900,
          letterSpacing: 4,
        ),
      ),
    ),
  ),
);

进行内联格式化更改

#

一个 Text 小部件允许您显示具有某些格式特征的文本。要显示使用多种样式(在此示例中,一个带有强调的单词)的文本,请改用 RichText 小部件。其 text 属性可以指定一个或多个可以单独设置样式的 TextSpan 对象。

在以下示例中,“Lorem”位于具有默认(继承)文本样式的 TextSpan 中,“ipsum”位于具有自定义样式的单独 TextSpan 中。

css
<div class="grey-box">
  <div class="red-box">
    Lorem <em>ipsum</em>
  </div>
</div>

.grey-box {
    background-color: #e0e0e0; /* grey 300 */
    width: 320px;
    height: 240px;
    font: 900 24px Roboto;
    display: flex;
    align-items: center;
    justify-content: center;
}
.red-box {
    background-color: #ef5350; /* red 400 */
    padding: 16px;
    color: #ffffff;
}
.red-box em {
    font: 300 48px Roboto;
    font-style: italic;
}
dart
final container = Container(
  // grey box
  width: 320,
  height: 240,
  color: Colors.grey[300],
  child: Center(
    child: Container(
      // red box
      decoration: BoxDecoration(
        color: Colors.red[400],
      ),
      padding: const EdgeInsets.all(16),
      child: RichText(
        text: TextSpan(
          style: bold24Roboto,
          children: const <TextSpan>[
            TextSpan(text: 'Lorem '),
            TextSpan(
              text: 'ipsum',
              style: TextStyle(
                fontWeight: FontWeight.w300,
                fontStyle: FontStyle.italic,
                fontSize: 48,
              ),
            ),
          ],
        ),
      ),
    ),
  ),
);

创建文本摘录

#

摘录显示段落的初始行,并处理溢出文本,通常使用省略号。

在 Flutter 中,使用 Text 小部件的 maxLines 属性指定摘录中包含的行数,并使用 overflow 属性处理溢出文本。

css
<div class="grey-box">
  <div class="red-box">
    Lorem ipsum dolor sit amet, consec etur
  </div>
</div>

.grey-box {
    background-color: #e0e0e0; /* grey 300 */
    width: 320px;
    height: 240px;
    font: 900 24px Roboto;
    display: flex;
    align-items: center;
    justify-content: center;
}
.red-box {
    background-color: #ef5350; /* red 400 */
    padding: 16px;
    color: #ffffff;
    overflow: hidden;
    display: -webkit-box;
    -webkit-box-orient: vertical;
    -webkit-line-clamp: 2;
}
dart
final container = Container(
  // grey box
  width: 320,
  height: 240,
  color: Colors.grey[300],
  child: Center(
    child: Container(
      // red box
      decoration: BoxDecoration(
        color: Colors.red[400],
      ),
      padding: const EdgeInsets.all(16),
      child: Text(
        'Lorem ipsum dolor sit amet, consec etur',
        style: bold24Roboto,
        overflow: TextOverflow.ellipsis,
        maxLines: 1,
      ),
    ),
  ),
);