index_page.dart 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. import 'dart:async';
  2. import 'dart:math' as math;
  3. import 'package:flutter/material.dart';
  4. import 'package:flutter_clock/pages/alarm_page.dart';
  5. import 'package:flutter_clock/pages/clock_page.dart';
  6. import 'package:flutter_clock/pages/stopwatch_page.dart';
  7. import 'package:flutter_clock/pages/timer/timer_page.dart';
  8. import 'package:flutter_screenutil/flutter_screenutil.dart';
  9. /// Description: main page
  10. /// Time : 04/06/2025 Sunday
  11. /// Author : liuyuqi.gov@msn.cn
  12. class IndexPage extends StatefulWidget {
  13. @override
  14. State<StatefulWidget> createState() {
  15. return _IndexPageState();
  16. }
  17. }
  18. class _IndexPageState extends State<IndexPage>
  19. with SingleTickerProviderStateMixin {
  20. late TabController _tabController;
  21. late List<Widget> _tabs;
  22. final _utcMidnightRadiansOffset = radiansFromDegrees(-64);
  23. static const _secondsInDay = 86400;
  24. DateTime _localTime = DateTime.now();
  25. @override
  26. void initState() {
  27. super.initState();
  28. _tabController = TabController(
  29. length: 4,
  30. vsync: this,
  31. initialIndex: 3); // Timer tab is selected by default
  32. // Initialize tabs with screen adaptation
  33. _tabs = [
  34. Tab(child: Text('Alarm', style: TextStyle(fontSize: 16.sp))),
  35. Tab(child: Text('Clock', style: TextStyle(fontSize: 16.sp))),
  36. Tab(child: Text('Stopwatch', style: TextStyle(fontSize: 16.sp))),
  37. Tab(child: Text('Timer', style: TextStyle(fontSize: 16.sp))),
  38. ];
  39. Timer.periodic(Duration(seconds: 1),
  40. (_) => setState(() => _localTime = DateTime.now()));
  41. }
  42. @override
  43. void dispose() {
  44. _tabController.dispose();
  45. super.dispose();
  46. }
  47. @override
  48. Widget build(BuildContext context) {
  49. return Scaffold(
  50. appBar: AppBar(
  51. backgroundColor: Colors.white,
  52. elevation: 0,
  53. toolbarHeight: 50.h,
  54. title: TabBar(
  55. controller: _tabController,
  56. tabs: _tabs,
  57. labelColor: Colors.blue,
  58. unselectedLabelColor: Colors.black,
  59. indicatorColor: Colors.blue,
  60. indicatorWeight: 3.h,
  61. ),
  62. // actions: [
  63. // IconButton(
  64. // icon: Icon(Icons.more_vert, color: Colors.black),
  65. // onPressed: () {
  66. // // More options menu
  67. // },
  68. // ),
  69. // ],
  70. ),
  71. body: TabBarView(
  72. controller: _tabController,
  73. children: [
  74. AlarmPage(),
  75. ClockPage(),
  76. StopwatchPage(),
  77. TimerPage(),
  78. ],
  79. ),
  80. );
  81. }
  82. static double radiansFromDegrees(double degrees) => degrees * math.pi / 180;
  83. static double radiansFromTime(DateTime time) {
  84. final midnightToday = DateTime(time.year, time.month, time.day);
  85. final secondsSinceMidnight = midnightToday.difference(time).inSeconds;
  86. final percent = secondsSinceMidnight / _secondsInDay;
  87. final degrees = percent * 360;
  88. return radiansFromDegrees(degrees);
  89. }
  90. }