123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108 |
- import 'package:flutter/material.dart';
- import 'package:flutter_clock/pages/alarm_page.dart';
- import 'package:flutter_clock/pages/clock_page.dart';
- import 'package:flutter_clock/pages/stopwatch_page.dart';
- import 'package:flutter_clock/pages/timer/timer_page.dart';
- import 'package:flutter_screenutil/flutter_screenutil.dart';
- /// Description: main page
- /// Time : 04/06/2025 Sunday
- /// Author : liuyuqi.gov@msn.cn
- class IndexPage extends StatefulWidget {
- @override
- State<StatefulWidget> createState() {
- return _IndexPageState();
- }
- }
- class _IndexPageState extends State<IndexPage>
- with SingleTickerProviderStateMixin {
- late TabController _tabController;
- late List<Widget> _tabs;
- @override
- void initState() {
- super.initState();
- _tabController = TabController(
- length: 4,
- vsync: this,
- initialIndex: 3); // Timer tab is selected by default
-
- // Initialize tabs with screen adaptation
- _tabs = [
- 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))),
- ];
-
- }
- @override
- void dispose() {
- _tabController.dispose();
- super.dispose();
- }
- @override
- Widget build(BuildContext context) {
- return Scaffold(
- appBar: AppBar(
- backgroundColor: Colors.white,
- elevation: 0,
- toolbarHeight: 50.h,
- title: TabBar(
- controller: _tabController,
- tabs: _tabs,
- labelColor: Colors.blue,
- unselectedLabelColor: Colors.black,
- indicatorColor: Colors.blue,
- indicatorWeight: 3.h,
- labelStyle: TextStyle(fontWeight: FontWeight.bold),
- unselectedLabelStyle: TextStyle(fontWeight: FontWeight.normal),
- ),
- // actions: [
- // IconButton(
- // icon: Icon(Icons.more_vert, color: Colors.black),
- // onPressed: () {
- // // More options menu
- // },
- // ),
- // ],
- ),
- body: TabBarView(
- controller: _tabController,
- children: [
- // 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;
- }
- }
|