@@ -84,27 +84,86 @@ void stopTestBackend() {
8484 }
8585}
8686
87- class MyApp extends StatelessWidget {
87+ class MyApp extends StatefulWidget {
8888 const MyApp ({Key ? key}) : super (key: key);
8989
90- // 简单主题与入口
90+ @override
91+ State <MyApp > createState () => _MyAppState ();
92+ }
93+
94+ class _MyAppState extends State <MyApp > {
95+ ThemeMode _themeMode = ThemeMode .light;
96+
97+ void _toggleTheme () {
98+ setState (() {
99+ _themeMode = _themeMode == ThemeMode .light ? ThemeMode .dark : ThemeMode .light;
100+ });
101+ }
102+
103+ ThemeData _buildLightTheme () {
104+ return ThemeData (
105+ colorScheme: ColorScheme .fromSeed (seedColor: Colors .blueGrey),
106+ useMaterial3: true ,
107+ scaffoldBackgroundColor: Colors .grey.shade100,
108+ cardTheme: CardThemeData (
109+ color: Colors .white,
110+ elevation: 0 ,
111+ shape: RoundedRectangleBorder (
112+ borderRadius: BorderRadius .circular (12 ),
113+ side: BorderSide (color: Colors .grey.shade200, width: 1 ),
114+ ),
115+ ),
116+ appBarTheme: const AppBarTheme (
117+ backgroundColor: Colors .white,
118+ foregroundColor: Colors .black,
119+ elevation: 0 ,
120+ ),
121+ bottomNavigationBarTheme: const BottomNavigationBarThemeData (
122+ backgroundColor: Colors .white,
123+ ),
124+ );
125+ }
126+
127+ ThemeData _buildDarkTheme () {
128+ return ThemeData (
129+ colorScheme: ColorScheme .fromSeed (
130+ seedColor: Colors .blueGrey,
131+ brightness: Brightness .dark,
132+ ),
133+ useMaterial3: true ,
134+ scaffoldBackgroundColor: const Color (0xFF121212 ),
135+ cardTheme: CardThemeData (
136+ color: const Color (0xFF1E1E1E ),
137+ elevation: 0 ,
138+ shape: RoundedRectangleBorder (
139+ borderRadius: BorderRadius .circular (12 ),
140+ side: BorderSide (color: Colors .grey.shade800, width: 1 ),
141+ ),
142+ ),
143+ appBarTheme: const AppBarTheme (
144+ backgroundColor: Color (0xFF1E1E1E ),
145+ foregroundColor: Colors .white,
146+ elevation: 0 ,
147+ ),
148+ bottomNavigationBarTheme: const BottomNavigationBarThemeData (
149+ backgroundColor: Color (0xFF1E1E1E ),
150+ unselectedItemColor: Colors .white70,
151+ selectedItemColor: Colors .white,
152+ ),
153+ );
154+ }
155+
91156 @override
92157 Widget build (BuildContext context) {
93158 return MaterialApp (
94159 title: '机器狗 Flutter 迁移示例' ,
95- theme: ThemeData (
96- colorScheme: ColorScheme .fromSeed (seedColor: Colors .deepPurple),
97- useMaterial3: true ,
98- scaffoldBackgroundColor: Colors .white,
99- cardTheme: CardThemeData (
100- elevation: 0 ,
101- shape: RoundedRectangleBorder (
102- borderRadius: BorderRadius .circular (12 ),
103- side: BorderSide (color: Colors .grey.shade200, width: 1 ),
104- ),
105- ),
160+ theme: _buildLightTheme (),
161+ darkTheme: _buildDarkTheme (),
162+ themeMode: _themeMode,
163+ home: RootPage (
164+ onToggleTheme: _toggleTheme,
165+ themeMode: _themeMode,
106166 ),
107- home: const RootPage (),
108167 builder: (context, child) {
109168 return InAppNotificationOverlay (child: child ?? const SizedBox .shrink ());
110169 },
@@ -114,7 +173,10 @@ class MyApp extends StatelessWidget {
114173}
115174
116175class RootPage extends StatefulWidget {
117- const RootPage ({Key ? key}) : super (key: key);
176+ const RootPage ({Key ? key, required this .onToggleTheme, required this .themeMode}) : super (key: key);
177+
178+ final VoidCallback onToggleTheme;
179+ final ThemeMode themeMode;
118180
119181 @override
120182 State <RootPage > createState () => _RootPageState ();
@@ -424,6 +486,11 @@ class _RootPageState extends State<RootPage> {
424486 appBar: AppBar (
425487 title: Text (userType == 'patient' ? '患者端' : '家属端' ),
426488 actions: [
489+ IconButton (
490+ icon: Icon (widget.themeMode == ThemeMode .dark ? Icons .dark_mode : Icons .light_mode),
491+ onPressed: widget.onToggleTheme,
492+ tooltip: widget.themeMode == ThemeMode .dark ? '切换到亮色' : '切换到暗色' ,
493+ ),
427494 IconButton (
428495 icon: const Icon (Icons .notifications),
429496 onPressed: () {
0 commit comments