timer_settings_page.dart 6.0 KB

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