flutter_dashboard/lib/screens/settings_screen.dart
jdysya 89c0dfc1aa feat(gitea): 添加 Gitea 数据卡片并更新相关设置
- 在 HomeScreen 中添加 GiteaCard 组件
- 在 SettingsScreen 中增加 Gitea 用户名和 API Token 设置
- 优化 LeetCodeCard 组件,增加空 cookie 检查
- 调整 SettingsScreen 中平台设置的布局结构
2025-06-09 16:10:40 +08:00

181 lines
5.5 KiB
Dart

import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import '../services/auth_service.dart';
class SettingsScreen extends StatefulWidget {
const SettingsScreen({super.key});
@override
State<SettingsScreen> createState() => _SettingsScreenState();
}
class _SettingsScreenState extends State<SettingsScreen> {
final _formKey = GlobalKey<FormState>();
final _leetcodeController = TextEditingController();
final _giteaController = TextEditingController();
final _giteaUsernameController = TextEditingController();
final _kodboxController = TextEditingController();
@override
void initState() {
super.initState();
_loadSettings();
}
@override
void dispose() {
_leetcodeController.dispose();
_giteaController.dispose();
_giteaUsernameController.dispose();
_kodboxController.dispose();
super.dispose();
}
Future<void> _loadSettings() async {
final authService = Provider.of<AuthService>(context, listen: false);
await authService.loadCredentials();
setState(() {
_leetcodeController.text = authService.credentials['leetcode'] ?? '';
_giteaController.text = authService.credentials['gitea'] ?? '';
_giteaUsernameController.text =
authService.credentials['gitea_username'] ?? '';
_kodboxController.text = authService.credentials['kodbox'] ?? '';
});
}
Future<void> _saveSettings() async {
if (_formKey.currentState!.validate()) {
final authService = Provider.of<AuthService>(context, listen: false);
if (_leetcodeController.text.isNotEmpty) {
await authService.saveCredentials('leetcode', _leetcodeController.text);
}
if (_giteaController.text.isNotEmpty) {
await authService.saveCredentials('gitea', _giteaController.text);
}
if (_giteaUsernameController.text.isNotEmpty) {
await authService.saveCredentials(
'gitea_username',
_giteaUsernameController.text,
);
}
if (_kodboxController.text.isNotEmpty) {
await authService.saveCredentials('kodbox', _kodboxController.text);
}
if (mounted) {
ScaffoldMessenger.of(
context,
).showSnackBar(const SnackBar(content: Text('设置已保存')));
Navigator.pop(context);
}
}
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: const Text('设置')),
body: Form(
key: _formKey,
child: ListView(
padding: const EdgeInsets.all(16.0),
children: [
_buildPlatformSettings(
title: 'LeetCode 设置',
icon: Icons.code,
children: [
TextFormField(
controller: _leetcodeController,
decoration: const InputDecoration(
labelText: 'Cookie',
hintText: '请输入 LeetCode Cookie',
border: OutlineInputBorder(),
),
obscureText: true,
),
],
),
const SizedBox(height: 16),
_buildPlatformSettings(
title: 'Gitea 设置',
icon: Icons.storage,
children: [
TextFormField(
controller: _giteaUsernameController,
decoration: const InputDecoration(
labelText: '用户名',
hintText: '请输入 Gitea 用户名',
border: OutlineInputBorder(),
),
),
const SizedBox(height: 16),
TextFormField(
controller: _giteaController,
decoration: const InputDecoration(
labelText: 'API Token',
hintText: '请输入 Gitea API Token',
border: OutlineInputBorder(),
),
obscureText: true,
),
],
),
const SizedBox(height: 16),
_buildPlatformSettings(
title: 'KodBox 设置',
icon: Icons.folder,
children: [
TextFormField(
controller: _kodboxController,
decoration: const InputDecoration(
labelText: 'API Token',
hintText: '请输入 KodBox API Token',
border: OutlineInputBorder(),
),
obscureText: true,
),
],
),
const SizedBox(height: 24),
ElevatedButton(onPressed: _saveSettings, child: const Text('保存设置')),
],
),
),
);
}
Widget _buildPlatformSettings({
required String title,
required IconData icon,
required List<Widget> children,
}) {
return Card(
child: Padding(
padding: const EdgeInsets.all(16.0),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Row(
children: [
Icon(icon),
const SizedBox(width: 8),
Text(
title,
style: const TextStyle(
fontSize: 18,
fontWeight: FontWeight.bold,
),
),
],
),
const SizedBox(height: 16),
...children,
],
),
),
);
}
}