timer_page.dart 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504
  1. import 'dart:async';
  2. import 'package:flutter/material.dart';
  3. import 'package:flutter_clock/model/timer_data.dart';
  4. import 'package:flutter_clock/model/timer_settings.dart';
  5. import 'package:flutter_clock/pages/timer/timer_settings_page.dart';
  6. import 'package:flutter_clock/utils/audio_manager.dart';
  7. import 'package:flutter_clock/utils/screen_manager.dart';
  8. import 'package:flutter_clock/utils/background_timer_service.dart';
  9. import 'package:flutter_clock/utils/notification_manager.dart';
  10. /// Description: 倒计时页面
  11. /// Time : 04/06/2025 Sunday
  12. /// Author : liuyuqi.gov@msn.cn
  13. class TimerPage extends StatefulWidget {
  14. @override
  15. _TimerPageState createState() => _TimerPageState();
  16. }
  17. enum TimerState { prepare, running, pause, finish }
  18. class _TimerPageState extends State<TimerPage> with WidgetsBindingObserver {
  19. // Timer duration values
  20. int _hours = 0;
  21. int _minutes = 10;
  22. int _seconds = 0;
  23. // For timer controller
  24. TimerState timerState = TimerState.prepare;
  25. int _remainingSeconds = 0;
  26. // Total timer duration in seconds (for progress calculation)
  27. int _totalDurationSeconds = 0;
  28. // Background services
  29. final BackgroundTimerService _backgroundTimerService =
  30. BackgroundTimerService();
  31. final NotificationManager _notificationManager = NotificationManager();
  32. // Settings
  33. late TimerSettings _settings;
  34. bool _settingsLoaded = false;
  35. final AudioManager _audioManager = AudioManager();
  36. // Wheel controllers
  37. final FixedExtentScrollController _hoursController =
  38. FixedExtentScrollController(initialItem: 0);
  39. final FixedExtentScrollController _minutesController =
  40. FixedExtentScrollController(initialItem: 10);
  41. final FixedExtentScrollController _secondsController =
  42. FixedExtentScrollController(initialItem: 0);
  43. @override
  44. void initState() {
  45. super.initState();
  46. WidgetsBinding.instance.addObserver(this);
  47. _initializeBackgroundTimer();
  48. _loadSettings();
  49. _restoreTimerState();
  50. }
  51. @override
  52. void dispose() {
  53. _audioManager.dispose();
  54. _backgroundTimerService.dispose();
  55. if (ScreenManager.isWakeLockEnabled) {
  56. ScreenManager.disableWakeLock();
  57. }
  58. WidgetsBinding.instance.removeObserver(this);
  59. _hoursController.dispose();
  60. _minutesController.dispose();
  61. _secondsController.dispose();
  62. super.dispose();
  63. }
  64. @override
  65. void didChangeAppLifecycleState(AppLifecycleState state) {
  66. // No additional handling required as background service handles the state
  67. }
  68. /// Restore timer state from persistent storage
  69. Future<void> _restoreTimerState() async {
  70. final timerData = await TimerData.load();
  71. if (timerData.isRunning) {
  72. setState(() {
  73. timerState = TimerState.running;
  74. _remainingSeconds = timerData.calculateRemainingSeconds();
  75. _totalDurationSeconds = _backgroundTimerService.totalDurationSeconds;
  76. });
  77. } else if (timerData.isPaused && timerData.pausedRemaining != null) {
  78. setState(() {
  79. timerState = TimerState.pause;
  80. _remainingSeconds = timerData.pausedRemaining!;
  81. _totalDurationSeconds = _backgroundTimerService.totalDurationSeconds;
  82. });
  83. }
  84. }
  85. /// Initialize the background timer service
  86. Future<void> _initializeBackgroundTimer() async {
  87. await _backgroundTimerService.initialize(
  88. onTimerComplete: _timerCompleted,
  89. onTimerTick: (remainingSeconds) {
  90. setState(() {
  91. _remainingSeconds = remainingSeconds;
  92. if (remainingSeconds <= 0 && timerState != TimerState.finish) {
  93. timerState = TimerState.finish;
  94. }
  95. });
  96. },
  97. );
  98. }
  99. Future<void> _loadSettings() async {
  100. _settings = await TimerSettings.loadSettings();
  101. setState(() {
  102. _settingsLoaded = true;
  103. });
  104. }
  105. /// Start a new timer or resume an existing paused timer
  106. void _startTimer(bool resumed) {
  107. if (resumed) {
  108. setState(() {
  109. timerState = TimerState.running;
  110. });
  111. _backgroundTimerService.resumeTimer(
  112. _remainingSeconds, _totalDurationSeconds);
  113. } else {
  114. // 获取界面输入的时间
  115. _hours = _hoursController.selectedItem;
  116. _minutes = _minutesController.selectedItem;
  117. _seconds = _secondsController.selectedItem;
  118. // 计算总秒数
  119. final totalSeconds = _hours * 3600 + _minutes * 60 + _seconds;
  120. if (totalSeconds <= 0) return;
  121. setState(() {
  122. timerState = TimerState.running;
  123. _remainingSeconds = totalSeconds;
  124. _totalDurationSeconds = totalSeconds;
  125. });
  126. _backgroundTimerService.startTimer(totalSeconds);
  127. }
  128. }
  129. /// Pause the running timer
  130. void _pauseTimer() {
  131. _backgroundTimerService.pauseTimer();
  132. setState(() {
  133. timerState = TimerState.pause;
  134. });
  135. }
  136. /// Reset the timer to initial state
  137. void _resetTimer() {
  138. _backgroundTimerService.cancelTimer();
  139. _audioManager.stopSound();
  140. _audioManager.stopVibration();
  141. _notificationManager.stopVibration();
  142. setState(() {
  143. timerState = TimerState.prepare;
  144. });
  145. }
  146. /// Handle timer completion
  147. Future<void> _timerCompleted() async {
  148. setState(() {
  149. timerState = TimerState.finish;
  150. });
  151. // Play sound and vibrate
  152. if (_settings.vibrate) {
  153. _notificationManager.notifyTimerCompletion();
  154. }
  155. _audioManager.playSound(_settings.sound, _settings.volume, _settings.loop);
  156. // If loop is enabled, restart the timer
  157. if (_settings.loop) {
  158. Future.delayed(Duration(seconds: 5), () {
  159. _audioManager.stopSound();
  160. _notificationManager.stopVibration();
  161. });
  162. _startTimer(false);
  163. }
  164. }
  165. String _formatTime(int seconds) {
  166. final hours = seconds ~/ 3600;
  167. final minutes = (seconds % 3600) ~/ 60;
  168. final secs = seconds % 60;
  169. return '${hours.toString().padLeft(2, '0')}:${minutes.toString().padLeft(2, '0')}:${secs.toString().padLeft(2, '0')}';
  170. }
  171. @override
  172. Widget build(BuildContext context) {
  173. if (!_settingsLoaded) {
  174. return Center(child: CircularProgressIndicator());
  175. }
  176. if (timerState != TimerState.prepare) {
  177. return _buildCountdownView();
  178. } else {
  179. return _buildTimerSetupView();
  180. }
  181. }
  182. Widget _buildTimerSetupView() {
  183. return Scaffold(
  184. backgroundColor: Colors.white,
  185. body: Column(
  186. mainAxisAlignment: MainAxisAlignment.center,
  187. children: [
  188. Expanded(
  189. child: Row(
  190. children: [
  191. Expanded(
  192. child: _buildTimerWheel(
  193. _hoursController,
  194. List.generate(24, (index) => index),
  195. (value) {
  196. setState(() {
  197. _hours = value;
  198. });
  199. },
  200. 'H',
  201. ),
  202. ),
  203. Expanded(
  204. child: _buildTimerWheel(
  205. _minutesController,
  206. List.generate(60, (index) => index),
  207. (value) {
  208. setState(() {
  209. _minutes = value;
  210. });
  211. },
  212. 'M',
  213. ),
  214. ),
  215. Expanded(
  216. child: _buildTimerWheel(
  217. _secondsController,
  218. List.generate(60, (index) => index),
  219. (value) {
  220. setState(() {
  221. _seconds = value;
  222. });
  223. },
  224. 'S',
  225. ),
  226. ),
  227. ],
  228. ),
  229. ),
  230. SizedBox(height: 20),
  231. Padding(
  232. padding: const EdgeInsets.symmetric(horizontal: 50, vertical: 20),
  233. child: Row(
  234. mainAxisAlignment: MainAxisAlignment.spaceAround,
  235. children: [
  236. _buildCircleButton(
  237. Icons.phone_android,
  238. Colors.grey[600]!,
  239. () {
  240. ScreenManager.toggleWakeLock();
  241. setState(() {});
  242. },
  243. isActive: ScreenManager.isWakeLockEnabled,
  244. ),
  245. _buildCircleButton(
  246. Icons.play_arrow,
  247. Colors.blue,
  248. () => _startTimer(false),
  249. ),
  250. _buildCircleButton(
  251. Icons.settings,
  252. Colors.grey[600]!,
  253. () async {
  254. final result = await Navigator.push(
  255. context,
  256. MaterialPageRoute(
  257. builder: (context) =>
  258. TimerSettingsPage(settings: _settings)),
  259. );
  260. if (result != null) {
  261. setState(() {
  262. _settings = result;
  263. });
  264. }
  265. },
  266. ),
  267. ],
  268. ),
  269. ),
  270. ],
  271. ),
  272. );
  273. }
  274. Widget _buildCountdownView() {
  275. final totalMinutes = _remainingSeconds ~/ 60;
  276. return Scaffold(
  277. backgroundColor: Colors.white,
  278. body: Column(
  279. mainAxisAlignment: MainAxisAlignment.center,
  280. children: [
  281. Expanded(
  282. child: Center(
  283. child: Column(
  284. mainAxisAlignment: MainAxisAlignment.center,
  285. children: [
  286. Container(
  287. width: 300,
  288. height: 300,
  289. decoration: BoxDecoration(
  290. shape: BoxShape.circle,
  291. border: Border.all(
  292. color: Colors.blue.withOpacity(0.3),
  293. width: 3,
  294. ),
  295. ),
  296. child: Stack(
  297. alignment: Alignment.center,
  298. children: [
  299. // Timer progress
  300. SizedBox(
  301. width: 300,
  302. height: 300,
  303. child: CircularProgressIndicator(
  304. value: timerState == TimerState.finish
  305. ? 1
  306. : _totalDurationSeconds > 0
  307. ? _remainingSeconds / _totalDurationSeconds
  308. : 0,
  309. strokeWidth: 5,
  310. backgroundColor: Colors.grey.withOpacity(0.1),
  311. color: Colors.blue,
  312. ),
  313. ),
  314. // Time display
  315. Column(
  316. mainAxisAlignment: MainAxisAlignment.center,
  317. children: [
  318. Text(
  319. _formatTime(_remainingSeconds),
  320. style: TextStyle(
  321. fontSize: 40, fontWeight: FontWeight.bold),
  322. ),
  323. Text(
  324. 'Total ${totalMinutes} minutes',
  325. style:
  326. TextStyle(fontSize: 16, color: Colors.grey),
  327. ),
  328. ],
  329. ),
  330. // Indicator dot
  331. Positioned(
  332. bottom: 0,
  333. child: Container(
  334. width: 20,
  335. height: 20,
  336. decoration: BoxDecoration(
  337. color: Colors.blue,
  338. shape: BoxShape.circle,
  339. ),
  340. ),
  341. ),
  342. ],
  343. ),
  344. ),
  345. ],
  346. ),
  347. ),
  348. ),
  349. Padding(
  350. padding: const EdgeInsets.symmetric(horizontal: 50, vertical: 20),
  351. child: Row(
  352. mainAxisAlignment: MainAxisAlignment.spaceAround,
  353. children: [
  354. // 唤醒屏幕
  355. _buildCircleButton(
  356. Icons.phone_android,
  357. Colors.grey[600]!,
  358. () {
  359. ScreenManager.toggleWakeLock();
  360. setState(() {});
  361. },
  362. isActive: ScreenManager.isWakeLockEnabled,
  363. ),
  364. // 暂停/开始
  365. timerState == TimerState.running
  366. ? _buildCircleButton(
  367. Icons.pause,
  368. Colors.blue,
  369. () => _pauseTimer(),
  370. )
  371. : _buildCircleButton(
  372. Icons.play_arrow,
  373. Colors.blue,
  374. () => _startTimer(true),
  375. ),
  376. // 重置
  377. _buildCircleButton(
  378. Icons.stop,
  379. Colors.red,
  380. () => _resetTimer(),
  381. ),
  382. ],
  383. ),
  384. ),
  385. ],
  386. ),
  387. );
  388. }
  389. Widget _buildCircleButton(IconData icon, Color color, VoidCallback onPressed,
  390. {bool isActive = false}) {
  391. return Container(
  392. width: 70,
  393. height: 70,
  394. decoration: BoxDecoration(
  395. shape: BoxShape.circle,
  396. color: isActive ? color : Colors.white,
  397. boxShadow: [
  398. BoxShadow(
  399. color: Colors.black.withOpacity(0.1),
  400. blurRadius: 8,
  401. offset: Offset(0, 2),
  402. ),
  403. ],
  404. ),
  405. child: IconButton(
  406. icon: Icon(icon, size: 30),
  407. color: isActive ? Colors.white : color,
  408. onPressed: onPressed,
  409. ),
  410. );
  411. }
  412. Widget _buildTimerWheel(
  413. FixedExtentScrollController controller,
  414. List<int> items,
  415. ValueChanged<int> onChanged,
  416. String unit,
  417. ) {
  418. return Container(
  419. height: 400,
  420. decoration: BoxDecoration(
  421. border: Border(
  422. top: BorderSide(color: Colors.grey.withOpacity(0.3), width: 1),
  423. bottom: BorderSide(color: Colors.grey.withOpacity(0.3), width: 1),
  424. ),
  425. ),
  426. child: Stack(
  427. children: [
  428. // Center highlight
  429. Positioned.fill(
  430. child: Center(
  431. child: Container(
  432. height: 50,
  433. decoration: BoxDecoration(
  434. color: Colors.blue.withOpacity(0.1),
  435. borderRadius: BorderRadius.circular(8),
  436. ),
  437. ),
  438. ),
  439. ),
  440. Container(
  441. alignment: Alignment.centerRight,
  442. padding: EdgeInsets.only(right: 15),
  443. child: Text(
  444. unit,
  445. style: TextStyle(
  446. fontSize: 18,
  447. fontWeight: FontWeight.bold,
  448. ),
  449. ),
  450. ),
  451. ListWheelScrollView(
  452. controller: controller,
  453. physics: FixedExtentScrollPhysics(),
  454. diameterRatio: 1.5,
  455. itemExtent: 50,
  456. children: items.map((value) {
  457. return Center(
  458. child: Text(
  459. value.toString().padLeft(2, '0'),
  460. style: TextStyle(
  461. fontSize: 30,
  462. color: Colors.black,
  463. fontWeight: FontWeight.w500,
  464. ),
  465. ),
  466. );
  467. }).toList(),
  468. onSelectedItemChanged: onChanged,
  469. ),
  470. ],
  471. ),
  472. );
  473. }
  474. }