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 createState() => _SettingsScreenState(); } class _SettingsScreenState extends State { final _formKey = GlobalKey(); final _leetcodeController = TextEditingController(); final _leetcodeUserSlugController = TextEditingController(); final _giteaController = TextEditingController(); final _giteaUsernameController = TextEditingController(); final _kodboxController = TextEditingController(); final _githubUsernameController = TextEditingController(); final _githubTokenController = TextEditingController(); @override void initState() { super.initState(); _loadSettings(); } @override void dispose() { _leetcodeController.dispose(); _leetcodeUserSlugController.dispose(); _giteaController.dispose(); _giteaUsernameController.dispose(); _kodboxController.dispose(); _githubUsernameController.dispose(); _githubTokenController.dispose(); super.dispose(); } Future _loadSettings() async { final authService = Provider.of(context, listen: false); await authService.loadConfigs(); setState(() { _leetcodeController.text = authService.credentials['leetcode_cookie'] ?? ''; _leetcodeUserSlugController.text = authService.credentials['leetcode_user_slug'] ?? ''; _giteaController.text = authService.credentials['gitea_token'] ?? ''; _giteaUsernameController.text = authService.credentials['gitea_username'] ?? ''; _kodboxController.text = authService.credentials['kodbox_token'] ?? ''; _githubUsernameController.text = authService.credentials['github_username'] ?? ''; _githubTokenController.text = authService.credentials['github_token'] ?? ''; }); } Future _saveSettings() async { if (_formKey.currentState!.validate()) { final authService = Provider.of(context, listen: false); if (_leetcodeController.text.isNotEmpty) { await authService.saveConfigs( 'leetcode_cookie', _leetcodeController.text, ); } if (_leetcodeUserSlugController.text.isNotEmpty) { await authService.saveConfigs( 'leetcode_user_slug', _leetcodeUserSlugController.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 (_githubUsernameController.text.isNotEmpty) { await authService.saveConfigs( 'github_username', _githubUsernameController.text, ); } if (_githubTokenController.text.isNotEmpty) { await authService.saveConfigs( 'github_token', _githubTokenController.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), TextFormField( controller: _leetcodeUserSlugController, decoration: const InputDecoration( labelText: 'User Slug', hintText: '请输入 LeetCode User Slug', border: OutlineInputBorder(), ), ), ], ), 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: 16), _buildPlatformSettings( title: 'GitHub 设置', icon: Icons.code, children: [ TextFormField( controller: _githubUsernameController, decoration: const InputDecoration( labelText: '用户名', hintText: '请输入 GitHub 用户名', border: OutlineInputBorder(), ), ), const SizedBox(height: 16), TextFormField( controller: _githubTokenController, decoration: const InputDecoration( labelText: 'Personal Access Token', hintText: '请输入 GitHub Personal Access 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 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, ], ), ), ); } }