DietaryGuard/lib/screens/settings/alert.dart

168 lines
5.9 KiB
Dart
Raw Normal View History

import 'package:dietary_guard/controllers/alert.dart';
import 'package:dietary_guard/models/alert_configuration.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
class AlertSettingsScreen extends StatefulWidget {
const AlertSettingsScreen({super.key});
@override
State<AlertSettingsScreen> createState() => _AlertSettingsScreenState();
}
class _AlertSettingsScreenState extends State<AlertSettingsScreen> {
List<AlertConfiguration> _currentAlerts = List.empty(growable: true);
void _addAlert() {
setState(() {
_currentAlerts.add(AlertConfiguration(
2024-08-15 14:50:00 +00:00
name: 'Alert #${_currentAlerts.length}',
nutrientId: 0,
maxValue: 0,
minValue: 0,
));
});
}
void _removeAlert(AlertConfiguration item) {
setState(() {
_currentAlerts.remove(item);
});
}
Future<void> _applySettings() async {
final AlertController alert = Get.find();
await alert.setAlertConfiguration(_currentAlerts);
ScaffoldMessenger.of(context).showSnackBar(SnackBar(
content: Text('settingsApplied'.tr),
));
}
@override
void initState() {
final AlertController alert = Get.find();
_currentAlerts = List.from(alert.configuration, growable: true);
super.initState();
}
@override
Widget build(BuildContext context) {
return Material(
color: Theme.of(context).colorScheme.surface,
child: Scaffold(
appBar: AppBar(
title: Text('alertSettings'.tr),
),
body: ListView(
children: [
..._currentAlerts.map(
(x) => Card(
child: Column(
children: [
Row(
children: [
Expanded(
child: TextFormField(
2024-08-15 14:50:00 +00:00
initialValue: x.name,
decoration: InputDecoration(
border: const OutlineInputBorder(),
2024-08-15 14:50:00 +00:00
label: Text("alertName".tr),
isDense: true,
),
onTapOutside: (_) =>
FocusManager.instance.primaryFocus?.unfocus(),
onChanged: (value) {
2024-08-15 14:50:00 +00:00
x.name = value;
},
),
),
const SizedBox(width: 6),
IconButton(
icon: const Icon(Icons.close),
onPressed: () {
_removeAlert(x);
},
),
],
),
const SizedBox(height: 12),
2024-08-15 14:50:00 +00:00
TextFormField(
initialValue: x.nutrientId.toString(),
decoration: InputDecoration(
border: const OutlineInputBorder(),
label: Text("alertNutrientId".tr),
isDense: true,
),
onTapOutside: (_) =>
FocusManager.instance.primaryFocus?.unfocus(),
onChanged: (value) {
x.nutrientId = int.tryParse(value) ?? 0;
},
),
const SizedBox(height: 12),
Row(
children: [
Expanded(
child: TextFormField(
initialValue: x.maxValue.toString(),
decoration: InputDecoration(
border: const OutlineInputBorder(),
label: Text("alertMaxValue".tr),
isDense: true,
),
onTapOutside: (_) =>
FocusManager.instance.primaryFocus?.unfocus(),
onChanged: (value) {
x.maxValue = double.tryParse(value) ?? 0;
},
),
),
const SizedBox(width: 6),
Expanded(
child: TextFormField(
initialValue: x.minValue.toString(),
decoration: InputDecoration(
border: const OutlineInputBorder(),
label: Text("alertMinValue".tr),
isDense: true,
),
onTapOutside: (_) =>
FocusManager.instance.primaryFocus?.unfocus(),
onChanged: (value) {
x.minValue = double.tryParse(value) ?? 0;
},
),
),
],
),
],
).paddingAll(16),
),
),
Row(
mainAxisAlignment: MainAxisAlignment.end,
children: [
TextButton.icon(
icon: const Icon(Icons.save, size: 16),
label: Text('apply'.tr),
onPressed: () => _applySettings(),
),
ElevatedButton.icon(
style: const ButtonStyle(
foregroundColor: WidgetStatePropertyAll(Colors.teal),
),
icon: const Icon(Icons.add, size: 16),
label: Text('newAlert'.tr),
onPressed: () => _addAlert(),
),
],
).paddingSymmetric(vertical: 4),
],
).paddingSymmetric(horizontal: 12),
),
);
}
}