Browse Source

设置页面状态

heavyrain 3 months ago
parent
commit
4707aaf9c7
2 changed files with 43 additions and 9 deletions
  1. 30 9
      lib/index_page.dart
  2. 13 0
      lib/main.dart

+ 30 - 9
lib/index_page.dart

@@ -1,4 +1,3 @@
-
 import 'package:flutter/material.dart';
 import 'package:flutter/material.dart';
 import 'package:flutter_clock/pages/alarm_page.dart';
 import 'package:flutter_clock/pages/alarm_page.dart';
 import 'package:flutter_clock/pages/clock_page.dart';
 import 'package:flutter_clock/pages/clock_page.dart';
@@ -32,10 +31,10 @@ class _IndexPageState extends State<IndexPage>
     
     
     // Initialize tabs with screen adaptation
     // Initialize tabs with screen adaptation
     _tabs = [
     _tabs = [
-      Tab(child: Text('Alarm', style: TextStyle(fontSize: 16.sp, color: Colors.black))),
-      Tab(child: Text('Clock', style: TextStyle(fontSize: 16.sp, color: Colors.black))),
-      Tab(child: Text('Stopwatch', style: TextStyle(fontSize: 16.sp, color: Colors.black))),
-      Tab(child: Text('Timer', style: TextStyle(fontSize: 16.sp, color: Colors.black))),  
+      Tab(child: Text('Alarm', style: TextStyle(fontSize: 16.sp))),
+      Tab(child: Text('Clock', style: TextStyle(fontSize: 16.sp))),
+      Tab(child: Text('Stopwatch', style: TextStyle(fontSize: 16.sp))),
+      Tab(child: Text('Timer', style: TextStyle(fontSize: 16.sp))),  
     ];
     ];
     
     
   }
   }
@@ -60,6 +59,8 @@ class _IndexPageState extends State<IndexPage>
           unselectedLabelColor: Colors.black,
           unselectedLabelColor: Colors.black,
           indicatorColor: Colors.blue,
           indicatorColor: Colors.blue,
           indicatorWeight: 3.h,
           indicatorWeight: 3.h,
+          labelStyle: TextStyle(fontWeight: FontWeight.bold),
+          unselectedLabelStyle: TextStyle(fontWeight: FontWeight.normal),
         ),
         ),
         // actions: [
         // actions: [
         //   IconButton(
         //   IconButton(
@@ -74,14 +75,34 @@ class _IndexPageState extends State<IndexPage>
       body: TabBarView(
       body: TabBarView(
         controller: _tabController,
         controller: _tabController,
         children: [
         children: [
-          AlarmPage(),
-          ClockPage(),
-          StopwatchPage(),
-          TimerPage(),
+          // Wrap each page with AutomaticKeepAlive widget to preserve state
+          KeepAlivePage(child: AlarmPage()),
+          KeepAlivePage(child: ClockPage()),
+          KeepAlivePage(child: StopwatchPage()),
+          KeepAlivePage(child: TimerPage()),
         ],
         ],
       ),
       ),
     );
     );
   }
   }
+}
+
+/// A wrapper widget that keeps its child alive when switching between tabs
+class KeepAlivePage extends StatefulWidget {
+  final Widget child;
 
 
+  const KeepAlivePage({Key? key, required this.child}) : super(key: key);
+
+  @override
+  _KeepAlivePageState createState() => _KeepAlivePageState();
+}
 
 
+class _KeepAlivePageState extends State<KeepAlivePage> with AutomaticKeepAliveClientMixin {
+  @override
+  bool get wantKeepAlive => true;
+
+  @override
+  Widget build(BuildContext context) {
+    super.build(context); // Important: This must be called
+    return widget.child;
+  }
 }
 }

+ 13 - 0
lib/main.dart

@@ -69,6 +69,19 @@ class MyApp extends StatelessWidget {
             primarySwatch: Colors.blue,
             primarySwatch: Colors.blue,
             visualDensity: VisualDensity.adaptivePlatformDensity,
             visualDensity: VisualDensity.adaptivePlatformDensity,
             fontFamily: 'Roboto',
             fontFamily: 'Roboto',
+            brightness: Brightness.light,
+            tabBarTheme: TabBarTheme(
+              labelColor: Colors.blue,
+              unselectedLabelColor: Colors.black87,
+              indicatorSize: TabBarIndicatorSize.tab,
+              labelStyle: TextStyle(fontWeight: FontWeight.bold),
+              unselectedLabelStyle: TextStyle(fontWeight: FontWeight.normal),
+            ),
+            appBarTheme: AppBarTheme(
+              color: Colors.white,
+              foregroundColor: Colors.black,
+              elevation: 1.0,
+            ),
           ),
           ),
           home: IndexPage(),
           home: IndexPage(),
         );
         );