概述

#

Material 庫已更新以符合 Material 3 設計規範。更改包括新的元件和元件主題、更新的元件視覺效果等等。其中許多更新是無縫的。當您針對 3.16(或更高版本)釋出重新編譯您的應用程式時,您將看到受影響的部件的新版本。但要完成遷移,還需要一些手動工作。

遷移指南

#

在 3.16 版本之前,您可以透過將 useMaterial3 標誌設定為 true 來選擇啟用 Material 3 更改。自 Flutter 3.16 版本(2023 年 11 月)釋出以來,useMaterial3 預設為 true。

順便說一句,您可以透過將 useMaterial3 設定為 false 來在您的應用程式中恢復到 Material 2 行為。但是,這只是一個臨時解決方案。useMaterial3 標誌和 Material 2 實現最終將作為 Flutter 棄用策略的一部分被刪除。

顏色

#

ThemeData.colorScheme 的預設值已更新以匹配 Material 3 設計規範。

ColorScheme.fromSeed 建構函式會生成一個派生自給定 seedColorColorScheme。此建構函式生成的顏色旨在協同工作,並滿足 Material 3 設計系統中可訪問性的對比度要求。

當更新到 3.16 版本時,如果沒有正確的 ColorScheme,您的 UI 可能會看起來有些奇怪。要解決此問題,請遷移到從 ColorScheme.fromSeed 建構函式生成的 ColorScheme

遷移前的程式碼

dart
theme: ThemeData(
  colorScheme: ColorScheme.light(primary: Colors.blue),
),

遷移後的程式碼

dart
theme: ThemeData(
  colorScheme: ColorScheme.fromSeed(seedColor: Colors.blue),
),

要生成基於內容的動態顏色方案,請使用 ColorScheme.fromImageProvider 靜態方法。有關生成顏色方案的示例,請檢視來自網路圖片的 ColorScheme 示例。

Flutter Material 3 的變化包括新的背景顏色。ColorScheme.surfaceTint 表示一個高架部件。有些部件使用不同的顏色。

要將您的應用程式的 UI 恢復到之前的行為(我們不建議這樣做)

  • Colors.grey[50]! 設定為 ColorScheme.background(當主題為 Brightness.light 時)。
  • Colors.grey[850]! 設定為 ColorScheme.background(當主題為 Brightness.dark 時)。

遷移前的程式碼

dart
theme: ThemeData(
  colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple),
),

遷移後的程式碼

dart
theme: ThemeData(
  colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple).copyWith(
    background: Colors.grey[50]!,
  ),
),
dart
darkTheme: ThemeData(
  colorScheme: ColorScheme.fromSeed(
    seedColor: Colors.deepPurple,
    brightness: Brightness.dark,
  ).copyWith(background: Colors.grey[850]!),
),

ColorScheme.surfaceTint 值表示 Material 3 中元件的標高。一些部件可能同時使用 surfaceTintshadowColor 來表示標高(例如 CardElevatedButton),而其他部件可能只使用 surfaceTint 來表示標高(例如 AppBar)。

要恢復到部件的先前行為,請在主題中將 Colors.transparent 設定為 ColorScheme.surfaceTint。要在部件的陰影與內容之間進行區分(當它沒有陰影時),請在沒有預設陰影顏色的部件主題中將 ColorScheme.shadow 顏色設定為 shadowColor 屬性。

遷移前的程式碼

dart
theme: ThemeData(
  colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple),
),

遷移後的程式碼

dart
theme: ThemeData(
  colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple).copyWith(
    surfaceTint: Colors.transparent,
  ),
  appBarTheme: AppBarTheme(
   elevation: 4.0,
   shadowColor: Theme.of(context).colorScheme.shadow,
 ),
),

ElevatedButton 現在使用新的顏色組合進行樣式化。以前,當 useMaterial3 標誌設定為 false 時,ElevatedButton 使用 ColorScheme.primary 作為背景,ColorScheme.onPrimary 作為前景進行樣式化。要實現相同的視覺效果,請切換到新的 FilledButton 部件,而無需改變標高或投射陰影。

遷移前的程式碼

dart
ElevatedButton(
  onPressed: () {},
  child: const Text('Button'),
),

遷移後的程式碼

dart
ElevatedButton(
  style: ElevatedButton.styleFrom(
    backgroundColor: Theme.of(context).colorScheme.primary,
    foregroundColor: Theme.of(context).colorScheme.onPrimary,
  ),
  onPressed: () {},
  child: const Text('Button'),
),

排版

#

ThemeData.textTheme 的預設值已更新以匹配 Material 3 的預設值。更改包括更新的字型大小、字型粗細、字間距和行高。有關更多詳細資訊,請檢視 TextTheme 文件。

如以下示例所示,在 3.16 版本之前,使用 TextTheme.bodyLarge 且字串很長的 Text 部件在受約束的佈局中將文字包裝成兩行。然而,3.16 版本將文字包裝成三行。如果必須實現先前的行為,請調整文字樣式,如有必要,調整字間距。

遷移前的程式碼

dart
ConstrainedBox(
  constraints: const BoxConstraints(maxWidth: 200),
    child: Text(
      'This is a very long text that should wrap to multiple lines.',
      style: Theme.of(context).textTheme.bodyLarge,
  ),
),

遷移後的程式碼

dart
ConstrainedBox(
  constraints: const BoxConstraints(maxWidth: 200),
    child: Text(
      'This is a very long text that should wrap to multiple lines.',
      style: Theme.of(context).textTheme.bodyMedium!.copyWith(
        letterSpacing: 0.0,
      ),
  ),
),

元件

#

有些元件無法僅僅透過更新來匹配 Material 3 設計規範,而是需要全新的實現。由於 Flutter SDK 不知道您到底想要什麼,因此此類元件需要手動遷移。

用新的 NavigationBar 部件替換 Material 2 風格的 BottomNavigationBar 部件。它稍微高一些,包含藥丸狀導航指示器,並使用新的顏色對映。

遷移前的程式碼

dart
BottomNavigationBar(
  items: const <BottomNavigationBarItem>[
    BottomNavigationBarItem(
      icon: Icon(Icons.home),
      label: 'Home',
    ),
    BottomNavigationBarItem(
      icon: Icon(Icons.business),
      label: 'Business',
    ),
    BottomNavigationBarItem(
      icon: Icon(Icons.school),
      label: 'School',
    ),
  ],
),

遷移後的程式碼

dart
NavigationBar(
  destinations: const <Widget>[
    NavigationDestination(
      icon: Icon(Icons.home),
      label: 'Home',
    ),
    NavigationDestination(
      icon: Icon(Icons.business),
      label: 'Business',
    ),
    NavigationDestination(
      icon: Icon(Icons.school),
      label: 'School',
    ),
  ],
),

檢視BottomNavigationBar 遷移到 NavigationBar 的完整示例。

NavigationDrawer 替換 Drawer 部件,它提供藥丸狀導航指示器、圓角和新的顏色對映。

遷移前的程式碼

dart
Drawer(
  child: ListView(
    children: <Widget>[
      DrawerHeader(
        child: Text(
          'Drawer Header',
          style: Theme.of(context).textTheme.titleLarge,
        ),
      ),
      ListTile(
        leading: const Icon(Icons.message),
        title: const Text('Messages'),
        onTap: () { },
      ),
      ListTile(
        leading: const Icon(Icons.account_circle),
        title: const Text('Profile'),
        onTap: () {},
      ),
      ListTile(
        leading: const Icon(Icons.settings),
        title: const Text('Settings'),
        onTap: () { },
      ),
    ],
  ),
),

遷移後的程式碼

dart
NavigationDrawer(
  children: <Widget>[
    DrawerHeader(
      child: Text(
        'Drawer Header',
        style: Theme.of(context).textTheme.titleLarge,
      ),
    ),
    const NavigationDrawerDestination(
      icon: Icon(Icons.message),
      label: Text('Messages'),
    ),
    const NavigationDrawerDestination(
      icon: Icon(Icons.account_circle),
      label: Text('Profile'),
    ),
    const NavigationDrawerDestination(
      icon: Icon(Icons.settings),
      label: Text('Settings'),
    ),
  ],
),

檢視Drawer 遷移到 NavigationDrawer 的完整示例。

Material 3 引入了中型和大型應用欄,它們在滾動前顯示更大的標題。滾動時,不使用陰影,而是使用 ColorScheme.surfaceTint 顏色來建立與內容的分離。

以下程式碼演示瞭如何實現中型應用欄

dart
CustomScrollView(
  slivers: <Widget>[
    const SliverAppBar.medium(
      title: Text('Title'),
    ),
    SliverToBoxAdapter(
      child: Card(
        child: SizedBox(
          height: 1200,
          child: Padding(
            padding: const EdgeInsets.fromLTRB(8, 100, 8, 100),
            child: Text(
              'Here be scrolling content...',
              style: Theme.of(context).textTheme.headlineSmall,
            ),
          ),
        ),
      ),
    ),
  ],
),

現在有兩種型別的 TabBar 部件:主要和次要。次要選項卡用於內容區域內,以進一步分隔相關內容並建立層次結構。檢視 TabBar.secondary 示例。

新的 TabBar.tabAlignment 屬性指定了選項卡的水平對齊方式。

以下示例展示瞭如何在可滾動 TabBar 中修改選項卡對齊方式

dart
AppBar(
  title: const Text('Title'),
  bottom: const TabBar(
    tabAlignment: TabAlignment.start,
    isScrollable: true,
    tabs: <Widget>[
      Tab(
        icon: Icon(Icons.cloud_outlined),
      ),
      Tab(
        icon: Icon(Icons.beach_access_sharp),
      ),
      Tab(
        icon: Icon(Icons.brightness_5_sharp),
      ),
    ],
  ),
),

SegmentedButtonToggleButtons 的更新版本,它使用完全圓角,佈局高度和大小不同,並使用 Dart Set 來確定選定的專案。

遷移前的程式碼

dart
enum Weather { cloudy, rainy, sunny }

ToggleButtons(
  isSelected: const [false, true, false],
  onPressed: (int newSelection) { },
  children: const <Widget>[
    Icon(Icons.cloud_outlined),
    Icon(Icons.beach_access_sharp),
    Icon(Icons.brightness_5_sharp),
  ],
),

遷移後的程式碼

dart
enum Weather { cloudy, rainy, sunny }

SegmentedButton<Weather>(
  selected: const <Weather>{Weather.rainy},
  onSelectionChanged: (Set<Weather> newSelection) { },
  segments: const <ButtonSegment<Weather>>[
    ButtonSegment(
      icon: Icon(Icons.cloud_outlined),
      value: Weather.cloudy,
    ),
    ButtonSegment(
      icon: Icon(Icons.beach_access_sharp),
      value: Weather.rainy,
    ),
    ButtonSegment(
      icon: Icon(Icons.brightness_5_sharp),
      value: Weather.sunny,
    ),
  ],
),

檢視ToggleButtons 遷移到 SegmentedButton 的完整示例。

新元件

#
  • “選單欄和級聯選單”提供了一種桌面風格的菜單系統,可透過滑鼠或鍵盤完全遍歷。選單由 MenuBarMenuAnchor 錨定。新的菜單系統不是現有應用程式必須遷移到的,但是部署在 Web 或桌面平臺上的應用程式應考慮使用它,而不是 PopupMenuButton(和相關)類。
  • DropdownMenu 結合了文字欄位和選單,生成了有時被稱為_組合框_的東西。使用者可以透過輸入匹配的字串或透過觸控、滑鼠或鍵盤與選單互動,從可能很大的列表中選擇選單項。這可以很好地替代 DropdownButton 部件,儘管不是必需的。
  • SearchBarSearchAnchor 用於使用者輸入搜尋查詢、應用程式計算匹配響應列表,然後使用者選擇其中一個或調整查詢的互動。
  • Badge 用一個小標籤(只有幾個字元)裝飾其子項。例如“+1”。徽章通常用於裝飾 NavigationDestinationNavigationRailDestinationNavigationDrawerDestination 或按鈕圖示中的圖示,如 TextButton.icon
  • FilledButtonFilledButton.tonal 與沒有標高變化和陰影的 ElevatedButton 非常相似。
  • FilterChip.elevatedChoiceChip.elevatedActionChip.elevated 是相同晶片的升高變體,帶有陰影和填充顏色。
  • Dialog.fullscreen 充滿整個螢幕,通常包含標題、操作按鈕和頂部的關閉按鈕。

時間線

#

穩定版本:3.16

參考資料

#

文件

API 文件

相關問題

相關 PR