跳到主內容

Flutter 面向 Web 開發者

瞭解如何在構建 Flutter 應用時應用 Web 開發者知識。

本頁面向熟悉用於排列應用程式 UI 元件的 HTML 和 CSS 語法的使用者。它將 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 透過字型和顏色屬性處理的字型樣式、大小和其他文字屬性是 TextStyle 子項的單獨屬性,該子項是 Text 元件。

對於 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,如果父容器的寬度小於子容器的寬度,則子 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: black @80% alpha
  • xOffset: 0px, yOffset: 06x, blur: 20px, color: black @50% alpha

在 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% 的邊框半徑應用於矩形的四個側面,儘管存在 基本形狀

雖然可以使用 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-spacing 和 word-spacing 屬性提供長度值來指定每個字母或單詞之間的空格量。空格量可以是 px、pt、cm、em 等。

在 Flutter 中,您將空格指定為邏輯畫素(允許負值),用於 TextStyle 子項的 letterSpacingwordSpacing 屬性,該子項是 Text 元件。

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,
      ),
    ),
  ),
);