DietaryGuard/lib/screens/settings.dart

171 lines
5.9 KiB
Dart
Raw Normal View History

2024-08-15 03:53:01 +00:00
import 'package:dietary_guard/controllers/alert.dart';
2024-08-14 17:26:42 +00:00
import 'package:dietary_guard/controllers/food_data.dart';
2024-08-15 03:53:01 +00:00
import 'package:dietary_guard/models/alert_configuration.dart';
2024-08-14 15:43:21 +00:00
import 'package:flutter/material.dart';
2024-08-14 17:26:42 +00:00
import 'package:get/get.dart';
2024-08-14 15:43:21 +00:00
2024-08-14 17:26:42 +00:00
class SettingsScreen extends StatefulWidget {
2024-08-14 15:43:21 +00:00
const SettingsScreen({super.key});
2024-08-14 17:26:42 +00:00
@override
State<SettingsScreen> createState() => _SettingsScreenState();
}
class _SettingsScreenState extends State<SettingsScreen> {
final TextEditingController _fdcApiKeyController = TextEditingController();
2024-08-15 03:53:01 +00:00
List<AlertConfiguration> _currentAlerts = List.empty(growable: true);
void _addAlert() {
setState(() {
_currentAlerts.add(AlertConfiguration(
nutrientId: 0,
maxValue: 0,
minValue: 0,
));
});
}
2024-08-14 17:26:42 +00:00
Future<void> _applySettings() async {
final FoodDataController data = Get.find();
await data.setApiKey(_fdcApiKeyController.text);
2024-08-15 03:53:01 +00:00
final AlertController alert = Get.find();
await alert.setAlertConfiguration(_currentAlerts);
2024-08-14 17:26:42 +00:00
ScaffoldMessenger.of(context).showSnackBar(SnackBar(
content: Text('settingsApplied'.tr),
));
}
@override
void initState() {
final FoodDataController data = Get.find();
_fdcApiKeyController.text = data.getApiKey() ?? '';
2024-08-15 03:53:01 +00:00
final AlertController alert = Get.find();
_currentAlerts = List.from(alert.configuration, growable: true);
2024-08-14 17:26:42 +00:00
super.initState();
}
@override
void dispose() {
_fdcApiKeyController.dispose();
super.dispose();
}
2024-08-15 03:53:01 +00:00
Widget _buildSectionHeader(String title) {
return Container(
color: Theme.of(context).colorScheme.secondaryContainer,
padding: const EdgeInsets.symmetric(horizontal: 28, vertical: 8),
child: Text(
title,
style: Theme.of(context).textTheme.bodyLarge,
),
).marginOnly(bottom: 16);
}
2024-08-14 15:43:21 +00:00
@override
Widget build(BuildContext context) {
2024-08-15 03:53:01 +00:00
return Material(
color: Theme.of(context).colorScheme.surface,
child: Scaffold(
appBar: AppBar(
title: Text('settings'.tr),
),
body: ListView(
children: [
_buildSectionHeader('settingsAlertSection'.tr),
Column(
children: [
...(_currentAlerts.map((x) => Row(
children: [
Expanded(
child: 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(width: 6),
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;
},
),
),
],
))),
Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
TextButton.icon(
label: Text('newAlert'.tr),
onPressed: () => _addAlert(),
),
],
).paddingSymmetric(vertical: 12, horizontal: 24),
],
).paddingSymmetric(horizontal: 24),
_buildSectionHeader('settingsDataSection'.tr),
TextField(
controller: _fdcApiKeyController,
obscureText: true,
decoration: const InputDecoration(
border: UnderlineInputBorder(),
label: Text("FDC API Key"),
isDense: false,
2024-08-14 17:26:42 +00:00
),
2024-08-15 03:53:01 +00:00
onTapOutside: (_) =>
FocusManager.instance.primaryFocus?.unfocus(),
).paddingSymmetric(horizontal: 24),
Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
TextButton.icon(
label: Text('apply'.tr),
onPressed: () => _applySettings(),
),
],
).paddingSymmetric(vertical: 12, horizontal: 24),
],
),
),
2024-08-14 17:26:42 +00:00
);
2024-08-14 15:43:21 +00:00
}
}