- 在首页添加 Wakatime 卡片和相关功能 - 在设置页面添加 Wakatime API Token 配置 - 更新 auth_service 以支持 Wakatime 认证 - 添加 fl_chart 依赖用于 Wakatime 数据图表展示 - 更新 flutter_lints 版本到 2.0.0
275 lines
8.9 KiB
Dart
275 lines
8.9 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 _leetcodeUserSlugController = TextEditingController();
|
|
final _giteaController = TextEditingController();
|
|
final _giteaUsernameController = TextEditingController();
|
|
final _kodboxController = TextEditingController();
|
|
final _githubUsernameController = TextEditingController();
|
|
final _githubTokenController = TextEditingController();
|
|
final _wakatimeTokenController = TextEditingController();
|
|
|
|
@override
|
|
void initState() {
|
|
super.initState();
|
|
_loadSettings();
|
|
}
|
|
|
|
@override
|
|
void dispose() {
|
|
_leetcodeController.dispose();
|
|
_leetcodeUserSlugController.dispose();
|
|
_giteaController.dispose();
|
|
_giteaUsernameController.dispose();
|
|
_kodboxController.dispose();
|
|
_githubUsernameController.dispose();
|
|
_githubTokenController.dispose();
|
|
_wakatimeTokenController.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'] ?? '';
|
|
_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'] ?? '';
|
|
_wakatimeTokenController.text =
|
|
authService.credentials['wakatime_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 (_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 (_wakatimeTokenController.text.isNotEmpty) {
|
|
await authService.saveConfigs(
|
|
'wakatime_token',
|
|
_wakatimeTokenController.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: 16),
|
|
_buildPlatformSettings(
|
|
title: 'Wakatime 设置',
|
|
icon: Icons.timer,
|
|
children: [
|
|
TextFormField(
|
|
controller: _wakatimeTokenController,
|
|
decoration: const InputDecoration(
|
|
labelText: 'API Token',
|
|
hintText: '请输入 Wakatime 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,
|
|
],
|
|
),
|
|
),
|
|
);
|
|
}
|
|
}
|