5 Commits
+64 -38
View File
@@ -11,6 +11,18 @@ import 'package:alarm/alarm.dart';
import 'dart:convert';
/// TBD:
/// v0.2.1
/// - Fix time to get 0 prefixed if before 10:00 (5:30 becomes 05:30).
/// - Fix time to validate properly rather than allowing garbage (error 530).
/// v0.3.0
/// - Add sunrise section and change app name to Sunshine Alarm?
/// v1.0.0
/// - Follow system theme rather than using a custom theme.
/// v1.0.1
/// - Use a timepicker if the user clicks on the sunset/sunrise time.
/// :TBD
void main() async {
WidgetsFlutterBinding.ensureInitialized();
tz.initializeTimeZones();
@@ -54,6 +66,9 @@ class _HomeScreenState extends State<HomeScreen> {
int _minutesBefore = 30;
bool _loading = false;
String? _sunsetErrorText = null;
String? _minuteErrorText = null;
static const _alarmId = 1001;
@override
@@ -261,13 +276,13 @@ class _HomeScreenState extends State<HomeScreen> {
// Schedule:
final settings = AlarmSettings(
id: _alarmId,
dateTime: alarmTime, // your computed sunset time
//assetAudioPath: 'assets/alarm.mp3', // put an alarm sound in assets/
dateTime: alarmTime,
//assetAudioPath: 'assets/alarm.mp3', // use default rather than custom
loopAudio: true, // keeps ringing until dismissed
vibrate: true,
androidFullScreenIntent: true, // wakes screen like clock app
volumeSettings: VolumeSettings.fade(
volume: 0.8,
//volume: 0.8, // use default rather than custom
fadeDuration: Duration(seconds: 60),
),
notificationSettings: NotificationSettings(
@@ -291,7 +306,11 @@ class _HomeScreenState extends State<HomeScreen> {
} else {
_alarmTimer?.cancel();
if (!Platform.isLinux) {
await _notifications.cancel(id: _alarmId);
// Notification Method
//await _notifications.cancel(id: _alarmId);
// Alarm Method
await Alarm.stop(_alarmId);
}
_showSnack('Alarm cancelled.');
}
@@ -359,7 +378,7 @@ class _HomeScreenState extends State<HomeScreen> {
const SizedBox(width: 8),
IconButton.filledTonal(
icon: const Icon(Icons.my_location),
tooltip: 'Get GPS location',
tooltip: 'Get GPS Location',
onPressed: _getGpsLocation,
),
],
@@ -378,7 +397,7 @@ class _HomeScreenState extends State<HomeScreen> {
child: TextField(
controller: _sunsetController,
decoration: const InputDecoration(
labelText: 'Sunset time (HH:MM)',
labelText: 'Sunset Time (HH:MM)',
border: OutlineInputBorder(),
),
onChanged: (_) => _savePrefs(),
@@ -394,55 +413,62 @@ class _HomeScreenState extends State<HomeScreen> {
child: CircularProgressIndicator(strokeWidth: 2),
)
: const Icon(Icons.cloud_done_outlined),
tooltip: 'Fetch sunset from Open-Meteo',
tooltip: 'Fetch Sunset Time from Open-Meteo',
onPressed: _loading ? null : _fetchSunset,
),
],
),
const SizedBox(height: 16),
// --- Minutes before ---
const Text(
'Alert Before Sunset',
'Alert',
style: TextStyle(fontSize: 18, fontWeight: FontWeight.bold),
),
const SizedBox(height: 8),
// --- Minutes before ---
Row(
children: [
const Text('Minutes before: '),
SizedBox(
width: 80,
child: TextField(
controller: _minutesController,
keyboardType: TextInputType.number,
decoration: const InputDecoration(
border: OutlineInputBorder(),
if (!_alarmEnabled)
Expanded(
child: TextField(
controller: _minutesController,
keyboardType: TextInputType.number,
decoration: InputDecoration(
border: OutlineInputBorder(),
labelText: 'Minutes Before Sunset',
errorText: _minuteErrorText,
),
onChanged: (v) {
final val = int.tryParse(v);
setState(() {
if (val != null) {
_minuteErrorText = null;
_minutesBefore = val;
} else {
_minuteErrorText = 'Invalid Minute Value';
}
});
},
),
onChanged: (v) {
final val = int.tryParse(v);
if (val != null && val >= 0) {
_minutesBefore = val;
} else {
_showSnack('Invalid Minute Value');
}
},
),
// --- Toggle ---
Expanded(
child: SwitchListTile(
//title: const Text('Enable Sunset Alarm'),
title: Text(_alarmEnabled ? 'Sunset Alarm Enabled' : ''),
subtitle: Text(
_alarmEnabled
? 'Will alert ${_minutesBefore} min before sunset. ($_alarmDisplayTime)'
: '',
),
value: _alarmEnabled,
onChanged: _toggleAlarm,
),
),
],
),
const SizedBox(height: 24),
// --- Toggle ---
SwitchListTile(
title: const Text('Enable Sunset Alarm'),
subtitle: Text(
_alarmEnabled
? 'Will alert ${_minutesBefore} min before sunset. ($_alarmDisplayTime)'
: 'Alarm is off',
),
value: _alarmEnabled,
onChanged: _toggleAlarm,
),
],
),
),