cashumit/lib/screens/config_screen.dart

131 lines
3.9 KiB
Dart

// lib/screens/config_screen.dart
import 'package:flutter/material.dart';
import 'package:shared_preferences/shared_preferences.dart';
class ConfigScreen extends StatefulWidget {
const ConfigScreen({super.key});
@override
State<ConfigScreen> createState() => _ConfigScreenState();
}
class _ConfigScreenState extends State<ConfigScreen> {
final _formKey = GlobalKey<FormState>();
final _urlController = TextEditingController();
final _tokenController = TextEditingController();
static const String _prefsKeyUrl = 'firefly_url';
static const String _prefsKeyToken = 'firefly_token';
@override
void initState() {
super.initState();
_loadConfig();
}
/// Memuat konfigurasi yang tersimpan dari shared preferences.
Future<void> _loadConfig() async {
final prefs = await SharedPreferences.getInstance();
final savedUrl = prefs.getString(_prefsKeyUrl) ?? '';
final savedToken = prefs.getString(_prefsKeyToken) ?? '';
setState(() {
_urlController.text = savedUrl;
_tokenController.text = savedToken;
});
}
/// Menyimpan konfigurasi ke shared preferences.
Future<void> _saveConfig() async {
if (_formKey.currentState!.validate()) {
final prefs = await SharedPreferences.getInstance();
await prefs.setString(_prefsKeyUrl, _urlController.text);
await prefs.setString(_prefsKeyToken, _tokenController.text);
// Menampilkan snackbar konfirmasi
if (mounted) {
ScaffoldMessenger.of(context).showSnackBar(
const SnackBar(content: Text('Konfigurasi tersimpan!')),
);
}
}
}
@override
void dispose() {
_urlController.dispose();
_tokenController.dispose();
super.dispose();
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('Konfigurasi Firefly III'),
),
body: Padding(
padding: const EdgeInsets.all(16.0),
child: Form(
key: _formKey,
child: Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
const Text(
'Masukkan detail koneksi ke instance Firefly III Anda:',
style: TextStyle(fontSize: 16),
),
const SizedBox(height: 20),
// TextField untuk URL
TextFormField(
controller: _urlController,
decoration: const InputDecoration(
labelText: 'Firefly III URL',
hintText: 'https://firefly.yourdomain.com',
border: OutlineInputBorder(),
),
validator: (value) {
if (value == null || value.isEmpty) {
return 'URL tidak boleh kosong';
}
// Validasi sederhana untuk URL
if (!value.startsWith('http')) {
return 'URL harus dimulai dengan http:// atau https://';
}
return null;
},
),
const SizedBox(height: 20),
// TextField untuk Token
TextFormField(
controller: _tokenController,
decoration: const InputDecoration(
labelText: 'Personal Access Token',
border: OutlineInputBorder(),
),
obscureText: true, // Sembunyikan token
validator: (value) {
if (value == null || value.isEmpty) {
return 'Token tidak boleh kosong';
}
return null;
},
),
const SizedBox(height: 20),
// Tombol Simpan
ElevatedButton.icon(
onPressed: _saveConfig,
icon: const Icon(Icons.save),
label: const Text('Simpan Konfigurasi'),
),
],
),
),
),
);
}
}