import 'package:flutter/material.dart'; import 'package:flutter_clock/model/timer_settings.dart'; /// Description: 倒计时设置页面 /// Time : 04/22/2025 Tuesday /// Author : liuyuqi.gov@msn.cn 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 { late String _sound; late double _volume; late bool _vibrate; late bool _loop; late bool _alignToHour; final List _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; _alignToHour = widget.settings.alignToHour; } // Save settings Future _saveSettings() async { final updatedSettings = TimerSettings( sound: _sound, volume: _volume, vibrate: _vibrate, loop: _loop, alignToHour: _alignToHour, ); 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(), _buildAlignToHourSetting(), ], ), ), ); } 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; // 如果循环被关闭,也关闭整点对齐 if (!value) { _alignToHour = false; } }); _saveSettings(); }, ), ], ), ), Divider(), ], ); } Widget _buildAlignToHourSetting() { // 只有当倒计时满10分钟且启用了循环倒计时时才启用该选项 bool isEnabled = _loop; return Column( crossAxisAlignment: CrossAxisAlignment.start, children: [ Padding( padding: const EdgeInsets.fromLTRB(16, 8, 16, 8), child: Row( mainAxisAlignment: MainAxisAlignment.spaceBetween, children: [ Column( crossAxisAlignment: CrossAxisAlignment.start, children: [ Text( '整点对齐', style: TextStyle( fontSize: 18, color: isEnabled ? Colors.black : Colors.grey, ), ), Text( '倒计时将等待整10分钟再开始', style: TextStyle( fontSize: 12, color: Colors.grey, ), ), ], ), Switch( value: _alignToHour && isEnabled, activeColor: Colors.orange, onChanged: isEnabled ? (value) { setState(() { _alignToHour = value; }); _saveSettings(); } : null, ), ], ), ), 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(); } }