index_page.dart 3.0 KB

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