timer_settings_page.dart 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  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. @override
  24. Widget build(BuildContext context) {
  25. return Scaffold(
  26. backgroundColor: Colors.white,
  27. appBar: AppBar(
  28. backgroundColor: Colors.white,
  29. elevation: 0,
  30. leading: IconButton(
  31. icon: Icon(Icons.close, color: Colors.black),
  32. onPressed: () => Navigator.pop(context),
  33. ),
  34. title: Text(
  35. 'Settings',
  36. style: TextStyle(color: Colors.black, fontSize: 24),
  37. ),
  38. centerTitle: true,
  39. ),
  40. body: ListView(
  41. children: [
  42. _buildSoundSetting(),
  43. _buildVolumeSetting(),
  44. _buildVibrateSetting(),
  45. _buildLoopSetting(),
  46. ],
  47. ),
  48. );
  49. }
  50. Widget _buildSoundSetting() {
  51. return Column(
  52. crossAxisAlignment: CrossAxisAlignment.start,
  53. children: [
  54. Padding(
  55. padding: const EdgeInsets.fromLTRB(16, 16, 16, 8),
  56. child: Row(
  57. mainAxisAlignment: MainAxisAlignment.spaceBetween,
  58. children: [
  59. Text(
  60. 'Sound',
  61. style: TextStyle(fontSize: 18),
  62. ),
  63. GestureDetector(
  64. onTap: () {
  65. _showSoundPicker();
  66. },
  67. child: Row(
  68. children: [
  69. Text(
  70. _sound,
  71. style: TextStyle(fontSize: 16, color: Colors.grey[700]),
  72. ),
  73. Icon(
  74. Icons.chevron_right,
  75. color: Colors.grey[700],
  76. ),
  77. ],
  78. ),
  79. ),
  80. ],
  81. ),
  82. ),
  83. Divider(),
  84. ],
  85. );
  86. }
  87. Widget _buildVolumeSetting() {
  88. return Column(
  89. crossAxisAlignment: CrossAxisAlignment.start,
  90. children: [
  91. Padding(
  92. padding: const EdgeInsets.fromLTRB(16, 8, 16, 8),
  93. child: Text(
  94. 'Vol.',
  95. style: TextStyle(fontSize: 18),
  96. ),
  97. ),
  98. Slider(
  99. value: _volume,
  100. min: 0.0,
  101. max: 1.0,
  102. activeColor: Colors.orange,
  103. inactiveColor: Colors.grey[300],
  104. onChanged: (value) {
  105. setState(() {
  106. _volume = value;
  107. });
  108. },
  109. ),
  110. Divider(),
  111. ],
  112. );
  113. }
  114. Widget _buildVibrateSetting() {
  115. return Column(
  116. crossAxisAlignment: CrossAxisAlignment.start,
  117. children: [
  118. Padding(
  119. padding: const EdgeInsets.fromLTRB(16, 8, 16, 8),
  120. child: Row(
  121. mainAxisAlignment: MainAxisAlignment.spaceBetween,
  122. children: [
  123. Text(
  124. 'Vibrate on ring',
  125. style: TextStyle(fontSize: 18),
  126. ),
  127. Switch(
  128. value: _vibrate,
  129. activeColor: Colors.orange,
  130. onChanged: (value) {
  131. setState(() {
  132. _vibrate = value;
  133. });
  134. },
  135. ),
  136. ],
  137. ),
  138. ),
  139. Divider(),
  140. ],
  141. );
  142. }
  143. Widget _buildLoopSetting() {
  144. return Column(
  145. crossAxisAlignment: CrossAxisAlignment.start,
  146. children: [
  147. Padding(
  148. padding: const EdgeInsets.fromLTRB(16, 8, 16, 8),
  149. child: Row(
  150. mainAxisAlignment: MainAxisAlignment.spaceBetween,
  151. children: [
  152. Text(
  153. 'Loop timer',
  154. style: TextStyle(fontSize: 18),
  155. ),
  156. Switch(
  157. value: _loop,
  158. activeColor: Colors.orange,
  159. onChanged: (value) {
  160. setState(() {
  161. _loop = value;
  162. });
  163. },
  164. ),
  165. ],
  166. ),
  167. ),
  168. Divider(),
  169. ],
  170. );
  171. }
  172. void _showSoundPicker() {
  173. showModalBottomSheet(
  174. context: context,
  175. builder: (context) {
  176. return Column(
  177. mainAxisSize: MainAxisSize.min,
  178. children: _availableSounds.map((sound) {
  179. return ListTile(
  180. title: Text(sound),
  181. onTap: () {
  182. setState(() {
  183. _sound = sound;
  184. });
  185. Navigator.pop(context);
  186. },
  187. trailing: _sound == sound
  188. ? Icon(Icons.check, color: Colors.blue)
  189. : null,
  190. );
  191. }).toList(),
  192. );
  193. },
  194. );
  195. }
  196. @override
  197. void dispose() {
  198. // Save settings before exiting
  199. final updatedSettings = TimerSettings(
  200. sound: _sound,
  201. volume: _volume,
  202. vibrate: _vibrate,
  203. loop: _loop,
  204. );
  205. updatedSettings.saveSettings();
  206. Navigator.pop(context, updatedSettings);
  207. super.dispose();
  208. }
  209. }