timer_settings_page.dart 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. import 'package:flutter/material.dart';
  2. import 'package:flutter_clock/model/timer_settings.dart';
  3. /// Description: 倒计时设置页面
  4. /// Time : 04/22/2025 Tuesday
  5. /// Author : liuyuqi.gov@msn.cn
  6. class TimerSettingsPage extends StatefulWidget {
  7. final TimerSettings settings;
  8. const TimerSettingsPage({Key? key, required this.settings}) : super(key: key);
  9. @override
  10. _TimerSettingsPageState createState() => _TimerSettingsPageState();
  11. }
  12. class _TimerSettingsPageState extends State<TimerSettingsPage> {
  13. late String _sound;
  14. late double _volume;
  15. late bool _vibrate;
  16. late bool _loop;
  17. final List<String> _availableSounds = ['Dripping', 'Alarm', 'Bell'];
  18. @override
  19. void initState() {
  20. super.initState();
  21. _sound = widget.settings.sound;
  22. _volume = widget.settings.volume;
  23. _vibrate = widget.settings.vibrate;
  24. _loop = widget.settings.loop;
  25. }
  26. // Save settings
  27. Future<TimerSettings> _saveSettings() async {
  28. final updatedSettings = TimerSettings(
  29. sound: _sound,
  30. volume: _volume,
  31. vibrate: _vibrate,
  32. loop: _loop,
  33. );
  34. await updatedSettings.saveSettings();
  35. return updatedSettings;
  36. }
  37. @override
  38. Widget build(BuildContext context) {
  39. return WillPopScope(
  40. onWillPop: () async {
  41. final updatedSettings = await _saveSettings();
  42. Navigator.pop(context, updatedSettings);
  43. return false;
  44. },
  45. child: Scaffold(
  46. backgroundColor: Colors.white,
  47. appBar: AppBar(
  48. backgroundColor: Colors.white,
  49. elevation: 0,
  50. leading: IconButton(
  51. icon: Icon(Icons.close, color: Colors.black),
  52. onPressed: () async {
  53. final updatedSettings = await _saveSettings();
  54. Navigator.pop(context, updatedSettings);
  55. },
  56. ),
  57. title: Text(
  58. 'Settings',
  59. style: TextStyle(color: Colors.black, fontSize: 24),
  60. ),
  61. centerTitle: true,
  62. ),
  63. body: ListView(
  64. children: [
  65. _buildSoundSetting(),
  66. _buildVolumeSetting(),
  67. _buildVibrateSetting(),
  68. _buildLoopSetting(),
  69. ],
  70. ),
  71. ),
  72. );
  73. }
  74. Widget _buildSoundSetting() {
  75. return Column(
  76. crossAxisAlignment: CrossAxisAlignment.start,
  77. children: [
  78. Padding(
  79. padding: const EdgeInsets.fromLTRB(16, 16, 16, 8),
  80. child: Row(
  81. mainAxisAlignment: MainAxisAlignment.spaceBetween,
  82. children: [
  83. Text(
  84. 'Sound',
  85. style: TextStyle(fontSize: 18),
  86. ),
  87. GestureDetector(
  88. onTap: () {
  89. _showSoundPicker();
  90. },
  91. child: Row(
  92. children: [
  93. Text(
  94. _sound,
  95. style: TextStyle(fontSize: 16, color: Colors.grey[700]),
  96. ),
  97. Icon(
  98. Icons.chevron_right,
  99. color: Colors.grey[700],
  100. ),
  101. ],
  102. ),
  103. ),
  104. ],
  105. ),
  106. ),
  107. Divider(),
  108. ],
  109. );
  110. }
  111. Widget _buildVolumeSetting() {
  112. return Column(
  113. crossAxisAlignment: CrossAxisAlignment.start,
  114. children: [
  115. Padding(
  116. padding: const EdgeInsets.fromLTRB(16, 8, 16, 8),
  117. child: Text(
  118. 'Vol.',
  119. style: TextStyle(fontSize: 18),
  120. ),
  121. ),
  122. Slider(
  123. value: _volume,
  124. min: 0.0,
  125. max: 1.0,
  126. activeColor: Colors.orange,
  127. inactiveColor: Colors.grey[300],
  128. onChanged: (value) {
  129. setState(() {
  130. _volume = value;
  131. });
  132. _saveSettings();
  133. },
  134. ),
  135. Divider(),
  136. ],
  137. );
  138. }
  139. Widget _buildVibrateSetting() {
  140. return Column(
  141. crossAxisAlignment: CrossAxisAlignment.start,
  142. children: [
  143. Padding(
  144. padding: const EdgeInsets.fromLTRB(16, 8, 16, 8),
  145. child: Row(
  146. mainAxisAlignment: MainAxisAlignment.spaceBetween,
  147. children: [
  148. Text(
  149. 'Vibrate on ring',
  150. style: TextStyle(fontSize: 18),
  151. ),
  152. Switch(
  153. value: _vibrate,
  154. activeColor: Colors.orange,
  155. onChanged: (value) {
  156. setState(() {
  157. _vibrate = value;
  158. });
  159. _saveSettings();
  160. },
  161. ),
  162. ],
  163. ),
  164. ),
  165. Divider(),
  166. ],
  167. );
  168. }
  169. Widget _buildLoopSetting() {
  170. return Column(
  171. crossAxisAlignment: CrossAxisAlignment.start,
  172. children: [
  173. Padding(
  174. padding: const EdgeInsets.fromLTRB(16, 8, 16, 8),
  175. child: Row(
  176. mainAxisAlignment: MainAxisAlignment.spaceBetween,
  177. children: [
  178. Text(
  179. 'Loop timer',
  180. style: TextStyle(fontSize: 18),
  181. ),
  182. Switch(
  183. value: _loop,
  184. activeColor: Colors.orange,
  185. onChanged: (value) {
  186. setState(() {
  187. _loop = value;
  188. });
  189. _saveSettings();
  190. },
  191. ),
  192. ],
  193. ),
  194. ),
  195. Divider(),
  196. ],
  197. );
  198. }
  199. void _showSoundPicker() {
  200. showModalBottomSheet(
  201. context: context,
  202. builder: (context) {
  203. return Column(
  204. mainAxisSize: MainAxisSize.min,
  205. children: _availableSounds.map((sound) {
  206. return ListTile(
  207. title: Text(sound),
  208. onTap: () {
  209. setState(() {
  210. _sound = sound;
  211. });
  212. _saveSettings();
  213. Navigator.pop(context);
  214. },
  215. trailing: _sound == sound
  216. ? Icon(Icons.check, color: Colors.blue)
  217. : null,
  218. );
  219. }).toList(),
  220. );
  221. },
  222. );
  223. }
  224. @override
  225. void dispose() {
  226. super.dispose();
  227. }
  228. }