Update settings screen
This commit is contained in:
parent
1e576b9245
commit
6254a50e5c
2 changed files with 80 additions and 63 deletions
|
|
@ -1,3 +1,11 @@
|
||||||
{
|
{
|
||||||
"mainTitle": "Traccar Client"
|
"mainTitle": "Traccar Client",
|
||||||
|
"saveButton": "Save",
|
||||||
|
"cancelButton": "Cancel",
|
||||||
|
"idLabel": "Device identifier",
|
||||||
|
"urlLabel": "Server URL",
|
||||||
|
"accuracyLabel": "Location accuracy",
|
||||||
|
"intervalLabel": "Frequency",
|
||||||
|
"distanceLabel": "Distance",
|
||||||
|
"bufferLabel": "Offline buffering"
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,9 @@
|
||||||
import 'package:flutter/material.dart';
|
import 'package:flutter/material.dart';
|
||||||
|
import 'package:flutter/services.dart';
|
||||||
import 'package:shared_preferences/shared_preferences.dart';
|
import 'package:shared_preferences/shared_preferences.dart';
|
||||||
|
import 'package:flutter_gen/gen_l10n/app_localizations.dart';
|
||||||
|
|
||||||
|
import 'preferences.dart';
|
||||||
|
|
||||||
class SettingsScreen extends StatefulWidget {
|
class SettingsScreen extends StatefulWidget {
|
||||||
const SettingsScreen({super.key});
|
const SettingsScreen({super.key});
|
||||||
|
|
@ -9,91 +13,96 @@ class SettingsScreen extends StatefulWidget {
|
||||||
}
|
}
|
||||||
|
|
||||||
class _SettingsScreenState extends State<SettingsScreen> {
|
class _SettingsScreenState extends State<SettingsScreen> {
|
||||||
late SharedPreferences prefs;
|
bool loading = true;
|
||||||
|
late SharedPreferences preferences;
|
||||||
final deviceIdController = TextEditingController();
|
bool buffering = true;
|
||||||
final serverUrlController = TextEditingController();
|
|
||||||
final accuracyController = TextEditingController();
|
|
||||||
final distanceController = TextEditingController();
|
|
||||||
final frequencyController = TextEditingController();
|
|
||||||
bool offlineBuffering = false;
|
|
||||||
|
|
||||||
@override
|
@override
|
||||||
void initState() {
|
void initState() {
|
||||||
super.initState();
|
super.initState();
|
||||||
_loadSettings();
|
_initPreferences();
|
||||||
}
|
}
|
||||||
|
|
||||||
Future<void> _loadSettings() async {
|
void _initPreferences() async {
|
||||||
prefs = await SharedPreferences.getInstance();
|
preferences = await SharedPreferences.getInstance();
|
||||||
|
|
||||||
setState(() {
|
setState(() {
|
||||||
deviceIdController.text = prefs.getString('deviceId') ?? '';
|
loading = false;
|
||||||
serverUrlController.text = prefs.getString('serverUrl') ?? '';
|
buffering = preferences.getBool(Preferences.buffer) ?? true;
|
||||||
accuracyController.text = prefs.getString('accuracy') ?? 'high';
|
|
||||||
distanceController.text = prefs.getString('distance') ?? '50';
|
|
||||||
frequencyController.text = prefs.getString('frequency') ?? '60';
|
|
||||||
offlineBuffering = prefs.getBool('offlineBuffering') ?? true;
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
Future<void> _saveSettings() async {
|
Future<void> _editSetting(String title, String key, bool isInt) async {
|
||||||
await prefs.setString('deviceId', deviceIdController.text);
|
final initialValue = isInt
|
||||||
await prefs.setString('serverUrl', serverUrlController.text);
|
? preferences.getInt(key)?.toString() ?? '0'
|
||||||
await prefs.setString('accuracy', accuracyController.text);
|
: preferences.getString(key) ?? '';
|
||||||
await prefs.setString('distance', distanceController.text);
|
|
||||||
await prefs.setString('frequency', frequencyController.text);
|
|
||||||
await prefs.setBool('offlineBuffering', offlineBuffering);
|
|
||||||
|
|
||||||
if (mounted) {
|
final controller = TextEditingController(text: initialValue);
|
||||||
ScaffoldMessenger.of(context).showSnackBar(
|
|
||||||
const SnackBar(content: Text('Settings saved')),
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@override
|
final result = await showDialog<String>(
|
||||||
void dispose() {
|
context: context,
|
||||||
deviceIdController.dispose();
|
builder: (context) => AlertDialog(
|
||||||
serverUrlController.dispose();
|
title: Text(title),
|
||||||
accuracyController.dispose();
|
content: TextField(
|
||||||
distanceController.dispose();
|
|
||||||
frequencyController.dispose();
|
|
||||||
super.dispose();
|
|
||||||
}
|
|
||||||
|
|
||||||
Widget _buildTextField(String label, TextEditingController controller) {
|
|
||||||
return TextField(
|
|
||||||
controller: controller,
|
controller: controller,
|
||||||
decoration: InputDecoration(labelText: label),
|
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);
|
||||||
|
}
|
||||||
|
setState(() {});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
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),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
@override
|
@override
|
||||||
Widget build(BuildContext context) {
|
Widget build(BuildContext context) {
|
||||||
|
if (loading) {
|
||||||
|
return const Center(child: CircularProgressIndicator());
|
||||||
|
}
|
||||||
|
|
||||||
return Scaffold(
|
return Scaffold(
|
||||||
appBar: AppBar(
|
appBar: AppBar(title: const Text('Settings')),
|
||||||
title: const Text('Settings'),
|
|
||||||
actions: [
|
|
||||||
IconButton(
|
|
||||||
icon: const Icon(Icons.save),
|
|
||||||
onPressed: _saveSettings,
|
|
||||||
),
|
|
||||||
],
|
|
||||||
),
|
|
||||||
body: ListView(
|
body: ListView(
|
||||||
padding: const EdgeInsets.all(16),
|
|
||||||
children: [
|
children: [
|
||||||
_buildTextField('Device ID', deviceIdController),
|
_buildListTile(AppLocalizations.of(context)!.idLabel, Preferences.id, false),
|
||||||
_buildTextField('Server URL', serverUrlController),
|
_buildListTile(AppLocalizations.of(context)!.urlLabel, Preferences.url, false),
|
||||||
_buildTextField('Location Accuracy', accuracyController),
|
_buildListTile(AppLocalizations.of(context)!.accuracyLabel, Preferences.accuracy, false),
|
||||||
_buildTextField('Distance (meters)', distanceController),
|
_buildListTile(AppLocalizations.of(context)!.intervalLabel, Preferences.interval, true),
|
||||||
_buildTextField('Frequency (seconds)', frequencyController),
|
_buildListTile(AppLocalizations.of(context)!.distanceLabel, Preferences.distance, true),
|
||||||
SwitchListTile(
|
SwitchListTile(
|
||||||
title: const Text('Offline Buffering'),
|
title: Text(AppLocalizations.of(context)!.bufferLabel),
|
||||||
value: offlineBuffering,
|
value: buffering,
|
||||||
onChanged: (value) {
|
onChanged: (value) async {
|
||||||
setState(() => offlineBuffering = value);
|
await preferences.setBool(Preferences.buffer, value);
|
||||||
|
setState(() => buffering = value);
|
||||||
},
|
},
|
||||||
),
|
),
|
||||||
],
|
],
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue