1、新建资源文件color.dart
import 'package:flutter/material.dart'; const kShrinePink50 = const Color(0xFFFEEAE6); const kShrinePink100 = const Color(0xFFFEDBD0); const kShrinePink300 = const Color(0xFFFBB8AC); const kShrinePink400 = const Color(0xFFEAA4A4); const kShrineBrown900 = const Color(0xFF442B2D); const kShrineErrorRed = const Color(0xFFC5032B); const kShrineSurfaceWhite = const Color(0xFFFFFBFA); const kShrineBackgroundWhite = Colors.white;
2、导入color.dart
import 'colors.dart';
3、在MaterialApp作用域之外新建Theme
ThemeData _buildShrineTheme() { final ThemeData base = ThemeData.light(); return base.copyWith( accentColor: kShrineBrown900, primaryColor: kShrinePink100, buttonColor: kShrinePink100, scaffoldBackgroundColor: kShrineBackgroundWhite, textSelectionColor: kShrinePink100, errorColor: kShrineErrorRed, ); }
4、替换ThemeData
替换前
title: 'Flutter Demo', theme: ThemeData( primarySwatch: Colors.blue, ), home: _HomeWidget(),
替换后
theme: _buildShrineTheme(),