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 _giteaController = TextEditingController(); final _kodboxController = TextEditingController(); @override void initState() { super.initState(); _loadSettings(); } @override void dispose() { _leetcodeController.dispose(); _giteaController.dispose(); _kodboxController.dispose(); super.dispose(); } Future _loadSettings() async { final authService = Provider.of(context, listen: false); await authService.loadCredentials(); setState(() { _leetcodeController.text = authService.credentials['leetcode'] ?? ''; _giteaController.text = authService.credentials['gitea'] ?? ''; _kodboxController.text = authService.credentials['kodbox'] ?? ''; }); } Future _saveSettings() async { if (_formKey.currentState!.validate()) { final authService = Provider.of(context, listen: false); if (_leetcodeController.text.isNotEmpty) { await authService.saveCredentials('leetcode', _leetcodeController.text); } if (_giteaController.text.isNotEmpty) { await authService.saveCredentials('gitea', _giteaController.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, controller: _leetcodeController, hintText: '请输入 LeetCode Cookie', ), const SizedBox(height: 16), _buildPlatformSettings( title: 'Gitea 设置', icon: Icons.storage, controller: _giteaController, hintText: '请输入 Gitea API Token', ), const SizedBox(height: 16), _buildPlatformSettings( title: 'KodBox 设置', icon: Icons.folder, controller: _kodboxController, hintText: '请输入 KodBox API Token', ), const SizedBox(height: 24), ElevatedButton(onPressed: _saveSettings, child: const Text('保存设置')), ], ), ), ); } Widget _buildPlatformSettings({ required String title, required IconData icon, required TextEditingController controller, required String hintText, }) { 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), TextFormField( controller: controller, decoration: InputDecoration( labelText: '认证信息', hintText: hintText, border: const OutlineInputBorder(), ), obscureText: true, ), ], ), ), ); } }