index_page.dart 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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. /// 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. final List<Widget> _tabs = [
  21. Tab(child: Text('Alarm', style: TextStyle(fontSize: 16))),
  22. Tab(child: Text('Clock', style: TextStyle(fontSize: 16))),
  23. Tab(child: Text('Stopwatch', style: TextStyle(fontSize: 16))),
  24. Tab(child: Text('Timer', style: TextStyle(fontSize: 16))),
  25. ];
  26. final _utcMidnightRadiansOffset = radiansFromDegrees(-64);
  27. static const _secondsInDay = 86400;
  28. DateTime _localTime = DateTime.now();
  29. @override
  30. void initState() {
  31. super.initState();
  32. _tabController = TabController(
  33. length: 4,
  34. vsync: this,
  35. initialIndex: 3); // Timer tab is selected by default
  36. Timer.periodic(Duration(seconds: 1),
  37. (_) => setState(() => _localTime = DateTime.now()));
  38. }
  39. @override
  40. void dispose() {
  41. _tabController.dispose();
  42. super.dispose();
  43. }
  44. @override
  45. Widget build(BuildContext context) {
  46. return Scaffold(
  47. appBar: AppBar(
  48. backgroundColor: Colors.white,
  49. elevation: 0,
  50. title: TabBar(
  51. controller: _tabController,
  52. tabs: _tabs,
  53. labelColor: Colors.blue,
  54. unselectedLabelColor: Colors.black,
  55. indicatorColor: Colors.blue,
  56. indicatorWeight: 3,
  57. ),
  58. // actions: [
  59. // IconButton(
  60. // icon: Icon(Icons.more_vert, color: Colors.black),
  61. // onPressed: () {
  62. // // More options menu
  63. // },
  64. // ),
  65. // ],
  66. ),
  67. body: TabBarView(
  68. controller: _tabController,
  69. children: [
  70. AlarmPage(),
  71. ClockPage(),
  72. StopwatchPage(),
  73. TimerPage(),
  74. ],
  75. ),
  76. );
  77. }
  78. static double radiansFromDegrees(double degrees) => degrees * math.pi / 180;
  79. static double radiansFromTime(DateTime time) {
  80. final midnightToday = DateTime(time.year, time.month, time.day);
  81. final secondsSinceMidnight = midnightToday.difference(time).inSeconds;
  82. final percent = secondsSinceMidnight / _secondsInDay;
  83. final degrees = percent * 360;
  84. return radiansFromDegrees(degrees);
  85. }
  86. }