index_page.dart 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. import 'package:flutter/material.dart';
  2. import 'package:flutter_clock/model/constants.dart';
  3. import 'package:flutter_clock/pages/alarm_page.dart';
  4. import 'package:flutter_clock/pages/clock_page.dart';
  5. import 'package:flutter_clock/pages/stopwatch_page.dart';
  6. import 'package:flutter_clock/pages/timer/timer_page.dart';
  7. import 'package:flutter_screenutil/flutter_screenutil.dart';
  8. /// Description: main page
  9. /// Time : 04/06/2025 Sunday
  10. /// Author : liuyuqi.gov@msn.cn
  11. class IndexPage extends StatefulWidget {
  12. @override
  13. State<StatefulWidget> createState() {
  14. return _IndexPageState();
  15. }
  16. }
  17. class _IndexPageState extends State<IndexPage>
  18. with SingleTickerProviderStateMixin {
  19. late TabController _tabController;
  20. late List<Widget> _tabs;
  21. @override
  22. void initState() {
  23. super.initState();
  24. _tabController = TabController(
  25. length: 4,
  26. vsync: this,
  27. initialIndex: 3); // Timer tab is selected by default
  28. // Initialize tabs with screen adaptation
  29. _tabs = [
  30. Tab(text: 'Alarm'),
  31. Tab(text: 'Clock'),
  32. Tab(text: 'Stopwatch'),
  33. Tab(text: 'Timer'),
  34. ];
  35. }
  36. @override
  37. void dispose() {
  38. _tabController.dispose();
  39. super.dispose();
  40. }
  41. @override
  42. Widget build(BuildContext context) {
  43. return Scaffold(
  44. appBar: AppBar(
  45. backgroundColor: Colors.white,
  46. elevation: 0,
  47. toolbarHeight: 60.h,
  48. title: TabBar(
  49. controller: _tabController,
  50. tabs: _tabs,
  51. labelColor: kPrimaryColor,
  52. unselectedLabelColor: kGrayColor,
  53. indicatorColor: kPrimaryColor,
  54. indicatorWeight: 3.h,
  55. labelPadding: EdgeInsets.symmetric(horizontal: 4.w),
  56. labelStyle: TextStyle(fontWeight: FontWeight.bold, fontSize: 15.sp),
  57. unselectedLabelStyle: TextStyle(fontWeight: FontWeight.normal, fontSize: 15.sp),
  58. ),
  59. ),
  60. body: TabBarView(
  61. controller: _tabController,
  62. children: [
  63. // Wrap each page with AutomaticKeepAlive widget to preserve state
  64. KeepAlivePage(child: AlarmPage()),
  65. KeepAlivePage(child: ClockPage()),
  66. KeepAlivePage(child: StopwatchPage()),
  67. KeepAlivePage(child: TimerPage()),
  68. ],
  69. ),
  70. );
  71. }
  72. }
  73. /// A wrapper widget that keeps its child alive when switching between tabs
  74. class KeepAlivePage extends StatefulWidget {
  75. final Widget child;
  76. const KeepAlivePage({Key? key, required this.child}) : super(key: key);
  77. @override
  78. _KeepAlivePageState createState() => _KeepAlivePageState();
  79. }
  80. class _KeepAlivePageState extends State<KeepAlivePage> with AutomaticKeepAliveClientMixin {
  81. @override
  82. bool get wantKeepAlive => true;
  83. @override
  84. Widget build(BuildContext context) {
  85. super.build(context); // Important: This must be called
  86. return widget.child;
  87. }
  88. }