Files
busylight-buddy/lib/screens/settings_screen.dart
T
2026-10-16 19:12:16 +02:00

142 lines
5.1 KiB
Dart

import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:shared_preferences/shared_preferences.dart';
import '../providers/busylight_provider.dart';
import '../services/autostart_service.dart';
class SettingsScreen extends ConsumerStatefulWidget {
const SettingsScreen({super.key});
@override
ConsumerState<SettingsScreen> createState() => _SettingsScreenState();
}
class _SettingsScreenState extends ConsumerState<SettingsScreen> {
late TextEditingController _hostController;
bool _startWithSession = false;
@override
void initState() {
super.initState();
_hostController = TextEditingController(text: ref.read(deviceHostProvider));
_loadAutostart();
}
Future<void> _loadAutostart() async {
final enabled = await AutostartService.isEnabled();
if (mounted) setState(() => _startWithSession = enabled);
}
@override
void dispose() {
_hostController.dispose();
super.dispose();
}
Future<void> _save() async {
final host = _hostController.text.trim();
if (host.isEmpty) return;
ref.read(deviceHostProvider.notifier).state = host;
await AutostartService.setEnabled(_startWithSession);
final prefs = await SharedPreferences.getInstance();
await prefs.setString('busylight_host', host);
if (mounted) {
ScaffoldMessenger.of(context).showSnackBar(
const SnackBar(content: Text('Settings saved')),
);
Navigator.pop(context);
}
}
@override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: Colors.black,
appBar: AppBar(
backgroundColor: Colors.black,
title: const Text('Settings', style: TextStyle(color: Colors.white)),
iconTheme: const IconThemeData(color: Colors.white),
),
body: Padding(
padding: const EdgeInsets.all(24),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
// ── Device address ──────────────────────────────────────────────
Text('Device address',
style: TextStyle(color: Colors.grey.shade300, fontSize: 13, fontWeight: FontWeight.w600)),
const SizedBox(height: 8),
TextField(
controller: _hostController,
style: const TextStyle(color: Colors.white),
decoration: InputDecoration(
hintText: 'http://igox-busylight.local',
hintStyle: TextStyle(color: Colors.grey.shade700),
filled: true,
fillColor: Colors.grey.shade900,
border: OutlineInputBorder(
borderRadius: BorderRadius.circular(10),
borderSide: BorderSide.none,
),
),
),
const SizedBox(height: 6),
Text(
'Use hostname (igox-busylight.local) or IP address (http://192.168.x.x)',
style: TextStyle(color: Colors.grey.shade600, fontSize: 12),
),
const SizedBox(height: 32),
// ── Start with session (macOS + Windows only) ───────────────────
if (AutostartService.isSupported) ...[
const SizedBox(height: 32),
Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text('Start with session',
style: TextStyle(color: Colors.grey.shade300, fontSize: 13, fontWeight: FontWeight.w600)),
const SizedBox(height: 2),
Text(
'Launch automatically at login',
style: TextStyle(color: Colors.grey.shade600, fontSize: 12),
),
],
),
Switch(
value: _startWithSession,
onChanged: (v) => setState(() => _startWithSession = v),
activeColor: Colors.amber,
),
],
),
],
const Spacer(),
// ── Save ────────────────────────────────────────────────────────
SizedBox(
width: double.infinity,
child: ElevatedButton(
onPressed: _save,
style: ElevatedButton.styleFrom(
backgroundColor: Colors.amber,
foregroundColor: Colors.black,
padding: const EdgeInsets.symmetric(vertical: 16),
shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(10)),
),
child: const Text('Save', style: TextStyle(fontWeight: FontWeight.w600)),
),
),
],
),
),
);
}
}