要分享整個應用的顏色和字型樣式,請使用主題。

您可以定義應用範圍的主題。您可以擴充套件主題以更改某個元件的主題樣式。每個主題都定義了適用於 Material 元件型別的顏色、字型樣式和其他引數。

Flutter 按以下順序應用樣式:

  1. 應用於特定小部件的樣式。
  2. 覆蓋直接父主題的主題。
  3. 整個應用的主題。

定義 Theme 後,在您自己的小部件中使用它。Flutter 的 Material 小部件使用您的主題來設定應用欄、按鈕、複選框等的背景顏色和字型樣式。

建立應用主題

#

要在整個應用中共享 Theme,請將 theme 屬性設定為您的 MaterialApp 建構函式。此屬性接受一個 ThemeData 例項。

從 Flutter 3.16 版本開始,Material 3 是 Flutter 的預設主題。

如果您未在建構函式中指定主題,Flutter 會為您建立一個預設主題。

dart
MaterialApp(
  title: appName,
  theme: ThemeData(
    // Define the default brightness and colors.
    colorScheme: ColorScheme.fromSeed(
      seedColor: Colors.purple,
      // ···
      brightness: Brightness.dark,
    ),

    // Define the default `TextTheme`. Use this to specify the default
    // text styling for headlines, titles, bodies of text, and more.
    textTheme: TextTheme(
      displayLarge: const TextStyle(
        fontSize: 72,
        fontWeight: FontWeight.bold,
      ),
      // ···
      titleLarge: GoogleFonts.oswald(
        fontSize: 30,
        fontStyle: FontStyle.italic,
      ),
      bodyMedium: GoogleFonts.merriweather(),
      displaySmall: GoogleFonts.pacifico(),
    ),
  ),
  home: const MyHomePage(title: appName),
);

大多數 ThemeData 例項會設定以下兩個屬性的值。這些屬性會影響整個應用。

  1. colorScheme 定義顏色。
  2. textTheme 定義文字樣式。

要了解您可以定義哪些顏色、字型和其他屬性,請檢視 ThemeData 文件。

應用主題

#

要應用您的新主題,請在指定小部件的樣式屬性時使用 Theme.of(context) 方法。這些屬性可以包括但不限於 stylecolor

Theme.of(context) 方法會查詢小部件樹並檢索樹中最接近的 Theme。如果您有一個獨立式 Theme,則會應用該主題。如果沒有,Flutter 會應用應用的主題。

在以下示例中,Container 建構函式使用此技術來設定其 color

dart
Container(
  padding: const EdgeInsets.symmetric(horizontal: 12, vertical: 12),
  color: Theme.of(context).colorScheme.primary,
  child: Text(
    'Text with a background color',
    // ···
    style: Theme.of(context).textTheme.bodyMedium!.copyWith(
      color: Theme.of(context).colorScheme.onPrimary,
    ),
  ),
),

覆蓋主題

#

要覆蓋應用部分中的整體主題,請將該部分應用包裝在 Theme 小部件中。

您可以透過兩種方式覆蓋主題:

  1. 建立唯一的 ThemeData 例項。
  2. 擴充套件父主題。

設定唯一的 ThemeData 例項

#

如果您希望應用的某個元件忽略整體主題,請建立一個 ThemeData 例項。將該例項傳遞給 Theme 小部件。

dart
Theme(
  // Create a unique theme with `ThemeData`.
  data: ThemeData(colorScheme: ColorScheme.fromSeed(seedColor: Colors.pink)),
  child: FloatingActionButton(onPressed: () {}, child: const Icon(Icons.add)),
);

擴充套件父主題

#

與其覆蓋所有內容,不如考慮擴充套件父主題。要擴充套件主題,請使用 copyWith() 方法。

dart
Theme(
  // Find and extend the parent theme using `copyWith`.
  // To learn more, check out the section on `Theme.of`.
  data: Theme.of(
    context,
  ).copyWith(colorScheme: ColorScheme.fromSeed(seedColor: Colors.pink)),
  child: const FloatingActionButton(onPressed: null, child: Icon(Icons.add)),
);

觀看有關 Theme 的影片

#

要了解更多資訊,請觀看這個關於 Theme 小部件的“本週小部件”短影片:

在新標籤頁中在 YouTube 上觀看:“Theme | Flutter widget of the week”

嘗試互動式示例

#
import 'package:flutter/material.dart';
// Include the Google Fonts package to provide more text format options
// https://pub.dev/packages/google_fonts
import 'package:google_fonts/google_fonts.dart';

void main() {
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  @override
  Widget build(BuildContext context) {
    const appName = 'Custom Themes';

    return MaterialApp(
      title: appName,
      theme: ThemeData(
        // Define the default brightness and colors.
        colorScheme: ColorScheme.fromSeed(
          seedColor: Colors.purple,
          // TRY THIS: Change to "Brightness.light"
          //           and see that all colors change
          //           to better contrast a light background.
          brightness: Brightness.dark,
        ),

        // Define the default `TextTheme`. Use this to specify the default
        // text styling for headlines, titles, bodies of text, and more.
        textTheme: TextTheme(
          displayLarge: const TextStyle(
            fontSize: 72,
            fontWeight: FontWeight.bold,
          ),
          // TRY THIS: Change one of the GoogleFonts
          //           to "lato", "poppins", or "lora".
          //           The title uses "titleLarge"
          //           and the middle text uses "bodyMedium".
          titleLarge: GoogleFonts.oswald(
            fontSize: 30,
            fontStyle: FontStyle.italic,
          ),
          bodyMedium: GoogleFonts.merriweather(),
          displaySmall: GoogleFonts.pacifico(),
        ),
      ),
      home: const MyHomePage(title: appName),
    );
  }
}

class MyHomePage extends StatelessWidget {
  final String title;

  const MyHomePage({super.key, required this.title});

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(
          title,
          style: Theme.of(context).textTheme.titleLarge!.copyWith(
            color: Theme.of(context).colorScheme.onSecondary,
          ),
        ),
        backgroundColor: Theme.of(context).colorScheme.secondary,
      ),
      body: Center(
        child: Container(
          padding: const EdgeInsets.symmetric(horizontal: 12, vertical: 12),
          color: Theme.of(context).colorScheme.primary,
          child: Text(
            'Text with a background color',
            // TRY THIS: Change the Text value
            //           or change the Theme.of(context).textTheme
            //           to "displayLarge" or "displaySmall".
            style: Theme.of(context).textTheme.bodyMedium!.copyWith(
              color: Theme.of(context).colorScheme.onPrimary,
            ),
          ),
        ),
      ),
      floatingActionButton: Theme(
        data: Theme.of(context).copyWith(
          // TRY THIS: Change the seedColor to "Colors.red" or
          //           "Colors.blue".
          colorScheme: ColorScheme.fromSeed(
            seedColor: Colors.pink,
            brightness: Brightness.dark,
          ),
        ),
        child: FloatingActionButton(
          onPressed: () {},
          child: const Icon(Icons.add),
        ),
      ),
    );
  }
}