calorimeter/lib/utils/settings.dart

70 lines
2.4 KiB
Dart

/* SPDX-License-Identifier: GPL-3.0-or-later */
/* Copyright (C) 2024 Marco Groß <mgross@sw-gross.de> */
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:calorimeter/utils/app_drawer.dart';
import 'package:calorimeter/utils/settings_bloc.dart';
import 'package:settings_ui/settings_ui.dart';
import 'package:flutter_gen/gen_l10n/app_localizations.dart';
class SettingsWidget extends StatefulWidget {
const SettingsWidget({super.key});
@override
State<SettingsWidget> createState() => _SettingsWidgetState();
}
class _SettingsWidgetState extends State<SettingsWidget> {
var kcalPerDayCtrl = TextEditingController();
@override
Widget build(BuildContext context) {
return Scaffold(
body: BlocBuilder<SettingsDataBloc, SettingsState>(
builder: (context, state) {
return SettingsList(sections: [
SettingsSection(
title: Text(AppLocalizations.of(context)!.yourSettings),
tiles: [
SettingsTile.navigation(
leading: const Icon(Icons.food_bank),
title: Text(AppLocalizations.of(context)!.dayLimit),
value: Text(state.kcalLimit.toString()),
onPressed: (context) async {
await showDialog(
builder: (ctx) {
return AlertDialog(
title: Text(AppLocalizations.of(context)!.dayLimit),
content: TextField(
controller: kcalPerDayCtrl,
onSubmitted: (val) => submitDailyKcal()),
actions: [
TextButton(
onPressed: () => submitDailyKcal(),
child: Text(AppLocalizations.of(context)!.ok))
],
);
},
context: context);
}),
],
),
]);
}),
drawer: const AppDrawer(),
appBar: AppBar(title: Text(AppLocalizations.of(context)!.settings)),
);
}
void submitDailyKcal() {
double setting;
try {
setting = double.parse(kcalPerDayCtrl.text);
} catch (e) {
setting = 2000.0;
}
context.read<SettingsDataBloc>().add(DailyKcalLimitUpdated(kcal: setting));
Navigator.of(context).pop();
}
}