timer_page.dart 14 KB

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