123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239 |
- import 'package:flutter/material.dart';
- import 'package:flutter_clock/model/timer_settings.dart';
- class TimerSettingsPage extends StatefulWidget {
- final TimerSettings settings;
- const TimerSettingsPage({Key? key, required this.settings}) : super(key: key);
- @override
- _TimerSettingsPageState createState() => _TimerSettingsPageState();
- }
- class _TimerSettingsPageState extends State<TimerSettingsPage> {
- late String _sound;
- late double _volume;
- late bool _vibrate;
- late bool _loop;
- final List<String> _availableSounds = ['Dripping', 'Alarm', 'Bell'];
- @override
- void initState() {
- super.initState();
- _sound = widget.settings.sound;
- _volume = widget.settings.volume;
- _vibrate = widget.settings.vibrate;
- _loop = widget.settings.loop;
- }
-
- // Save settings
- Future<TimerSettings> _saveSettings() async {
- final updatedSettings = TimerSettings(
- sound: _sound,
- volume: _volume,
- vibrate: _vibrate,
- loop: _loop,
- );
- await updatedSettings.saveSettings();
- return updatedSettings;
- }
- @override
- Widget build(BuildContext context) {
- return WillPopScope(
- onWillPop: () async {
- final updatedSettings = await _saveSettings();
- Navigator.pop(context, updatedSettings);
- return false;
- },
- child: Scaffold(
- backgroundColor: Colors.white,
- appBar: AppBar(
- backgroundColor: Colors.white,
- elevation: 0,
- leading: IconButton(
- icon: Icon(Icons.close, color: Colors.black),
- onPressed: () async {
- final updatedSettings = await _saveSettings();
- Navigator.pop(context, updatedSettings);
- },
- ),
- title: Text(
- 'Settings',
- style: TextStyle(color: Colors.black, fontSize: 24),
- ),
- centerTitle: true,
- ),
- body: ListView(
- children: [
- _buildSoundSetting(),
- _buildVolumeSetting(),
- _buildVibrateSetting(),
- _buildLoopSetting(),
- ],
- ),
- ),
- );
- }
- Widget _buildSoundSetting() {
- return Column(
- crossAxisAlignment: CrossAxisAlignment.start,
- children: [
- Padding(
- padding: const EdgeInsets.fromLTRB(16, 16, 16, 8),
- child: Row(
- mainAxisAlignment: MainAxisAlignment.spaceBetween,
- children: [
- Text(
- 'Sound',
- style: TextStyle(fontSize: 18),
- ),
- GestureDetector(
- onTap: () {
- _showSoundPicker();
- },
- child: Row(
- children: [
- Text(
- _sound,
- style: TextStyle(fontSize: 16, color: Colors.grey[700]),
- ),
- Icon(
- Icons.chevron_right,
- color: Colors.grey[700],
- ),
- ],
- ),
- ),
- ],
- ),
- ),
- Divider(),
- ],
- );
- }
- Widget _buildVolumeSetting() {
- return Column(
- crossAxisAlignment: CrossAxisAlignment.start,
- children: [
- Padding(
- padding: const EdgeInsets.fromLTRB(16, 8, 16, 8),
- child: Text(
- 'Vol.',
- style: TextStyle(fontSize: 18),
- ),
- ),
- Slider(
- value: _volume,
- min: 0.0,
- max: 1.0,
- activeColor: Colors.orange,
- inactiveColor: Colors.grey[300],
- onChanged: (value) {
- setState(() {
- _volume = value;
- });
- _saveSettings();
- },
- ),
- Divider(),
- ],
- );
- }
- Widget _buildVibrateSetting() {
- return Column(
- crossAxisAlignment: CrossAxisAlignment.start,
- children: [
- Padding(
- padding: const EdgeInsets.fromLTRB(16, 8, 16, 8),
- child: Row(
- mainAxisAlignment: MainAxisAlignment.spaceBetween,
- children: [
- Text(
- 'Vibrate on ring',
- style: TextStyle(fontSize: 18),
- ),
- Switch(
- value: _vibrate,
- activeColor: Colors.orange,
- onChanged: (value) {
- setState(() {
- _vibrate = value;
- });
- _saveSettings();
- },
- ),
- ],
- ),
- ),
- Divider(),
- ],
- );
- }
- Widget _buildLoopSetting() {
- return Column(
- crossAxisAlignment: CrossAxisAlignment.start,
- children: [
- Padding(
- padding: const EdgeInsets.fromLTRB(16, 8, 16, 8),
- child: Row(
- mainAxisAlignment: MainAxisAlignment.spaceBetween,
- children: [
- Text(
- 'Loop timer',
- style: TextStyle(fontSize: 18),
- ),
- Switch(
- value: _loop,
- activeColor: Colors.orange,
- onChanged: (value) {
- setState(() {
- _loop = value;
- });
- _saveSettings();
- },
- ),
- ],
- ),
- ),
- Divider(),
- ],
- );
- }
- void _showSoundPicker() {
- showModalBottomSheet(
- context: context,
- builder: (context) {
- return Column(
- mainAxisSize: MainAxisSize.min,
- children: _availableSounds.map((sound) {
- return ListTile(
- title: Text(sound),
- onTap: () {
- setState(() {
- _sound = sound;
- });
- _saveSettings();
- Navigator.pop(context);
- },
- trailing: _sound == sound
- ? Icon(Icons.check, color: Colors.blue)
- : null,
- );
- }).toList(),
- );
- },
- );
- }
- @override
- void dispose() {
- super.dispose();
- }
- }
|