-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.dart
90 lines (82 loc) · 2.47 KB
/
main.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
import 'package:dynamic_theme_mode/dynamic_theme_mode.dart';
import 'package:flutter/material.dart';
void main() {
runApp(DynamicThemeMode(
initialThemeMode: ThemeMode.system,
builder: (context, themeMode) => ExampleApp(themeMode: themeMode),
));
}
class ExampleApp extends StatelessWidget {
const ExampleApp({
Key? key,
required this.themeMode,
}) : super(key: key);
final ThemeMode themeMode;
@override
Widget build(BuildContext context) {
return MaterialApp(
theme: ThemeData(
brightness: Brightness.light,
primarySwatch: Colors.orange,
textTheme: ThemeData().textTheme.apply(
displayColor: Colors.indigo,
),
),
darkTheme: ThemeData(
brightness: Brightness.dark,
primarySwatch: Colors.yellow,
textTheme: ThemeData().textTheme.apply(
displayColor: Colors.red,
),
),
themeMode: this.themeMode,
home: ExampleHome(),
);
}
}
class ExampleHome extends StatelessWidget {
const ExampleHome({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
final ThemeModeManager manager = DynamicThemeMode.managerOf(context);
final buttonStyle = ElevatedButton.styleFrom(
padding: const EdgeInsets.symmetric(horizontal: 30.0),
fixedSize: Size.fromWidth(MediaQuery.of(context).size.width / 2.5),
);
return Scaffold(
appBar: AppBar(
centerTitle: true,
title: const Text('DynamicThemeMode Example'),
),
body: Center(
child: SizedBox(
height: MediaQuery.of(context).size.height / 2,
child: Column(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: [
Text(
'${manager.themeMode}',
style: Theme.of(context).textTheme.headline4,
),
ElevatedButton(
child: const Text('System mode'),
style: buttonStyle,
onPressed: manager.useSystemMode,
),
ElevatedButton(
child: const Text('Light mode'),
style: buttonStyle,
onPressed: () => manager.setThemeMode(ThemeMode.light),
),
ElevatedButton(
child: const Text('Dark mode'),
style: buttonStyle,
onPressed: manager.useDarkMode,
),
],
),
),
),
);
}
}