-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp_theme.dart
115 lines (99 loc) · 3.41 KB
/
app_theme.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
import 'package:flutter/material.dart';
import 'package:theme_extensions/app_colors_extension.dart';
import 'package:theme_extensions/app_text_theme_extension.dart';
import 'package:theme_extensions/app_typography.dart';
/// Simple Flutter app theme with `ChangeNotifier` and `ThemeExtension`.
/// With support for changing between light/dark mode.
///
/// You can also register it in `get_it` or any other package you use.
class AppTheme with ChangeNotifier {
ThemeMode _themeMode = ThemeMode.system;
ThemeMode get themeMode => _themeMode;
set themeMode(ThemeMode themeMode) {
_themeMode = themeMode;
notifyListeners();
}
//
// Light theme
//
static final light = () {
final defaultTheme = ThemeData.light();
return defaultTheme.copyWith(
textTheme: defaultTheme.textTheme.copyWith(
// Default text style for Text widget.
bodyMedium: AppTypography.body1.copyWith(color: Colors.black),
),
extensions: [
_lightAppColors,
_lightTextTheme,
],
);
}();
static final _lightAppColors = AppColorsExtension(
primary: const Color(0xff6200ee),
onPrimary: Colors.white,
secondary: const Color(0xff03dac6),
onSecondary: Colors.black,
error: const Color(0xffb00020),
onError: Colors.white,
background: Colors.white,
onBackground: Colors.black,
surface: Colors.white,
onSurface: Colors.black,
);
static final _lightTextTheme = SimpleAppTextThemeExtension(
body1: AppTypography.body1.copyWith(color: _lightAppColors.onBackground),
h1: AppTypography.h1.copyWith(color: Colors.black),
);
//
// Dark theme
//
static final dark = () {
final defaultTheme = ThemeData.dark();
return defaultTheme.copyWith(
textTheme: defaultTheme.textTheme.copyWith(
// Default text style for Text widget.
bodyMedium: AppTypography.body1.copyWith(color: Colors.white),
),
extensions: [
_darkAppColors,
_darkTextTheme,
],
);
}();
static final _darkAppColors = AppColorsExtension(
primary: const Color(0xffbb86fc),
onPrimary: Colors.black,
secondary: const Color(0xff03dac6),
onSecondary: Colors.black,
error: const Color(0xffcf6679),
onError: Colors.black,
background: const Color(0xff121212),
onBackground: Colors.white,
surface: const Color(0xff121212),
onSurface: Colors.white,
);
static final _darkTextTheme = SimpleAppTextThemeExtension(
body1: AppTypography.body1.copyWith(color: _darkAppColors.onBackground),
h1: AppTypography.h1.copyWith(color: Colors.white),
);
}
/// Here you should define getters for your `ThemeExtension`s.
///
/// Never use `Theme.of(context).extension<MyColors>()!`
/// how they do it in the [official documentation](https://api.flutter.dev/flutter/material/ThemeExtension-class.html),
/// because it's not safe. Always create a getter for your `ThemeExtension` and use it instead.
///
/// Usage example: `Theme.of(context).appColors`.
extension AppThemeExtension on ThemeData {
AppColorsExtension get appColors =>
extension<AppColorsExtension>() ?? AppTheme._lightAppColors;
SimpleAppTextThemeExtension get appTextTheme =>
extension<SimpleAppTextThemeExtension>() ?? AppTheme._lightTextTheme;
}
/// A more convenient way to get `ThemeData` from the `BuildContext`.
///
/// Usage example: `context.theme`.
extension ThemeGetter on BuildContext {
ThemeData get theme => Theme.of(this);
}