快乐学习 一个网站喵查铺子(catpuzi.com)全搞定~

Flutter更换ThemeData 切换,使用

flutter开发 尔雅学习君 2020-01-06 扫描二维码
文章目录[隐藏]

1、新建资源文件color.dart

Flutter更换ThemeData 切换,使用

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

Flutter更换ThemeData 切换,使用

喜欢 (0)
关于作者: