- New SettingsScreen with server_url and api_token fields, saved to SharedPreferences - Add settings gear icon to HomeScreen AppBar - Allows users to configure backend connection before first upload Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
91 lines
2.9 KiB
Dart
91 lines
2.9 KiB
Dart
import 'package:flutter/material.dart';
|
|
import 'package:shared_preferences/shared_preferences.dart';
|
|
|
|
class SettingsScreen extends StatefulWidget {
|
|
const SettingsScreen({super.key});
|
|
|
|
@override
|
|
State<SettingsScreen> createState() => _SettingsScreenState();
|
|
}
|
|
|
|
class _SettingsScreenState extends State<SettingsScreen> {
|
|
final _serverController = TextEditingController();
|
|
final _tokenController = TextEditingController();
|
|
bool _initialized = false;
|
|
|
|
@override
|
|
void initState() {
|
|
super.initState();
|
|
_loadSettings();
|
|
}
|
|
|
|
Future<void> _loadSettings() async {
|
|
final prefs = await SharedPreferences.getInstance();
|
|
_serverController.text = prefs.getString('server_url') ?? '';
|
|
_tokenController.text = prefs.getString('api_token') ?? '';
|
|
setState(() => _initialized = true);
|
|
}
|
|
|
|
Future<void> _save() async {
|
|
final prefs = await SharedPreferences.getInstance();
|
|
await prefs.setString('server_url', _serverController.text.trim());
|
|
await prefs.setString('api_token', _tokenController.text.trim());
|
|
if (!mounted) return;
|
|
ScaffoldMessenger.of(context).showSnackBar(
|
|
const SnackBar(content: Text('设置已保存'), duration: Duration(seconds: 1)),
|
|
);
|
|
Navigator.of(context).pop();
|
|
}
|
|
|
|
@override
|
|
void dispose() {
|
|
_serverController.dispose();
|
|
_tokenController.dispose();
|
|
super.dispose();
|
|
}
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
return Scaffold(
|
|
appBar: AppBar(title: const Text('设置')),
|
|
body: _initialized
|
|
? Padding(
|
|
padding: const EdgeInsets.all(16),
|
|
child: Column(
|
|
crossAxisAlignment: CrossAxisAlignment.start,
|
|
children: [
|
|
TextField(
|
|
controller: _serverController,
|
|
decoration: const InputDecoration(
|
|
labelText: '服务器地址',
|
|
hintText: 'https://your-server.com',
|
|
border: OutlineInputBorder(),
|
|
),
|
|
keyboardType: TextInputType.url,
|
|
),
|
|
const SizedBox(height: 16),
|
|
TextField(
|
|
controller: _tokenController,
|
|
decoration: const InputDecoration(
|
|
labelText: 'API Token',
|
|
hintText: '输入认证令牌',
|
|
border: OutlineInputBorder(),
|
|
),
|
|
),
|
|
const SizedBox(height: 24),
|
|
SizedBox(
|
|
width: double.infinity,
|
|
child: FilledButton.icon(
|
|
onPressed: _save,
|
|
icon: const Icon(Icons.save),
|
|
label: const Text('保存'),
|
|
),
|
|
),
|
|
],
|
|
),
|
|
)
|
|
: const Center(child: CircularProgressIndicator()),
|
|
);
|
|
}
|
|
}
|