flutter_dashboard/lib/screens/settings_screen.dart
jdysya 7b1b7288eb feat(auth): 重构认证服务并添加时间格式化功能
- 重命名和更新认证服务中的配置键名
- 在主入口文件中添加时间格式化库的初始化
- 更新设置屏幕中的配置加载和保存逻辑
- 在各个卡片组件中使用时间格式化库显示时间信息
2025-06-09 18:49:10 +08:00

185 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.loadConfigs();
setState(() {
_leetcodeController.text =
authService.credentials['leetcode_cookie'] ?? '';
_giteaController.text = authService.credentials['gitea_token'] ?? '';
_giteaUsernameController.text =
authService.credentials['gitea_username'] ?? '';
_kodboxController.text = authService.credentials['kodbox_token'] ?? '';
});
}
Future<void> _saveSettings() async {
if (_formKey.currentState!.validate()) {
final authService = Provider.of<AuthService>(context, listen: false);
if (_leetcodeController.text.isNotEmpty) {
await authService.saveConfigs(
'leetcode_cookie',
_leetcodeController.text,
);
}
if (_giteaController.text.isNotEmpty) {
await authService.saveConfigs('gitea_token', _giteaController.text);
}
if (_giteaUsernameController.text.isNotEmpty) {
await authService.saveConfigs(
'gitea_username',
_giteaUsernameController.text,
);
}
if (_kodboxController.text.isNotEmpty) {
await authService.saveConfigs('kodbox_token', _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,
],
),
),
);
}
}