trcr/lib/settings_screen.dart

116 lines
3.7 KiB
Dart
Raw Normal View History

2025-05-05 08:02:02 -07:00
import 'package:flutter/material.dart';
2025-05-07 07:46:20 -07:00
import 'package:flutter/services.dart';
2025-05-05 08:02:02 -07:00
import 'package:shared_preferences/shared_preferences.dart';
2025-05-07 07:46:20 -07:00
import 'package:flutter_gen/gen_l10n/app_localizations.dart';
2025-05-07 17:42:07 -07:00
import 'package:flutter_background_geolocation/flutter_background_geolocation.dart' as bg;
2025-05-07 07:46:20 -07:00
import 'preferences.dart';
2025-05-05 08:02:02 -07:00
class SettingsScreen extends StatefulWidget {
const SettingsScreen({super.key});
@override
State<SettingsScreen> createState() => _SettingsScreenState();
}
class _SettingsScreenState extends State<SettingsScreen> {
2025-05-07 07:46:20 -07:00
bool loading = true;
late SharedPreferences preferences;
bool buffering = true;
2025-05-05 08:02:02 -07:00
@override
void initState() {
super.initState();
2025-05-07 07:46:20 -07:00
_initPreferences();
2025-05-05 08:02:02 -07:00
}
2025-05-07 07:46:20 -07:00
void _initPreferences() async {
preferences = await SharedPreferences.getInstance();
2025-05-05 08:02:02 -07:00
setState(() {
2025-05-07 07:46:20 -07:00
loading = false;
buffering = preferences.getBool(Preferences.buffer) ?? true;
2025-05-05 08:02:02 -07:00
});
}
2025-05-07 07:46:20 -07:00
Future<void> _editSetting(String title, String key, bool isInt) async {
final initialValue = isInt
? preferences.getInt(key)?.toString() ?? '0'
: preferences.getString(key) ?? '';
2025-05-05 08:02:02 -07:00
2025-05-07 07:46:20 -07:00
final controller = TextEditingController(text: initialValue);
2025-05-05 08:02:02 -07:00
2025-05-07 07:46:20 -07:00
final result = await showDialog<String>(
context: context,
builder: (context) => AlertDialog(
title: Text(title),
content: TextField(
controller: controller,
keyboardType: isInt ? TextInputType.number : TextInputType.text,
inputFormatters: isInt ? [FilteringTextInputFormatter.digitsOnly] : [],
),
actions: [
TextButton(
onPressed: () => Navigator.pop(context),
child: Text(AppLocalizations.of(context)!.cancelButton),
),
TextButton(
onPressed: () => Navigator.pop(context, controller.text),
child: Text(AppLocalizations.of(context)!.saveButton),
),
],
),
);
if (result != null && result.isNotEmpty) {
if (isInt) {
final intValue = int.tryParse(result);
if (intValue != null) {
await preferences.setInt(key, intValue);
}
} else {
await preferences.setString(key, result);
}
2025-05-07 17:42:07 -07:00
await bg.BackgroundGeolocation.setConfig(Preferences.geolocationConfig(preferences));
2025-05-07 07:46:20 -07:00
setState(() {});
}
2025-05-05 08:02:02 -07:00
}
2025-05-07 07:46:20 -07:00
Widget _buildListTile(String title, String key, bool isInt) {
final value = isInt ? preferences.getInt(key)?.toString() : preferences.getString(key);
return ListTile(
title: Text(title),
subtitle: Text(value ?? ''),
onTap: () => _editSetting(title, key, isInt),
2025-05-05 08:02:02 -07:00
);
}
@override
Widget build(BuildContext context) {
2025-05-07 07:46:20 -07:00
if (loading) {
return const Center(child: CircularProgressIndicator());
}
2025-05-05 08:02:02 -07:00
return Scaffold(
2025-05-07 07:46:20 -07:00
appBar: AppBar(title: const Text('Settings')),
2025-05-05 08:02:02 -07:00
body: ListView(
children: [
2025-05-07 07:46:20 -07:00
_buildListTile(AppLocalizations.of(context)!.idLabel, Preferences.id, false),
_buildListTile(AppLocalizations.of(context)!.urlLabel, Preferences.url, false),
_buildListTile(AppLocalizations.of(context)!.accuracyLabel, Preferences.accuracy, false),
_buildListTile(AppLocalizations.of(context)!.intervalLabel, Preferences.interval, true),
_buildListTile(AppLocalizations.of(context)!.distanceLabel, Preferences.distance, true),
2025-05-05 08:02:02 -07:00
SwitchListTile(
2025-05-07 07:46:20 -07:00
title: Text(AppLocalizations.of(context)!.bufferLabel),
value: buffering,
onChanged: (value) async {
await preferences.setBool(Preferences.buffer, value);
2025-05-07 17:42:07 -07:00
await bg.BackgroundGeolocation.setConfig(Preferences.geolocationConfig(preferences));
2025-05-07 07:46:20 -07:00
setState(() => buffering = value);
2025-05-05 08:02:02 -07:00
},
),
],
),
);
}
}