马良AI写作初始化仓库
This commit is contained in:
@@ -0,0 +1,426 @@
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
import '../../../models/prompt_models.dart';
|
||||
import '../../../services/api_service/repositories/impl/admin_repository_impl.dart';
|
||||
import '../../../utils/web_theme.dart';
|
||||
import '../../../widgets/common/dialog_container.dart';
|
||||
import '../../../widgets/common/dialog_header.dart';
|
||||
|
||||
/// 添加增强模板对话框
|
||||
class AddEnhancedTemplateDialog extends StatefulWidget {
|
||||
final EnhancedUserPromptTemplate? template;
|
||||
final VoidCallback? onSuccess;
|
||||
final ValueChanged<EnhancedUserPromptTemplate>? onUpdated;
|
||||
|
||||
const AddEnhancedTemplateDialog({
|
||||
Key? key,
|
||||
this.template,
|
||||
this.onSuccess,
|
||||
this.onUpdated,
|
||||
}) : super(key: key);
|
||||
|
||||
@override
|
||||
State<AddEnhancedTemplateDialog> createState() => _AddEnhancedTemplateDialogState();
|
||||
}
|
||||
|
||||
class _AddEnhancedTemplateDialogState extends State<AddEnhancedTemplateDialog> {
|
||||
final _formKey = GlobalKey<FormState>();
|
||||
final _nameController = TextEditingController();
|
||||
final _descriptionController = TextEditingController();
|
||||
final _systemPromptController = TextEditingController();
|
||||
final _userPromptController = TextEditingController();
|
||||
final _tagsController = TextEditingController();
|
||||
|
||||
String _featureType = 'TEXT_EXPANSION';
|
||||
String _language = 'zh';
|
||||
bool _isVerified = false;
|
||||
bool _isLoading = false;
|
||||
|
||||
// 功能类型由 AIFeatureTypeHelper.allFeatures 动态提供
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
// 如果是编辑模式,填充现有数据
|
||||
if (widget.template != null) {
|
||||
final template = widget.template!;
|
||||
_nameController.text = template.name;
|
||||
_descriptionController.text = template.description ?? '';
|
||||
_systemPromptController.text = template.systemPrompt;
|
||||
_userPromptController.text = template.userPrompt;
|
||||
_tagsController.text = template.tags.join(', ');
|
||||
_featureType = template.featureType.toApiString();
|
||||
_language = template.language ?? 'zh';
|
||||
_isVerified = template.isVerified;
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
void dispose() {
|
||||
_nameController.dispose();
|
||||
_descriptionController.dispose();
|
||||
_systemPromptController.dispose();
|
||||
_userPromptController.dispose();
|
||||
_tagsController.dispose();
|
||||
super.dispose();
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return DialogContainer(
|
||||
child: Column(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
children: [
|
||||
DialogHeader(
|
||||
title: widget.template != null ? '编辑模板' : '添加官方模板',
|
||||
onClose: () => Navigator.of(context).pop(),
|
||||
),
|
||||
Expanded(
|
||||
child: SingleChildScrollView(
|
||||
padding: const EdgeInsets.all(24),
|
||||
child: Form(
|
||||
key: _formKey,
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
_buildBasicInfo(),
|
||||
const SizedBox(height: 24),
|
||||
_buildPromptContent(),
|
||||
const SizedBox(height: 24),
|
||||
_buildAdvancedSettings(),
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
_buildActions(),
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
Widget _buildBasicInfo() {
|
||||
return Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
Text(
|
||||
'基础信息',
|
||||
style: TextStyle(
|
||||
fontSize: 18,
|
||||
fontWeight: FontWeight.bold,
|
||||
color: WebTheme.getTextColor(context),
|
||||
),
|
||||
),
|
||||
const SizedBox(height: 16),
|
||||
TextFormField(
|
||||
controller: _nameController,
|
||||
decoration: const InputDecoration(
|
||||
labelText: '模板名称 *',
|
||||
hintText: '请输入模板名称',
|
||||
border: OutlineInputBorder(),
|
||||
),
|
||||
validator: (value) {
|
||||
if (value == null || value.trim().isEmpty) {
|
||||
return '请输入模板名称';
|
||||
}
|
||||
return null;
|
||||
},
|
||||
),
|
||||
const SizedBox(height: 16),
|
||||
TextFormField(
|
||||
controller: _descriptionController,
|
||||
decoration: const InputDecoration(
|
||||
labelText: '模板描述',
|
||||
hintText: '请输入模板描述',
|
||||
border: OutlineInputBorder(),
|
||||
),
|
||||
maxLines: 3,
|
||||
),
|
||||
const SizedBox(height: 16),
|
||||
DropdownButtonFormField<String>(
|
||||
value: _featureType,
|
||||
decoration: const InputDecoration(
|
||||
labelText: '功能类型 *',
|
||||
border: OutlineInputBorder(),
|
||||
),
|
||||
items: AIFeatureTypeHelper.allFeatures.map((type) {
|
||||
final api = type.toApiString();
|
||||
return DropdownMenuItem(
|
||||
value: api,
|
||||
child: Text(type.displayName),
|
||||
);
|
||||
}).toList(),
|
||||
onChanged: (value) {
|
||||
if (value != null) {
|
||||
setState(() {
|
||||
_featureType = value;
|
||||
});
|
||||
}
|
||||
},
|
||||
),
|
||||
const SizedBox(height: 16),
|
||||
TextFormField(
|
||||
controller: _tagsController,
|
||||
decoration: const InputDecoration(
|
||||
labelText: '标签',
|
||||
hintText: '请输入标签,用逗号分隔',
|
||||
border: OutlineInputBorder(),
|
||||
),
|
||||
),
|
||||
],
|
||||
);
|
||||
}
|
||||
|
||||
Widget _buildPromptContent() {
|
||||
return Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
Text(
|
||||
'提示词内容',
|
||||
style: TextStyle(
|
||||
fontSize: 18,
|
||||
fontWeight: FontWeight.bold,
|
||||
color: WebTheme.getTextColor(context),
|
||||
),
|
||||
),
|
||||
const SizedBox(height: 16),
|
||||
TextFormField(
|
||||
controller: _systemPromptController,
|
||||
decoration: const InputDecoration(
|
||||
labelText: '系统提示词',
|
||||
hintText: '请输入系统提示词内容',
|
||||
border: OutlineInputBorder(),
|
||||
),
|
||||
maxLines: 5,
|
||||
validator: (value) {
|
||||
if (value == null || value.trim().isEmpty) {
|
||||
return '请输入系统提示词内容';
|
||||
}
|
||||
return null;
|
||||
},
|
||||
),
|
||||
const SizedBox(height: 16),
|
||||
TextFormField(
|
||||
controller: _userPromptController,
|
||||
decoration: const InputDecoration(
|
||||
labelText: '用户提示词',
|
||||
hintText: '请输入用户提示词内容',
|
||||
border: OutlineInputBorder(),
|
||||
),
|
||||
maxLines: 5,
|
||||
validator: (value) {
|
||||
if (value == null || value.trim().isEmpty) {
|
||||
return '请输入用户提示词内容';
|
||||
}
|
||||
return null;
|
||||
},
|
||||
),
|
||||
],
|
||||
);
|
||||
}
|
||||
|
||||
Widget _buildAdvancedSettings() {
|
||||
return Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
Text(
|
||||
'高级设置',
|
||||
style: TextStyle(
|
||||
fontSize: 18,
|
||||
fontWeight: FontWeight.bold,
|
||||
color: WebTheme.getTextColor(context),
|
||||
),
|
||||
),
|
||||
const SizedBox(height: 16),
|
||||
DropdownButtonFormField<String>(
|
||||
value: _language,
|
||||
decoration: const InputDecoration(
|
||||
labelText: '语言',
|
||||
border: OutlineInputBorder(),
|
||||
),
|
||||
items: const [
|
||||
DropdownMenuItem(value: 'zh', child: Text('中文')),
|
||||
DropdownMenuItem(value: 'en', child: Text('English')),
|
||||
DropdownMenuItem(value: 'ja', child: Text('日本語')),
|
||||
DropdownMenuItem(value: 'ko', child: Text('한국어')),
|
||||
],
|
||||
onChanged: (value) {
|
||||
if (value != null) {
|
||||
setState(() {
|
||||
_language = value;
|
||||
});
|
||||
}
|
||||
},
|
||||
),
|
||||
const SizedBox(height: 16),
|
||||
CheckboxListTile(
|
||||
title: const Text('设为官方认证模板'),
|
||||
subtitle: const Text('官方认证的模板会显示认证标识'),
|
||||
value: _isVerified,
|
||||
onChanged: (value) {
|
||||
setState(() {
|
||||
_isVerified = value ?? false;
|
||||
});
|
||||
},
|
||||
controlAffinity: ListTileControlAffinity.leading,
|
||||
),
|
||||
],
|
||||
);
|
||||
}
|
||||
|
||||
Widget _buildActions() {
|
||||
return Container(
|
||||
padding: const EdgeInsets.all(24),
|
||||
decoration: BoxDecoration(
|
||||
color: WebTheme.getCardColor(context),
|
||||
border: Border(
|
||||
top: BorderSide(
|
||||
color: WebTheme.getBorderColor(context),
|
||||
),
|
||||
),
|
||||
),
|
||||
child: Row(
|
||||
mainAxisAlignment: MainAxisAlignment.end,
|
||||
children: [
|
||||
TextButton(
|
||||
onPressed: _isLoading ? null : () => Navigator.of(context).pop(),
|
||||
child: const Text('取消'),
|
||||
),
|
||||
const SizedBox(width: 16),
|
||||
ElevatedButton(
|
||||
onPressed: _isLoading ? null : _createTemplate,
|
||||
child: _isLoading
|
||||
? const SizedBox(
|
||||
width: 16,
|
||||
height: 16,
|
||||
child: CircularProgressIndicator(strokeWidth: 2),
|
||||
)
|
||||
: Text(widget.template != null ? '更新' : '创建'),
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
Future<void> _createTemplate() async {
|
||||
if (!_formKey.currentState!.validate()) {
|
||||
return;
|
||||
}
|
||||
|
||||
setState(() {
|
||||
_isLoading = true;
|
||||
});
|
||||
|
||||
try {
|
||||
EnhancedUserPromptTemplate? saved;
|
||||
// 解析标签
|
||||
List<String> tags = [];
|
||||
if (_tagsController.text.trim().isNotEmpty) {
|
||||
tags = _tagsController.text
|
||||
.split(',')
|
||||
.map((tag) => tag.trim())
|
||||
.where((tag) => tag.isNotEmpty)
|
||||
.toList();
|
||||
}
|
||||
|
||||
final adminRepository = AdminRepositoryImpl();
|
||||
|
||||
if (widget.template != null) {
|
||||
// 编辑模式 - 更新现有模板
|
||||
final updatedTemplate = widget.template!.copyWith(
|
||||
name: _nameController.text.trim(),
|
||||
description: _descriptionController.text.trim().isNotEmpty
|
||||
? _descriptionController.text.trim()
|
||||
: null,
|
||||
featureType: _getFeatureTypeFromString(_featureType),
|
||||
systemPrompt: _systemPromptController.text.trim(),
|
||||
userPrompt: _userPromptController.text.trim(),
|
||||
tags: tags,
|
||||
language: _language,
|
||||
isVerified: _isVerified,
|
||||
);
|
||||
|
||||
saved = await adminRepository.updateEnhancedTemplate(
|
||||
widget.template!.id,
|
||||
updatedTemplate,
|
||||
);
|
||||
} else {
|
||||
// 创建模式 - 新建模板
|
||||
final now = DateTime.now();
|
||||
final template = EnhancedUserPromptTemplate(
|
||||
id: '', // 将由后端生成
|
||||
userId: 'admin', // 管理员创建
|
||||
name: _nameController.text.trim(),
|
||||
description: _descriptionController.text.trim().isNotEmpty
|
||||
? _descriptionController.text.trim()
|
||||
: null,
|
||||
featureType: _getFeatureTypeFromString(_featureType),
|
||||
systemPrompt: _systemPromptController.text.trim(),
|
||||
userPrompt: _userPromptController.text.trim(),
|
||||
tags: tags,
|
||||
createdAt: now,
|
||||
updatedAt: now,
|
||||
isPublic: true, // 官方模板默认为公开
|
||||
isVerified: _isVerified,
|
||||
version: 1,
|
||||
language: _language,
|
||||
);
|
||||
|
||||
saved = await adminRepository.createOfficialEnhancedTemplate(template);
|
||||
}
|
||||
|
||||
if (mounted) {
|
||||
Navigator.of(context).pop();
|
||||
if (widget.onUpdated != null) {
|
||||
widget.onUpdated!(saved);
|
||||
} else {
|
||||
widget.onSuccess?.call();
|
||||
}
|
||||
ScaffoldMessenger.of(context).showSnackBar(
|
||||
SnackBar(content: Text(widget.template != null ? '模板更新成功' : '官方模板创建成功')),
|
||||
);
|
||||
}
|
||||
} catch (e) {
|
||||
if (mounted) {
|
||||
ScaffoldMessenger.of(context).showSnackBar(
|
||||
SnackBar(content: Text('创建失败: $e')),
|
||||
);
|
||||
}
|
||||
} finally {
|
||||
if (mounted) {
|
||||
setState(() {
|
||||
_isLoading = false;
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
AIFeatureType _getFeatureTypeFromString(String featureType) {
|
||||
switch (featureType) {
|
||||
case 'TEXT_EXPANSION':
|
||||
return AIFeatureType.textExpansion;
|
||||
case 'TEXT_REFACTOR':
|
||||
return AIFeatureType.textRefactor;
|
||||
case 'TEXT_SUMMARY':
|
||||
return AIFeatureType.textSummary;
|
||||
case 'AI_CHAT':
|
||||
return AIFeatureType.aiChat;
|
||||
case 'NOVEL_GENERATION':
|
||||
return AIFeatureType.novelGeneration;
|
||||
case 'PROFESSIONAL_FICTION_CONTINUATION':
|
||||
return AIFeatureType.professionalFictionContinuation;
|
||||
case 'SCENE_BEAT_GENERATION':
|
||||
return AIFeatureType.sceneBeatGeneration;
|
||||
case 'SCENE_TO_SUMMARY':
|
||||
return AIFeatureType.sceneToSummary;
|
||||
case 'SUMMARY_TO_SCENE':
|
||||
return AIFeatureType.summaryToScene;
|
||||
case 'NOVEL_COMPOSE':
|
||||
return AIFeatureType.novelCompose;
|
||||
case 'SETTING_TREE_GENERATION':
|
||||
return AIFeatureType.settingTreeGeneration;
|
||||
default:
|
||||
return AIFeatureType.textExpansion;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,377 @@
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
import '../../../models/prompt_models.dart';
|
||||
import '../../../services/api_service/repositories/impl/admin_repository_impl.dart';
|
||||
import '../../../utils/logger.dart';
|
||||
|
||||
/// 添加官方模板对话框
|
||||
class AddOfficialTemplateDialog extends StatefulWidget {
|
||||
final VoidCallback? onSuccess;
|
||||
|
||||
const AddOfficialTemplateDialog({
|
||||
Key? key,
|
||||
this.onSuccess,
|
||||
}) : super(key: key);
|
||||
|
||||
@override
|
||||
State<AddOfficialTemplateDialog> createState() => _AddOfficialTemplateDialogState();
|
||||
}
|
||||
|
||||
class _AddOfficialTemplateDialogState extends State<AddOfficialTemplateDialog> {
|
||||
final _formKey = GlobalKey<FormState>();
|
||||
final _nameController = TextEditingController();
|
||||
final _descriptionController = TextEditingController();
|
||||
final _templateContentController = TextEditingController();
|
||||
final _authorNameController = TextEditingController();
|
||||
final _versionController = TextEditingController(text: '1.0.0');
|
||||
final _tagsController = TextEditingController();
|
||||
|
||||
String _selectedFeatureType = 'CHAT';
|
||||
bool _isPublic = true;
|
||||
bool _isVerified = true;
|
||||
bool _isLoading = false;
|
||||
|
||||
final AdminRepositoryImpl _adminRepository = AdminRepositoryImpl();
|
||||
// 功能类型动态来源:AIFeatureTypeHelper.allFeatures
|
||||
|
||||
@override
|
||||
void dispose() {
|
||||
_nameController.dispose();
|
||||
_descriptionController.dispose();
|
||||
_templateContentController.dispose();
|
||||
_authorNameController.dispose();
|
||||
_versionController.dispose();
|
||||
_tagsController.dispose();
|
||||
super.dispose();
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Dialog(
|
||||
child: Container(
|
||||
width: 700,
|
||||
constraints: const BoxConstraints(maxHeight: 800),
|
||||
padding: const EdgeInsets.all(24),
|
||||
child: Column(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
Row(
|
||||
children: [
|
||||
const Icon(Icons.verified, size: 24, color: Colors.blue),
|
||||
const SizedBox(width: 8),
|
||||
const Text(
|
||||
'添加官方模板',
|
||||
style: TextStyle(fontSize: 20, fontWeight: FontWeight.bold),
|
||||
),
|
||||
const Spacer(),
|
||||
IconButton(
|
||||
onPressed: () => Navigator.of(context).pop(),
|
||||
icon: const Icon(Icons.close),
|
||||
),
|
||||
],
|
||||
),
|
||||
const SizedBox(height: 24),
|
||||
|
||||
Expanded(
|
||||
child: Form(
|
||||
key: _formKey,
|
||||
child: SingleChildScrollView(
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
_buildBasicInfoSection(),
|
||||
const SizedBox(height: 24),
|
||||
_buildTemplateContentSection(),
|
||||
const SizedBox(height: 24),
|
||||
_buildSettingsSection(),
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
|
||||
const SizedBox(height: 24),
|
||||
_buildActionButtons(),
|
||||
],
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
Widget _buildBasicInfoSection() {
|
||||
return Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
const Text(
|
||||
'基本信息',
|
||||
style: TextStyle(fontSize: 16, fontWeight: FontWeight.bold),
|
||||
),
|
||||
const SizedBox(height: 16),
|
||||
|
||||
Row(
|
||||
children: [
|
||||
Expanded(
|
||||
flex: 2,
|
||||
child: TextFormField(
|
||||
controller: _nameController,
|
||||
decoration: const InputDecoration(
|
||||
labelText: '模板名称 *',
|
||||
hintText: '请输入模板名称',
|
||||
border: OutlineInputBorder(),
|
||||
),
|
||||
validator: (value) {
|
||||
if (value == null || value.trim().isEmpty) {
|
||||
return '请输入模板名称';
|
||||
}
|
||||
return null;
|
||||
},
|
||||
),
|
||||
),
|
||||
const SizedBox(width: 16),
|
||||
Expanded(
|
||||
flex: 1,
|
||||
child: TextFormField(
|
||||
controller: _versionController,
|
||||
decoration: const InputDecoration(
|
||||
labelText: '版本号 *',
|
||||
hintText: '1.0.0',
|
||||
border: OutlineInputBorder(),
|
||||
),
|
||||
validator: (value) {
|
||||
if (value == null || value.trim().isEmpty) {
|
||||
return '请输入版本号';
|
||||
}
|
||||
return null;
|
||||
},
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
const SizedBox(height: 16),
|
||||
|
||||
TextFormField(
|
||||
controller: _descriptionController,
|
||||
decoration: const InputDecoration(
|
||||
labelText: '模板描述',
|
||||
hintText: '请输入模板描述',
|
||||
border: OutlineInputBorder(),
|
||||
),
|
||||
maxLines: 2,
|
||||
),
|
||||
const SizedBox(height: 16),
|
||||
|
||||
Row(
|
||||
children: [
|
||||
Expanded(
|
||||
child: DropdownButtonFormField<String>(
|
||||
value: _selectedFeatureType,
|
||||
decoration: const InputDecoration(
|
||||
labelText: '功能类型 *',
|
||||
border: OutlineInputBorder(),
|
||||
),
|
||||
items: AIFeatureTypeHelper.allFeatures.map((t) {
|
||||
final api = t.toApiString();
|
||||
return DropdownMenuItem(
|
||||
value: api,
|
||||
child: Text(t.displayName),
|
||||
);
|
||||
}).toList(),
|
||||
onChanged: (value) {
|
||||
if (value != null) {
|
||||
setState(() {
|
||||
_selectedFeatureType = value;
|
||||
});
|
||||
}
|
||||
},
|
||||
),
|
||||
),
|
||||
const SizedBox(width: 16),
|
||||
Expanded(
|
||||
child: TextFormField(
|
||||
controller: _authorNameController,
|
||||
decoration: const InputDecoration(
|
||||
labelText: '作者名称',
|
||||
hintText: '请输入作者名称',
|
||||
border: OutlineInputBorder(),
|
||||
),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
const SizedBox(height: 16),
|
||||
|
||||
TextFormField(
|
||||
controller: _tagsController,
|
||||
decoration: const InputDecoration(
|
||||
labelText: '标签',
|
||||
hintText: '请输入标签,用逗号分隔',
|
||||
border: OutlineInputBorder(),
|
||||
),
|
||||
),
|
||||
],
|
||||
);
|
||||
}
|
||||
|
||||
Widget _buildTemplateContentSection() {
|
||||
return Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
const Text(
|
||||
'模板内容',
|
||||
style: TextStyle(fontSize: 16, fontWeight: FontWeight.bold),
|
||||
),
|
||||
const SizedBox(height: 16),
|
||||
|
||||
TextFormField(
|
||||
controller: _templateContentController,
|
||||
decoration: const InputDecoration(
|
||||
labelText: '模板内容 *',
|
||||
hintText: '请输入模板内容,支持变量占位符如 {{变量名}}',
|
||||
border: OutlineInputBorder(),
|
||||
alignLabelWithHint: true,
|
||||
),
|
||||
maxLines: 10,
|
||||
validator: (value) {
|
||||
if (value == null || value.trim().isEmpty) {
|
||||
return '请输入模板内容';
|
||||
}
|
||||
return null;
|
||||
},
|
||||
),
|
||||
const SizedBox(height: 8),
|
||||
Text(
|
||||
'提示:可以使用 {{变量名}} 作为占位符,用户使用时可以填入具体内容',
|
||||
style: TextStyle(
|
||||
fontSize: 12,
|
||||
color: Theme.of(context).colorScheme.onSurface.withOpacity(0.6),
|
||||
),
|
||||
),
|
||||
],
|
||||
);
|
||||
}
|
||||
|
||||
Widget _buildSettingsSection() {
|
||||
return Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
const Text(
|
||||
'设置选项',
|
||||
style: TextStyle(fontSize: 16, fontWeight: FontWeight.bold),
|
||||
),
|
||||
const SizedBox(height: 16),
|
||||
|
||||
CheckboxListTile(
|
||||
title: const Text('立即发布'),
|
||||
subtitle: const Text('创建后立即发布到公共模板库'),
|
||||
value: _isPublic,
|
||||
onChanged: (value) {
|
||||
setState(() {
|
||||
_isPublic = value ?? false;
|
||||
});
|
||||
},
|
||||
),
|
||||
|
||||
CheckboxListTile(
|
||||
title: const Text('设为认证'),
|
||||
subtitle: const Text('标记为官方认证模板'),
|
||||
value: _isVerified,
|
||||
onChanged: (value) {
|
||||
setState(() {
|
||||
_isVerified = value ?? false;
|
||||
});
|
||||
},
|
||||
),
|
||||
],
|
||||
);
|
||||
}
|
||||
|
||||
Widget _buildActionButtons() {
|
||||
return Row(
|
||||
mainAxisAlignment: MainAxisAlignment.end,
|
||||
children: [
|
||||
TextButton(
|
||||
onPressed: _isLoading ? null : () => Navigator.of(context).pop(),
|
||||
child: const Text('取消'),
|
||||
),
|
||||
const SizedBox(width: 12),
|
||||
ElevatedButton(
|
||||
onPressed: _isLoading ? null : _createOfficialTemplate,
|
||||
child: _isLoading
|
||||
? const SizedBox(
|
||||
width: 20,
|
||||
height: 20,
|
||||
child: CircularProgressIndicator(strokeWidth: 2),
|
||||
)
|
||||
: const Text('创建'),
|
||||
),
|
||||
],
|
||||
);
|
||||
}
|
||||
|
||||
Future<void> _createOfficialTemplate() async {
|
||||
if (!_formKey.currentState!.validate()) {
|
||||
return;
|
||||
}
|
||||
|
||||
setState(() {
|
||||
_isLoading = true;
|
||||
});
|
||||
|
||||
try {
|
||||
final tags = _tagsController.text
|
||||
.split(',')
|
||||
.map((tag) => tag.trim())
|
||||
.where((tag) => tag.isNotEmpty)
|
||||
.toList();
|
||||
|
||||
final now = DateTime.now();
|
||||
final template = PromptTemplate(
|
||||
id: '', // 将由后端生成
|
||||
name: _nameController.text.trim(),
|
||||
content: _templateContentController.text.trim(),
|
||||
featureType: _getFeatureTypeEnum(_selectedFeatureType),
|
||||
isPublic: _isPublic,
|
||||
isVerified: _isVerified,
|
||||
createdAt: now,
|
||||
updatedAt: now,
|
||||
description: _descriptionController.text.trim().isEmpty
|
||||
? null : _descriptionController.text.trim(),
|
||||
authorName: _authorNameController.text.trim().isEmpty
|
||||
? null : _authorNameController.text.trim(),
|
||||
templateTags: tags.isEmpty ? null : tags,
|
||||
);
|
||||
|
||||
await _adminRepository.createOfficialTemplate(template);
|
||||
|
||||
if (mounted) {
|
||||
Navigator.of(context).pop();
|
||||
ScaffoldMessenger.of(context).showSnackBar(
|
||||
SnackBar(content: Text('官方模板 "${template.name}" 创建成功')),
|
||||
);
|
||||
widget.onSuccess?.call();
|
||||
}
|
||||
} catch (e) {
|
||||
AppLogger.e('AddOfficialTemplateDialog', '创建官方模板失败', e);
|
||||
if (mounted) {
|
||||
ScaffoldMessenger.of(context).showSnackBar(
|
||||
SnackBar(content: Text('创建失败: $e')),
|
||||
);
|
||||
}
|
||||
} finally {
|
||||
if (mounted) {
|
||||
setState(() {
|
||||
_isLoading = false;
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
AIFeatureType _getFeatureTypeEnum(String featureType) {
|
||||
try {
|
||||
return AIFeatureTypeHelper.fromApiString(featureType.toUpperCase());
|
||||
} catch (_) {
|
||||
return AIFeatureType.aiChat;
|
||||
}
|
||||
}
|
||||
}
|
||||
722
AINoval/lib/screens/admin/widgets/add_public_model_dialog.dart
Normal file
722
AINoval/lib/screens/admin/widgets/add_public_model_dialog.dart
Normal file
@@ -0,0 +1,722 @@
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
import '../../../models/prompt_models.dart';
|
||||
import '../../../models/public_model_config.dart';
|
||||
import '../../../services/api_service/repositories/impl/admin_repository_impl.dart';
|
||||
import '../../../utils/logger.dart';
|
||||
import '../../../utils/web_theme.dart';
|
||||
import 'validation_results_dialog.dart';
|
||||
|
||||
/// 添加公共模型对话框 - 直接配置表单
|
||||
class AddPublicModelDialog extends StatefulWidget {
|
||||
const AddPublicModelDialog({
|
||||
super.key,
|
||||
required this.onSuccess,
|
||||
this.selectedProvider,
|
||||
this.sourceConfig,
|
||||
});
|
||||
|
||||
final VoidCallback onSuccess;
|
||||
final String? selectedProvider;
|
||||
final PublicModelConfigDetails? sourceConfig;
|
||||
|
||||
@override
|
||||
State<AddPublicModelDialog> createState() => _AddPublicModelDialogState();
|
||||
}
|
||||
|
||||
class _AddPublicModelDialogState extends State<AddPublicModelDialog> {
|
||||
final String _tag = 'AddPublicModelDialog';
|
||||
late final AdminRepositoryImpl _adminRepository;
|
||||
|
||||
// 表单数据
|
||||
final _formKey = GlobalKey<FormState>();
|
||||
final _providerController = TextEditingController();
|
||||
final _modelIdController = TextEditingController();
|
||||
final _displayNameController = TextEditingController();
|
||||
final _apiEndpointController = TextEditingController();
|
||||
final _apiKeysController = TextEditingController();
|
||||
final _keyNotesController = TextEditingController();
|
||||
final _descriptionController = TextEditingController();
|
||||
final _tagsController = TextEditingController();
|
||||
final _creditRateController = TextEditingController(text: '1.0');
|
||||
final _maxConcurrentController = TextEditingController(text: '-1');
|
||||
final _dailyLimitController = TextEditingController(text: '-1');
|
||||
final _hourlyLimitController = TextEditingController(text: '-1');
|
||||
final _priorityController = TextEditingController(text: '0');
|
||||
|
||||
final Set<AIFeatureType> _selectedFeatures = {AIFeatureType.aiChat};
|
||||
bool _enabled = true;
|
||||
bool _isLoading = false;
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
_adminRepository = AdminRepositoryImpl();
|
||||
|
||||
// 如果指定了提供商,预填充
|
||||
if (widget.selectedProvider != null) {
|
||||
_providerController.text = widget.selectedProvider!;
|
||||
}
|
||||
|
||||
// 如果有源配置,预填充数据(复制模式)
|
||||
if (widget.sourceConfig != null) {
|
||||
_initializeFromSource(widget.sourceConfig!);
|
||||
}
|
||||
}
|
||||
|
||||
void _initializeFromSource(PublicModelConfigDetails source) {
|
||||
// 基本信息 - 添加副本标识
|
||||
_providerController.text = source.provider;
|
||||
_modelIdController.text = '${source.modelId}-copy';
|
||||
_displayNameController.text = '${source.displayName ?? source.modelId} (副本)';
|
||||
_apiEndpointController.text = source.apiEndpoint ?? '';
|
||||
_enabled = source.enabled ?? true;
|
||||
|
||||
// 配置信息
|
||||
_descriptionController.text = source.description ?? '';
|
||||
_tagsController.text = source.tags?.join(', ') ?? '';
|
||||
_creditRateController.text = source.creditRateMultiplier?.toString() ?? '1.0';
|
||||
_maxConcurrentController.text = source.maxConcurrentRequests?.toString() ?? '-1';
|
||||
_dailyLimitController.text = source.dailyRequestLimit?.toString() ?? '-1';
|
||||
_hourlyLimitController.text = source.hourlyRequestLimit?.toString() ?? '-1';
|
||||
_priorityController.text = source.priority?.toString() ?? '0';
|
||||
|
||||
// 功能授权
|
||||
_selectedFeatures.clear();
|
||||
if (source.enabledForFeatures != null) {
|
||||
for (final featureStr in source.enabledForFeatures!) {
|
||||
final feature = AIFeatureTypeHelper.fromApiString(featureStr);
|
||||
_selectedFeatures.add(feature);
|
||||
}
|
||||
}
|
||||
|
||||
// 如果没有选中任何功能,默认选中AI聊天
|
||||
if (_selectedFeatures.isEmpty) {
|
||||
_selectedFeatures.add(AIFeatureType.aiChat);
|
||||
}
|
||||
|
||||
// 加载完整的配置信息包括API Keys
|
||||
_loadFullConfigWithApiKeys(source.id!);
|
||||
}
|
||||
|
||||
Future<void> _loadFullConfigWithApiKeys(String configId) async {
|
||||
try {
|
||||
final fullConfig = await _adminRepository.getPublicModelConfigById(configId);
|
||||
|
||||
if (mounted) {
|
||||
setState(() {
|
||||
// 复制实际的API Keys,每行一个
|
||||
if (fullConfig.apiKeyStatuses?.isNotEmpty == true) {
|
||||
final apiKeys = fullConfig.apiKeyStatuses!
|
||||
.map((status) => status.apiKey ?? '')
|
||||
.where((key) => key.isNotEmpty)
|
||||
.join('\n');
|
||||
_apiKeysController.text = apiKeys;
|
||||
_keyNotesController.text = fullConfig.apiKeyStatuses!
|
||||
.map((status) => status.note ?? '')
|
||||
.join('\n');
|
||||
} else {
|
||||
_apiKeysController.text = '';
|
||||
_keyNotesController.text = '';
|
||||
}
|
||||
});
|
||||
}
|
||||
} catch (e) {
|
||||
AppLogger.e(_tag, '加载源配置API Keys失败', e);
|
||||
if (mounted) {
|
||||
setState(() {
|
||||
// 如果加载失败,留空让用户重新配置
|
||||
_apiKeysController.text = '';
|
||||
_keyNotesController.text = '';
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
void dispose() {
|
||||
_providerController.dispose();
|
||||
_modelIdController.dispose();
|
||||
_displayNameController.dispose();
|
||||
_apiEndpointController.dispose();
|
||||
_apiKeysController.dispose();
|
||||
_keyNotesController.dispose();
|
||||
_descriptionController.dispose();
|
||||
_tagsController.dispose();
|
||||
_creditRateController.dispose();
|
||||
_maxConcurrentController.dispose();
|
||||
_dailyLimitController.dispose();
|
||||
_hourlyLimitController.dispose();
|
||||
_priorityController.dispose();
|
||||
super.dispose();
|
||||
}
|
||||
|
||||
/// 保存配置
|
||||
Future<void> _saveConfig({required bool validate}) async {
|
||||
if (!_formKey.currentState!.validate()) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (_providerController.text.isEmpty) {
|
||||
_showSnackBar('请输入提供商', isError: true);
|
||||
return;
|
||||
}
|
||||
|
||||
if (_modelIdController.text.isEmpty) {
|
||||
_showSnackBar('请输入模型ID', isError: true);
|
||||
return;
|
||||
}
|
||||
|
||||
if (_apiKeysController.text.trim().isEmpty) {
|
||||
_showSnackBar('请至少输入一个API Key', isError: true);
|
||||
return;
|
||||
}
|
||||
|
||||
setState(() {
|
||||
_isLoading = true;
|
||||
});
|
||||
|
||||
try {
|
||||
// 解析API Keys
|
||||
final apiKeyLines = _apiKeysController.text.split('\n').where((line) => line.trim().isNotEmpty).toList();
|
||||
final noteLines = _keyNotesController.text.split('\n');
|
||||
|
||||
final apiKeys = <ApiKeyRequest>[];
|
||||
for (int i = 0; i < apiKeyLines.length; i++) {
|
||||
final note = i < noteLines.length ? noteLines[i].trim() : '';
|
||||
apiKeys.add(ApiKeyRequest(
|
||||
apiKey: apiKeyLines[i].trim(),
|
||||
note: note.isEmpty ? null : note,
|
||||
));
|
||||
}
|
||||
|
||||
// 解析标签
|
||||
final tags = _tagsController.text.split(',').map((tag) => tag.trim()).where((tag) => tag.isNotEmpty).toList();
|
||||
|
||||
// 使用扩展方法转换功能类型枚举为字符串
|
||||
final enabledFeaturesStrings = AIFeatureTypeHelper.toApiStringList(_selectedFeatures);
|
||||
|
||||
final request = PublicModelConfigRequest(
|
||||
provider: _providerController.text,
|
||||
modelId: _modelIdController.text,
|
||||
displayName: _displayNameController.text.isEmpty ? null : _displayNameController.text,
|
||||
enabled: _enabled,
|
||||
apiKeys: apiKeys,
|
||||
apiEndpoint: _apiEndpointController.text.isEmpty ? null : _apiEndpointController.text,
|
||||
enabledForFeatures: enabledFeaturesStrings,
|
||||
creditRateMultiplier: double.tryParse(_creditRateController.text),
|
||||
maxConcurrentRequests: int.tryParse(_maxConcurrentController.text),
|
||||
dailyRequestLimit: int.tryParse(_dailyLimitController.text),
|
||||
hourlyRequestLimit: int.tryParse(_hourlyLimitController.text),
|
||||
priority: int.tryParse(_priorityController.text),
|
||||
description: _descriptionController.text.isEmpty ? null : _descriptionController.text,
|
||||
tags: tags,
|
||||
);
|
||||
|
||||
// 调用API创建配置,传递验证参数
|
||||
final result = await _adminRepository.createPublicModelConfig(request, validate: validate);
|
||||
|
||||
AppLogger.i(_tag, validate ? '✅ 创建并验证模型配置成功' : '✅ 创建模型配置成功');
|
||||
|
||||
if (validate) {
|
||||
// 拉取包含Key的明细并弹出结果
|
||||
try {
|
||||
final withKeys = await _adminRepository.getPublicModelConfigById(result.id!);
|
||||
if (mounted) {
|
||||
showDialog(
|
||||
context: context,
|
||||
builder: (context) => ValidationResultsDialog(config: withKeys),
|
||||
);
|
||||
}
|
||||
} catch (_) {}
|
||||
} else {
|
||||
_showSnackBar('模型配置创建成功!', isError: false);
|
||||
}
|
||||
|
||||
widget.onSuccess();
|
||||
if (mounted) {
|
||||
Navigator.of(context).pop();
|
||||
}
|
||||
} catch (e) {
|
||||
AppLogger.e(_tag, validate ? '创建并验证模型配置失败' : '创建模型配置失败', e);
|
||||
if (mounted) {
|
||||
_showSnackBar(validate ? '创建并验证失败: ${e.toString()}' : '创建失败: ${e.toString()}', isError: true);
|
||||
}
|
||||
} finally {
|
||||
if (mounted) {
|
||||
setState(() {
|
||||
_isLoading = false;
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Dialog(
|
||||
backgroundColor: WebTheme.getCardColor(context),
|
||||
child: Container(
|
||||
width: 900,
|
||||
height: 700,
|
||||
padding: const EdgeInsets.all(24),
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
// 头部
|
||||
Row(
|
||||
children: [
|
||||
Text(
|
||||
widget.sourceConfig != null ? '复制公共模型配置' : '添加公共模型配置',
|
||||
style: TextStyle(
|
||||
fontSize: 20,
|
||||
fontWeight: FontWeight.w600,
|
||||
color: WebTheme.getTextColor(context),
|
||||
),
|
||||
),
|
||||
if (widget.sourceConfig != null) ...[
|
||||
const SizedBox(width: 8),
|
||||
Container(
|
||||
padding: const EdgeInsets.symmetric(horizontal: 8, vertical: 4),
|
||||
decoration: BoxDecoration(
|
||||
color: Colors.orange.withOpacity(0.1),
|
||||
borderRadius: BorderRadius.circular(12),
|
||||
border: Border.all(color: Colors.orange.withOpacity(0.3)),
|
||||
),
|
||||
child: Text(
|
||||
'基于: ${widget.sourceConfig!.displayName ?? widget.sourceConfig!.modelId}',
|
||||
style: const TextStyle(
|
||||
fontSize: 12,
|
||||
color: Colors.orange,
|
||||
fontWeight: FontWeight.w500,
|
||||
),
|
||||
),
|
||||
),
|
||||
],
|
||||
const Spacer(),
|
||||
IconButton(
|
||||
onPressed: () => Navigator.of(context).pop(),
|
||||
icon: Icon(Icons.close, color: WebTheme.getTextColor(context)),
|
||||
),
|
||||
],
|
||||
),
|
||||
|
||||
const SizedBox(height: 16),
|
||||
|
||||
// 表单内容
|
||||
Expanded(
|
||||
child: Form(
|
||||
key: _formKey,
|
||||
child: SingleChildScrollView(
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
// 基本信息 - 两列布局
|
||||
_buildSectionTitle('基本信息'),
|
||||
Row(
|
||||
children: [
|
||||
Expanded(
|
||||
child: _buildTextField(
|
||||
controller: _providerController,
|
||||
label: '提供商 *',
|
||||
hint: '如: openai, anthropic',
|
||||
validator: (value) => value?.trim().isEmpty == true ? '请输入提供商名称' : null,
|
||||
),
|
||||
),
|
||||
const SizedBox(width: 12),
|
||||
Expanded(
|
||||
child: _buildTextField(
|
||||
controller: _modelIdController,
|
||||
label: '模型ID *',
|
||||
hint: '如: gpt-4, claude-3-opus',
|
||||
validator: (value) => value?.trim().isEmpty == true ? '请输入模型ID' : null,
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
|
||||
const SizedBox(height: 12),
|
||||
|
||||
Row(
|
||||
children: [
|
||||
Expanded(
|
||||
child: _buildTextField(
|
||||
controller: _displayNameController,
|
||||
label: '显示名称 *',
|
||||
hint: '用户界面显示的名称',
|
||||
validator: (value) => value?.trim().isEmpty == true ? '请输入显示名称' : null,
|
||||
),
|
||||
),
|
||||
const SizedBox(width: 12),
|
||||
Expanded(
|
||||
child: _buildTextField(
|
||||
controller: _apiEndpointController,
|
||||
label: 'API Endpoint',
|
||||
hint: '可选,自定义API地址',
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
|
||||
const SizedBox(height: 16),
|
||||
|
||||
// API Keys配置
|
||||
_buildSectionTitle('API Keys配置'),
|
||||
Row(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
Expanded(
|
||||
child: _buildTextField(
|
||||
controller: _apiKeysController,
|
||||
label: 'API Keys *',
|
||||
hint: '每行一个API Key',
|
||||
maxLines: 3,
|
||||
validator: (value) => value?.trim().isEmpty == true ? '请至少输入一个API Key' : null,
|
||||
),
|
||||
),
|
||||
const SizedBox(width: 12),
|
||||
Expanded(
|
||||
child: _buildTextField(
|
||||
controller: _keyNotesController,
|
||||
label: 'Key备注',
|
||||
hint: '每行一个备注(可选)',
|
||||
maxLines: 3,
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
|
||||
const SizedBox(height: 16),
|
||||
|
||||
// 功能授权
|
||||
_buildSectionTitle('功能授权'),
|
||||
_buildFeatureSelection(),
|
||||
|
||||
const SizedBox(height: 16),
|
||||
|
||||
// 限制配置 - 三列布局
|
||||
_buildSectionTitle('限制配置'),
|
||||
Row(
|
||||
children: [
|
||||
Expanded(
|
||||
child: _buildTextField(
|
||||
controller: _creditRateController,
|
||||
label: '积分倍数',
|
||||
hint: '默认 1.0',
|
||||
keyboardType: TextInputType.number,
|
||||
validator: (value) {
|
||||
if (value?.isNotEmpty == true) {
|
||||
final parsed = double.tryParse(value!);
|
||||
if (parsed == null || parsed <= 0) return '请输入大于0的数字';
|
||||
}
|
||||
return null;
|
||||
},
|
||||
),
|
||||
),
|
||||
const SizedBox(width: 12),
|
||||
Expanded(
|
||||
child: _buildTextField(
|
||||
controller: _maxConcurrentController,
|
||||
label: '最大并发',
|
||||
hint: '-1表示无限制',
|
||||
keyboardType: TextInputType.number,
|
||||
),
|
||||
),
|
||||
const SizedBox(width: 12),
|
||||
Expanded(
|
||||
child: _buildTextField(
|
||||
controller: _priorityController,
|
||||
label: '优先级',
|
||||
hint: '数字越大优先级越高',
|
||||
keyboardType: TextInputType.number,
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
|
||||
const SizedBox(height: 12),
|
||||
|
||||
Row(
|
||||
children: [
|
||||
Expanded(
|
||||
child: _buildTextField(
|
||||
controller: _dailyLimitController,
|
||||
label: '每日请求限制',
|
||||
hint: '-1表示无限制',
|
||||
keyboardType: TextInputType.number,
|
||||
),
|
||||
),
|
||||
const SizedBox(width: 12),
|
||||
Expanded(
|
||||
child: _buildTextField(
|
||||
controller: _hourlyLimitController,
|
||||
label: '每小时请求限制',
|
||||
hint: '-1表示无限制',
|
||||
keyboardType: TextInputType.number,
|
||||
),
|
||||
),
|
||||
const SizedBox(width: 12),
|
||||
// 启用状态开关
|
||||
Expanded(
|
||||
child: SwitchListTile(
|
||||
title: Text(
|
||||
'启用状态',
|
||||
style: TextStyle(
|
||||
fontSize: 13,
|
||||
color: WebTheme.getTextColor(context),
|
||||
),
|
||||
),
|
||||
value: _enabled,
|
||||
onChanged: (value) => setState(() => _enabled = value),
|
||||
activeColor: Colors.green,
|
||||
contentPadding: EdgeInsets.zero,
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
|
||||
const SizedBox(height: 16),
|
||||
|
||||
// 其他信息
|
||||
_buildSectionTitle('其他信息'),
|
||||
_buildTextField(
|
||||
controller: _descriptionController,
|
||||
label: '描述',
|
||||
hint: '模型用途、特点等描述信息',
|
||||
maxLines: 2,
|
||||
),
|
||||
|
||||
const SizedBox(height: 12),
|
||||
|
||||
_buildTextField(
|
||||
controller: _tagsController,
|
||||
label: '标签',
|
||||
hint: '用逗号分隔,如: 高性能,推荐,beta',
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
|
||||
const SizedBox(height: 16),
|
||||
|
||||
// 底部按钮
|
||||
Row(
|
||||
mainAxisAlignment: MainAxisAlignment.end,
|
||||
children: [
|
||||
TextButton(
|
||||
onPressed: _isLoading ? null : () => Navigator.of(context).pop(),
|
||||
child: Text(
|
||||
'取消',
|
||||
style: TextStyle(color: WebTheme.getSecondaryTextColor(context)),
|
||||
),
|
||||
),
|
||||
const SizedBox(width: 12),
|
||||
ElevatedButton(
|
||||
onPressed: _isLoading ? null : () => _saveConfig(validate: false),
|
||||
style: ElevatedButton.styleFrom(
|
||||
backgroundColor: WebTheme.getSecondaryTextColor(context),
|
||||
foregroundColor: WebTheme.getBackgroundColor(context),
|
||||
),
|
||||
child: _isLoading
|
||||
? const SizedBox(
|
||||
width: 16,
|
||||
height: 16,
|
||||
child: CircularProgressIndicator(strokeWidth: 2),
|
||||
)
|
||||
: const Text('仅保存'),
|
||||
),
|
||||
const SizedBox(width: 8),
|
||||
ElevatedButton(
|
||||
onPressed: _isLoading ? null : () => _saveConfig(validate: true),
|
||||
style: ElevatedButton.styleFrom(
|
||||
backgroundColor: Colors.blue,
|
||||
foregroundColor: Colors.white,
|
||||
),
|
||||
child: _isLoading
|
||||
? const SizedBox(
|
||||
width: 16,
|
||||
height: 16,
|
||||
child: CircularProgressIndicator(strokeWidth: 2, color: Colors.white),
|
||||
)
|
||||
: const Text('保存并验证'),
|
||||
),
|
||||
],
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
Widget _buildSectionTitle(String title) {
|
||||
return Padding(
|
||||
padding: const EdgeInsets.only(bottom: 8),
|
||||
child: Text(
|
||||
title,
|
||||
style: TextStyle(
|
||||
fontSize: 14,
|
||||
fontWeight: FontWeight.w600,
|
||||
color: WebTheme.getTextColor(context),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
Widget _buildTextField({
|
||||
required TextEditingController controller,
|
||||
required String label,
|
||||
String? hint,
|
||||
int maxLines = 1,
|
||||
TextInputType? keyboardType,
|
||||
String? Function(String?)? validator,
|
||||
}) {
|
||||
return TextFormField(
|
||||
controller: controller,
|
||||
style: TextStyle(color: WebTheme.getTextColor(context), fontSize: 13),
|
||||
decoration: InputDecoration(
|
||||
labelText: label,
|
||||
hintText: hint,
|
||||
labelStyle: TextStyle(
|
||||
color: WebTheme.getSecondaryTextColor(context),
|
||||
fontSize: 12,
|
||||
),
|
||||
hintStyle: TextStyle(
|
||||
color: WebTheme.getSecondaryTextColor(context).withValues(alpha: 0.7),
|
||||
fontSize: 12,
|
||||
),
|
||||
border: OutlineInputBorder(
|
||||
borderRadius: BorderRadius.circular(6),
|
||||
borderSide: BorderSide(
|
||||
color: WebTheme.getBorderColor(context),
|
||||
width: 1,
|
||||
),
|
||||
),
|
||||
enabledBorder: OutlineInputBorder(
|
||||
borderRadius: BorderRadius.circular(6),
|
||||
borderSide: BorderSide(
|
||||
color: WebTheme.getBorderColor(context),
|
||||
width: 1,
|
||||
),
|
||||
),
|
||||
focusedBorder: OutlineInputBorder(
|
||||
borderRadius: BorderRadius.circular(6),
|
||||
borderSide: const BorderSide(
|
||||
color: Colors.blue,
|
||||
width: 1.5,
|
||||
),
|
||||
),
|
||||
contentPadding: const EdgeInsets.symmetric(horizontal: 12, vertical: 8),
|
||||
filled: true,
|
||||
fillColor: WebTheme.getBackgroundColor(context),
|
||||
isDense: true,
|
||||
),
|
||||
maxLines: maxLines,
|
||||
keyboardType: keyboardType,
|
||||
validator: validator,
|
||||
);
|
||||
}
|
||||
|
||||
Widget _buildFeatureSelection() {
|
||||
return Container(
|
||||
padding: const EdgeInsets.all(12),
|
||||
decoration: BoxDecoration(
|
||||
border: Border.all(color: WebTheme.getBorderColor(context)),
|
||||
borderRadius: BorderRadius.circular(6),
|
||||
color: WebTheme.getBackgroundColor(context),
|
||||
),
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
Text(
|
||||
'选择授权功能 (至少选择一个)',
|
||||
style: TextStyle(
|
||||
fontSize: 12,
|
||||
color: WebTheme.getSecondaryTextColor(context),
|
||||
),
|
||||
),
|
||||
const SizedBox(height: 8),
|
||||
Wrap(
|
||||
spacing: 8,
|
||||
runSpacing: 4,
|
||||
children: AIFeatureTypeHelper.allFeatures.map((featureType) {
|
||||
final bool isSelected = _selectedFeatures.contains(featureType);
|
||||
return FilterChip(
|
||||
label: Text(
|
||||
featureType.displayName,
|
||||
style: TextStyle(
|
||||
fontSize: 12,
|
||||
color: isSelected ? Colors.white : WebTheme.getTextColor(context),
|
||||
),
|
||||
),
|
||||
tooltip: _getFeatureDescription(featureType),
|
||||
selected: isSelected,
|
||||
onSelected: (selected) {
|
||||
setState(() {
|
||||
if (selected) {
|
||||
_selectedFeatures.add(featureType);
|
||||
} else {
|
||||
_selectedFeatures.remove(featureType);
|
||||
}
|
||||
});
|
||||
},
|
||||
selectedColor: Colors.blue,
|
||||
backgroundColor: WebTheme.getCardColor(context),
|
||||
showCheckmark: false,
|
||||
materialTapTargetSize: MaterialTapTargetSize.shrinkWrap,
|
||||
visualDensity: VisualDensity.compact,
|
||||
);
|
||||
}).toList(),
|
||||
),
|
||||
if (_selectedFeatures.isEmpty)
|
||||
const Padding(
|
||||
padding: EdgeInsets.only(top: 4),
|
||||
child: Text(
|
||||
'请至少选择一个功能',
|
||||
style: TextStyle(
|
||||
fontSize: 11,
|
||||
color: Colors.red,
|
||||
),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
String _getFeatureDescription(AIFeatureType type) {
|
||||
switch (type) {
|
||||
case AIFeatureType.aiChat:
|
||||
return 'AI对话功能';
|
||||
case AIFeatureType.textExpansion:
|
||||
return '文本内容扩展';
|
||||
case AIFeatureType.textRefactor:
|
||||
return '文本结构重构';
|
||||
case AIFeatureType.textSummary:
|
||||
return '文本内容总结';
|
||||
case AIFeatureType.sceneToSummary:
|
||||
return '场景生成摘要';
|
||||
case AIFeatureType.summaryToScene:
|
||||
return '摘要生成场景';
|
||||
case AIFeatureType.novelGeneration:
|
||||
return '小说内容生成';
|
||||
case AIFeatureType.professionalFictionContinuation:
|
||||
return '专业小说续写';
|
||||
case AIFeatureType.sceneBeatGeneration:
|
||||
return '场景节拍生成';
|
||||
case AIFeatureType.novelCompose:
|
||||
return '设定编排(大纲/章节/组合)';
|
||||
case AIFeatureType.settingTreeGeneration:
|
||||
return '设定树生成';
|
||||
}
|
||||
}
|
||||
|
||||
void _showSnackBar(String message, {bool isError = false}) {
|
||||
ScaffoldMessenger.of(context).showSnackBar(
|
||||
SnackBar(
|
||||
content: Text(message),
|
||||
backgroundColor: isError ? Colors.red : Colors.green,
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
439
AINoval/lib/screens/admin/widgets/add_system_preset_dialog.dart
Normal file
439
AINoval/lib/screens/admin/widgets/add_system_preset_dialog.dart
Normal file
@@ -0,0 +1,439 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'dart:convert';
|
||||
import 'package:crypto/crypto.dart';
|
||||
|
||||
import '../../../models/preset_models.dart';
|
||||
import '../../../models/prompt_models.dart';
|
||||
import '../../../models/context_selection_models.dart';
|
||||
import '../../../models/ai_request_models.dart';
|
||||
import '../../../services/api_service/repositories/impl/admin_repository_impl.dart';
|
||||
import '../../../utils/logger.dart';
|
||||
import '../../../widgets/common/form_dialog_template.dart';
|
||||
|
||||
/// 添加系统预设对话框
|
||||
class AddSystemPresetDialog extends StatefulWidget {
|
||||
final VoidCallback? onSuccess;
|
||||
|
||||
const AddSystemPresetDialog({
|
||||
Key? key,
|
||||
this.onSuccess,
|
||||
}) : super(key: key);
|
||||
|
||||
@override
|
||||
State<AddSystemPresetDialog> createState() => _AddSystemPresetDialogState();
|
||||
}
|
||||
|
||||
class _AddSystemPresetDialogState extends State<AddSystemPresetDialog> {
|
||||
final _formKey = GlobalKey<FormState>();
|
||||
final _nameController = TextEditingController();
|
||||
final _descriptionController = TextEditingController();
|
||||
final _systemPromptController = TextEditingController();
|
||||
final _userPromptController = TextEditingController();
|
||||
final _tagsController = TextEditingController();
|
||||
|
||||
String _selectedFeatureType = 'AI_CHAT';
|
||||
bool _showInQuickAccess = false;
|
||||
bool _enableSmartContext = true;
|
||||
double _temperature = 0.7;
|
||||
double _topP = 0.9;
|
||||
String? _selectedTemplateId;
|
||||
late ContextSelectionData _contextSelectionData;
|
||||
bool _isLoading = false;
|
||||
|
||||
final AdminRepositoryImpl _adminRepository = AdminRepositoryImpl();
|
||||
|
||||
// 功能类型由 AIFeatureTypeHelper.allFeatures 动态提供
|
||||
|
||||
@override
|
||||
void dispose() {
|
||||
_nameController.dispose();
|
||||
_descriptionController.dispose();
|
||||
_systemPromptController.dispose();
|
||||
_userPromptController.dispose();
|
||||
_tagsController.dispose();
|
||||
super.dispose();
|
||||
}
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
_contextSelectionData = FormFieldFactory.createPresetTemplateContextData();
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Dialog(
|
||||
child: Container(
|
||||
width: 600,
|
||||
constraints: const BoxConstraints(maxHeight: 700),
|
||||
padding: const EdgeInsets.all(24),
|
||||
child: Column(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
Row(
|
||||
children: [
|
||||
const Icon(Icons.smart_button, size: 24),
|
||||
const SizedBox(width: 8),
|
||||
const Text(
|
||||
'添加系统预设',
|
||||
style: TextStyle(fontSize: 20, fontWeight: FontWeight.bold),
|
||||
),
|
||||
const Spacer(),
|
||||
IconButton(
|
||||
onPressed: () => Navigator.of(context).pop(),
|
||||
icon: const Icon(Icons.close),
|
||||
),
|
||||
],
|
||||
),
|
||||
const SizedBox(height: 24),
|
||||
|
||||
Expanded(
|
||||
child: Form(
|
||||
key: _formKey,
|
||||
child: SingleChildScrollView(
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
_buildBasicInfoSection(),
|
||||
const SizedBox(height: 24),
|
||||
_buildPromptSection(),
|
||||
const SizedBox(height: 24),
|
||||
_buildSettingsSection(),
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
|
||||
const SizedBox(height: 24),
|
||||
_buildActionButtons(),
|
||||
],
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
Widget _buildBasicInfoSection() {
|
||||
return Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
const Text(
|
||||
'基本信息',
|
||||
style: TextStyle(fontSize: 16, fontWeight: FontWeight.bold),
|
||||
),
|
||||
const SizedBox(height: 16),
|
||||
|
||||
TextFormField(
|
||||
controller: _nameController,
|
||||
decoration: const InputDecoration(
|
||||
labelText: '预设名称 *',
|
||||
hintText: '请输入预设名称',
|
||||
border: OutlineInputBorder(),
|
||||
),
|
||||
validator: (value) {
|
||||
if (value == null || value.trim().isEmpty) {
|
||||
return '请输入预设名称';
|
||||
}
|
||||
return null;
|
||||
},
|
||||
),
|
||||
const SizedBox(height: 16),
|
||||
|
||||
TextFormField(
|
||||
controller: _descriptionController,
|
||||
decoration: const InputDecoration(
|
||||
labelText: '预设描述',
|
||||
hintText: '请输入预设描述',
|
||||
border: OutlineInputBorder(),
|
||||
),
|
||||
maxLines: 2,
|
||||
),
|
||||
const SizedBox(height: 16),
|
||||
|
||||
DropdownButtonFormField<String>(
|
||||
value: _selectedFeatureType,
|
||||
decoration: const InputDecoration(
|
||||
labelText: '功能类型 *',
|
||||
border: OutlineInputBorder(),
|
||||
),
|
||||
items: AIFeatureTypeHelper.allFeatures.map((t) {
|
||||
final api = t.toApiString();
|
||||
return DropdownMenuItem(
|
||||
value: api,
|
||||
child: Text(t.displayName),
|
||||
);
|
||||
}).toList(),
|
||||
onChanged: (value) {
|
||||
if (value != null) {
|
||||
setState(() {
|
||||
_selectedFeatureType = value;
|
||||
});
|
||||
}
|
||||
},
|
||||
),
|
||||
const SizedBox(height: 16),
|
||||
|
||||
TextFormField(
|
||||
controller: _tagsController,
|
||||
decoration: const InputDecoration(
|
||||
labelText: '标签',
|
||||
hintText: '请输入标签,用逗号分隔',
|
||||
border: OutlineInputBorder(),
|
||||
),
|
||||
),
|
||||
],
|
||||
);
|
||||
}
|
||||
|
||||
Widget _buildPromptSection() {
|
||||
return Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
const Text(
|
||||
'提示词配置',
|
||||
style: TextStyle(fontSize: 16, fontWeight: FontWeight.bold),
|
||||
),
|
||||
const SizedBox(height: 16),
|
||||
|
||||
TextFormField(
|
||||
controller: _systemPromptController,
|
||||
decoration: const InputDecoration(
|
||||
labelText: '系统提示词 *',
|
||||
hintText: '请输入系统提示词',
|
||||
border: OutlineInputBorder(),
|
||||
),
|
||||
maxLines: 5,
|
||||
validator: (value) {
|
||||
if (value == null || value.trim().isEmpty) {
|
||||
return '请输入系统提示词';
|
||||
}
|
||||
return null;
|
||||
},
|
||||
),
|
||||
const SizedBox(height: 16),
|
||||
|
||||
TextFormField(
|
||||
controller: _userPromptController,
|
||||
decoration: const InputDecoration(
|
||||
labelText: '用户提示词',
|
||||
hintText: '请输入用户提示词',
|
||||
border: OutlineInputBorder(),
|
||||
),
|
||||
maxLines: 3,
|
||||
),
|
||||
],
|
||||
);
|
||||
}
|
||||
|
||||
Widget _buildSettingsSection() {
|
||||
return Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
const Text(
|
||||
'设置选项',
|
||||
style: TextStyle(fontSize: 16, fontWeight: FontWeight.bold),
|
||||
),
|
||||
const SizedBox(height: 16),
|
||||
|
||||
CheckboxListTile(
|
||||
title: const Text('显示在快捷访问'),
|
||||
subtitle: const Text('用户可以在快捷访问列表中看到此预设'),
|
||||
value: _showInQuickAccess,
|
||||
onChanged: (value) {
|
||||
setState(() {
|
||||
_showInQuickAccess = value ?? false;
|
||||
});
|
||||
},
|
||||
),
|
||||
|
||||
const SizedBox(height: 8),
|
||||
CheckboxListTile(
|
||||
title: const Text('启用智能上下文'),
|
||||
value: _enableSmartContext,
|
||||
onChanged: (v) {
|
||||
setState(() {
|
||||
_enableSmartContext = v ?? true;
|
||||
});
|
||||
},
|
||||
),
|
||||
|
||||
const SizedBox(height: 8),
|
||||
// 温度
|
||||
FormFieldFactory.createTemperatureSliderField(
|
||||
context: context,
|
||||
value: _temperature,
|
||||
onChanged: (v) => setState(() => _temperature = v),
|
||||
),
|
||||
|
||||
const SizedBox(height: 8),
|
||||
// Top-P
|
||||
FormFieldFactory.createTopPSliderField(
|
||||
context: context,
|
||||
value: _topP,
|
||||
onChanged: (v) => setState(() => _topP = v),
|
||||
),
|
||||
|
||||
const SizedBox(height: 8),
|
||||
// 上下文选择
|
||||
FormFieldFactory.createContextSelectionField(
|
||||
contextData: _contextSelectionData,
|
||||
onSelectionChanged: (d) => setState(() => _contextSelectionData = d),
|
||||
title: '上下文选择',
|
||||
description: '选择参与提示词生成的上下文信息',
|
||||
),
|
||||
],
|
||||
);
|
||||
}
|
||||
|
||||
Widget _buildActionButtons() {
|
||||
return Row(
|
||||
mainAxisAlignment: MainAxisAlignment.end,
|
||||
children: [
|
||||
TextButton(
|
||||
onPressed: _isLoading ? null : () => Navigator.of(context).pop(),
|
||||
child: const Text('取消'),
|
||||
),
|
||||
const SizedBox(width: 12),
|
||||
ElevatedButton(
|
||||
onPressed: _isLoading ? null : _createPreset,
|
||||
child: _isLoading
|
||||
? const SizedBox(
|
||||
width: 20,
|
||||
height: 20,
|
||||
child: CircularProgressIndicator(strokeWidth: 2),
|
||||
)
|
||||
: const Text('创建'),
|
||||
),
|
||||
],
|
||||
);
|
||||
}
|
||||
|
||||
Future<void> _createPreset() async {
|
||||
if (!_formKey.currentState!.validate()) {
|
||||
return;
|
||||
}
|
||||
|
||||
setState(() {
|
||||
_isLoading = true;
|
||||
});
|
||||
|
||||
try {
|
||||
final tags = _tagsController.text
|
||||
.split(',')
|
||||
.map((tag) => tag.trim())
|
||||
.where((tag) => tag.isNotEmpty)
|
||||
.toList();
|
||||
|
||||
final now = DateTime.now();
|
||||
// 构建 requestData 与哈希
|
||||
final requestJson = _buildRequestDataJson();
|
||||
final newHash = _generatePresetHash(requestJson);
|
||||
|
||||
final preset = AIPromptPreset(
|
||||
presetId: '',
|
||||
userId: 'system',
|
||||
presetName: _nameController.text.trim(),
|
||||
presetDescription: _descriptionController.text.trim().isNotEmpty
|
||||
? _descriptionController.text.trim()
|
||||
: null,
|
||||
aiFeatureType: _selectedFeatureType,
|
||||
systemPrompt: _systemPromptController.text.trim(),
|
||||
userPrompt: _userPromptController.text.trim(),
|
||||
presetTags: tags.isEmpty ? null : tags,
|
||||
presetHash: newHash,
|
||||
requestData: requestJson,
|
||||
isSystem: true,
|
||||
createdAt: now,
|
||||
updatedAt: now,
|
||||
showInQuickAccess: _showInQuickAccess,
|
||||
isFavorite: false,
|
||||
isPublic: false,
|
||||
useCount: 0,
|
||||
);
|
||||
|
||||
await _adminRepository.createSystemPreset(preset);
|
||||
|
||||
if (mounted) {
|
||||
Navigator.of(context).pop();
|
||||
ScaffoldMessenger.of(context).showSnackBar(
|
||||
SnackBar(content: Text('系统预设 "${preset.presetName}" 创建成功')),
|
||||
);
|
||||
widget.onSuccess?.call();
|
||||
}
|
||||
} catch (e) {
|
||||
AppLogger.e('AddSystemPresetDialog', '创建系统预设失败', e);
|
||||
if (mounted) {
|
||||
ScaffoldMessenger.of(context).showSnackBar(
|
||||
SnackBar(content: Text('创建失败: $e')),
|
||||
);
|
||||
}
|
||||
} finally {
|
||||
if (mounted) {
|
||||
setState(() {
|
||||
_isLoading = false;
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
String _buildRequestDataJson() {
|
||||
final reqType = _mapFeatureTypeToRequestType(_selectedFeatureType);
|
||||
final request = UniversalAIRequest(
|
||||
requestType: reqType,
|
||||
userId: 'system',
|
||||
novelId: _contextSelectionData.novelId,
|
||||
instructions: _userPromptController.text.trim().isNotEmpty
|
||||
? _userPromptController.text.trim()
|
||||
: null,
|
||||
contextSelections: _contextSelectionData,
|
||||
enableSmartContext: _enableSmartContext,
|
||||
parameters: {
|
||||
'enableSmartContext': _enableSmartContext,
|
||||
'temperature': _temperature,
|
||||
'topP': _topP,
|
||||
if (_selectedTemplateId != null) 'promptTemplateId': _selectedTemplateId,
|
||||
},
|
||||
metadata: {
|
||||
'source': 'admin_system_preset_creator',
|
||||
},
|
||||
);
|
||||
return jsonEncode(request.toApiJson());
|
||||
}
|
||||
|
||||
AIRequestType _mapFeatureTypeToRequestType(String featureType) {
|
||||
try {
|
||||
final ft = AIFeatureTypeHelper.fromApiString(featureType.toUpperCase());
|
||||
switch (ft) {
|
||||
case AIFeatureType.textExpansion:
|
||||
return AIRequestType.expansion;
|
||||
case AIFeatureType.textSummary:
|
||||
return AIRequestType.summary;
|
||||
case AIFeatureType.textRefactor:
|
||||
return AIRequestType.refactor;
|
||||
case AIFeatureType.aiChat:
|
||||
return AIRequestType.chat;
|
||||
case AIFeatureType.sceneToSummary:
|
||||
return AIRequestType.sceneSummary;
|
||||
case AIFeatureType.novelGeneration:
|
||||
return AIRequestType.generation;
|
||||
case AIFeatureType.novelCompose:
|
||||
return AIRequestType.novelCompose;
|
||||
default:
|
||||
return AIRequestType.expansion;
|
||||
}
|
||||
} catch (_) {
|
||||
return AIRequestType.expansion;
|
||||
}
|
||||
}
|
||||
|
||||
String _generatePresetHash(String requestDataJson) {
|
||||
try {
|
||||
final bytes = utf8.encode(requestDataJson);
|
||||
final digest = sha256.convert(bytes);
|
||||
return digest.toString();
|
||||
} catch (_) {
|
||||
return DateTime.now().millisecondsSinceEpoch.toString();
|
||||
}
|
||||
}
|
||||
}
|
||||
102
AINoval/lib/screens/admin/widgets/admin_data_table.dart
Normal file
102
AINoval/lib/screens/admin/widgets/admin_data_table.dart
Normal file
@@ -0,0 +1,102 @@
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
class AdminDataTable extends StatelessWidget {
|
||||
final String title;
|
||||
final List<String> headers;
|
||||
final List<List<String>> rows;
|
||||
final List<VoidCallback>? actions;
|
||||
final List<String>? actionLabels;
|
||||
|
||||
const AdminDataTable({
|
||||
super.key,
|
||||
required this.title,
|
||||
required this.headers,
|
||||
required this.rows,
|
||||
this.actions,
|
||||
this.actionLabels,
|
||||
});
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Card(
|
||||
elevation: 4,
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
// 标题栏
|
||||
Container(
|
||||
padding: const EdgeInsets.all(16),
|
||||
decoration: BoxDecoration(
|
||||
color: Theme.of(context).colorScheme.primary.withOpacity(0.1),
|
||||
borderRadius: const BorderRadius.only(
|
||||
topLeft: Radius.circular(8),
|
||||
topRight: Radius.circular(8),
|
||||
),
|
||||
),
|
||||
child: Row(
|
||||
mainAxisAlignment: MainAxisAlignment.spaceBetween,
|
||||
children: [
|
||||
Text(
|
||||
title,
|
||||
style: Theme.of(context).textTheme.titleLarge?.copyWith(
|
||||
fontWeight: FontWeight.bold,
|
||||
),
|
||||
),
|
||||
if (actions != null && actionLabels != null)
|
||||
Row(
|
||||
children: List.generate(
|
||||
actions!.length,
|
||||
(index) => Padding(
|
||||
padding: const EdgeInsets.only(left: 8),
|
||||
child: ElevatedButton(
|
||||
onPressed: actions![index],
|
||||
child: Text(actionLabels![index]),
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
// 数据表格
|
||||
if (rows.isNotEmpty)
|
||||
SingleChildScrollView(
|
||||
scrollDirection: Axis.horizontal,
|
||||
child: DataTable(
|
||||
columns: headers
|
||||
.map((header) => DataColumn(
|
||||
label: Text(
|
||||
header,
|
||||
style: const TextStyle(fontWeight: FontWeight.bold),
|
||||
),
|
||||
))
|
||||
.toList(),
|
||||
rows: rows
|
||||
.map((row) => DataRow(
|
||||
cells: row
|
||||
.map((cell) => DataCell(Text(cell)))
|
||||
.toList(),
|
||||
))
|
||||
.toList(),
|
||||
),
|
||||
)
|
||||
else
|
||||
Container(
|
||||
padding: const EdgeInsets.all(32),
|
||||
child: Center(
|
||||
child: Text(
|
||||
'暂无数据',
|
||||
style: Theme.of(context).textTheme.bodyLarge?.copyWith(
|
||||
color: Theme.of(context)
|
||||
.colorScheme
|
||||
.onSurface
|
||||
.withOpacity(0.6),
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
212
AINoval/lib/screens/admin/widgets/admin_sidebar.dart
Normal file
212
AINoval/lib/screens/admin/widgets/admin_sidebar.dart
Normal file
@@ -0,0 +1,212 @@
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
import '../../../utils/web_theme.dart';
|
||||
import '../../../widgets/common/permission_guard.dart';
|
||||
import '../../../services/permission_service.dart';
|
||||
|
||||
class AdminSidebar extends StatelessWidget {
|
||||
final int selectedIndex;
|
||||
final ValueChanged<int> onItemSelected;
|
||||
|
||||
const AdminSidebar({
|
||||
super.key,
|
||||
required this.selectedIndex,
|
||||
required this.onItemSelected,
|
||||
});
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Container(
|
||||
width: 250,
|
||||
decoration: BoxDecoration(
|
||||
color: WebTheme.getCardColor(context),
|
||||
border: Border(
|
||||
right: BorderSide(
|
||||
color: WebTheme.getBorderColor(context),
|
||||
width: 1,
|
||||
),
|
||||
),
|
||||
),
|
||||
child: Column(
|
||||
children: [
|
||||
// 标题
|
||||
Container(
|
||||
padding: const EdgeInsets.all(20),
|
||||
child: Column(
|
||||
children: [
|
||||
Icon(
|
||||
Icons.admin_panel_settings,
|
||||
size: 48,
|
||||
color: WebTheme.getTextColor(context),
|
||||
),
|
||||
const SizedBox(height: 12),
|
||||
Text(
|
||||
'AI小说助手',
|
||||
style: TextStyle(
|
||||
fontSize: 18,
|
||||
fontWeight: FontWeight.bold,
|
||||
color: WebTheme.getTextColor(context),
|
||||
),
|
||||
textAlign: TextAlign.center,
|
||||
),
|
||||
Text(
|
||||
'管理后台',
|
||||
style: TextStyle(
|
||||
fontSize: 14,
|
||||
color: WebTheme.getSecondaryTextColor(context),
|
||||
),
|
||||
textAlign: TextAlign.center,
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
Divider(
|
||||
color: WebTheme.getBorderColor(context),
|
||||
height: 1,
|
||||
),
|
||||
// 菜单项
|
||||
Expanded(
|
||||
child: ListView(
|
||||
children: [
|
||||
PermissionGuard.permission(
|
||||
PermissionService.STATISTICS_VIEW,
|
||||
child: _buildMenuItem(
|
||||
context,
|
||||
icon: Icons.dashboard,
|
||||
title: '仪表板',
|
||||
index: 0,
|
||||
),
|
||||
),
|
||||
PermissionGuard.permission(
|
||||
PermissionService.STATISTICS_VIEW,
|
||||
child: _buildMenuItem(
|
||||
context,
|
||||
icon: Icons.visibility,
|
||||
title: 'LLM可观测性',
|
||||
index: 1,
|
||||
),
|
||||
),
|
||||
PermissionGuard.permission(
|
||||
PermissionService.USER_MANAGEMENT,
|
||||
child: _buildMenuItem(
|
||||
context,
|
||||
icon: Icons.people,
|
||||
title: '用户管理',
|
||||
index: 2,
|
||||
),
|
||||
),
|
||||
PermissionGuard.permission(
|
||||
PermissionService.USER_MANAGEMENT,
|
||||
child: _buildMenuItem(
|
||||
context,
|
||||
icon: Icons.security,
|
||||
title: '角色管理',
|
||||
index: 3,
|
||||
),
|
||||
),
|
||||
PermissionGuard.permission(
|
||||
PermissionService.SUBSCRIPTION_MANAGEMENT,
|
||||
child: _buildMenuItem(
|
||||
context,
|
||||
icon: Icons.subscriptions,
|
||||
title: '订阅管理',
|
||||
index: 4,
|
||||
),
|
||||
),
|
||||
PermissionGuard.permission(
|
||||
PermissionService.MODEL_MANAGEMENT,
|
||||
child: _buildMenuItem(
|
||||
context,
|
||||
icon: Icons.cloud,
|
||||
title: '公共模型',
|
||||
index: 5,
|
||||
),
|
||||
),
|
||||
PermissionGuard.permission(
|
||||
PermissionService.PRESET_MANAGEMENT,
|
||||
child: _buildMenuItem(
|
||||
context,
|
||||
icon: Icons.smart_button,
|
||||
title: '系统预设',
|
||||
index: 6,
|
||||
),
|
||||
),
|
||||
PermissionGuard.permission(
|
||||
PermissionService.TEMPLATE_MANAGEMENT,
|
||||
child: _buildMenuItem(
|
||||
context,
|
||||
icon: Icons.article,
|
||||
title: '公共模板',
|
||||
index: 7,
|
||||
),
|
||||
),
|
||||
PermissionGuard.permission(
|
||||
PermissionService.SYSTEM_CONFIG,
|
||||
child: _buildMenuItem(
|
||||
context,
|
||||
icon: Icons.settings,
|
||||
title: '系统配置',
|
||||
index: 8,
|
||||
),
|
||||
),
|
||||
PermissionGuard.permission(
|
||||
PermissionService.TEMPLATE_MANAGEMENT,
|
||||
child: _buildMenuItem(
|
||||
context,
|
||||
icon: Icons.auto_awesome,
|
||||
title: '增强模板',
|
||||
index: 9,
|
||||
),
|
||||
),
|
||||
PermissionGuard.permission(
|
||||
PermissionService.SYSTEM_CONFIG,
|
||||
child: _buildMenuItem(
|
||||
context,
|
||||
icon: Icons.receipt_long,
|
||||
title: '计费审计',
|
||||
index: 10,
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
Widget _buildMenuItem(
|
||||
BuildContext context, {
|
||||
required IconData icon,
|
||||
required String title,
|
||||
required int index,
|
||||
}) {
|
||||
final isSelected = selectedIndex == index;
|
||||
return Container(
|
||||
margin: const EdgeInsets.symmetric(horizontal: 8, vertical: 2),
|
||||
child: ListTile(
|
||||
leading: Icon(
|
||||
icon,
|
||||
color: isSelected
|
||||
? WebTheme.getTextColor(context)
|
||||
: WebTheme.getSecondaryTextColor(context),
|
||||
),
|
||||
title: Text(
|
||||
title,
|
||||
style: TextStyle(
|
||||
color: isSelected
|
||||
? WebTheme.getTextColor(context)
|
||||
: WebTheme.getSecondaryTextColor(context),
|
||||
fontWeight: isSelected ? FontWeight.w600 : FontWeight.normal,
|
||||
),
|
||||
),
|
||||
selected: isSelected,
|
||||
selectedTileColor: WebTheme.getTextColor(context).withOpacity(0.1),
|
||||
shape: RoundedRectangleBorder(
|
||||
borderRadius: BorderRadius.circular(8),
|
||||
),
|
||||
onTap: () => onItemSelected(index),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
424
AINoval/lib/screens/admin/widgets/batch_operation_dialog.dart
Normal file
424
AINoval/lib/screens/admin/widgets/batch_operation_dialog.dart
Normal file
@@ -0,0 +1,424 @@
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
import '../../../models/prompt_models.dart';
|
||||
import '../../../utils/web_theme.dart';
|
||||
import '../../../widgets/common/dialog_container.dart';
|
||||
import '../../../widgets/common/dialog_header.dart';
|
||||
|
||||
/// 批量操作确认对话框
|
||||
class BatchOperationDialog extends StatefulWidget {
|
||||
final String operation;
|
||||
final String title;
|
||||
final String description;
|
||||
final List<EnhancedUserPromptTemplate> templates;
|
||||
final Function(String? comment) onConfirm;
|
||||
final Color? actionColor;
|
||||
final bool requiresComment;
|
||||
final String? commentHint;
|
||||
|
||||
const BatchOperationDialog({
|
||||
Key? key,
|
||||
required this.operation,
|
||||
required this.title,
|
||||
required this.description,
|
||||
required this.templates,
|
||||
required this.onConfirm,
|
||||
this.actionColor,
|
||||
this.requiresComment = false,
|
||||
this.commentHint,
|
||||
}) : super(key: key);
|
||||
|
||||
@override
|
||||
State<BatchOperationDialog> createState() => _BatchOperationDialogState();
|
||||
}
|
||||
|
||||
class _BatchOperationDialogState extends State<BatchOperationDialog> {
|
||||
final _commentController = TextEditingController();
|
||||
bool _isLoading = false;
|
||||
|
||||
@override
|
||||
void dispose() {
|
||||
_commentController.dispose();
|
||||
super.dispose();
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return DialogContainer(
|
||||
child: Column(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
children: [
|
||||
DialogHeader(
|
||||
title: widget.title,
|
||||
onClose: () => Navigator.of(context).pop(),
|
||||
),
|
||||
Expanded(
|
||||
child: SingleChildScrollView(
|
||||
padding: const EdgeInsets.all(24),
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
_buildWarningSection(),
|
||||
const SizedBox(height: 24),
|
||||
_buildTemplatesList(),
|
||||
if (widget.requiresComment || widget.commentHint != null) ...[
|
||||
const SizedBox(height: 24),
|
||||
_buildCommentSection(),
|
||||
],
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
_buildActions(),
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
Widget _buildWarningSection() {
|
||||
return Container(
|
||||
padding: const EdgeInsets.all(16),
|
||||
decoration: BoxDecoration(
|
||||
color: (widget.actionColor ?? Colors.orange).withOpacity(0.1),
|
||||
borderRadius: BorderRadius.circular(8),
|
||||
border: Border.all(
|
||||
color: (widget.actionColor ?? Colors.orange).withOpacity(0.3),
|
||||
),
|
||||
),
|
||||
child: Row(
|
||||
children: [
|
||||
Icon(
|
||||
Icons.warning,
|
||||
color: widget.actionColor ?? Colors.orange,
|
||||
size: 24,
|
||||
),
|
||||
const SizedBox(width: 16),
|
||||
Expanded(
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
Text(
|
||||
'批量操作确认',
|
||||
style: TextStyle(
|
||||
fontWeight: FontWeight.bold,
|
||||
color: widget.actionColor ?? Colors.orange,
|
||||
fontSize: 16,
|
||||
),
|
||||
),
|
||||
const SizedBox(height: 4),
|
||||
Text(
|
||||
widget.description,
|
||||
style: TextStyle(
|
||||
color: WebTheme.getTextColor(context).withOpacity(0.8),
|
||||
fontSize: 14,
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
Widget _buildTemplatesList() {
|
||||
return Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
Row(
|
||||
children: [
|
||||
const Icon(Icons.list, size: 20),
|
||||
const SizedBox(width: 8),
|
||||
Text(
|
||||
'影响的模板 (${widget.templates.length} 个)',
|
||||
style: TextStyle(
|
||||
fontSize: 18,
|
||||
fontWeight: FontWeight.bold,
|
||||
color: WebTheme.getTextColor(context),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
const SizedBox(height: 16),
|
||||
Container(
|
||||
constraints: const BoxConstraints(maxHeight: 200),
|
||||
decoration: BoxDecoration(
|
||||
border: Border.all(color: Colors.grey.withOpacity(0.3)),
|
||||
borderRadius: BorderRadius.circular(8),
|
||||
),
|
||||
child: ListView.separated(
|
||||
shrinkWrap: true,
|
||||
itemCount: widget.templates.length,
|
||||
separatorBuilder: (context, index) => const Divider(height: 1),
|
||||
itemBuilder: (context, index) {
|
||||
final template = widget.templates[index];
|
||||
return ListTile(
|
||||
dense: true,
|
||||
leading: CircleAvatar(
|
||||
radius: 12,
|
||||
backgroundColor: WebTheme.getPrimaryColor(context).withOpacity(0.1),
|
||||
child: Text(
|
||||
'${index + 1}',
|
||||
style: TextStyle(
|
||||
fontSize: 10,
|
||||
color: WebTheme.getPrimaryColor(context),
|
||||
fontWeight: FontWeight.bold,
|
||||
),
|
||||
),
|
||||
),
|
||||
title: Text(
|
||||
template.name,
|
||||
style: const TextStyle(fontSize: 14),
|
||||
),
|
||||
subtitle: Text(
|
||||
_getFeatureTypeLabel(template.featureType.toApiString()),
|
||||
style: const TextStyle(fontSize: 12),
|
||||
),
|
||||
trailing: _buildTemplateStatusBadge(template),
|
||||
);
|
||||
},
|
||||
),
|
||||
),
|
||||
],
|
||||
);
|
||||
}
|
||||
|
||||
Widget _buildTemplateStatusBadge(EnhancedUserPromptTemplate template) {
|
||||
String status;
|
||||
Color color;
|
||||
|
||||
if (template.isVerified) {
|
||||
status = '认证';
|
||||
color = Colors.green;
|
||||
} else if (template.isPublic) {
|
||||
status = '公开';
|
||||
color = Colors.blue;
|
||||
} else {
|
||||
status = '私有';
|
||||
color = Colors.grey;
|
||||
}
|
||||
|
||||
return Container(
|
||||
padding: const EdgeInsets.symmetric(horizontal: 6, vertical: 2),
|
||||
decoration: BoxDecoration(
|
||||
color: color.withOpacity(0.1),
|
||||
borderRadius: BorderRadius.circular(10),
|
||||
border: Border.all(color: color.withOpacity(0.3)),
|
||||
),
|
||||
child: Text(
|
||||
status,
|
||||
style: TextStyle(
|
||||
fontSize: 10,
|
||||
color: color,
|
||||
fontWeight: FontWeight.w500,
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
Widget _buildCommentSection() {
|
||||
return Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
Row(
|
||||
children: [
|
||||
const Icon(Icons.comment, size: 20),
|
||||
const SizedBox(width: 8),
|
||||
Text(
|
||||
widget.requiresComment ? '操作备注 *' : '操作备注(可选)',
|
||||
style: TextStyle(
|
||||
fontSize: 18,
|
||||
fontWeight: FontWeight.bold,
|
||||
color: WebTheme.getTextColor(context),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
const SizedBox(height: 16),
|
||||
TextField(
|
||||
controller: _commentController,
|
||||
decoration: InputDecoration(
|
||||
hintText: widget.commentHint ?? '请输入操作备注...',
|
||||
border: const OutlineInputBorder(),
|
||||
alignLabelWithHint: true,
|
||||
),
|
||||
maxLines: 3,
|
||||
),
|
||||
if (widget.requiresComment)
|
||||
Padding(
|
||||
padding: const EdgeInsets.only(top: 8),
|
||||
child: Text(
|
||||
'* 此操作需要填写备注信息',
|
||||
style: TextStyle(
|
||||
fontSize: 12,
|
||||
color: Colors.red.withOpacity(0.7),
|
||||
),
|
||||
),
|
||||
),
|
||||
],
|
||||
);
|
||||
}
|
||||
|
||||
Widget _buildActions() {
|
||||
return Container(
|
||||
padding: const EdgeInsets.all(24),
|
||||
decoration: BoxDecoration(
|
||||
color: WebTheme.getCardColor(context),
|
||||
border: Border(
|
||||
top: BorderSide(
|
||||
color: WebTheme.getBorderColor(context),
|
||||
),
|
||||
),
|
||||
),
|
||||
child: Row(
|
||||
mainAxisAlignment: MainAxisAlignment.end,
|
||||
children: [
|
||||
TextButton(
|
||||
onPressed: _isLoading ? null : () => Navigator.of(context).pop(),
|
||||
child: const Text('取消'),
|
||||
),
|
||||
const SizedBox(width: 16),
|
||||
ElevatedButton(
|
||||
onPressed: _isLoading ? null : _handleConfirm,
|
||||
style: ElevatedButton.styleFrom(
|
||||
backgroundColor: widget.actionColor ?? WebTheme.getPrimaryColor(context),
|
||||
foregroundColor: WebTheme.white,
|
||||
),
|
||||
child: _isLoading
|
||||
? const SizedBox(
|
||||
width: 16,
|
||||
height: 16,
|
||||
child: CircularProgressIndicator(strokeWidth: 2, color: Colors.white),
|
||||
)
|
||||
: Text('确认${widget.operation}'),
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
Future<void> _handleConfirm() async {
|
||||
// 检查是否需要备注且未填写
|
||||
if (widget.requiresComment && _commentController.text.trim().isEmpty) {
|
||||
ScaffoldMessenger.of(context).showSnackBar(
|
||||
const SnackBar(content: Text('请填写操作备注')),
|
||||
);
|
||||
return;
|
||||
}
|
||||
|
||||
setState(() {
|
||||
_isLoading = true;
|
||||
});
|
||||
|
||||
try {
|
||||
final comment = _commentController.text.trim();
|
||||
await widget.onConfirm(comment.isNotEmpty ? comment : null);
|
||||
|
||||
if (mounted) {
|
||||
Navigator.of(context).pop();
|
||||
}
|
||||
} catch (e) {
|
||||
if (mounted) {
|
||||
ScaffoldMessenger.of(context).showSnackBar(
|
||||
SnackBar(content: Text('操作失败: $e')),
|
||||
);
|
||||
}
|
||||
} finally {
|
||||
if (mounted) {
|
||||
setState(() {
|
||||
_isLoading = false;
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
String _getFeatureTypeLabel(String? featureType) {
|
||||
switch (featureType) {
|
||||
case 'AI_CHAT':
|
||||
return 'AI聊天';
|
||||
case 'TEXT_EXPANSION':
|
||||
return '文本扩写';
|
||||
case 'TEXT_REFACTOR':
|
||||
return '文本润色';
|
||||
case 'TEXT_SUMMARY':
|
||||
return '文本总结';
|
||||
case 'SCENE_TO_SUMMARY':
|
||||
return '场景转摘要';
|
||||
case 'SUMMARY_TO_SCENE':
|
||||
return '摘要转场景';
|
||||
case 'NOVEL_GENERATION':
|
||||
return '小说生成';
|
||||
case 'PROFESSIONAL_FICTION_CONTINUATION':
|
||||
return '专业续写';
|
||||
case 'SCENE_BEAT_GENERATION':
|
||||
return '场景节拍生成';
|
||||
default:
|
||||
return featureType ?? '未知';
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// 批量操作类型枚举
|
||||
enum BatchOperationType {
|
||||
review,
|
||||
verify,
|
||||
publish,
|
||||
delete,
|
||||
export,
|
||||
}
|
||||
|
||||
/// 批量操作配置
|
||||
class BatchOperationConfig {
|
||||
final BatchOperationType type;
|
||||
final String title;
|
||||
final String description;
|
||||
final Color actionColor;
|
||||
final bool requiresComment;
|
||||
final String? commentHint;
|
||||
|
||||
const BatchOperationConfig({
|
||||
required this.type,
|
||||
required this.title,
|
||||
required this.description,
|
||||
required this.actionColor,
|
||||
this.requiresComment = false,
|
||||
this.commentHint,
|
||||
});
|
||||
|
||||
static const Map<BatchOperationType, BatchOperationConfig> configs = {
|
||||
BatchOperationType.review: BatchOperationConfig(
|
||||
type: BatchOperationType.review,
|
||||
title: '批量审核',
|
||||
description: '您即将批量审核选中的模板。审核通过的模板将被发布为公共模板。',
|
||||
actionColor: Colors.green,
|
||||
requiresComment: false,
|
||||
commentHint: '可以添加审核意见(可选)',
|
||||
),
|
||||
BatchOperationType.verify: BatchOperationConfig(
|
||||
type: BatchOperationType.verify,
|
||||
title: '批量认证',
|
||||
description: '您即将为选中的模板添加官方认证标识。认证后的模板将显示认证徽章。',
|
||||
actionColor: Colors.blue,
|
||||
),
|
||||
BatchOperationType.publish: BatchOperationConfig(
|
||||
type: BatchOperationType.publish,
|
||||
title: '批量发布',
|
||||
description: '您即将批量发布选中的模板。发布后的模板将对所有用户可见。',
|
||||
actionColor: Colors.indigo,
|
||||
),
|
||||
BatchOperationType.delete: BatchOperationConfig(
|
||||
type: BatchOperationType.delete,
|
||||
title: '批量删除',
|
||||
description: '您即将永久删除选中的模板。此操作不可撤销,请谨慎操作!',
|
||||
actionColor: Colors.red,
|
||||
requiresComment: true,
|
||||
commentHint: '请说明删除原因',
|
||||
),
|
||||
BatchOperationType.export: BatchOperationConfig(
|
||||
type: BatchOperationType.export,
|
||||
title: '批量导出',
|
||||
description: '您即将导出选中的模板数据。导出的数据可用于备份或迁移。',
|
||||
actionColor: Colors.orange,
|
||||
),
|
||||
};
|
||||
}
|
||||
175
AINoval/lib/screens/admin/widgets/credit_operation_dialog.dart
Normal file
175
AINoval/lib/screens/admin/widgets/credit_operation_dialog.dart
Normal file
@@ -0,0 +1,175 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter/services.dart';
|
||||
import '../../../models/admin/admin_models.dart';
|
||||
|
||||
class CreditOperationDialog extends StatefulWidget {
|
||||
final AdminUser user;
|
||||
final bool isAdd; // true为添加积分,false为扣减积分
|
||||
|
||||
const CreditOperationDialog({
|
||||
super.key,
|
||||
required this.user,
|
||||
required this.isAdd,
|
||||
});
|
||||
|
||||
@override
|
||||
State<CreditOperationDialog> createState() => _CreditOperationDialogState();
|
||||
}
|
||||
|
||||
class _CreditOperationDialogState extends State<CreditOperationDialog> {
|
||||
final _formKey = GlobalKey<FormState>();
|
||||
final _amountController = TextEditingController();
|
||||
final _reasonController = TextEditingController();
|
||||
|
||||
@override
|
||||
void dispose() {
|
||||
_amountController.dispose();
|
||||
_reasonController.dispose();
|
||||
super.dispose();
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return AlertDialog(
|
||||
title: Text(
|
||||
widget.isAdd ? '添加积分' : '扣减积分',
|
||||
style: Theme.of(context).textTheme.titleLarge?.copyWith(
|
||||
fontWeight: FontWeight.bold,
|
||||
),
|
||||
),
|
||||
content: SizedBox(
|
||||
width: 400,
|
||||
child: Form(
|
||||
key: _formKey,
|
||||
child: Column(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
// 用户信息
|
||||
Container(
|
||||
padding: const EdgeInsets.all(12),
|
||||
decoration: BoxDecoration(
|
||||
color: Theme.of(context).colorScheme.surfaceVariant,
|
||||
borderRadius: BorderRadius.circular(8),
|
||||
),
|
||||
child: Row(
|
||||
children: [
|
||||
Icon(
|
||||
Icons.person,
|
||||
color: Theme.of(context).colorScheme.onSurfaceVariant,
|
||||
),
|
||||
const SizedBox(width: 8),
|
||||
Expanded(
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
Text(
|
||||
widget.user.username,
|
||||
style: Theme.of(context).textTheme.titleMedium?.copyWith(
|
||||
fontWeight: FontWeight.w600,
|
||||
),
|
||||
),
|
||||
Text(
|
||||
'当前积分: ${widget.user.credits}',
|
||||
style: Theme.of(context).textTheme.bodyMedium?.copyWith(
|
||||
color: Theme.of(context).colorScheme.primary,
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
const SizedBox(height: 16),
|
||||
|
||||
// 积分数量输入
|
||||
TextFormField(
|
||||
controller: _amountController,
|
||||
decoration: InputDecoration(
|
||||
labelText: '积分数量',
|
||||
hintText: '请输入${widget.isAdd ? "添加" : "扣减"}的积分数量',
|
||||
prefixIcon: const Icon(Icons.monetization_on),
|
||||
border: const OutlineInputBorder(),
|
||||
),
|
||||
keyboardType: TextInputType.number,
|
||||
inputFormatters: [
|
||||
FilteringTextInputFormatter.digitsOnly,
|
||||
],
|
||||
validator: (value) {
|
||||
if (value == null || value.isEmpty) {
|
||||
return '请输入积分数量';
|
||||
}
|
||||
final amount = int.tryParse(value);
|
||||
if (amount == null || amount <= 0) {
|
||||
return '请输入有效的积分数量';
|
||||
}
|
||||
if (!widget.isAdd && amount > widget.user.credits) {
|
||||
return '扣减积分不能超过用户当前积分';
|
||||
}
|
||||
return null;
|
||||
},
|
||||
),
|
||||
const SizedBox(height: 16),
|
||||
|
||||
// 操作原因输入
|
||||
TextFormField(
|
||||
controller: _reasonController,
|
||||
decoration: InputDecoration(
|
||||
labelText: '操作原因',
|
||||
hintText: '请输入${widget.isAdd ? "添加" : "扣减"}积分的原因',
|
||||
prefixIcon: const Icon(Icons.note),
|
||||
border: const OutlineInputBorder(),
|
||||
),
|
||||
maxLines: 3,
|
||||
validator: (value) {
|
||||
if (value == null || value.trim().isEmpty) {
|
||||
return '请输入操作原因';
|
||||
}
|
||||
if (value.trim().length < 5) {
|
||||
return '操作原因至少需要5个字符';
|
||||
}
|
||||
return null;
|
||||
},
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
actions: [
|
||||
TextButton(
|
||||
onPressed: () => Navigator.of(context).pop(),
|
||||
child: const Text('取消'),
|
||||
),
|
||||
ElevatedButton(
|
||||
onPressed: _handleSubmit,
|
||||
style: ElevatedButton.styleFrom(
|
||||
backgroundColor: widget.isAdd
|
||||
? Theme.of(context).colorScheme.primary
|
||||
: Theme.of(context).colorScheme.error,
|
||||
),
|
||||
child: Text(
|
||||
widget.isAdd ? '添加积分' : '扣减积分',
|
||||
style: TextStyle(
|
||||
color: widget.isAdd
|
||||
? Theme.of(context).colorScheme.onPrimary
|
||||
: Theme.of(context).colorScheme.onError,
|
||||
),
|
||||
),
|
||||
),
|
||||
],
|
||||
);
|
||||
}
|
||||
|
||||
void _handleSubmit() {
|
||||
if (_formKey.currentState?.validate() == true) {
|
||||
final amount = int.parse(_amountController.text);
|
||||
final reason = _reasonController.text.trim();
|
||||
|
||||
Navigator.of(context).pop({
|
||||
'amount': amount,
|
||||
'reason': reason,
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
720
AINoval/lib/screens/admin/widgets/edit_public_model_dialog.dart
Normal file
720
AINoval/lib/screens/admin/widgets/edit_public_model_dialog.dart
Normal file
@@ -0,0 +1,720 @@
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
import '../../../models/prompt_models.dart';
|
||||
import '../../../models/public_model_config.dart';
|
||||
import '../../../services/api_service/repositories/impl/admin_repository_impl.dart';
|
||||
import '../../../utils/logger.dart';
|
||||
import '../../../utils/web_theme.dart';
|
||||
import 'validation_results_dialog.dart';
|
||||
|
||||
/// 编辑公共模型对话框
|
||||
class EditPublicModelDialog extends StatefulWidget {
|
||||
const EditPublicModelDialog({
|
||||
super.key,
|
||||
required this.config,
|
||||
required this.onSuccess,
|
||||
});
|
||||
|
||||
final PublicModelConfigDetails config;
|
||||
final VoidCallback onSuccess;
|
||||
|
||||
@override
|
||||
State<EditPublicModelDialog> createState() => _EditPublicModelDialogState();
|
||||
}
|
||||
|
||||
class _EditPublicModelDialogState extends State<EditPublicModelDialog> {
|
||||
final String _tag = 'EditPublicModelDialog';
|
||||
late final AdminRepositoryImpl _adminRepository;
|
||||
|
||||
// 表单数据
|
||||
final _formKey = GlobalKey<FormState>();
|
||||
final _providerController = TextEditingController();
|
||||
final _modelIdController = TextEditingController();
|
||||
final _displayNameController = TextEditingController();
|
||||
final _apiEndpointController = TextEditingController();
|
||||
final _apiKeysController = TextEditingController();
|
||||
final _keyNotesController = TextEditingController();
|
||||
final _descriptionController = TextEditingController();
|
||||
final _tagsController = TextEditingController();
|
||||
final _creditRateController = TextEditingController();
|
||||
final _maxConcurrentController = TextEditingController();
|
||||
final _dailyLimitController = TextEditingController();
|
||||
final _hourlyLimitController = TextEditingController();
|
||||
final _priorityController = TextEditingController();
|
||||
|
||||
final Set<AIFeatureType> _selectedFeatures = {};
|
||||
bool _enabled = true;
|
||||
bool _isLoading = false;
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
_adminRepository = AdminRepositoryImpl();
|
||||
_initializeForm();
|
||||
}
|
||||
|
||||
void _initializeForm() {
|
||||
final config = widget.config;
|
||||
|
||||
// 基本信息
|
||||
_providerController.text = config.provider;
|
||||
_modelIdController.text = config.modelId;
|
||||
_displayNameController.text = config.displayName ?? '';
|
||||
_apiEndpointController.text = config.apiEndpoint ?? '';
|
||||
_enabled = config.enabled ?? true;
|
||||
|
||||
// 配置信息
|
||||
_descriptionController.text = config.description ?? '';
|
||||
_tagsController.text = config.tags?.join(', ') ?? '';
|
||||
_creditRateController.text = config.creditRateMultiplier?.toString() ?? '1.0';
|
||||
_maxConcurrentController.text = config.maxConcurrentRequests?.toString() ?? '-1';
|
||||
_dailyLimitController.text = config.dailyRequestLimit?.toString() ?? '-1';
|
||||
_hourlyLimitController.text = config.hourlyRequestLimit?.toString() ?? '-1';
|
||||
_priorityController.text = config.priority?.toString() ?? '0';
|
||||
|
||||
// 功能授权
|
||||
if (config.enabledForFeatures != null) {
|
||||
for (final featureStr in config.enabledForFeatures!) {
|
||||
final feature = AIFeatureTypeHelper.fromApiString(featureStr);
|
||||
_selectedFeatures.add(feature);
|
||||
}
|
||||
}
|
||||
|
||||
// 如果没有选中任何功能,默认选中AI聊天
|
||||
if (_selectedFeatures.isEmpty) {
|
||||
_selectedFeatures.add(AIFeatureType.aiChat);
|
||||
}
|
||||
|
||||
// 加载完整的配置信息包括API Keys
|
||||
_loadFullConfigWithApiKeys();
|
||||
}
|
||||
|
||||
Future<void> _loadFullConfigWithApiKeys() async {
|
||||
try {
|
||||
final fullConfig = await _adminRepository.getPublicModelConfigById(widget.config.id!);
|
||||
|
||||
if (mounted) {
|
||||
setState(() {
|
||||
// 显示实际的API Keys,每行一个
|
||||
if (fullConfig.apiKeyStatuses?.isNotEmpty == true) {
|
||||
final apiKeys = fullConfig.apiKeyStatuses!
|
||||
.map((status) => status.apiKey ?? '')
|
||||
.where((key) => key.isNotEmpty)
|
||||
.join('\n');
|
||||
_apiKeysController.text = apiKeys;
|
||||
_keyNotesController.text = fullConfig.apiKeyStatuses!
|
||||
.map((status) => status.note ?? '')
|
||||
.join('\n');
|
||||
} else {
|
||||
_apiKeysController.text = '';
|
||||
_keyNotesController.text = '';
|
||||
}
|
||||
});
|
||||
}
|
||||
} catch (e) {
|
||||
AppLogger.e(_tag, '加载完整配置信息失败', e);
|
||||
if (mounted) {
|
||||
setState(() {
|
||||
// 如果加载失败,显示占位符
|
||||
_apiKeysController.text = '*** 加载API Keys失败 ***';
|
||||
_keyNotesController.text = widget.config.apiKeyStatuses?.map((status) => status.note ?? '').join('\n') ?? '';
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
void dispose() {
|
||||
_providerController.dispose();
|
||||
_modelIdController.dispose();
|
||||
_displayNameController.dispose();
|
||||
_apiEndpointController.dispose();
|
||||
_apiKeysController.dispose();
|
||||
_keyNotesController.dispose();
|
||||
_descriptionController.dispose();
|
||||
_tagsController.dispose();
|
||||
_creditRateController.dispose();
|
||||
_maxConcurrentController.dispose();
|
||||
_dailyLimitController.dispose();
|
||||
_hourlyLimitController.dispose();
|
||||
_priorityController.dispose();
|
||||
super.dispose();
|
||||
}
|
||||
|
||||
/// 保存配置
|
||||
Future<void> _saveConfig({required bool validate}) async {
|
||||
if (!_formKey.currentState!.validate()) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (_providerController.text.isEmpty) {
|
||||
_showSnackBar('请输入提供商', isError: true);
|
||||
return;
|
||||
}
|
||||
|
||||
if (_modelIdController.text.isEmpty) {
|
||||
_showSnackBar('请输入模型ID', isError: true);
|
||||
return;
|
||||
}
|
||||
|
||||
setState(() {
|
||||
_isLoading = true;
|
||||
});
|
||||
|
||||
try {
|
||||
// 解析API Keys - 如果用户修改了API Keys
|
||||
List<ApiKeyRequest>? apiKeys;
|
||||
if (_apiKeysController.text.trim() != '*** API Keys已配置 ***') {
|
||||
final apiKeyLines = _apiKeysController.text.split('\n').where((line) => line.trim().isNotEmpty).toList();
|
||||
final noteLines = _keyNotesController.text.split('\n');
|
||||
|
||||
apiKeys = <ApiKeyRequest>[];
|
||||
for (int i = 0; i < apiKeyLines.length; i++) {
|
||||
final note = i < noteLines.length ? noteLines[i].trim() : '';
|
||||
apiKeys.add(ApiKeyRequest(
|
||||
apiKey: apiKeyLines[i].trim(),
|
||||
note: note.isEmpty ? null : note,
|
||||
));
|
||||
}
|
||||
}
|
||||
|
||||
// 解析标签
|
||||
final tags = _tagsController.text.split(',').map((tag) => tag.trim()).where((tag) => tag.isNotEmpty).toList();
|
||||
|
||||
// 使用扩展方法转换功能类型枚举为字符串
|
||||
final enabledFeaturesStrings = AIFeatureTypeHelper.toApiStringList(_selectedFeatures);
|
||||
|
||||
final request = PublicModelConfigRequest(
|
||||
provider: _providerController.text,
|
||||
modelId: _modelIdController.text,
|
||||
displayName: _displayNameController.text.isEmpty ? null : _displayNameController.text,
|
||||
enabled: _enabled,
|
||||
apiKeys: apiKeys, // 如果为null,后端保持原有API Keys不变
|
||||
apiEndpoint: _apiEndpointController.text.isEmpty ? null : _apiEndpointController.text,
|
||||
enabledForFeatures: enabledFeaturesStrings,
|
||||
creditRateMultiplier: double.tryParse(_creditRateController.text),
|
||||
maxConcurrentRequests: int.tryParse(_maxConcurrentController.text),
|
||||
dailyRequestLimit: int.tryParse(_dailyLimitController.text),
|
||||
hourlyRequestLimit: int.tryParse(_hourlyLimitController.text),
|
||||
priority: int.tryParse(_priorityController.text),
|
||||
description: _descriptionController.text.isEmpty ? null : _descriptionController.text,
|
||||
tags: tags,
|
||||
);
|
||||
|
||||
// 调用API更新配置
|
||||
await _adminRepository.updatePublicModelConfig(widget.config.id!, request, validate: validate);
|
||||
|
||||
AppLogger.i(_tag, validate ? '✅ 更新并验证模型配置成功' : '✅ 更新模型配置成功');
|
||||
|
||||
if (validate) {
|
||||
try {
|
||||
final withKeys = await _adminRepository.getPublicModelConfigById(widget.config.id!);
|
||||
if (mounted) {
|
||||
showDialog(
|
||||
context: context,
|
||||
builder: (context) => ValidationResultsDialog(config: withKeys),
|
||||
);
|
||||
}
|
||||
} catch (_) {
|
||||
_showSnackBar('模型配置更新成功,验证完成!', isError: false);
|
||||
}
|
||||
} else {
|
||||
_showSnackBar('模型配置更新成功!', isError: false);
|
||||
}
|
||||
|
||||
widget.onSuccess();
|
||||
if (mounted) {
|
||||
Navigator.of(context).pop();
|
||||
}
|
||||
} catch (e) {
|
||||
AppLogger.e(_tag, validate ? '更新并验证模型配置失败' : '更新模型配置失败', e);
|
||||
if (mounted) {
|
||||
_showSnackBar(validate ? '更新并验证失败: ${e.toString()}' : '更新失败: ${e.toString()}', isError: true);
|
||||
}
|
||||
} finally {
|
||||
if (mounted) {
|
||||
setState(() {
|
||||
_isLoading = false;
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Dialog(
|
||||
backgroundColor: WebTheme.getCardColor(context),
|
||||
child: Container(
|
||||
width: 900,
|
||||
height: 700,
|
||||
padding: const EdgeInsets.all(24),
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
// 头部
|
||||
Row(
|
||||
children: [
|
||||
Text(
|
||||
'编辑公共模型配置',
|
||||
style: TextStyle(
|
||||
fontSize: 20,
|
||||
fontWeight: FontWeight.w600,
|
||||
color: WebTheme.getTextColor(context),
|
||||
),
|
||||
),
|
||||
const SizedBox(width: 8),
|
||||
Container(
|
||||
padding: const EdgeInsets.symmetric(horizontal: 8, vertical: 4),
|
||||
decoration: BoxDecoration(
|
||||
color: Colors.blue.withOpacity(0.1),
|
||||
borderRadius: BorderRadius.circular(12),
|
||||
border: Border.all(color: Colors.blue.withOpacity(0.3)),
|
||||
),
|
||||
child: Text(
|
||||
widget.config.provider,
|
||||
style: const TextStyle(
|
||||
fontSize: 12,
|
||||
color: Colors.blue,
|
||||
fontWeight: FontWeight.w500,
|
||||
),
|
||||
),
|
||||
),
|
||||
const Spacer(),
|
||||
IconButton(
|
||||
onPressed: () => Navigator.of(context).pop(),
|
||||
icon: Icon(Icons.close, color: WebTheme.getTextColor(context)),
|
||||
),
|
||||
],
|
||||
),
|
||||
|
||||
const SizedBox(height: 16),
|
||||
|
||||
// 表单内容
|
||||
Expanded(
|
||||
child: Form(
|
||||
key: _formKey,
|
||||
child: SingleChildScrollView(
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
// 基本信息 - 两列布局
|
||||
_buildSectionTitle('基本信息'),
|
||||
Row(
|
||||
children: [
|
||||
Expanded(
|
||||
child: _buildTextField(
|
||||
controller: _providerController,
|
||||
label: '提供商 *',
|
||||
hint: '如: openai, anthropic',
|
||||
validator: (value) => value?.trim().isEmpty == true ? '请输入提供商名称' : null,
|
||||
),
|
||||
),
|
||||
const SizedBox(width: 12),
|
||||
Expanded(
|
||||
child: _buildTextField(
|
||||
controller: _modelIdController,
|
||||
label: '模型ID *',
|
||||
hint: '如: gpt-4, claude-3-opus',
|
||||
validator: (value) => value?.trim().isEmpty == true ? '请输入模型ID' : null,
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
|
||||
const SizedBox(height: 12),
|
||||
|
||||
Row(
|
||||
children: [
|
||||
Expanded(
|
||||
child: _buildTextField(
|
||||
controller: _displayNameController,
|
||||
label: '显示名称',
|
||||
hint: '用户界面显示的名称',
|
||||
),
|
||||
),
|
||||
const SizedBox(width: 12),
|
||||
Expanded(
|
||||
child: _buildTextField(
|
||||
controller: _apiEndpointController,
|
||||
label: 'API Endpoint',
|
||||
hint: '可选,自定义API地址',
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
|
||||
const SizedBox(height: 16),
|
||||
|
||||
// API Keys配置
|
||||
_buildSectionTitle('API Keys配置'),
|
||||
Row(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
Expanded(
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
_buildTextField(
|
||||
controller: _apiKeysController,
|
||||
label: 'API Keys',
|
||||
hint: '每行一个API Key,或保持不变',
|
||||
maxLines: 3,
|
||||
),
|
||||
const SizedBox(height: 4),
|
||||
Text(
|
||||
'提示: 如需修改API Keys,请清空并重新输入',
|
||||
style: TextStyle(
|
||||
fontSize: 11,
|
||||
color: WebTheme.getSecondaryTextColor(context),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
const SizedBox(width: 12),
|
||||
Expanded(
|
||||
child: _buildTextField(
|
||||
controller: _keyNotesController,
|
||||
label: 'Key备注',
|
||||
hint: '每行一个备注(可选)',
|
||||
maxLines: 3,
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
|
||||
const SizedBox(height: 16),
|
||||
|
||||
// 功能授权
|
||||
_buildSectionTitle('功能授权'),
|
||||
_buildFeatureSelection(),
|
||||
|
||||
const SizedBox(height: 16),
|
||||
|
||||
// 限制配置 - 三列布局
|
||||
_buildSectionTitle('限制配置'),
|
||||
Row(
|
||||
children: [
|
||||
Expanded(
|
||||
child: _buildTextField(
|
||||
controller: _creditRateController,
|
||||
label: '积分倍数',
|
||||
hint: '默认 1.0',
|
||||
keyboardType: TextInputType.number,
|
||||
validator: (value) {
|
||||
if (value?.isNotEmpty == true) {
|
||||
final parsed = double.tryParse(value!);
|
||||
if (parsed == null || parsed <= 0) return '请输入大于0的数字';
|
||||
}
|
||||
return null;
|
||||
},
|
||||
),
|
||||
),
|
||||
const SizedBox(width: 12),
|
||||
Expanded(
|
||||
child: _buildTextField(
|
||||
controller: _maxConcurrentController,
|
||||
label: '最大并发',
|
||||
hint: '-1表示无限制',
|
||||
keyboardType: TextInputType.number,
|
||||
),
|
||||
),
|
||||
const SizedBox(width: 12),
|
||||
Expanded(
|
||||
child: _buildTextField(
|
||||
controller: _priorityController,
|
||||
label: '优先级',
|
||||
hint: '数字越大优先级越高',
|
||||
keyboardType: TextInputType.number,
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
|
||||
const SizedBox(height: 12),
|
||||
|
||||
Row(
|
||||
children: [
|
||||
Expanded(
|
||||
child: _buildTextField(
|
||||
controller: _dailyLimitController,
|
||||
label: '每日请求限制',
|
||||
hint: '-1表示无限制',
|
||||
keyboardType: TextInputType.number,
|
||||
),
|
||||
),
|
||||
const SizedBox(width: 12),
|
||||
Expanded(
|
||||
child: _buildTextField(
|
||||
controller: _hourlyLimitController,
|
||||
label: '每小时请求限制',
|
||||
hint: '-1表示无限制',
|
||||
keyboardType: TextInputType.number,
|
||||
),
|
||||
),
|
||||
const SizedBox(width: 12),
|
||||
// 启用状态开关
|
||||
Expanded(
|
||||
child: SwitchListTile(
|
||||
title: Text(
|
||||
'启用状态',
|
||||
style: TextStyle(
|
||||
fontSize: 13,
|
||||
color: WebTheme.getTextColor(context),
|
||||
),
|
||||
),
|
||||
value: _enabled,
|
||||
onChanged: (value) => setState(() => _enabled = value),
|
||||
activeColor: Colors.green,
|
||||
contentPadding: EdgeInsets.zero,
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
|
||||
const SizedBox(height: 16),
|
||||
|
||||
// 其他信息
|
||||
_buildSectionTitle('其他信息'),
|
||||
_buildTextField(
|
||||
controller: _descriptionController,
|
||||
label: '描述',
|
||||
hint: '模型用途、特点等描述信息',
|
||||
maxLines: 2,
|
||||
),
|
||||
|
||||
const SizedBox(height: 12),
|
||||
|
||||
_buildTextField(
|
||||
controller: _tagsController,
|
||||
label: '标签',
|
||||
hint: '用逗号分隔,如: 高性能,推荐,beta',
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
|
||||
const SizedBox(height: 16),
|
||||
|
||||
// 底部按钮
|
||||
Row(
|
||||
mainAxisAlignment: MainAxisAlignment.end,
|
||||
children: [
|
||||
TextButton(
|
||||
onPressed: _isLoading ? null : () => Navigator.of(context).pop(),
|
||||
child: Text(
|
||||
'取消',
|
||||
style: TextStyle(color: WebTheme.getSecondaryTextColor(context)),
|
||||
),
|
||||
),
|
||||
const SizedBox(width: 12),
|
||||
ElevatedButton(
|
||||
onPressed: _isLoading ? null : () => _saveConfig(validate: false),
|
||||
style: ElevatedButton.styleFrom(
|
||||
backgroundColor: WebTheme.getSecondaryTextColor(context),
|
||||
foregroundColor: WebTheme.getBackgroundColor(context),
|
||||
),
|
||||
child: _isLoading
|
||||
? const SizedBox(
|
||||
width: 16,
|
||||
height: 16,
|
||||
child: CircularProgressIndicator(strokeWidth: 2),
|
||||
)
|
||||
: const Text('仅保存'),
|
||||
),
|
||||
const SizedBox(width: 8),
|
||||
ElevatedButton(
|
||||
onPressed: _isLoading ? null : () => _saveConfig(validate: true),
|
||||
style: ElevatedButton.styleFrom(
|
||||
backgroundColor: Colors.blue,
|
||||
foregroundColor: Colors.white,
|
||||
),
|
||||
child: _isLoading
|
||||
? const SizedBox(
|
||||
width: 16,
|
||||
height: 16,
|
||||
child: CircularProgressIndicator(strokeWidth: 2, color: Colors.white),
|
||||
)
|
||||
: const Text('保存并验证'),
|
||||
),
|
||||
],
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
Widget _buildSectionTitle(String title) {
|
||||
return Padding(
|
||||
padding: const EdgeInsets.only(bottom: 8),
|
||||
child: Text(
|
||||
title,
|
||||
style: TextStyle(
|
||||
fontSize: 14,
|
||||
fontWeight: FontWeight.w600,
|
||||
color: WebTheme.getTextColor(context),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
Widget _buildTextField({
|
||||
required TextEditingController controller,
|
||||
required String label,
|
||||
String? hint,
|
||||
int maxLines = 1,
|
||||
TextInputType? keyboardType,
|
||||
String? Function(String?)? validator,
|
||||
}) {
|
||||
return TextFormField(
|
||||
controller: controller,
|
||||
style: TextStyle(color: WebTheme.getTextColor(context), fontSize: 13),
|
||||
decoration: InputDecoration(
|
||||
labelText: label,
|
||||
hintText: hint,
|
||||
labelStyle: TextStyle(
|
||||
color: WebTheme.getSecondaryTextColor(context),
|
||||
fontSize: 12,
|
||||
),
|
||||
hintStyle: TextStyle(
|
||||
color: WebTheme.getSecondaryTextColor(context).withValues(alpha: 0.7),
|
||||
fontSize: 12,
|
||||
),
|
||||
border: OutlineInputBorder(
|
||||
borderRadius: BorderRadius.circular(6),
|
||||
borderSide: BorderSide(
|
||||
color: WebTheme.getBorderColor(context),
|
||||
width: 1,
|
||||
),
|
||||
),
|
||||
enabledBorder: OutlineInputBorder(
|
||||
borderRadius: BorderRadius.circular(6),
|
||||
borderSide: BorderSide(
|
||||
color: WebTheme.getBorderColor(context),
|
||||
width: 1,
|
||||
),
|
||||
),
|
||||
focusedBorder: OutlineInputBorder(
|
||||
borderRadius: BorderRadius.circular(6),
|
||||
borderSide: const BorderSide(
|
||||
color: Colors.blue,
|
||||
width: 1.5,
|
||||
),
|
||||
),
|
||||
contentPadding: const EdgeInsets.symmetric(horizontal: 12, vertical: 8),
|
||||
filled: true,
|
||||
fillColor: WebTheme.getBackgroundColor(context),
|
||||
isDense: true,
|
||||
),
|
||||
maxLines: maxLines,
|
||||
keyboardType: keyboardType,
|
||||
validator: validator,
|
||||
);
|
||||
}
|
||||
|
||||
Widget _buildFeatureSelection() {
|
||||
return Container(
|
||||
padding: const EdgeInsets.all(12),
|
||||
decoration: BoxDecoration(
|
||||
border: Border.all(color: WebTheme.getBorderColor(context)),
|
||||
borderRadius: BorderRadius.circular(6),
|
||||
color: WebTheme.getBackgroundColor(context),
|
||||
),
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
Text(
|
||||
'选择授权功能 (至少选择一个)',
|
||||
style: TextStyle(
|
||||
fontSize: 12,
|
||||
color: WebTheme.getSecondaryTextColor(context),
|
||||
),
|
||||
),
|
||||
const SizedBox(height: 8),
|
||||
Wrap(
|
||||
spacing: 8,
|
||||
runSpacing: 4,
|
||||
children: AIFeatureTypeHelper.allFeatures.map((featureType) {
|
||||
final bool isSelected = _selectedFeatures.contains(featureType);
|
||||
return FilterChip(
|
||||
label: Text(
|
||||
featureType.displayName,
|
||||
style: TextStyle(
|
||||
fontSize: 12,
|
||||
color: isSelected ? Colors.white : WebTheme.getTextColor(context),
|
||||
),
|
||||
),
|
||||
tooltip: _getFeatureDescription(featureType),
|
||||
selected: isSelected,
|
||||
onSelected: (selected) {
|
||||
setState(() {
|
||||
if (selected) {
|
||||
_selectedFeatures.add(featureType);
|
||||
} else {
|
||||
_selectedFeatures.remove(featureType);
|
||||
}
|
||||
});
|
||||
},
|
||||
selectedColor: Colors.blue,
|
||||
backgroundColor: WebTheme.getCardColor(context),
|
||||
showCheckmark: false,
|
||||
materialTapTargetSize: MaterialTapTargetSize.shrinkWrap,
|
||||
visualDensity: VisualDensity.compact,
|
||||
);
|
||||
}).toList(),
|
||||
),
|
||||
if (_selectedFeatures.isEmpty)
|
||||
const Padding(
|
||||
padding: EdgeInsets.only(top: 4),
|
||||
child: Text(
|
||||
'请至少选择一个功能',
|
||||
style: TextStyle(
|
||||
fontSize: 11,
|
||||
color: Colors.red,
|
||||
),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
String _getFeatureDescription(AIFeatureType type) {
|
||||
switch (type) {
|
||||
case AIFeatureType.aiChat:
|
||||
return 'AI对话功能';
|
||||
case AIFeatureType.textExpansion:
|
||||
return '文本内容扩展';
|
||||
case AIFeatureType.textRefactor:
|
||||
return '文本结构重构';
|
||||
case AIFeatureType.textSummary:
|
||||
return '文本内容总结';
|
||||
case AIFeatureType.sceneToSummary:
|
||||
return '场景生成摘要';
|
||||
case AIFeatureType.summaryToScene:
|
||||
return '摘要生成场景';
|
||||
case AIFeatureType.novelGeneration:
|
||||
return '小说内容生成';
|
||||
case AIFeatureType.professionalFictionContinuation:
|
||||
return '专业小说续写';
|
||||
case AIFeatureType.sceneBeatGeneration:
|
||||
return '场景节拍生成';
|
||||
case AIFeatureType.novelCompose:
|
||||
return '设定编排(大纲/章节/组合)';
|
||||
case AIFeatureType.settingTreeGeneration:
|
||||
return '设定树生成';
|
||||
}
|
||||
}
|
||||
|
||||
void _showSnackBar(String message, {bool isError = false}) {
|
||||
ScaffoldMessenger.of(context).showSnackBar(
|
||||
SnackBar(
|
||||
content: Text(message),
|
||||
backgroundColor: isError ? Colors.red : Colors.green,
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
559
AINoval/lib/screens/admin/widgets/edit_system_preset_dialog.dart
Normal file
559
AINoval/lib/screens/admin/widgets/edit_system_preset_dialog.dart
Normal file
@@ -0,0 +1,559 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'dart:convert';
|
||||
import 'package:crypto/crypto.dart';
|
||||
|
||||
import '../../../models/preset_models.dart';
|
||||
import '../../../models/prompt_models.dart';
|
||||
import '../../../models/context_selection_models.dart';
|
||||
import '../../../models/ai_request_models.dart';
|
||||
import '../../../services/api_service/repositories/impl/admin_repository_impl.dart';
|
||||
import '../../../utils/logger.dart';
|
||||
import '../../../widgets/common/form_dialog_template.dart';
|
||||
|
||||
/// 编辑系统预设对话框
|
||||
class EditSystemPresetDialog extends StatefulWidget {
|
||||
final AIPromptPreset preset;
|
||||
final VoidCallback? onSuccess;
|
||||
|
||||
const EditSystemPresetDialog({
|
||||
Key? key,
|
||||
required this.preset,
|
||||
this.onSuccess,
|
||||
}) : super(key: key);
|
||||
|
||||
@override
|
||||
State<EditSystemPresetDialog> createState() => _EditSystemPresetDialogState();
|
||||
}
|
||||
|
||||
class _EditSystemPresetDialogState extends State<EditSystemPresetDialog> {
|
||||
final _formKey = GlobalKey<FormState>();
|
||||
late final TextEditingController _nameController;
|
||||
late final TextEditingController _descriptionController;
|
||||
late final TextEditingController _systemPromptController;
|
||||
late final TextEditingController _userPromptController;
|
||||
late final TextEditingController _tagsController;
|
||||
|
||||
late String _selectedFeatureType;
|
||||
late bool _showInQuickAccess;
|
||||
bool _enableSmartContext = true;
|
||||
double _temperature = 0.7;
|
||||
double _topP = 0.9;
|
||||
String? _selectedTemplateId;
|
||||
late ContextSelectionData _contextSelectionData;
|
||||
bool _isLoading = false;
|
||||
|
||||
final AdminRepositoryImpl _adminRepository = AdminRepositoryImpl();
|
||||
// 功能类型选项改为从 AIFeatureTypeHelper.allFeatures 动态获取
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
_initializeControllers();
|
||||
}
|
||||
|
||||
void _initializeControllers() {
|
||||
_nameController = TextEditingController(text: widget.preset.presetName ?? '');
|
||||
_descriptionController = TextEditingController(text: widget.preset.presetDescription ?? '');
|
||||
_systemPromptController = TextEditingController(text: widget.preset.systemPrompt);
|
||||
_userPromptController = TextEditingController(text: widget.preset.userPrompt);
|
||||
_tagsController = TextEditingController(
|
||||
text: widget.preset.presetTags?.join(', ') ?? '',
|
||||
);
|
||||
|
||||
// 如果传入的功能类型不在枚举表中,退回到一个安全的默认值,避免 Dropdown 报错
|
||||
final allApi = AIFeatureTypeHelper.allFeatures.map((e) => e.toApiString()).toList();
|
||||
_selectedFeatureType = allApi.contains(widget.preset.aiFeatureType)
|
||||
? widget.preset.aiFeatureType
|
||||
: AIFeatureType.aiChat.toApiString();
|
||||
_showInQuickAccess = widget.preset.showInQuickAccess;
|
||||
|
||||
// 初始化上下文与参数(从请求数据解析)
|
||||
try {
|
||||
final request = widget.preset.parsedRequest;
|
||||
if (request != null) {
|
||||
_enableSmartContext = request.enableSmartContext;
|
||||
_contextSelectionData = request.contextSelections ?? FormFieldFactory.createPresetTemplateContextData();
|
||||
final temp = request.parameters['temperature'];
|
||||
if (temp is num) _temperature = temp.toDouble();
|
||||
final topP = request.parameters['topP'];
|
||||
if (topP is num) _topP = topP.toDouble();
|
||||
final tmpl = request.parameters['promptTemplateId'];
|
||||
if (tmpl is String && tmpl.isNotEmpty) _selectedTemplateId = tmpl;
|
||||
} else {
|
||||
_contextSelectionData = FormFieldFactory.createPresetTemplateContextData();
|
||||
}
|
||||
} catch (e) {
|
||||
_contextSelectionData = FormFieldFactory.createPresetTemplateContextData();
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
void dispose() {
|
||||
_nameController.dispose();
|
||||
_descriptionController.dispose();
|
||||
_systemPromptController.dispose();
|
||||
_userPromptController.dispose();
|
||||
_tagsController.dispose();
|
||||
super.dispose();
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Dialog(
|
||||
child: Container(
|
||||
width: 600,
|
||||
constraints: const BoxConstraints(maxHeight: 700),
|
||||
padding: const EdgeInsets.all(24),
|
||||
child: Column(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
Row(
|
||||
children: [
|
||||
const Icon(Icons.edit, size: 24),
|
||||
const SizedBox(width: 8),
|
||||
const Text(
|
||||
'编辑系统预设',
|
||||
style: TextStyle(fontSize: 20, fontWeight: FontWeight.bold),
|
||||
),
|
||||
const Spacer(),
|
||||
IconButton(
|
||||
onPressed: () => Navigator.of(context).pop(),
|
||||
icon: const Icon(Icons.close),
|
||||
),
|
||||
],
|
||||
),
|
||||
const SizedBox(height: 24),
|
||||
|
||||
Expanded(
|
||||
child: Form(
|
||||
key: _formKey,
|
||||
child: SingleChildScrollView(
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
_buildPresetInfo(),
|
||||
const SizedBox(height: 24),
|
||||
_buildBasicInfoSection(),
|
||||
const SizedBox(height: 24),
|
||||
_buildPromptSection(),
|
||||
const SizedBox(height: 24),
|
||||
_buildSettingsSection(),
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
|
||||
const SizedBox(height: 24),
|
||||
_buildActionButtons(),
|
||||
],
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
Widget _buildPresetInfo() {
|
||||
return Container(
|
||||
padding: const EdgeInsets.all(16),
|
||||
decoration: BoxDecoration(
|
||||
color: Theme.of(context).colorScheme.surface,
|
||||
borderRadius: BorderRadius.circular(8),
|
||||
border: Border.all(
|
||||
color: Theme.of(context).colorScheme.outline.withOpacity(0.2),
|
||||
),
|
||||
),
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
Row(
|
||||
children: [
|
||||
const Icon(Icons.info_outline, size: 20),
|
||||
const SizedBox(width: 8),
|
||||
const Text(
|
||||
'预设信息',
|
||||
style: TextStyle(fontWeight: FontWeight.w500),
|
||||
),
|
||||
],
|
||||
),
|
||||
const SizedBox(height: 12),
|
||||
Row(
|
||||
children: [
|
||||
Expanded(
|
||||
child: _buildInfoItem('预设ID', widget.preset.presetId),
|
||||
),
|
||||
const SizedBox(width: 16),
|
||||
Expanded(
|
||||
child: _buildInfoItem('使用次数', '${widget.preset.useCount}'),
|
||||
),
|
||||
],
|
||||
),
|
||||
const SizedBox(height: 8),
|
||||
Row(
|
||||
children: [
|
||||
Expanded(
|
||||
child: _buildInfoItem('创建时间', _formatDateTime(widget.preset.createdAt) ?? '未知'),
|
||||
),
|
||||
const SizedBox(width: 16),
|
||||
Expanded(
|
||||
child: _buildInfoItem('最后使用', _formatDateTime(widget.preset.lastUsedAt) ?? '从未使用'),
|
||||
),
|
||||
],
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
Widget _buildInfoItem(String label, String value) {
|
||||
return Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
Text(
|
||||
label,
|
||||
style: TextStyle(
|
||||
fontSize: 12,
|
||||
color: Theme.of(context).colorScheme.onSurface.withOpacity(0.6),
|
||||
),
|
||||
),
|
||||
Text(
|
||||
value,
|
||||
style: const TextStyle(fontSize: 14),
|
||||
),
|
||||
],
|
||||
);
|
||||
}
|
||||
|
||||
Widget _buildBasicInfoSection() {
|
||||
return Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
const Text(
|
||||
'基本信息',
|
||||
style: TextStyle(fontSize: 16, fontWeight: FontWeight.bold),
|
||||
),
|
||||
const SizedBox(height: 16),
|
||||
|
||||
TextFormField(
|
||||
controller: _nameController,
|
||||
decoration: const InputDecoration(
|
||||
labelText: '预设名称 *',
|
||||
hintText: '请输入预设名称',
|
||||
border: OutlineInputBorder(),
|
||||
),
|
||||
validator: (value) {
|
||||
if (value == null || value.trim().isEmpty) {
|
||||
return '请输入预设名称';
|
||||
}
|
||||
return null;
|
||||
},
|
||||
),
|
||||
const SizedBox(height: 16),
|
||||
|
||||
TextFormField(
|
||||
controller: _descriptionController,
|
||||
decoration: const InputDecoration(
|
||||
labelText: '预设描述',
|
||||
hintText: '请输入预设描述',
|
||||
border: OutlineInputBorder(),
|
||||
),
|
||||
maxLines: 2,
|
||||
),
|
||||
const SizedBox(height: 16),
|
||||
|
||||
DropdownButtonFormField<String>(
|
||||
value: _selectedFeatureType,
|
||||
decoration: const InputDecoration(
|
||||
labelText: '功能类型 *',
|
||||
border: OutlineInputBorder(),
|
||||
),
|
||||
items: AIFeatureTypeHelper.allFeatures.map((t) {
|
||||
final api = t.toApiString();
|
||||
return DropdownMenuItem(
|
||||
value: api,
|
||||
child: Text(t.displayName),
|
||||
);
|
||||
}).toList(),
|
||||
onChanged: (value) {
|
||||
if (value != null) {
|
||||
setState(() {
|
||||
_selectedFeatureType = value;
|
||||
});
|
||||
}
|
||||
},
|
||||
),
|
||||
const SizedBox(height: 16),
|
||||
|
||||
TextFormField(
|
||||
controller: _tagsController,
|
||||
decoration: const InputDecoration(
|
||||
labelText: '标签',
|
||||
hintText: '请输入标签,用逗号分隔',
|
||||
border: OutlineInputBorder(),
|
||||
),
|
||||
),
|
||||
],
|
||||
);
|
||||
}
|
||||
|
||||
Widget _buildPromptSection() {
|
||||
return Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
const Text(
|
||||
'提示词配置',
|
||||
style: TextStyle(fontSize: 16, fontWeight: FontWeight.bold),
|
||||
),
|
||||
const SizedBox(height: 16),
|
||||
|
||||
TextFormField(
|
||||
controller: _systemPromptController,
|
||||
decoration: const InputDecoration(
|
||||
labelText: '系统提示词 *',
|
||||
hintText: '请输入系统提示词',
|
||||
border: OutlineInputBorder(),
|
||||
),
|
||||
maxLines: 5,
|
||||
validator: (value) {
|
||||
if (value == null || value.trim().isEmpty) {
|
||||
return '请输入系统提示词';
|
||||
}
|
||||
return null;
|
||||
},
|
||||
),
|
||||
const SizedBox(height: 16),
|
||||
|
||||
TextFormField(
|
||||
controller: _userPromptController,
|
||||
decoration: const InputDecoration(
|
||||
labelText: '用户提示词',
|
||||
hintText: '请输入用户提示词',
|
||||
border: OutlineInputBorder(),
|
||||
),
|
||||
maxLines: 3,
|
||||
),
|
||||
],
|
||||
);
|
||||
}
|
||||
|
||||
Widget _buildSettingsSection() {
|
||||
return Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
const Text(
|
||||
'设置选项',
|
||||
style: TextStyle(fontSize: 16, fontWeight: FontWeight.bold),
|
||||
),
|
||||
const SizedBox(height: 16),
|
||||
|
||||
CheckboxListTile(
|
||||
title: const Text('显示在快捷访问'),
|
||||
subtitle: const Text('用户可以在快捷访问列表中看到此预设'),
|
||||
value: _showInQuickAccess,
|
||||
onChanged: (value) {
|
||||
setState(() {
|
||||
_showInQuickAccess = value ?? false;
|
||||
});
|
||||
},
|
||||
),
|
||||
|
||||
const SizedBox(height: 8),
|
||||
CheckboxListTile(
|
||||
title: const Text('启用智能上下文'),
|
||||
value: _enableSmartContext,
|
||||
onChanged: (v) {
|
||||
setState(() {
|
||||
_enableSmartContext = v ?? true;
|
||||
});
|
||||
},
|
||||
),
|
||||
|
||||
const SizedBox(height: 8),
|
||||
// 温度
|
||||
FormFieldFactory.createTemperatureSliderField(
|
||||
context: context,
|
||||
value: _temperature,
|
||||
onChanged: (v) => setState(() => _temperature = v),
|
||||
),
|
||||
|
||||
const SizedBox(height: 8),
|
||||
// Top-P
|
||||
FormFieldFactory.createTopPSliderField(
|
||||
context: context,
|
||||
value: _topP,
|
||||
onChanged: (v) => setState(() => _topP = v),
|
||||
),
|
||||
|
||||
const SizedBox(height: 8),
|
||||
// 上下文选择
|
||||
FormFieldFactory.createContextSelectionField(
|
||||
contextData: _contextSelectionData,
|
||||
onSelectionChanged: (d) => setState(() => _contextSelectionData = d),
|
||||
title: '上下文选择',
|
||||
description: '选择参与提示词生成的上下文信息',
|
||||
),
|
||||
],
|
||||
);
|
||||
}
|
||||
|
||||
Widget _buildActionButtons() {
|
||||
return Row(
|
||||
mainAxisAlignment: MainAxisAlignment.end,
|
||||
children: [
|
||||
TextButton(
|
||||
onPressed: _isLoading ? null : () => Navigator.of(context).pop(),
|
||||
child: const Text('取消'),
|
||||
),
|
||||
const SizedBox(width: 12),
|
||||
ElevatedButton(
|
||||
onPressed: _isLoading ? null : _updatePreset,
|
||||
child: _isLoading
|
||||
? const SizedBox(
|
||||
width: 20,
|
||||
height: 20,
|
||||
child: CircularProgressIndicator(strokeWidth: 2),
|
||||
)
|
||||
: const Text('保存'),
|
||||
),
|
||||
],
|
||||
);
|
||||
}
|
||||
|
||||
Future<void> _updatePreset() async {
|
||||
if (!_formKey.currentState!.validate()) {
|
||||
return;
|
||||
}
|
||||
|
||||
setState(() {
|
||||
_isLoading = true;
|
||||
});
|
||||
|
||||
try {
|
||||
final tags = _tagsController.text
|
||||
.split(',')
|
||||
.map((tag) => tag.trim())
|
||||
.where((tag) => tag.isNotEmpty)
|
||||
.toList();
|
||||
|
||||
// 构建统一请求数据(包含上下文与参数)
|
||||
final requestJson = _buildRequestDataJson();
|
||||
final newHash = _generatePresetHash(requestJson);
|
||||
|
||||
final updatedPreset = widget.preset.copyWith(
|
||||
presetName: _nameController.text.trim(),
|
||||
presetDescription: _descriptionController.text.trim().isEmpty
|
||||
? null : _descriptionController.text.trim(),
|
||||
aiFeatureType: _selectedFeatureType,
|
||||
systemPrompt: _systemPromptController.text.trim(),
|
||||
userPrompt: _userPromptController.text.trim().isEmpty
|
||||
? '' : _userPromptController.text.trim(),
|
||||
presetTags: tags.isEmpty ? null : tags,
|
||||
showInQuickAccess: _showInQuickAccess,
|
||||
requestData: requestJson,
|
||||
presetHash: newHash,
|
||||
updatedAt: DateTime.now(),
|
||||
);
|
||||
|
||||
await _adminRepository.updateSystemPreset(updatedPreset);
|
||||
|
||||
if (mounted) {
|
||||
Navigator.of(context).pop();
|
||||
ScaffoldMessenger.of(context).showSnackBar(
|
||||
SnackBar(content: Text('系统预设 "${updatedPreset.presetName}" 更新成功')),
|
||||
);
|
||||
widget.onSuccess?.call();
|
||||
}
|
||||
} catch (e) {
|
||||
AppLogger.e('EditSystemPresetDialog', '更新系统预设失败', e);
|
||||
if (mounted) {
|
||||
ScaffoldMessenger.of(context).showSnackBar(
|
||||
SnackBar(content: Text('更新失败: $e')),
|
||||
);
|
||||
}
|
||||
} finally {
|
||||
if (mounted) {
|
||||
setState(() {
|
||||
_isLoading = false;
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
String _buildRequestDataJson() {
|
||||
// 将系统预设编辑为一个可回放的 UniversalAIRequest
|
||||
final reqType = _mapFeatureTypeToRequestType(_selectedFeatureType);
|
||||
final request = UniversalAIRequest(
|
||||
requestType: reqType,
|
||||
userId: widget.preset.userId,
|
||||
novelId: _contextSelectionData.novelId,
|
||||
instructions: _userPromptController.text.trim().isNotEmpty
|
||||
? _userPromptController.text.trim()
|
||||
: null,
|
||||
contextSelections: _contextSelectionData,
|
||||
enableSmartContext: _enableSmartContext,
|
||||
parameters: {
|
||||
'enableSmartContext': _enableSmartContext,
|
||||
'temperature': _temperature,
|
||||
'topP': _topP,
|
||||
if (_selectedTemplateId != null) 'promptTemplateId': _selectedTemplateId,
|
||||
},
|
||||
metadata: {
|
||||
'source': 'admin_system_preset_editor',
|
||||
},
|
||||
);
|
||||
return jsonEncode(request.toApiJson());
|
||||
}
|
||||
|
||||
AIRequestType _mapFeatureTypeToRequestType(String featureType) {
|
||||
try {
|
||||
final ft = AIFeatureTypeHelper.fromApiString(featureType.toUpperCase());
|
||||
switch (ft) {
|
||||
case AIFeatureType.textExpansion:
|
||||
return AIRequestType.expansion;
|
||||
case AIFeatureType.textSummary:
|
||||
return AIRequestType.summary;
|
||||
case AIFeatureType.textRefactor:
|
||||
return AIRequestType.refactor;
|
||||
case AIFeatureType.aiChat:
|
||||
return AIRequestType.chat;
|
||||
case AIFeatureType.sceneToSummary:
|
||||
return AIRequestType.sceneSummary;
|
||||
case AIFeatureType.novelGeneration:
|
||||
return AIRequestType.generation;
|
||||
case AIFeatureType.novelCompose:
|
||||
return AIRequestType.novelCompose;
|
||||
default:
|
||||
return AIRequestType.expansion;
|
||||
}
|
||||
} catch (_) {
|
||||
return AIRequestType.expansion;
|
||||
}
|
||||
}
|
||||
|
||||
String _generatePresetHash(String requestDataJson) {
|
||||
try {
|
||||
final bytes = utf8.encode(requestDataJson);
|
||||
final digest = sha256.convert(bytes);
|
||||
return digest.toString();
|
||||
} catch (_) {
|
||||
return DateTime.now().millisecondsSinceEpoch.toString();
|
||||
}
|
||||
}
|
||||
|
||||
String? _formatDateTime(DateTime? dateTime) {
|
||||
if (dateTime == null) return null;
|
||||
|
||||
final now = DateTime.now();
|
||||
final difference = now.difference(dateTime);
|
||||
|
||||
if (difference.inDays > 0) {
|
||||
return '${difference.inDays}天前';
|
||||
} else if (difference.inHours > 0) {
|
||||
return '${difference.inHours}小时前';
|
||||
} else if (difference.inMinutes > 0) {
|
||||
return '${difference.inMinutes}分钟前';
|
||||
} else {
|
||||
return '刚刚';
|
||||
}
|
||||
}
|
||||
}
|
||||
744
AINoval/lib/screens/admin/widgets/edit_template_dialog.dart
Normal file
744
AINoval/lib/screens/admin/widgets/edit_template_dialog.dart
Normal file
@@ -0,0 +1,744 @@
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
import '../../../models/prompt_models.dart';
|
||||
import '../../../services/api_service/repositories/impl/admin_repository_templates_extension.dart';
|
||||
import '../../../services/api_service/repositories/impl/admin_repository_impl.dart';
|
||||
import '../../../utils/web_theme.dart';
|
||||
import '../../../utils/logger.dart';
|
||||
import '../../../widgets/common/dialog_container.dart';
|
||||
import '../../../widgets/common/dialog_header.dart';
|
||||
|
||||
/// 编辑提示词模板对话框
|
||||
class EditTemplateDialog extends StatefulWidget {
|
||||
final PromptTemplate template;
|
||||
final VoidCallback? onSuccess;
|
||||
|
||||
const EditTemplateDialog({
|
||||
Key? key,
|
||||
required this.template,
|
||||
this.onSuccess,
|
||||
}) : super(key: key);
|
||||
|
||||
@override
|
||||
State<EditTemplateDialog> createState() => _EditTemplateDialogState();
|
||||
}
|
||||
|
||||
class _EditTemplateDialogState extends State<EditTemplateDialog> with TickerProviderStateMixin {
|
||||
final _formKey = GlobalKey<FormState>();
|
||||
late final TextEditingController _nameController;
|
||||
late final TextEditingController _descriptionController;
|
||||
late final TextEditingController _systemPromptController;
|
||||
late final TextEditingController _userPromptController;
|
||||
late final TextEditingController _tagsController;
|
||||
|
||||
late AIFeatureType _featureType;
|
||||
late bool _isPublic;
|
||||
late bool _isVerified;
|
||||
late bool _isDefault;
|
||||
bool _isLoading = false;
|
||||
bool _isEdited = false;
|
||||
|
||||
late TabController _tabController;
|
||||
|
||||
final AdminRepositoryImpl _adminRepository = AdminRepositoryImpl();
|
||||
|
||||
final List<AIFeatureType> _featureTypes = [
|
||||
AIFeatureType.textExpansion,
|
||||
AIFeatureType.textRefactor,
|
||||
AIFeatureType.textSummary,
|
||||
AIFeatureType.sceneToSummary,
|
||||
AIFeatureType.summaryToScene,
|
||||
AIFeatureType.aiChat,
|
||||
AIFeatureType.novelGeneration,
|
||||
AIFeatureType.professionalFictionContinuation,
|
||||
AIFeatureType.sceneBeatGeneration,
|
||||
AIFeatureType.novelCompose,
|
||||
AIFeatureType.settingTreeGeneration,
|
||||
];
|
||||
|
||||
final Map<AIFeatureType, String> _featureTypeLabels = {
|
||||
AIFeatureType.textExpansion: '文本扩写',
|
||||
AIFeatureType.textRefactor: '文本润色',
|
||||
AIFeatureType.textSummary: '文本总结',
|
||||
AIFeatureType.sceneToSummary: '场景转摘要',
|
||||
AIFeatureType.summaryToScene: '摘要转场景',
|
||||
AIFeatureType.aiChat: 'AI对话',
|
||||
AIFeatureType.novelGeneration: '小说生成',
|
||||
AIFeatureType.professionalFictionContinuation: '专业续写',
|
||||
AIFeatureType.sceneBeatGeneration: '场景节拍生成',
|
||||
AIFeatureType.novelCompose: '设定编排',
|
||||
AIFeatureType.settingTreeGeneration: '设定树生成',
|
||||
};
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
_tabController = TabController(length: 2, vsync: this);
|
||||
_initializeControllers();
|
||||
}
|
||||
|
||||
void _initializeControllers() {
|
||||
_nameController = TextEditingController(text: widget.template.name);
|
||||
_descriptionController = TextEditingController(text: widget.template.description ?? '');
|
||||
// 将content拆分为systemPrompt和userPrompt,这里简单处理
|
||||
_systemPromptController = TextEditingController(text: '');
|
||||
_userPromptController = TextEditingController(text: widget.template.content);
|
||||
_tagsController = TextEditingController(text: widget.template.templateTags?.join(', ') ?? '');
|
||||
|
||||
_featureType = widget.template.featureType;
|
||||
_isPublic = widget.template.isPublic;
|
||||
_isVerified = widget.template.isVerified;
|
||||
_isDefault = widget.template.isDefault;
|
||||
}
|
||||
|
||||
@override
|
||||
void dispose() {
|
||||
_tabController.dispose();
|
||||
_nameController.dispose();
|
||||
_descriptionController.dispose();
|
||||
_systemPromptController.dispose();
|
||||
_userPromptController.dispose();
|
||||
_tagsController.dispose();
|
||||
super.dispose();
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return DialogContainer(
|
||||
maxWidth: 800,
|
||||
height: 700,
|
||||
child: Column(
|
||||
children: [
|
||||
DialogHeader(
|
||||
title: '编辑模板 - ${widget.template.name}',
|
||||
onClose: () => Navigator.of(context).pop(),
|
||||
),
|
||||
_buildTopBar(),
|
||||
_buildTabBar(),
|
||||
Expanded(
|
||||
child: TabBarView(
|
||||
controller: _tabController,
|
||||
children: [
|
||||
_buildContentEditor(),
|
||||
_buildPropertiesEditor(),
|
||||
],
|
||||
),
|
||||
),
|
||||
_buildActions(),
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
/// 构建顶部标题栏(参考业务组件)
|
||||
Widget _buildTopBar() {
|
||||
return Container(
|
||||
padding: const EdgeInsets.symmetric(horizontal: 20, vertical: 12),
|
||||
decoration: BoxDecoration(
|
||||
color: WebTheme.getSurfaceColor(context),
|
||||
border: Border(
|
||||
bottom: BorderSide(
|
||||
color: WebTheme.getBorderColor(context),
|
||||
width: 1.0,
|
||||
),
|
||||
),
|
||||
),
|
||||
child: Row(
|
||||
children: [
|
||||
// 模板标题编辑
|
||||
Expanded(
|
||||
child: TextField(
|
||||
controller: _nameController,
|
||||
style: TextStyle(
|
||||
fontSize: 18,
|
||||
fontWeight: FontWeight.w600,
|
||||
color: WebTheme.getTextColor(context),
|
||||
height: 1.2,
|
||||
),
|
||||
decoration: InputDecoration(
|
||||
hintText: '输入模板名称...',
|
||||
border: InputBorder.none,
|
||||
hintStyle: TextStyle(
|
||||
color: WebTheme.getSecondaryTextColor(context),
|
||||
),
|
||||
),
|
||||
onChanged: (value) {
|
||||
setState(() {
|
||||
_isEdited = true;
|
||||
});
|
||||
},
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
/// 构建标签栏
|
||||
Widget _buildTabBar() {
|
||||
return Container(
|
||||
decoration: BoxDecoration(
|
||||
color: WebTheme.getSurfaceColor(context),
|
||||
border: Border(
|
||||
bottom: BorderSide(
|
||||
color: WebTheme.getBorderColor(context),
|
||||
width: 1.0,
|
||||
),
|
||||
),
|
||||
),
|
||||
child: TabBar(
|
||||
controller: _tabController,
|
||||
labelColor: WebTheme.getTextColor(context),
|
||||
unselectedLabelColor: WebTheme.getSecondaryTextColor(context),
|
||||
indicatorColor: WebTheme.getTextColor(context),
|
||||
dividerColor: Colors.transparent,
|
||||
tabs: const [
|
||||
Tab(
|
||||
text: '内容编辑',
|
||||
icon: Icon(Icons.edit, size: 16),
|
||||
),
|
||||
Tab(
|
||||
text: '属性设置',
|
||||
icon: Icon(Icons.settings, size: 16),
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
/// 构建内容编辑器(参考 PromptContentEditor)
|
||||
Widget _buildContentEditor() {
|
||||
return Container(
|
||||
color: WebTheme.getSurfaceColor(context),
|
||||
padding: const EdgeInsets.all(16),
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
// 占位符提示
|
||||
_buildPlaceholderChips(),
|
||||
|
||||
const SizedBox(height: 16),
|
||||
|
||||
// 系统提示词编辑器
|
||||
_buildSystemPromptEditor(),
|
||||
|
||||
const SizedBox(height: 16),
|
||||
|
||||
// 用户提示词编辑器
|
||||
Expanded(
|
||||
child: _buildUserPromptEditor(),
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
/// 构建占位符提示
|
||||
Widget _buildPlaceholderChips() {
|
||||
final placeholders = [
|
||||
'content', 'context', 'requirement', 'style', 'length'
|
||||
];
|
||||
|
||||
return Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
Text(
|
||||
'可用占位符',
|
||||
style: TextStyle(
|
||||
fontSize: 14,
|
||||
fontWeight: FontWeight.w600,
|
||||
color: WebTheme.getTextColor(context),
|
||||
),
|
||||
),
|
||||
const SizedBox(height: 8),
|
||||
Wrap(
|
||||
spacing: 8,
|
||||
runSpacing: 4,
|
||||
children: placeholders.map((placeholder) => _buildPlaceholderChip(placeholder)).toList(),
|
||||
),
|
||||
],
|
||||
);
|
||||
}
|
||||
|
||||
/// 构建占位符芯片
|
||||
Widget _buildPlaceholderChip(String placeholder) {
|
||||
final primaryColor = WebTheme.getPrimaryColor(context);
|
||||
|
||||
return Tooltip(
|
||||
message: _getPlaceholderDescription(placeholder),
|
||||
child: ActionChip(
|
||||
label: Text(
|
||||
'{$placeholder}',
|
||||
style: TextStyle(
|
||||
fontSize: 12,
|
||||
color: primaryColor,
|
||||
),
|
||||
),
|
||||
onPressed: () {
|
||||
_insertPlaceholder(placeholder);
|
||||
},
|
||||
backgroundColor: primaryColor.withOpacity(0.1),
|
||||
side: BorderSide(
|
||||
color: primaryColor.withOpacity(0.3),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
/// 构建系统提示词编辑器
|
||||
Widget _buildSystemPromptEditor() {
|
||||
return Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
Text(
|
||||
'系统提示词 (System Prompt)',
|
||||
style: TextStyle(
|
||||
fontSize: 14,
|
||||
fontWeight: FontWeight.w600,
|
||||
color: WebTheme.getTextColor(context),
|
||||
),
|
||||
),
|
||||
const SizedBox(height: 8),
|
||||
Container(
|
||||
height: 120,
|
||||
decoration: BoxDecoration(
|
||||
border: Border.all(
|
||||
color: WebTheme.getBorderColor(context),
|
||||
),
|
||||
borderRadius: BorderRadius.circular(8),
|
||||
color: WebTheme.getSurfaceColor(context),
|
||||
),
|
||||
child: TextField(
|
||||
controller: _systemPromptController,
|
||||
maxLines: null,
|
||||
expands: true,
|
||||
textAlignVertical: TextAlignVertical.top,
|
||||
decoration: InputDecoration(
|
||||
hintText: '输入系统提示词...\n\n系统提示词用于设置AI的角色和基本行为规则。',
|
||||
hintStyle: TextStyle(
|
||||
color: WebTheme.getSecondaryTextColor(context),
|
||||
),
|
||||
border: InputBorder.none,
|
||||
contentPadding: const EdgeInsets.all(12),
|
||||
),
|
||||
style: TextStyle(
|
||||
fontSize: 14,
|
||||
height: 1.4,
|
||||
color: WebTheme.getTextColor(context),
|
||||
),
|
||||
onChanged: (value) {
|
||||
setState(() {
|
||||
_isEdited = true;
|
||||
});
|
||||
},
|
||||
),
|
||||
),
|
||||
],
|
||||
);
|
||||
}
|
||||
|
||||
/// 构建用户提示词编辑器
|
||||
Widget _buildUserPromptEditor() {
|
||||
return Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
Text(
|
||||
'用户提示词 (User Prompt)',
|
||||
style: TextStyle(
|
||||
fontSize: 14,
|
||||
fontWeight: FontWeight.w600,
|
||||
color: WebTheme.getTextColor(context),
|
||||
),
|
||||
),
|
||||
const SizedBox(height: 8),
|
||||
Expanded(
|
||||
child: Container(
|
||||
decoration: BoxDecoration(
|
||||
border: Border.all(
|
||||
color: WebTheme.getBorderColor(context),
|
||||
),
|
||||
borderRadius: BorderRadius.circular(8),
|
||||
color: WebTheme.getSurfaceColor(context),
|
||||
),
|
||||
child: TextField(
|
||||
controller: _userPromptController,
|
||||
maxLines: null,
|
||||
expands: true,
|
||||
textAlignVertical: TextAlignVertical.top,
|
||||
decoration: InputDecoration(
|
||||
hintText: '输入用户提示词...\n\n用户提示词包含具体的任务指令和要求。可以使用占位符来动态插入内容。',
|
||||
hintStyle: TextStyle(
|
||||
color: WebTheme.getSecondaryTextColor(context),
|
||||
),
|
||||
border: InputBorder.none,
|
||||
contentPadding: const EdgeInsets.all(12),
|
||||
),
|
||||
style: TextStyle(
|
||||
fontSize: 14,
|
||||
height: 1.4,
|
||||
color: WebTheme.getTextColor(context),
|
||||
),
|
||||
onChanged: (value) {
|
||||
setState(() {
|
||||
_isEdited = true;
|
||||
});
|
||||
},
|
||||
),
|
||||
),
|
||||
),
|
||||
],
|
||||
);
|
||||
}
|
||||
|
||||
/// 构建属性编辑器(参考 PromptPropertiesEditor)
|
||||
Widget _buildPropertiesEditor() {
|
||||
return Container(
|
||||
color: WebTheme.getSurfaceColor(context),
|
||||
padding: const EdgeInsets.all(16),
|
||||
child: SingleChildScrollView(
|
||||
child: Form(
|
||||
key: _formKey,
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
_buildBasicInfo(),
|
||||
const SizedBox(height: 24),
|
||||
_buildSettings(),
|
||||
const SizedBox(height: 24),
|
||||
_buildMetadata(),
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
Widget _buildBasicInfo() {
|
||||
return Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
Text(
|
||||
'基础信息',
|
||||
style: TextStyle(
|
||||
fontSize: 18,
|
||||
fontWeight: FontWeight.bold,
|
||||
color: WebTheme.getTextColor(context),
|
||||
),
|
||||
),
|
||||
const SizedBox(height: 16),
|
||||
TextFormField(
|
||||
controller: _nameController,
|
||||
decoration: const InputDecoration(
|
||||
labelText: '模板名称 *',
|
||||
hintText: '请输入模板名称',
|
||||
border: OutlineInputBorder(),
|
||||
),
|
||||
validator: (value) {
|
||||
if (value == null || value.trim().isEmpty) {
|
||||
return '请输入模板名称';
|
||||
}
|
||||
return null;
|
||||
},
|
||||
),
|
||||
const SizedBox(height: 16),
|
||||
TextFormField(
|
||||
controller: _descriptionController,
|
||||
decoration: const InputDecoration(
|
||||
labelText: '模板描述',
|
||||
hintText: '请输入模板描述',
|
||||
border: OutlineInputBorder(),
|
||||
),
|
||||
maxLines: 3,
|
||||
onChanged: (value) {
|
||||
setState(() {
|
||||
_isEdited = true;
|
||||
});
|
||||
},
|
||||
),
|
||||
const SizedBox(height: 16),
|
||||
DropdownButtonFormField<AIFeatureType>(
|
||||
value: _featureType,
|
||||
decoration: const InputDecoration(
|
||||
labelText: '功能类型 *',
|
||||
border: OutlineInputBorder(),
|
||||
),
|
||||
items: _featureTypes.map((type) {
|
||||
return DropdownMenuItem(
|
||||
value: type,
|
||||
child: Text(_featureTypeLabels[type] ?? type.name),
|
||||
);
|
||||
}).toList(),
|
||||
onChanged: (value) {
|
||||
if (value != null) {
|
||||
setState(() {
|
||||
_featureType = value;
|
||||
_isEdited = true;
|
||||
});
|
||||
}
|
||||
},
|
||||
),
|
||||
const SizedBox(height: 16),
|
||||
TextFormField(
|
||||
controller: _tagsController,
|
||||
decoration: const InputDecoration(
|
||||
labelText: '标签',
|
||||
hintText: '请输入标签,用逗号分隔',
|
||||
border: OutlineInputBorder(),
|
||||
),
|
||||
onChanged: (value) {
|
||||
setState(() {
|
||||
_isEdited = true;
|
||||
});
|
||||
},
|
||||
),
|
||||
],
|
||||
);
|
||||
}
|
||||
|
||||
/// 插入占位符
|
||||
void _insertPlaceholder(String placeholder) {
|
||||
final currentText = _userPromptController.text;
|
||||
final selection = _userPromptController.selection;
|
||||
final newText = currentText.replaceRange(
|
||||
selection.start,
|
||||
selection.end,
|
||||
'{$placeholder}',
|
||||
);
|
||||
_userPromptController.text = newText;
|
||||
_userPromptController.selection = TextSelection.fromPosition(
|
||||
TextPosition(offset: selection.start + placeholder.length + 2),
|
||||
);
|
||||
setState(() {
|
||||
_isEdited = true;
|
||||
});
|
||||
}
|
||||
|
||||
/// 获取占位符描述
|
||||
String _getPlaceholderDescription(String placeholder) {
|
||||
switch (placeholder) {
|
||||
case 'content':
|
||||
return '要处理的主要内容';
|
||||
case 'context':
|
||||
return '上下文信息';
|
||||
case 'requirement':
|
||||
return '具体要求';
|
||||
case 'style':
|
||||
return '风格要求';
|
||||
case 'length':
|
||||
return '长度要求';
|
||||
default:
|
||||
return '占位符:$placeholder';
|
||||
}
|
||||
}
|
||||
|
||||
/// 构建元数据显示
|
||||
Widget _buildMetadata() {
|
||||
return Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
Text(
|
||||
'元数据',
|
||||
style: TextStyle(
|
||||
fontSize: 14,
|
||||
fontWeight: FontWeight.w600,
|
||||
color: WebTheme.getTextColor(context),
|
||||
),
|
||||
),
|
||||
const SizedBox(height: 12),
|
||||
_buildMetadataRow('创建时间', _formatDateTime(widget.template.createdAt)),
|
||||
_buildMetadataRow('更新时间', _formatDateTime(widget.template.updatedAt)),
|
||||
_buildMetadataRow('使用次数', widget.template.useCount?.toString() ?? '0'),
|
||||
_buildMetadataRow('评分', widget.template.averageRating?.toStringAsFixed(1) ?? '无'),
|
||||
],
|
||||
);
|
||||
}
|
||||
|
||||
/// 构建元数据行
|
||||
Widget _buildMetadataRow(String label, String value) {
|
||||
return Padding(
|
||||
padding: const EdgeInsets.symmetric(vertical: 4),
|
||||
child: Row(
|
||||
children: [
|
||||
SizedBox(
|
||||
width: 80,
|
||||
child: Text(
|
||||
label,
|
||||
style: TextStyle(
|
||||
fontSize: 13,
|
||||
color: WebTheme.getSecondaryTextColor(context),
|
||||
),
|
||||
),
|
||||
),
|
||||
Text(
|
||||
value,
|
||||
style: TextStyle(
|
||||
fontSize: 13,
|
||||
fontWeight: FontWeight.w500,
|
||||
color: WebTheme.getTextColor(context),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
/// 格式化日期时间
|
||||
String _formatDateTime(DateTime dateTime) {
|
||||
return '${dateTime.year}-${dateTime.month.toString().padLeft(2, '0')}-${dateTime.day.toString().padLeft(2, '0')} '
|
||||
'${dateTime.hour.toString().padLeft(2, '0')}:${dateTime.minute.toString().padLeft(2, '0')}';
|
||||
}
|
||||
|
||||
Widget _buildSettings() {
|
||||
return Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
Text(
|
||||
'设置选项',
|
||||
style: TextStyle(
|
||||
fontSize: 18,
|
||||
fontWeight: FontWeight.bold,
|
||||
color: WebTheme.getTextColor(context),
|
||||
),
|
||||
),
|
||||
const SizedBox(height: 16),
|
||||
CheckboxListTile(
|
||||
title: const Text('公开模板'),
|
||||
subtitle: const Text('是否将此模板设为公开可见'),
|
||||
value: _isPublic,
|
||||
onChanged: (value) {
|
||||
setState(() {
|
||||
_isPublic = value ?? false;
|
||||
_isEdited = true;
|
||||
});
|
||||
},
|
||||
),
|
||||
CheckboxListTile(
|
||||
title: const Text('官方认证'),
|
||||
subtitle: const Text('是否标记为官方认证模板'),
|
||||
value: _isVerified,
|
||||
onChanged: (value) {
|
||||
setState(() {
|
||||
_isVerified = value ?? false;
|
||||
_isEdited = true;
|
||||
});
|
||||
},
|
||||
),
|
||||
CheckboxListTile(
|
||||
title: const Text('默认模板'),
|
||||
subtitle: const Text('是否设为该功能类型的默认模板'),
|
||||
value: _isDefault,
|
||||
onChanged: (value) {
|
||||
setState(() {
|
||||
_isDefault = value ?? false;
|
||||
_isEdited = true;
|
||||
});
|
||||
},
|
||||
),
|
||||
],
|
||||
);
|
||||
}
|
||||
|
||||
Widget _buildActions() {
|
||||
return Container(
|
||||
padding: const EdgeInsets.all(24),
|
||||
decoration: BoxDecoration(
|
||||
border: Border(
|
||||
top: BorderSide(color: WebTheme.getBorderColor(context)),
|
||||
),
|
||||
),
|
||||
child: Row(
|
||||
mainAxisAlignment: MainAxisAlignment.end,
|
||||
children: [
|
||||
TextButton(
|
||||
onPressed: _isLoading ? null : () => Navigator.of(context).pop(),
|
||||
child: const Text('取消'),
|
||||
),
|
||||
const SizedBox(width: 16),
|
||||
if (_isEdited || _isLoading)
|
||||
ElevatedButton(
|
||||
onPressed: _isLoading ? null : _saveTemplate,
|
||||
child: _isLoading
|
||||
? const SizedBox(
|
||||
width: 16,
|
||||
height: 16,
|
||||
child: CircularProgressIndicator(strokeWidth: 2),
|
||||
)
|
||||
: const Text('保存'),
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
Future<void> _saveTemplate() async {
|
||||
if (!_formKey.currentState!.validate()) {
|
||||
return;
|
||||
}
|
||||
|
||||
setState(() {
|
||||
_isLoading = true;
|
||||
});
|
||||
|
||||
try {
|
||||
final tags = _tagsController.text
|
||||
.split(',')
|
||||
.map((tag) => tag.trim())
|
||||
.where((tag) => tag.isNotEmpty)
|
||||
.toList();
|
||||
|
||||
final updatedTemplate = widget.template.copyWith(
|
||||
name: _nameController.text.trim(),
|
||||
description: _descriptionController.text.trim().isNotEmpty
|
||||
? _descriptionController.text.trim()
|
||||
: null,
|
||||
content: _combinePrompts(),
|
||||
featureType: _featureType,
|
||||
templateTags: tags,
|
||||
isPublic: _isPublic,
|
||||
isVerified: _isVerified,
|
||||
isDefault: _isDefault,
|
||||
updatedAt: DateTime.now(),
|
||||
);
|
||||
|
||||
await _adminRepository.updateTemplate(widget.template.id, updatedTemplate);
|
||||
|
||||
if (mounted) {
|
||||
setState(() {
|
||||
_isEdited = false;
|
||||
});
|
||||
Navigator.of(context).pop();
|
||||
widget.onSuccess?.call();
|
||||
ScaffoldMessenger.of(context).showSnackBar(
|
||||
const SnackBar(content: Text('模板更新成功')),
|
||||
);
|
||||
}
|
||||
} catch (e) {
|
||||
AppLogger.e('EditTemplateDialog', '更新模板失败', e);
|
||||
if (mounted) {
|
||||
ScaffoldMessenger.of(context).showSnackBar(
|
||||
SnackBar(content: Text('更新失败: $e')),
|
||||
);
|
||||
}
|
||||
} finally {
|
||||
if (mounted) {
|
||||
setState(() {
|
||||
_isLoading = false;
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// 合并系统提示词和用户提示词
|
||||
String _combinePrompts() {
|
||||
final systemPrompt = _systemPromptController.text.trim();
|
||||
final userPrompt = _userPromptController.text.trim();
|
||||
|
||||
if (systemPrompt.isEmpty) {
|
||||
return userPrompt;
|
||||
} else if (userPrompt.isEmpty) {
|
||||
return systemPrompt;
|
||||
} else {
|
||||
return '$systemPrompt\n\n$userPrompt';
|
||||
}
|
||||
}
|
||||
}
|
||||
332
AINoval/lib/screens/admin/widgets/enhanced_template_card.dart
Normal file
332
AINoval/lib/screens/admin/widgets/enhanced_template_card.dart
Normal file
@@ -0,0 +1,332 @@
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
import '../../../models/prompt_models.dart';
|
||||
import '../../../utils/web_theme.dart';
|
||||
|
||||
/// 增强模板卡片组件
|
||||
class EnhancedTemplateCard extends StatelessWidget {
|
||||
final EnhancedUserPromptTemplate template;
|
||||
final bool isSelected;
|
||||
final bool batchMode;
|
||||
final VoidCallback? onTap;
|
||||
final VoidCallback? onEdit;
|
||||
final VoidCallback? onDelete;
|
||||
final VoidCallback? onReview;
|
||||
final VoidCallback? onToggleVerified;
|
||||
final VoidCallback? onTogglePublish;
|
||||
final VoidCallback? onViewStats;
|
||||
final VoidCallback? onViewDetails;
|
||||
final VoidCallback? onDuplicate;
|
||||
final ValueChanged<bool>? onSelectionChanged;
|
||||
|
||||
const EnhancedTemplateCard({
|
||||
Key? key,
|
||||
required this.template,
|
||||
this.isSelected = false,
|
||||
this.batchMode = false,
|
||||
this.onTap,
|
||||
this.onEdit,
|
||||
this.onDelete,
|
||||
this.onReview,
|
||||
this.onToggleVerified,
|
||||
this.onTogglePublish,
|
||||
this.onViewStats,
|
||||
this.onViewDetails,
|
||||
this.onDuplicate,
|
||||
this.onSelectionChanged,
|
||||
}) : super(key: key);
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Card(
|
||||
margin: const EdgeInsets.only(bottom: 12),
|
||||
color: WebTheme.getCardColor(context),
|
||||
child: InkWell(
|
||||
onTap: onTap,
|
||||
borderRadius: BorderRadius.circular(8),
|
||||
child: Padding(
|
||||
padding: const EdgeInsets.all(16),
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
Row(
|
||||
children: [
|
||||
if (batchMode)
|
||||
Padding(
|
||||
padding: const EdgeInsets.only(right: 12),
|
||||
child: Checkbox(
|
||||
value: isSelected,
|
||||
onChanged: onSelectionChanged != null ? (value) => onSelectionChanged!(value ?? false) : null,
|
||||
),
|
||||
),
|
||||
Expanded(
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
Row(
|
||||
children: [
|
||||
Expanded(
|
||||
child: Text(
|
||||
template.name,
|
||||
style: TextStyle(
|
||||
fontSize: 18,
|
||||
fontWeight: FontWeight.bold,
|
||||
color: WebTheme.getTextColor(context),
|
||||
),
|
||||
),
|
||||
),
|
||||
_buildStatusBadges(),
|
||||
],
|
||||
),
|
||||
if (template.description?.isNotEmpty == true)
|
||||
Padding(
|
||||
padding: const EdgeInsets.only(top: 8),
|
||||
child: Text(
|
||||
template.description!,
|
||||
style: TextStyle(
|
||||
color: WebTheme.getTextColor(context).withOpacity(0.7),
|
||||
),
|
||||
maxLines: 2,
|
||||
overflow: TextOverflow.ellipsis,
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
if (!batchMode)
|
||||
PopupMenuButton<String>(
|
||||
onSelected: _handleMenuAction,
|
||||
itemBuilder: (context) => [
|
||||
const PopupMenuItem(value: 'duplicate', child: Text('复制为新模板')),
|
||||
if (template.isPublic == true && template.isVerified != true)
|
||||
const PopupMenuItem(value: 'review', child: Text('审核')),
|
||||
PopupMenuItem(
|
||||
value: 'verify',
|
||||
child: Text(template.isVerified == true ? '取消认证' : '设为认证'),
|
||||
),
|
||||
PopupMenuItem(
|
||||
value: 'publish',
|
||||
child: Text(template.isPublic == true ? '取消发布' : '发布'),
|
||||
),
|
||||
const PopupMenuItem(value: 'stats', child: Text('统计信息')),
|
||||
const PopupMenuItem(value: 'delete', child: Text('删除')),
|
||||
],
|
||||
),
|
||||
],
|
||||
),
|
||||
const SizedBox(height: 12),
|
||||
_buildTemplateInfo(),
|
||||
const SizedBox(height: 12),
|
||||
_buildTemplateStats(),
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
Widget _buildStatusBadges() {
|
||||
return Row(
|
||||
children: [
|
||||
if (template.isVerified == true)
|
||||
Container(
|
||||
padding: const EdgeInsets.symmetric(horizontal: 8, vertical: 4),
|
||||
margin: const EdgeInsets.only(left: 8),
|
||||
decoration: BoxDecoration(
|
||||
color: Colors.green.withOpacity(0.1),
|
||||
borderRadius: BorderRadius.circular(4),
|
||||
border: Border.all(color: Colors.green),
|
||||
),
|
||||
child: const Text(
|
||||
'已认证',
|
||||
style: TextStyle(
|
||||
fontSize: 12,
|
||||
color: Colors.green,
|
||||
fontWeight: FontWeight.w500,
|
||||
),
|
||||
),
|
||||
),
|
||||
if (template.isPublic)
|
||||
Container(
|
||||
padding: const EdgeInsets.symmetric(horizontal: 8, vertical: 4),
|
||||
margin: const EdgeInsets.only(left: 8),
|
||||
decoration: BoxDecoration(
|
||||
color: Colors.blue.withOpacity(0.1),
|
||||
borderRadius: BorderRadius.circular(4),
|
||||
border: Border.all(color: Colors.blue),
|
||||
),
|
||||
child: const Text(
|
||||
'公开',
|
||||
style: TextStyle(
|
||||
fontSize: 12,
|
||||
color: Colors.blue,
|
||||
fontWeight: FontWeight.w500,
|
||||
),
|
||||
),
|
||||
),
|
||||
if (template.isPublic && !template.isVerified)
|
||||
Container(
|
||||
padding: const EdgeInsets.symmetric(horizontal: 8, vertical: 4),
|
||||
margin: const EdgeInsets.only(left: 8),
|
||||
decoration: BoxDecoration(
|
||||
color: Colors.orange.withOpacity(0.1),
|
||||
borderRadius: BorderRadius.circular(4),
|
||||
border: Border.all(color: Colors.orange),
|
||||
),
|
||||
child: const Text(
|
||||
'待审核',
|
||||
style: TextStyle(
|
||||
fontSize: 12,
|
||||
color: Colors.orange,
|
||||
fontWeight: FontWeight.w500,
|
||||
),
|
||||
),
|
||||
),
|
||||
],
|
||||
);
|
||||
}
|
||||
|
||||
Widget _buildTemplateInfo() {
|
||||
return Wrap(
|
||||
spacing: 16,
|
||||
runSpacing: 8,
|
||||
children: [
|
||||
_buildInfoItem(Icons.category, '功能类型', template.featureType.displayName),
|
||||
_buildInfoItem(Icons.language, '语言', template.language ?? 'zh'),
|
||||
if (template.tags.isNotEmpty)
|
||||
_buildInfoItem(Icons.label, '标签', template.tags.take(3).join(', ')),
|
||||
_buildInfoItem(Icons.person, '作者', template.authorId ?? '未知'),
|
||||
if (template.version != null)
|
||||
_buildInfoItem(Icons.history, '版本', template.version.toString()),
|
||||
],
|
||||
);
|
||||
}
|
||||
|
||||
Widget _buildInfoItem(IconData icon, String label, String value) {
|
||||
return Row(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
children: [
|
||||
Icon(
|
||||
icon,
|
||||
size: 16,
|
||||
color: Colors.grey[600],
|
||||
),
|
||||
const SizedBox(width: 4),
|
||||
Text(
|
||||
'$label: ',
|
||||
style: TextStyle(
|
||||
fontSize: 12,
|
||||
color: Colors.grey[600],
|
||||
fontWeight: FontWeight.w500,
|
||||
),
|
||||
),
|
||||
Text(
|
||||
value,
|
||||
style: const TextStyle(
|
||||
fontSize: 12,
|
||||
color: Colors.grey,
|
||||
),
|
||||
),
|
||||
],
|
||||
);
|
||||
}
|
||||
|
||||
Widget _buildTemplateStats() {
|
||||
return Row(
|
||||
children: [
|
||||
_buildStatChip(
|
||||
icon: Icons.play_arrow,
|
||||
label: '使用',
|
||||
value: template.usageCount.toString(),
|
||||
),
|
||||
const SizedBox(width: 8),
|
||||
_buildStatChip(
|
||||
icon: Icons.favorite,
|
||||
label: '收藏',
|
||||
value: (template.favoriteCount ?? 0).toString(),
|
||||
),
|
||||
const SizedBox(width: 8),
|
||||
if (template.rating > 0)
|
||||
_buildStatChip(
|
||||
icon: Icons.star,
|
||||
label: '评分',
|
||||
value: template.rating.toStringAsFixed(1),
|
||||
),
|
||||
const Spacer(),
|
||||
Text(
|
||||
'创建于 ${_formatDate(template.createdAt)}',
|
||||
style: const TextStyle(
|
||||
fontSize: 12,
|
||||
color: Colors.grey,
|
||||
),
|
||||
),
|
||||
],
|
||||
);
|
||||
}
|
||||
|
||||
Widget _buildStatChip({
|
||||
required IconData icon,
|
||||
required String label,
|
||||
required String value,
|
||||
}) {
|
||||
return Container(
|
||||
padding: const EdgeInsets.symmetric(horizontal: 8, vertical: 4),
|
||||
decoration: BoxDecoration(
|
||||
color: Colors.grey.withOpacity(0.1),
|
||||
borderRadius: BorderRadius.circular(12),
|
||||
),
|
||||
child: Row(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
children: [
|
||||
Icon(
|
||||
icon,
|
||||
size: 14,
|
||||
color: Colors.grey[600],
|
||||
),
|
||||
const SizedBox(width: 4),
|
||||
Text(
|
||||
value,
|
||||
style: const TextStyle(
|
||||
fontSize: 12,
|
||||
fontWeight: FontWeight.w500,
|
||||
),
|
||||
),
|
||||
Text(
|
||||
' $label',
|
||||
style: const TextStyle(
|
||||
fontSize: 12,
|
||||
color: Colors.grey,
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
String _formatDate(DateTime date) {
|
||||
return '${date.year}-${date.month.toString().padLeft(2, '0')}-${date.day.toString().padLeft(2, '0')}';
|
||||
}
|
||||
|
||||
void _handleMenuAction(String action) {
|
||||
switch (action) {
|
||||
case 'duplicate':
|
||||
onDuplicate?.call();
|
||||
break;
|
||||
case 'review':
|
||||
onReview?.call();
|
||||
break;
|
||||
case 'verify':
|
||||
onToggleVerified?.call();
|
||||
break;
|
||||
case 'publish':
|
||||
onTogglePublish?.call();
|
||||
break;
|
||||
case 'stats':
|
||||
onViewStats?.call();
|
||||
break;
|
||||
case 'delete':
|
||||
onDelete?.call();
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
424
AINoval/lib/screens/admin/widgets/enhanced_template_editor.dart
Normal file
424
AINoval/lib/screens/admin/widgets/enhanced_template_editor.dart
Normal file
@@ -0,0 +1,424 @@
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
import '../../../models/prompt_models.dart';
|
||||
import '../../../services/api_service/repositories/impl/admin_repository_impl.dart';
|
||||
import '../../../utils/web_theme.dart';
|
||||
|
||||
/// 增强模板右侧编辑器(可创建/更新)
|
||||
class EnhancedTemplateEditor extends StatefulWidget {
|
||||
final EnhancedUserPromptTemplate? template;
|
||||
final VoidCallback? onCancel;
|
||||
final ValueChanged<EnhancedUserPromptTemplate>? onSaved;
|
||||
|
||||
const EnhancedTemplateEditor({
|
||||
super.key,
|
||||
this.template,
|
||||
this.onCancel,
|
||||
this.onSaved,
|
||||
});
|
||||
|
||||
@override
|
||||
State<EnhancedTemplateEditor> createState() => _EnhancedTemplateEditorState();
|
||||
}
|
||||
|
||||
class _EnhancedTemplateEditorState extends State<EnhancedTemplateEditor>
|
||||
with TickerProviderStateMixin {
|
||||
late final TextEditingController _nameController;
|
||||
late final TextEditingController _descriptionController;
|
||||
late final TextEditingController _systemPromptController;
|
||||
late final TextEditingController _userPromptController;
|
||||
late final TextEditingController _tagsController;
|
||||
late final TextEditingController _authorIdController;
|
||||
late final TextEditingController _userIdController;
|
||||
late final TextEditingController _categoriesController;
|
||||
|
||||
late String _featureType;
|
||||
late String _language;
|
||||
late bool _isVerified;
|
||||
bool _isSaving = false;
|
||||
|
||||
late TabController _tabController;
|
||||
|
||||
// 功能类型由 AIFeatureTypeHelper.allFeatures 动态提供
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
_tabController = TabController(length: 2, vsync: this);
|
||||
final t = widget.template;
|
||||
_nameController = TextEditingController(text: t?.name ?? '');
|
||||
_descriptionController = TextEditingController(text: t?.description ?? '');
|
||||
_systemPromptController = TextEditingController(text: t?.systemPrompt ?? '');
|
||||
_userPromptController = TextEditingController(text: t?.userPrompt ?? '');
|
||||
_tagsController = TextEditingController(text: (t?.tags ?? const []).join(', '));
|
||||
_authorIdController = TextEditingController(text: t?.authorId ?? 'system');
|
||||
_userIdController = TextEditingController(text: t?.userId ?? 'system');
|
||||
_categoriesController = TextEditingController(text: (t?.categories ?? const []).join(', '));
|
||||
_featureType = t?.featureType.toApiString() ?? 'TEXT_EXPANSION';
|
||||
_language = t?.language ?? 'zh';
|
||||
_isVerified = t?.isVerified ?? false;
|
||||
}
|
||||
|
||||
@override
|
||||
void didUpdateWidget(covariant EnhancedTemplateEditor oldWidget) {
|
||||
super.didUpdateWidget(oldWidget);
|
||||
if (oldWidget.template?.id != widget.template?.id) {
|
||||
final t = widget.template;
|
||||
_nameController.text = t?.name ?? '';
|
||||
_descriptionController.text = t?.description ?? '';
|
||||
_systemPromptController.text = t?.systemPrompt ?? '';
|
||||
_userPromptController.text = t?.userPrompt ?? '';
|
||||
_tagsController.text = (t?.tags ?? const []).join(', ');
|
||||
setState(() {
|
||||
_featureType = t?.featureType.toApiString() ?? 'TEXT_EXPANSION';
|
||||
_language = t?.language ?? 'zh';
|
||||
_isVerified = t?.isVerified ?? false;
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
void dispose() {
|
||||
_tabController.dispose();
|
||||
_nameController.dispose();
|
||||
_descriptionController.dispose();
|
||||
_systemPromptController.dispose();
|
||||
_userPromptController.dispose();
|
||||
_tagsController.dispose();
|
||||
_authorIdController.dispose();
|
||||
_userIdController.dispose();
|
||||
_categoriesController.dispose();
|
||||
super.dispose();
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final isCreate = widget.template == null;
|
||||
return Column(
|
||||
children: [
|
||||
_buildTopBar(isCreate),
|
||||
_buildTabBar(),
|
||||
Expanded(
|
||||
child: TabBarView(
|
||||
controller: _tabController,
|
||||
children: [
|
||||
_buildContentTab(),
|
||||
_buildPropertiesTab(),
|
||||
],
|
||||
),
|
||||
),
|
||||
],
|
||||
);
|
||||
}
|
||||
|
||||
Widget _buildTopBar(bool isCreate) {
|
||||
return Container(
|
||||
height: 56,
|
||||
padding: const EdgeInsets.symmetric(horizontal: 16),
|
||||
decoration: BoxDecoration(
|
||||
color: WebTheme.getSurfaceColor(context),
|
||||
border: Border(
|
||||
bottom: BorderSide(
|
||||
color: WebTheme.getBorderColor(context),
|
||||
width: 1,
|
||||
),
|
||||
),
|
||||
),
|
||||
child: Row(
|
||||
children: [
|
||||
if (widget.onCancel != null)
|
||||
IconButton(
|
||||
icon: const Icon(Icons.arrow_back),
|
||||
tooltip: '返回',
|
||||
onPressed: _isSaving ? null : widget.onCancel,
|
||||
),
|
||||
Expanded(
|
||||
child: TextField(
|
||||
controller: _nameController,
|
||||
decoration: WebTheme.getBorderlessInputDecoration(
|
||||
hintText: isCreate ? '输入新模板名称…' : '编辑模板名称…',
|
||||
context: context,
|
||||
),
|
||||
maxLines: 1,
|
||||
),
|
||||
),
|
||||
const SizedBox(width: 8),
|
||||
ElevatedButton.icon(
|
||||
onPressed: _isSaving ? null : _save,
|
||||
icon: _isSaving
|
||||
? const SizedBox(
|
||||
width: 14,
|
||||
height: 14,
|
||||
child: CircularProgressIndicator(strokeWidth: 2, color: Colors.white),
|
||||
)
|
||||
: const Icon(Icons.save, size: 16),
|
||||
label: Text(_isSaving ? '保存中…' : '保存'),
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
Widget _buildTabBar() {
|
||||
return Container(
|
||||
decoration: BoxDecoration(
|
||||
color: WebTheme.getSurfaceColor(context),
|
||||
border: Border(
|
||||
bottom: BorderSide(color: WebTheme.getBorderColor(context), width: 1),
|
||||
),
|
||||
),
|
||||
child: TabBar(
|
||||
controller: _tabController,
|
||||
labelColor: WebTheme.getPrimaryColor(context),
|
||||
unselectedLabelColor: WebTheme.getSecondaryTextColor(context),
|
||||
indicatorColor: WebTheme.getPrimaryColor(context),
|
||||
tabs: const [
|
||||
Tab(icon: Icon(Icons.notes_outlined, size: 18), text: '提示词内容'),
|
||||
Tab(icon: Icon(Icons.tune, size: 18), text: '基础信息'),
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
Widget _buildContentTab() {
|
||||
return SingleChildScrollView(
|
||||
padding: const EdgeInsets.all(16),
|
||||
child: Column(
|
||||
children: [
|
||||
TextFormField(
|
||||
controller: _systemPromptController,
|
||||
decoration: const InputDecoration(
|
||||
labelText: '系统提示词 *',
|
||||
border: OutlineInputBorder(),
|
||||
),
|
||||
keyboardType: TextInputType.multiline,
|
||||
minLines: 6,
|
||||
maxLines: null,
|
||||
),
|
||||
const SizedBox(height: 16),
|
||||
TextFormField(
|
||||
controller: _userPromptController,
|
||||
decoration: const InputDecoration(
|
||||
labelText: '用户提示词 *',
|
||||
border: OutlineInputBorder(),
|
||||
),
|
||||
keyboardType: TextInputType.multiline,
|
||||
minLines: 6,
|
||||
maxLines: null,
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
Widget _buildPropertiesTab() {
|
||||
return SingleChildScrollView(
|
||||
padding: const EdgeInsets.all(16),
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
TextFormField(
|
||||
controller: _descriptionController,
|
||||
decoration: const InputDecoration(
|
||||
labelText: '模板描述',
|
||||
border: OutlineInputBorder(),
|
||||
),
|
||||
maxLines: 3,
|
||||
),
|
||||
const SizedBox(height: 16),
|
||||
Row(
|
||||
children: [
|
||||
Expanded(
|
||||
child: TextFormField(
|
||||
controller: _userIdController,
|
||||
decoration: const InputDecoration(
|
||||
labelText: '用户ID (userId)',
|
||||
border: OutlineInputBorder(),
|
||||
),
|
||||
),
|
||||
),
|
||||
const SizedBox(width: 12),
|
||||
Expanded(
|
||||
child: TextFormField(
|
||||
controller: _authorIdController,
|
||||
decoration: const InputDecoration(
|
||||
labelText: '作者ID (authorId)',
|
||||
border: OutlineInputBorder(),
|
||||
),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
const SizedBox(height: 16),
|
||||
DropdownButtonFormField<String>(
|
||||
value: _featureType,
|
||||
decoration: const InputDecoration(
|
||||
labelText: '功能类型 *',
|
||||
border: OutlineInputBorder(),
|
||||
),
|
||||
items: AIFeatureTypeHelper.allFeatures
|
||||
.map((t) => DropdownMenuItem<String>(
|
||||
value: t.toApiString(),
|
||||
child: Text(t.displayName),
|
||||
))
|
||||
.toList(),
|
||||
onChanged: (v) => setState(() => _featureType = v ?? _featureType),
|
||||
),
|
||||
const SizedBox(height: 16),
|
||||
DropdownButtonFormField<String>(
|
||||
value: _language,
|
||||
decoration: const InputDecoration(
|
||||
labelText: '语言',
|
||||
border: OutlineInputBorder(),
|
||||
),
|
||||
items: const [
|
||||
DropdownMenuItem(value: 'zh', child: Text('中文')),
|
||||
DropdownMenuItem(value: 'en', child: Text('English')),
|
||||
DropdownMenuItem(value: 'ja', child: Text('日本語')),
|
||||
DropdownMenuItem(value: 'ko', child: Text('한국어')),
|
||||
],
|
||||
onChanged: (v) => setState(() => _language = v ?? _language),
|
||||
),
|
||||
const SizedBox(height: 16),
|
||||
TextFormField(
|
||||
controller: _tagsController,
|
||||
decoration: const InputDecoration(
|
||||
labelText: '标签(用逗号分隔)',
|
||||
border: OutlineInputBorder(),
|
||||
),
|
||||
),
|
||||
const SizedBox(height: 16),
|
||||
TextFormField(
|
||||
controller: _categoriesController,
|
||||
decoration: const InputDecoration(
|
||||
labelText: '分类(用逗号分隔)',
|
||||
border: OutlineInputBorder(),
|
||||
),
|
||||
),
|
||||
const SizedBox(height: 8),
|
||||
CheckboxListTile(
|
||||
title: const Text('设为官方认证模板'),
|
||||
value: _isVerified,
|
||||
onChanged: (v) => setState(() => _isVerified = v ?? false),
|
||||
controlAffinity: ListTileControlAffinity.leading,
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
Future<void> _save() async {
|
||||
if (_nameController.text.trim().isEmpty) {
|
||||
ScaffoldMessenger.of(context).showSnackBar(const SnackBar(content: Text('模板名称不能为空')));
|
||||
return;
|
||||
}
|
||||
if (_systemPromptController.text.trim().isEmpty) {
|
||||
ScaffoldMessenger.of(context).showSnackBar(const SnackBar(content: Text('系统提示词不能为空')));
|
||||
return;
|
||||
}
|
||||
if (_userPromptController.text.trim().isEmpty) {
|
||||
ScaffoldMessenger.of(context).showSnackBar(const SnackBar(content: Text('用户提示词不能为空')));
|
||||
return;
|
||||
}
|
||||
|
||||
setState(() => _isSaving = true);
|
||||
try {
|
||||
final adminRepo = AdminRepositoryImpl();
|
||||
final List<String> tags = _tagsController.text
|
||||
.split(',')
|
||||
.map((e) => e.trim())
|
||||
.where((e) => e.isNotEmpty)
|
||||
.toList();
|
||||
final List<String> categories = _categoriesController.text
|
||||
.split(',')
|
||||
.map((e) => e.trim())
|
||||
.where((e) => e.isNotEmpty)
|
||||
.toList();
|
||||
|
||||
EnhancedUserPromptTemplate saved;
|
||||
if (widget.template != null) {
|
||||
final updated = widget.template!.copyWith(
|
||||
name: _nameController.text.trim(),
|
||||
description: _descriptionController.text.trim().isEmpty
|
||||
? null
|
||||
: _descriptionController.text.trim(),
|
||||
systemPrompt: _systemPromptController.text.trim(),
|
||||
userPrompt: _userPromptController.text.trim(),
|
||||
tags: tags,
|
||||
categories: categories,
|
||||
language: _language,
|
||||
featureType: _getFeatureTypeFromString(_featureType),
|
||||
isVerified: _isVerified,
|
||||
userId: _userIdController.text.trim().isEmpty ? null : _userIdController.text.trim(),
|
||||
authorId: _authorIdController.text.trim().isEmpty ? null : _authorIdController.text.trim(),
|
||||
);
|
||||
saved = await adminRepo.updateEnhancedTemplate(widget.template!.id, updated);
|
||||
} else {
|
||||
final now = DateTime.now();
|
||||
final t = EnhancedUserPromptTemplate(
|
||||
id: '',
|
||||
userId: _userIdController.text.trim().isEmpty ? 'system' : _userIdController.text.trim(),
|
||||
name: _nameController.text.trim(),
|
||||
description: _descriptionController.text.trim().isEmpty
|
||||
? null
|
||||
: _descriptionController.text.trim(),
|
||||
featureType: _getFeatureTypeFromString(_featureType),
|
||||
systemPrompt: _systemPromptController.text.trim(),
|
||||
userPrompt: _userPromptController.text.trim(),
|
||||
tags: tags,
|
||||
categories: categories,
|
||||
createdAt: now,
|
||||
updatedAt: now,
|
||||
isPublic: true,
|
||||
isVerified: _isVerified,
|
||||
version: 1,
|
||||
language: _language,
|
||||
authorId: _authorIdController.text.trim().isEmpty ? null : _authorIdController.text.trim(),
|
||||
);
|
||||
saved = await adminRepo.createOfficialEnhancedTemplate(t);
|
||||
}
|
||||
|
||||
if (mounted) {
|
||||
ScaffoldMessenger.of(context).showSnackBar(const SnackBar(content: Text('模板保存成功')));
|
||||
widget.onSaved?.call(saved);
|
||||
}
|
||||
} catch (e) {
|
||||
if (mounted) {
|
||||
ScaffoldMessenger.of(context).showSnackBar(SnackBar(content: Text('保存失败: $e')));
|
||||
}
|
||||
} finally {
|
||||
if (mounted) setState(() => _isSaving = false);
|
||||
}
|
||||
}
|
||||
|
||||
AIFeatureType _getFeatureTypeFromString(String featureType) {
|
||||
switch (featureType) {
|
||||
case 'TEXT_EXPANSION':
|
||||
return AIFeatureType.textExpansion;
|
||||
case 'TEXT_REFACTOR':
|
||||
return AIFeatureType.textRefactor;
|
||||
case 'TEXT_SUMMARY':
|
||||
return AIFeatureType.textSummary;
|
||||
case 'AI_CHAT':
|
||||
return AIFeatureType.aiChat;
|
||||
case 'NOVEL_GENERATION':
|
||||
return AIFeatureType.novelGeneration;
|
||||
case 'PROFESSIONAL_FICTION_CONTINUATION':
|
||||
return AIFeatureType.professionalFictionContinuation;
|
||||
case 'SCENE_BEAT_GENERATION':
|
||||
return AIFeatureType.sceneBeatGeneration;
|
||||
case 'SCENE_TO_SUMMARY':
|
||||
return AIFeatureType.sceneToSummary;
|
||||
case 'SUMMARY_TO_SCENE':
|
||||
return AIFeatureType.summaryToScene;
|
||||
case 'NOVEL_COMPOSE':
|
||||
return AIFeatureType.novelCompose;
|
||||
case 'SETTING_TREE_GENERATION':
|
||||
return AIFeatureType.settingTreeGeneration;
|
||||
default:
|
||||
return AIFeatureType.textExpansion;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,829 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import '../../../models/public_model_config.dart';
|
||||
import '../../../models/prompt_models.dart';
|
||||
import '../../../config/provider_icons.dart';
|
||||
import '../../../utils/web_theme.dart';
|
||||
|
||||
/// 公共模型提供商分组卡片
|
||||
/// 显示提供商信息和其下的公共模型列表
|
||||
class PublicModelProviderGroupCard extends StatelessWidget {
|
||||
const PublicModelProviderGroupCard({
|
||||
super.key,
|
||||
required this.provider,
|
||||
required this.providerName,
|
||||
required this.description,
|
||||
required this.configs,
|
||||
required this.isExpanded,
|
||||
required this.onToggleExpanded,
|
||||
required this.onAddModel,
|
||||
required this.onValidate,
|
||||
required this.onEdit,
|
||||
required this.onDelete,
|
||||
required this.onToggleStatus,
|
||||
required this.onCopy,
|
||||
});
|
||||
|
||||
final String provider;
|
||||
final String providerName;
|
||||
final String description;
|
||||
final List<PublicModelConfigDetails> configs;
|
||||
final bool isExpanded;
|
||||
final VoidCallback onToggleExpanded;
|
||||
final VoidCallback onAddModel;
|
||||
final Function(String) onValidate;
|
||||
final Function(String) onEdit;
|
||||
final Function(String) onDelete;
|
||||
final Function(String, bool) onToggleStatus;
|
||||
final Function(String) onCopy;
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final color = ProviderIcons.getProviderColor(provider);
|
||||
|
||||
// 统计状态
|
||||
final enabledCount = configs.where((c) => c.enabled == true).length;
|
||||
final validatedCount = configs.where((c) => c.isValidated == true).length;
|
||||
final totalCount = configs.length;
|
||||
|
||||
return Container(
|
||||
decoration: BoxDecoration(
|
||||
color: WebTheme.getCardColor(context),
|
||||
borderRadius: BorderRadius.circular(12),
|
||||
border: Border.all(
|
||||
color: WebTheme.getBorderColor(context),
|
||||
width: 1,
|
||||
),
|
||||
boxShadow: [
|
||||
BoxShadow(
|
||||
color: WebTheme.getShadowColor(context, opacity: 0.1),
|
||||
blurRadius: 8,
|
||||
offset: const Offset(0, 2),
|
||||
),
|
||||
],
|
||||
),
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
// 提供商头部
|
||||
InkWell(
|
||||
onTap: onToggleExpanded,
|
||||
borderRadius: const BorderRadius.vertical(top: Radius.circular(12)),
|
||||
child: Container(
|
||||
padding: const EdgeInsets.all(16),
|
||||
child: Row(
|
||||
children: [
|
||||
// 提供商图标
|
||||
Container(
|
||||
width: 48,
|
||||
height: 48,
|
||||
decoration: BoxDecoration(
|
||||
color: color.withOpacity(0.1),
|
||||
borderRadius: BorderRadius.circular(12),
|
||||
border: Border.all(
|
||||
color: color.withOpacity(0.3),
|
||||
width: 1,
|
||||
),
|
||||
),
|
||||
child: ProviderIcons.getProviderIconForContext(
|
||||
provider,
|
||||
iconSize: IconSize.medium,
|
||||
color: color,
|
||||
),
|
||||
),
|
||||
|
||||
const SizedBox(width: 16),
|
||||
|
||||
// 提供商信息
|
||||
Expanded(
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
Row(
|
||||
children: [
|
||||
Expanded(
|
||||
child: Text(
|
||||
providerName,
|
||||
style: TextStyle(
|
||||
fontSize: 16,
|
||||
fontWeight: FontWeight.w600,
|
||||
color: WebTheme.getTextColor(context),
|
||||
),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
const SizedBox(height: 4),
|
||||
Text(
|
||||
description,
|
||||
style: TextStyle(
|
||||
fontSize: 12,
|
||||
color: WebTheme.getSecondaryTextColor(context),
|
||||
),
|
||||
),
|
||||
const SizedBox(height: 8),
|
||||
// 状态统计
|
||||
Row(
|
||||
children: [
|
||||
_buildStatusChip(
|
||||
context,
|
||||
'总计: $totalCount',
|
||||
Colors.blue,
|
||||
),
|
||||
const SizedBox(width: 8),
|
||||
_buildStatusChip(
|
||||
context,
|
||||
'启用: $enabledCount',
|
||||
Colors.green,
|
||||
),
|
||||
const SizedBox(width: 8),
|
||||
_buildStatusChip(
|
||||
context,
|
||||
'已验证: $validatedCount',
|
||||
Colors.orange,
|
||||
),
|
||||
],
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
|
||||
// 展开/折叠图标
|
||||
Icon(
|
||||
isExpanded ? Icons.expand_less : Icons.expand_more,
|
||||
color: WebTheme.getSecondaryTextColor(context),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
|
||||
// 模型列表
|
||||
if (isExpanded) ...[
|
||||
Divider(
|
||||
height: 1,
|
||||
color: WebTheme.getBorderColor(context),
|
||||
),
|
||||
if (configs.isEmpty)
|
||||
Padding(
|
||||
padding: const EdgeInsets.all(24),
|
||||
child: Center(
|
||||
child: Column(
|
||||
children: [
|
||||
Icon(
|
||||
Icons.cloud_off,
|
||||
size: 48,
|
||||
color: WebTheme.getSecondaryTextColor(context),
|
||||
),
|
||||
const SizedBox(height: 12),
|
||||
Text(
|
||||
'该提供商暂无公共模型配置',
|
||||
style: TextStyle(
|
||||
fontSize: 14,
|
||||
color: WebTheme.getSecondaryTextColor(context),
|
||||
),
|
||||
),
|
||||
const SizedBox(height: 16),
|
||||
OutlinedButton.icon(
|
||||
onPressed: onAddModel,
|
||||
icon: const Icon(Icons.add, size: 16),
|
||||
label: const Text('添加模型'),
|
||||
style: OutlinedButton.styleFrom(
|
||||
foregroundColor: color,
|
||||
side: BorderSide(color: color),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
)
|
||||
else
|
||||
ListView.separated(
|
||||
shrinkWrap: true,
|
||||
physics: const NeverScrollableScrollPhysics(),
|
||||
padding: const EdgeInsets.all(16),
|
||||
itemCount: configs.length,
|
||||
separatorBuilder: (context, index) => const SizedBox(height: 12),
|
||||
itemBuilder: (context, index) {
|
||||
final config = configs[index];
|
||||
return _buildModelConfigCard(context, config);
|
||||
},
|
||||
),
|
||||
],
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
Widget _buildStatusChip(BuildContext context, String label, Color color) {
|
||||
return Container(
|
||||
padding: const EdgeInsets.symmetric(horizontal: 8, vertical: 4),
|
||||
decoration: BoxDecoration(
|
||||
color: color.withOpacity(0.1),
|
||||
borderRadius: BorderRadius.circular(12),
|
||||
border: Border.all(
|
||||
color: color.withOpacity(0.3),
|
||||
width: 1,
|
||||
),
|
||||
),
|
||||
child: Text(
|
||||
label,
|
||||
style: TextStyle(
|
||||
color: color,
|
||||
fontSize: 11,
|
||||
fontWeight: FontWeight.w500,
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
Widget _buildModelConfigCard(BuildContext context, PublicModelConfigDetails config) {
|
||||
final color = ProviderIcons.getProviderColor(provider);
|
||||
|
||||
return Container(
|
||||
decoration: BoxDecoration(
|
||||
color: WebTheme.getBackgroundColor(context),
|
||||
borderRadius: BorderRadius.circular(12),
|
||||
border: Border.all(
|
||||
color: WebTheme.getBorderColor(context),
|
||||
),
|
||||
boxShadow: [
|
||||
BoxShadow(
|
||||
color: WebTheme.getShadowColor(context, opacity: 0.05),
|
||||
blurRadius: 4,
|
||||
offset: const Offset(0, 2),
|
||||
),
|
||||
],
|
||||
),
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
// 模型头部
|
||||
Container(
|
||||
padding: const EdgeInsets.all(16),
|
||||
decoration: BoxDecoration(
|
||||
color: color.withOpacity(0.03),
|
||||
borderRadius: const BorderRadius.vertical(top: Radius.circular(12)),
|
||||
border: Border(
|
||||
bottom: BorderSide(
|
||||
color: WebTheme.getBorderColor(context),
|
||||
width: 1,
|
||||
),
|
||||
),
|
||||
),
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
// 标题行
|
||||
Row(
|
||||
children: [
|
||||
Expanded(
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
Row(
|
||||
children: [
|
||||
Expanded(
|
||||
child: Text(
|
||||
config.displayName ?? config.modelId,
|
||||
style: TextStyle(
|
||||
fontSize: 16,
|
||||
fontWeight: FontWeight.w600,
|
||||
color: WebTheme.getTextColor(context),
|
||||
),
|
||||
),
|
||||
),
|
||||
// 复制按钮
|
||||
IconButton(
|
||||
onPressed: () => onCopy(config.id!),
|
||||
icon: Icon(
|
||||
Icons.content_copy,
|
||||
color: color,
|
||||
size: 18,
|
||||
),
|
||||
tooltip: '复制配置',
|
||||
constraints: const BoxConstraints(
|
||||
minWidth: 32,
|
||||
minHeight: 32,
|
||||
),
|
||||
padding: EdgeInsets.zero,
|
||||
),
|
||||
],
|
||||
),
|
||||
if (config.displayName != null && config.displayName != config.modelId)
|
||||
Text(
|
||||
config.modelId,
|
||||
style: TextStyle(
|
||||
fontSize: 13,
|
||||
color: WebTheme.getSecondaryTextColor(context),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
// 启用状态开关
|
||||
Switch(
|
||||
value: config.enabled ?? false,
|
||||
onChanged: (value) => onToggleStatus(config.id!, value),
|
||||
activeColor: color,
|
||||
inactiveThumbColor: WebTheme.getSecondaryTextColor(context),
|
||||
inactiveTrackColor: WebTheme.getSecondaryTextColor(context).withOpacity(0.3),
|
||||
),
|
||||
],
|
||||
),
|
||||
|
||||
// 描述信息
|
||||
if (config.description != null && config.description!.isNotEmpty) ...[
|
||||
const SizedBox(height: 8),
|
||||
Text(
|
||||
config.description!,
|
||||
style: TextStyle(
|
||||
fontSize: 12,
|
||||
color: WebTheme.getSecondaryTextColor(context),
|
||||
height: 1.3,
|
||||
),
|
||||
maxLines: 2,
|
||||
overflow: TextOverflow.ellipsis,
|
||||
),
|
||||
],
|
||||
|
||||
const SizedBox(height: 12),
|
||||
|
||||
// 状态标签行
|
||||
Wrap(
|
||||
spacing: 8,
|
||||
runSpacing: 4,
|
||||
children: [
|
||||
_buildConfigStatusChip(
|
||||
context,
|
||||
config.isValidated == true ? '已验证' : '未验证',
|
||||
config.isValidated == true ? Colors.green : Colors.red,
|
||||
),
|
||||
if (config.apiKeyPoolStatus != null)
|
||||
_buildConfigStatusChip(
|
||||
context,
|
||||
'Keys: ${config.apiKeyPoolStatus}',
|
||||
Colors.blue,
|
||||
),
|
||||
if (config.priority != null && config.priority! > 0)
|
||||
_buildConfigStatusChip(
|
||||
context,
|
||||
'优先级: ${config.priority}',
|
||||
Colors.purple,
|
||||
),
|
||||
if (config.tags != null && config.tags!.isNotEmpty)
|
||||
...config.tags!.take(3).map((tag) => _buildConfigStatusChip(
|
||||
context,
|
||||
tag,
|
||||
Colors.orange,
|
||||
)),
|
||||
],
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
|
||||
// 详细信息
|
||||
Padding(
|
||||
padding: const EdgeInsets.all(16),
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
// 功能授权
|
||||
if (config.enabledForFeatures != null && config.enabledForFeatures!.isNotEmpty) ...[
|
||||
_buildDetailSection(
|
||||
context,
|
||||
'授权功能',
|
||||
Icons.verified_user,
|
||||
color,
|
||||
Wrap(
|
||||
spacing: 6,
|
||||
runSpacing: 4,
|
||||
children: config.enabledForFeatures!.map((feature) {
|
||||
return Container(
|
||||
padding: const EdgeInsets.symmetric(horizontal: 8, vertical: 2),
|
||||
decoration: BoxDecoration(
|
||||
color: color.withOpacity(0.1),
|
||||
borderRadius: BorderRadius.circular(12),
|
||||
border: Border.all(color: color.withOpacity(0.3)),
|
||||
),
|
||||
child: Text(
|
||||
_getFeatureDisplayName(feature),
|
||||
style: TextStyle(
|
||||
fontSize: 10,
|
||||
color: color,
|
||||
fontWeight: FontWeight.w500,
|
||||
),
|
||||
),
|
||||
);
|
||||
}).toList(),
|
||||
),
|
||||
),
|
||||
const SizedBox(height: 12),
|
||||
],
|
||||
|
||||
// 配置信息
|
||||
Row(
|
||||
children: [
|
||||
Expanded(
|
||||
child: _buildInfoGrid(context, config, color),
|
||||
),
|
||||
],
|
||||
),
|
||||
|
||||
// 定价信息
|
||||
if (config.pricingInfo?.hasPricingData == true) ...[
|
||||
const SizedBox(height: 12),
|
||||
_buildPricingInfo(context, config.pricingInfo!, color),
|
||||
],
|
||||
|
||||
// 使用统计
|
||||
if (config.usageStatistics?.hasUsageData == true) ...[
|
||||
const SizedBox(height: 12),
|
||||
_buildUsageStatistics(context, config.usageStatistics!, color),
|
||||
],
|
||||
|
||||
// 时间信息
|
||||
if (config.createdAt != null || config.updatedAt != null) ...[
|
||||
const SizedBox(height: 12),
|
||||
_buildTimeInfo(context, config),
|
||||
],
|
||||
|
||||
const SizedBox(height: 16),
|
||||
|
||||
// 操作按钮
|
||||
Row(
|
||||
children: [
|
||||
Expanded(
|
||||
child: OutlinedButton.icon(
|
||||
onPressed: () => onValidate(config.id!),
|
||||
icon: const Icon(Icons.verified, size: 16),
|
||||
label: const Text('验证'),
|
||||
style: OutlinedButton.styleFrom(
|
||||
foregroundColor: Colors.green,
|
||||
side: const BorderSide(color: Colors.green),
|
||||
padding: const EdgeInsets.symmetric(vertical: 8),
|
||||
),
|
||||
),
|
||||
),
|
||||
const SizedBox(width: 8),
|
||||
Expanded(
|
||||
child: OutlinedButton.icon(
|
||||
onPressed: () => onEdit(config.id!),
|
||||
icon: const Icon(Icons.edit, size: 16),
|
||||
label: const Text('编辑'),
|
||||
style: OutlinedButton.styleFrom(
|
||||
foregroundColor: color,
|
||||
side: BorderSide(color: color),
|
||||
padding: const EdgeInsets.symmetric(vertical: 8),
|
||||
),
|
||||
),
|
||||
),
|
||||
const SizedBox(width: 8),
|
||||
Expanded(
|
||||
child: OutlinedButton.icon(
|
||||
onPressed: () => onDelete(config.id!),
|
||||
icon: const Icon(Icons.delete, size: 16),
|
||||
label: const Text('删除'),
|
||||
style: OutlinedButton.styleFrom(
|
||||
foregroundColor: Colors.red,
|
||||
side: const BorderSide(color: Colors.red),
|
||||
padding: const EdgeInsets.symmetric(vertical: 8),
|
||||
),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
Widget _buildDetailSection(BuildContext context, String title, IconData icon, Color color, Widget content) {
|
||||
return Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
Row(
|
||||
children: [
|
||||
Icon(icon, size: 14, color: color),
|
||||
const SizedBox(width: 6),
|
||||
Text(
|
||||
title,
|
||||
style: TextStyle(
|
||||
fontSize: 12,
|
||||
fontWeight: FontWeight.w600,
|
||||
color: color,
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
const SizedBox(height: 6),
|
||||
content,
|
||||
],
|
||||
);
|
||||
}
|
||||
|
||||
Widget _buildInfoGrid(BuildContext context, PublicModelConfigDetails config, Color color) {
|
||||
final items = <Widget>[];
|
||||
|
||||
if (config.creditRateMultiplier != null) {
|
||||
items.add(_buildInfoItem(context, '积分倍数', '${config.creditRateMultiplier}x'));
|
||||
}
|
||||
|
||||
if (config.maxConcurrentRequests != null) {
|
||||
items.add(_buildInfoItem(context, '最大并发',
|
||||
config.maxConcurrentRequests! > 0 ? '${config.maxConcurrentRequests}' : '无限制'));
|
||||
}
|
||||
|
||||
if (config.dailyRequestLimit != null) {
|
||||
items.add(_buildInfoItem(context, '日限制',
|
||||
config.dailyRequestLimit! > 0 ? '${config.dailyRequestLimit}' : '无限制'));
|
||||
}
|
||||
|
||||
if (config.hourlyRequestLimit != null) {
|
||||
items.add(_buildInfoItem(context, '时限制',
|
||||
config.hourlyRequestLimit! > 0 ? '${config.hourlyRequestLimit}' : '无限制'));
|
||||
}
|
||||
|
||||
if (config.apiEndpoint != null && config.apiEndpoint!.isNotEmpty) {
|
||||
items.add(_buildInfoItem(context, 'Endpoint', config.apiEndpoint!, isUrl: true));
|
||||
}
|
||||
|
||||
if (items.isEmpty) {
|
||||
return const SizedBox.shrink();
|
||||
}
|
||||
|
||||
return Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
Row(
|
||||
children: [
|
||||
Icon(Icons.settings, size: 14, color: color),
|
||||
const SizedBox(width: 6),
|
||||
Text(
|
||||
'配置信息',
|
||||
style: TextStyle(
|
||||
fontSize: 12,
|
||||
fontWeight: FontWeight.w600,
|
||||
color: color,
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
const SizedBox(height: 6),
|
||||
Wrap(
|
||||
spacing: 12,
|
||||
runSpacing: 6,
|
||||
children: items,
|
||||
),
|
||||
],
|
||||
);
|
||||
}
|
||||
|
||||
Widget _buildInfoItem(BuildContext context, String label, String value, {bool isUrl = false}) {
|
||||
return Container(
|
||||
constraints: const BoxConstraints(minWidth: 80),
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
Text(
|
||||
label,
|
||||
style: TextStyle(
|
||||
fontSize: 10,
|
||||
color: WebTheme.getSecondaryTextColor(context),
|
||||
),
|
||||
),
|
||||
const SizedBox(height: 2),
|
||||
Text(
|
||||
value,
|
||||
style: TextStyle(
|
||||
fontSize: 11,
|
||||
fontWeight: FontWeight.w500,
|
||||
color: WebTheme.getTextColor(context),
|
||||
),
|
||||
maxLines: isUrl ? 1 : null,
|
||||
overflow: isUrl ? TextOverflow.ellipsis : null,
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
Widget _buildPricingInfo(BuildContext context, PricingInfo pricing, Color color) {
|
||||
return _buildDetailSection(
|
||||
context,
|
||||
'定价信息',
|
||||
Icons.attach_money,
|
||||
color,
|
||||
Container(
|
||||
padding: const EdgeInsets.all(8),
|
||||
decoration: BoxDecoration(
|
||||
color: color.withOpacity(0.05),
|
||||
borderRadius: BorderRadius.circular(6),
|
||||
border: Border.all(color: color.withOpacity(0.2)),
|
||||
),
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
if (pricing.inputPricePerThousandTokens != null)
|
||||
Text(
|
||||
'输入: \$${pricing.inputPricePerThousandTokens!.toStringAsFixed(4)}/1K tokens',
|
||||
style: TextStyle(
|
||||
fontSize: 11,
|
||||
color: WebTheme.getTextColor(context),
|
||||
),
|
||||
),
|
||||
if (pricing.outputPricePerThousandTokens != null)
|
||||
Text(
|
||||
'输出: \$${pricing.outputPricePerThousandTokens!.toStringAsFixed(4)}/1K tokens',
|
||||
style: TextStyle(
|
||||
fontSize: 11,
|
||||
color: WebTheme.getTextColor(context),
|
||||
),
|
||||
),
|
||||
if (pricing.maxContextTokens != null)
|
||||
Text(
|
||||
'最大上下文: ${pricing.maxContextTokens!.toString().replaceAllMapped(RegExp(r'(\d{1,3})(?=(\d{3})+(?!\d))'), (match) => '${match[1]},')} tokens',
|
||||
style: TextStyle(
|
||||
fontSize: 11,
|
||||
color: WebTheme.getTextColor(context),
|
||||
),
|
||||
),
|
||||
if (pricing.supportsStreaming == true)
|
||||
Text(
|
||||
'支持流式输出',
|
||||
style: TextStyle(
|
||||
fontSize: 11,
|
||||
color: Colors.green,
|
||||
fontWeight: FontWeight.w500,
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
Widget _buildUsageStatistics(BuildContext context, UsageStatistics usage, Color color) {
|
||||
return _buildDetailSection(
|
||||
context,
|
||||
'使用统计',
|
||||
Icons.bar_chart,
|
||||
color,
|
||||
Container(
|
||||
padding: const EdgeInsets.all(8),
|
||||
decoration: BoxDecoration(
|
||||
color: color.withOpacity(0.05),
|
||||
borderRadius: BorderRadius.circular(6),
|
||||
border: Border.all(color: color.withOpacity(0.2)),
|
||||
),
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
if (usage.totalRequests != null)
|
||||
Text(
|
||||
'总请求: ${usage.totalRequests}',
|
||||
style: TextStyle(
|
||||
fontSize: 11,
|
||||
color: WebTheme.getTextColor(context),
|
||||
),
|
||||
),
|
||||
if (usage.totalCost != null)
|
||||
Text(
|
||||
'总成本: \$${usage.totalCost!.toStringAsFixed(4)}',
|
||||
style: TextStyle(
|
||||
fontSize: 11,
|
||||
color: WebTheme.getTextColor(context),
|
||||
),
|
||||
),
|
||||
if (usage.last30DaysRequests != null)
|
||||
Text(
|
||||
'近30天请求: ${usage.last30DaysRequests}',
|
||||
style: TextStyle(
|
||||
fontSize: 11,
|
||||
color: WebTheme.getTextColor(context),
|
||||
),
|
||||
),
|
||||
if (usage.averageCostPerRequest != null)
|
||||
Text(
|
||||
'平均每请求成本: \$${usage.averageCostPerRequest!.toStringAsFixed(6)}',
|
||||
style: TextStyle(
|
||||
fontSize: 11,
|
||||
color: WebTheme.getTextColor(context),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
Widget _buildTimeInfo(BuildContext context, PublicModelConfigDetails config) {
|
||||
return Container(
|
||||
padding: const EdgeInsets.all(8),
|
||||
decoration: BoxDecoration(
|
||||
color: WebTheme.getSecondaryTextColor(context).withOpacity(0.05),
|
||||
borderRadius: BorderRadius.circular(6),
|
||||
border: Border.all(color: WebTheme.getBorderColor(context)),
|
||||
),
|
||||
child: Row(
|
||||
children: [
|
||||
if (config.createdAt != null) ...[
|
||||
Expanded(
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
Text(
|
||||
'创建时间',
|
||||
style: TextStyle(
|
||||
fontSize: 10,
|
||||
color: WebTheme.getSecondaryTextColor(context),
|
||||
),
|
||||
),
|
||||
Text(
|
||||
formatDateTime(config.createdAt!),
|
||||
style: TextStyle(
|
||||
fontSize: 11,
|
||||
color: WebTheme.getTextColor(context),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
],
|
||||
if (config.updatedAt != null) ...[
|
||||
Expanded(
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
Text(
|
||||
'更新时间',
|
||||
style: TextStyle(
|
||||
fontSize: 10,
|
||||
color: WebTheme.getSecondaryTextColor(context),
|
||||
),
|
||||
),
|
||||
Text(
|
||||
formatDateTime(config.updatedAt!),
|
||||
style: TextStyle(
|
||||
fontSize: 11,
|
||||
color: WebTheme.getTextColor(context),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
],
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
String _getFeatureDisplayName(String feature) {
|
||||
try {
|
||||
final type = AIFeatureTypeHelper.fromApiString(feature.toUpperCase());
|
||||
return type.displayName;
|
||||
} catch (_) {
|
||||
return feature;
|
||||
}
|
||||
}
|
||||
|
||||
String formatDateTime(DateTime dateTime) {
|
||||
final now = DateTime.now();
|
||||
final difference = now.difference(dateTime);
|
||||
|
||||
if (difference.inDays > 0) {
|
||||
return '${difference.inDays}天前';
|
||||
} else if (difference.inHours > 0) {
|
||||
return '${difference.inHours}小时前';
|
||||
} else if (difference.inMinutes > 0) {
|
||||
return '${difference.inMinutes}分钟前';
|
||||
} else {
|
||||
return '刚刚';
|
||||
}
|
||||
}
|
||||
|
||||
Widget _buildConfigStatusChip(BuildContext context, String label, Color color) {
|
||||
return Container(
|
||||
padding: const EdgeInsets.symmetric(horizontal: 6, vertical: 2),
|
||||
decoration: BoxDecoration(
|
||||
color: color.withOpacity(0.1),
|
||||
borderRadius: BorderRadius.circular(10),
|
||||
border: Border.all(
|
||||
color: color.withOpacity(0.3),
|
||||
width: 1,
|
||||
),
|
||||
),
|
||||
child: Text(
|
||||
label,
|
||||
style: TextStyle(
|
||||
color: color,
|
||||
fontSize: 10,
|
||||
fontWeight: FontWeight.w500,
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
482
AINoval/lib/screens/admin/widgets/public_template_card.dart
Normal file
482
AINoval/lib/screens/admin/widgets/public_template_card.dart
Normal file
@@ -0,0 +1,482 @@
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
import '../../../models/prompt_models.dart';
|
||||
import '../../../utils/web_theme.dart';
|
||||
|
||||
/// 公共模板卡片组件
|
||||
class PublicTemplateCard extends StatelessWidget {
|
||||
final PromptTemplate template;
|
||||
final bool isSelected;
|
||||
final bool batchMode;
|
||||
final VoidCallback? onTap;
|
||||
final VoidCallback? onEdit;
|
||||
final VoidCallback? onDuplicate;
|
||||
final VoidCallback? onReview;
|
||||
final VoidCallback? onPublish;
|
||||
final VoidCallback? onSetVerified;
|
||||
final VoidCallback? onDelete;
|
||||
final ValueChanged<bool>? onSelectionChanged;
|
||||
|
||||
const PublicTemplateCard({
|
||||
Key? key,
|
||||
required this.template,
|
||||
this.isSelected = false,
|
||||
this.batchMode = false,
|
||||
this.onTap,
|
||||
this.onEdit,
|
||||
this.onDuplicate,
|
||||
this.onReview,
|
||||
this.onPublish,
|
||||
this.onSetVerified,
|
||||
this.onDelete,
|
||||
this.onSelectionChanged,
|
||||
}) : super(key: key);
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Card(
|
||||
margin: const EdgeInsets.only(bottom: 12),
|
||||
color: isSelected
|
||||
? WebTheme.getPrimaryColor(context).withOpacity(0.1)
|
||||
: WebTheme.getCardColor(context),
|
||||
child: InkWell(
|
||||
onTap: onTap,
|
||||
borderRadius: BorderRadius.circular(8),
|
||||
child: Padding(
|
||||
padding: const EdgeInsets.all(16),
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
_buildHeader(context),
|
||||
const SizedBox(height: 12),
|
||||
_buildContent(context),
|
||||
const SizedBox(height: 12),
|
||||
_buildFooter(context),
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
Widget _buildHeader(BuildContext context) {
|
||||
return Row(
|
||||
children: [
|
||||
if (batchMode) ...[
|
||||
Checkbox(
|
||||
value: isSelected,
|
||||
onChanged: (value) => onSelectionChanged?.call(value ?? false),
|
||||
),
|
||||
const SizedBox(width: 8),
|
||||
],
|
||||
|
||||
Expanded(
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
Row(
|
||||
children: [
|
||||
Expanded(
|
||||
child: Text(
|
||||
template.name,
|
||||
style: TextStyle(
|
||||
fontSize: 16,
|
||||
fontWeight: FontWeight.bold,
|
||||
color: WebTheme.getTextColor(context),
|
||||
),
|
||||
),
|
||||
),
|
||||
_buildStatusChips(context),
|
||||
],
|
||||
),
|
||||
if (template.description?.isNotEmpty == true) ...[
|
||||
const SizedBox(height: 4),
|
||||
Text(
|
||||
template.description!,
|
||||
style: TextStyle(
|
||||
fontSize: 14,
|
||||
color: WebTheme.getTextColor(context).withOpacity(0.7),
|
||||
),
|
||||
maxLines: 2,
|
||||
overflow: TextOverflow.ellipsis,
|
||||
),
|
||||
],
|
||||
],
|
||||
),
|
||||
),
|
||||
|
||||
if (!batchMode) ...[
|
||||
PopupMenuButton<String>(
|
||||
onSelected: (value) => _handleMenuAction(value),
|
||||
itemBuilder: (context) => _buildMenuItems(),
|
||||
),
|
||||
],
|
||||
],
|
||||
);
|
||||
}
|
||||
|
||||
Widget _buildStatusChips(BuildContext context) {
|
||||
return Row(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
children: [
|
||||
if (template.isVerified == true)
|
||||
_buildStatusChip(context, '认证', Colors.orange),
|
||||
if (template.isPublic == true)
|
||||
_buildStatusChip(context, '已发布', Colors.green),
|
||||
if (template.isPublic != true && template.isVerified != true)
|
||||
_buildStatusChip(context, '待审核', Colors.grey),
|
||||
],
|
||||
);
|
||||
}
|
||||
|
||||
Widget _buildStatusChip(BuildContext context, String label, Color color) {
|
||||
return Container(
|
||||
margin: const EdgeInsets.only(left: 6),
|
||||
padding: const EdgeInsets.symmetric(horizontal: 8, vertical: 4),
|
||||
decoration: BoxDecoration(
|
||||
color: color.withOpacity(0.1),
|
||||
borderRadius: BorderRadius.circular(12),
|
||||
border: Border.all(
|
||||
color: color.withOpacity(0.3),
|
||||
),
|
||||
),
|
||||
child: Text(
|
||||
label,
|
||||
style: TextStyle(
|
||||
fontSize: 12,
|
||||
color: color,
|
||||
fontWeight: FontWeight.w500,
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
Widget _buildContent(BuildContext context) {
|
||||
return Row(
|
||||
children: [
|
||||
if (template.templateTags?.isNotEmpty == true) ...[
|
||||
Expanded(
|
||||
child: Wrap(
|
||||
spacing: 6,
|
||||
runSpacing: 6,
|
||||
children: template.templateTags!.take(5).map((tag) =>
|
||||
_buildTag(context, tag)).toList(),
|
||||
),
|
||||
),
|
||||
] else
|
||||
const Expanded(child: SizedBox()),
|
||||
|
||||
if (template.aiFeatureType != null) ...[
|
||||
const SizedBox(width: 12),
|
||||
_buildFeatureTypeChip(context),
|
||||
],
|
||||
],
|
||||
);
|
||||
}
|
||||
|
||||
Widget _buildFeatureTypeChip(BuildContext context) {
|
||||
final featureType = _featureTypeToString(template.aiFeatureType!);
|
||||
|
||||
return Container(
|
||||
padding: const EdgeInsets.symmetric(horizontal: 8, vertical: 4),
|
||||
decoration: BoxDecoration(
|
||||
color: _getFeatureTypeColor(featureType).withOpacity(0.1),
|
||||
borderRadius: BorderRadius.circular(8),
|
||||
border: Border.all(
|
||||
color: _getFeatureTypeColor(featureType).withOpacity(0.3),
|
||||
),
|
||||
),
|
||||
child: Text(
|
||||
_getFeatureTypeLabel(featureType),
|
||||
style: TextStyle(
|
||||
fontSize: 12,
|
||||
color: _getFeatureTypeColor(featureType),
|
||||
fontWeight: FontWeight.w500,
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
Widget _buildTag(BuildContext context, String tag) {
|
||||
return Container(
|
||||
padding: const EdgeInsets.symmetric(horizontal: 6, vertical: 2),
|
||||
decoration: BoxDecoration(
|
||||
color: WebTheme.getTextColor(context).withOpacity(0.1),
|
||||
borderRadius: BorderRadius.circular(6),
|
||||
),
|
||||
child: Text(
|
||||
tag,
|
||||
style: TextStyle(
|
||||
fontSize: 11,
|
||||
color: WebTheme.getTextColor(context).withOpacity(0.7),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
Widget _buildFooter(BuildContext context) {
|
||||
return Row(
|
||||
children: [
|
||||
// 创建信息
|
||||
if (template.authorName != null) ...[
|
||||
Icon(
|
||||
Icons.person,
|
||||
size: 14,
|
||||
color: WebTheme.getTextColor(context).withOpacity(0.5),
|
||||
),
|
||||
const SizedBox(width: 4),
|
||||
Text(
|
||||
template.authorName!,
|
||||
style: TextStyle(
|
||||
fontSize: 12,
|
||||
color: WebTheme.getTextColor(context).withOpacity(0.5),
|
||||
),
|
||||
),
|
||||
const SizedBox(width: 16),
|
||||
],
|
||||
|
||||
// 创建时间
|
||||
Icon(
|
||||
Icons.access_time,
|
||||
size: 14,
|
||||
color: WebTheme.getTextColor(context).withOpacity(0.5),
|
||||
),
|
||||
const SizedBox(width: 4),
|
||||
Text(
|
||||
_formatDateTime(template.createdAt),
|
||||
style: TextStyle(
|
||||
fontSize: 12,
|
||||
color: WebTheme.getTextColor(context).withOpacity(0.5),
|
||||
),
|
||||
),
|
||||
|
||||
const SizedBox(width: 16),
|
||||
|
||||
// 使用次数
|
||||
Icon(
|
||||
Icons.play_circle_outline,
|
||||
size: 14,
|
||||
color: WebTheme.getTextColor(context).withOpacity(0.5),
|
||||
),
|
||||
const SizedBox(width: 4),
|
||||
Text(
|
||||
'使用 ${template.useCount ?? 0} 次',
|
||||
style: TextStyle(
|
||||
fontSize: 12,
|
||||
color: WebTheme.getTextColor(context).withOpacity(0.5),
|
||||
),
|
||||
),
|
||||
|
||||
const Spacer(),
|
||||
|
||||
// 评分信息
|
||||
if (template.averageRating != null && template.averageRating! > 0) ...[
|
||||
Icon(
|
||||
Icons.star,
|
||||
size: 14,
|
||||
color: Colors.amber,
|
||||
),
|
||||
const SizedBox(width: 4),
|
||||
Text(
|
||||
'${template.averageRating!.toStringAsFixed(1)} (${template.ratingCount ?? 0})',
|
||||
style: TextStyle(
|
||||
fontSize: 12,
|
||||
color: WebTheme.getTextColor(context).withOpacity(0.5),
|
||||
),
|
||||
),
|
||||
] else
|
||||
Text(
|
||||
'暂无评分',
|
||||
style: TextStyle(
|
||||
fontSize: 12,
|
||||
color: WebTheme.getTextColor(context).withOpacity(0.5),
|
||||
),
|
||||
),
|
||||
],
|
||||
);
|
||||
}
|
||||
|
||||
List<PopupMenuEntry<String>> _buildMenuItems() {
|
||||
List<PopupMenuEntry<String>> items = [];
|
||||
|
||||
// 复制选项
|
||||
items.add(const PopupMenuItem(
|
||||
value: 'duplicate',
|
||||
child: Row(
|
||||
children: [
|
||||
Icon(Icons.copy, size: 18),
|
||||
SizedBox(width: 8),
|
||||
Text('复制为新模板'),
|
||||
],
|
||||
),
|
||||
));
|
||||
|
||||
// 根据状态显示不同操作
|
||||
if (template.isPublic != true) {
|
||||
items.add(const PopupMenuItem(
|
||||
value: 'review',
|
||||
child: Row(
|
||||
children: [
|
||||
Icon(Icons.rate_review, size: 18),
|
||||
SizedBox(width: 8),
|
||||
Text('审核'),
|
||||
],
|
||||
),
|
||||
));
|
||||
|
||||
items.add(const PopupMenuItem(
|
||||
value: 'publish',
|
||||
child: Row(
|
||||
children: [
|
||||
Icon(Icons.publish, size: 18),
|
||||
SizedBox(width: 8),
|
||||
Text('发布'),
|
||||
],
|
||||
),
|
||||
));
|
||||
}
|
||||
|
||||
if (template.isVerified != true) {
|
||||
items.add(PopupMenuItem(
|
||||
value: 'verify',
|
||||
child: Row(
|
||||
children: [
|
||||
Icon(Icons.verified, size: 18, color: Colors.orange),
|
||||
SizedBox(width: 8),
|
||||
Text('设为认证', style: TextStyle(color: Colors.orange)),
|
||||
],
|
||||
),
|
||||
));
|
||||
}
|
||||
|
||||
items.add(const PopupMenuDivider());
|
||||
|
||||
// 删除选项
|
||||
items.add(const PopupMenuItem(
|
||||
value: 'delete',
|
||||
child: Row(
|
||||
children: [
|
||||
Icon(Icons.delete, size: 18, color: Colors.red),
|
||||
SizedBox(width: 8),
|
||||
Text('删除', style: TextStyle(color: Colors.red)),
|
||||
],
|
||||
),
|
||||
));
|
||||
|
||||
return items;
|
||||
}
|
||||
|
||||
Color _getFeatureTypeColor(String featureType) {
|
||||
switch (featureType) {
|
||||
case 'AI_CHAT':
|
||||
return Colors.blue;
|
||||
case 'SCENE_TO_SUMMARY':
|
||||
case 'SUMMARY_TO_SCENE':
|
||||
return Colors.green;
|
||||
case 'PROFESSIONAL_FICTION_CONTINUATION':
|
||||
case 'NOVEL_GENERATION':
|
||||
case 'NOVEL_COMPOSE':
|
||||
return Colors.orange;
|
||||
case 'TEXT_SUMMARY':
|
||||
return Colors.purple;
|
||||
case 'TEXT_EXPANSION':
|
||||
case 'TEXT_REFACTOR':
|
||||
return Colors.teal;
|
||||
case 'SCENE_BEAT_GENERATION':
|
||||
return Colors.indigo;
|
||||
default:
|
||||
return Colors.grey;
|
||||
}
|
||||
}
|
||||
|
||||
String _getFeatureTypeLabel(String featureType) {
|
||||
switch (featureType) {
|
||||
case 'AI_CHAT':
|
||||
return 'AI聊天';
|
||||
case 'SCENE_TO_SUMMARY':
|
||||
return '场景摘要';
|
||||
case 'SUMMARY_TO_SCENE':
|
||||
return '摘要场景';
|
||||
case 'TEXT_EXPANSION':
|
||||
return '文本扩写';
|
||||
case 'TEXT_REFACTOR':
|
||||
return '文本重构';
|
||||
case 'TEXT_SUMMARY':
|
||||
return '文本总结';
|
||||
case 'NOVEL_GENERATION':
|
||||
return '小说生成';
|
||||
case 'NOVEL_COMPOSE':
|
||||
return '设定编排';
|
||||
case 'PROFESSIONAL_FICTION_CONTINUATION':
|
||||
return '专业续写';
|
||||
case 'SCENE_BEAT_GENERATION':
|
||||
return '场景节拍';
|
||||
default:
|
||||
return featureType;
|
||||
}
|
||||
}
|
||||
|
||||
String _formatDateTime(DateTime? dateTime) {
|
||||
if (dateTime == null) return '';
|
||||
|
||||
final now = DateTime.now();
|
||||
final difference = now.difference(dateTime);
|
||||
|
||||
if (difference.inDays > 0) {
|
||||
return '${difference.inDays}天前';
|
||||
} else if (difference.inHours > 0) {
|
||||
return '${difference.inHours}小时前';
|
||||
} else if (difference.inMinutes > 0) {
|
||||
return '${difference.inMinutes}分钟前';
|
||||
} else {
|
||||
return '刚刚';
|
||||
}
|
||||
}
|
||||
|
||||
void _handleMenuAction(String action) {
|
||||
switch (action) {
|
||||
case 'duplicate':
|
||||
onDuplicate?.call();
|
||||
break;
|
||||
case 'review':
|
||||
onReview?.call();
|
||||
break;
|
||||
case 'publish':
|
||||
onPublish?.call();
|
||||
break;
|
||||
case 'verify':
|
||||
onSetVerified?.call();
|
||||
break;
|
||||
case 'delete':
|
||||
onDelete?.call();
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
String _featureTypeToString(AIFeatureType featureType) {
|
||||
switch (featureType) {
|
||||
case AIFeatureType.sceneToSummary:
|
||||
return 'SCENE_TO_SUMMARY';
|
||||
case AIFeatureType.summaryToScene:
|
||||
return 'SUMMARY_TO_SCENE';
|
||||
case AIFeatureType.textExpansion:
|
||||
return 'TEXT_EXPANSION';
|
||||
case AIFeatureType.textRefactor:
|
||||
return 'TEXT_REFACTOR';
|
||||
case AIFeatureType.textSummary:
|
||||
return 'TEXT_SUMMARY';
|
||||
case AIFeatureType.aiChat:
|
||||
return 'AI_CHAT';
|
||||
case AIFeatureType.novelGeneration:
|
||||
return 'NOVEL_GENERATION';
|
||||
case AIFeatureType.novelCompose:
|
||||
return 'NOVEL_COMPOSE';
|
||||
case AIFeatureType.professionalFictionContinuation:
|
||||
return 'PROFESSIONAL_FICTION_CONTINUATION';
|
||||
case AIFeatureType.sceneBeatGeneration:
|
||||
return 'SCENE_BEAT_GENERATION';
|
||||
case AIFeatureType.settingTreeGeneration:
|
||||
return 'SETTING_TREE_GENERATION';
|
||||
}
|
||||
}
|
||||
}
|
||||
482
AINoval/lib/screens/admin/widgets/role_management_table.dart
Normal file
482
AINoval/lib/screens/admin/widgets/role_management_table.dart
Normal file
@@ -0,0 +1,482 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_bloc/flutter_bloc.dart';
|
||||
import '../../../models/admin/admin_models.dart';
|
||||
import '../../../blocs/admin/admin_bloc.dart';
|
||||
import '../../../utils/web_theme.dart';
|
||||
|
||||
class RoleManagementTable extends StatelessWidget {
|
||||
final List<AdminRole> roles;
|
||||
|
||||
const RoleManagementTable({
|
||||
super.key,
|
||||
required this.roles,
|
||||
});
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Card(
|
||||
color: WebTheme.getCardColor(context),
|
||||
elevation: 2,
|
||||
shape: RoundedRectangleBorder(
|
||||
borderRadius: BorderRadius.circular(12),
|
||||
side: BorderSide(
|
||||
color: WebTheme.getBorderColor(context),
|
||||
width: 1,
|
||||
),
|
||||
),
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
// 标题栏
|
||||
Container(
|
||||
padding: const EdgeInsets.all(16),
|
||||
decoration: BoxDecoration(
|
||||
color: WebTheme.getTextColor(context).withOpacity(0.05),
|
||||
borderRadius: const BorderRadius.only(
|
||||
topLeft: Radius.circular(12),
|
||||
topRight: Radius.circular(12),
|
||||
),
|
||||
),
|
||||
child: Row(
|
||||
mainAxisAlignment: MainAxisAlignment.spaceBetween,
|
||||
children: [
|
||||
Text(
|
||||
'角色管理',
|
||||
style: TextStyle(
|
||||
fontSize: 18,
|
||||
fontWeight: FontWeight.bold,
|
||||
color: WebTheme.getTextColor(context),
|
||||
),
|
||||
),
|
||||
Row(
|
||||
children: [
|
||||
IconButton(
|
||||
icon: Icon(Icons.refresh, color: WebTheme.getTextColor(context)),
|
||||
onPressed: () => context.read<AdminBloc>().add(LoadRoles()),
|
||||
tooltip: '刷新角色列表',
|
||||
),
|
||||
const SizedBox(width: 8),
|
||||
Text(
|
||||
'总计: ${roles.length} 个角色',
|
||||
style: TextStyle(
|
||||
color: WebTheme.getSecondaryTextColor(context),
|
||||
fontWeight: FontWeight.w500,
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
// 数据表格
|
||||
if (roles.isNotEmpty)
|
||||
SingleChildScrollView(
|
||||
scrollDirection: Axis.horizontal,
|
||||
child: DataTable(
|
||||
columnSpacing: 16,
|
||||
dataRowMinHeight: 48,
|
||||
dataRowMaxHeight: 80,
|
||||
headingRowHeight: 56,
|
||||
headingRowColor: MaterialStateColor.resolveWith(
|
||||
(states) => WebTheme.getCardColor(context),
|
||||
),
|
||||
columns: [
|
||||
DataColumn(
|
||||
label: Text(
|
||||
'角色名称',
|
||||
style: TextStyle(
|
||||
fontWeight: FontWeight.bold,
|
||||
color: WebTheme.getTextColor(context),
|
||||
),
|
||||
),
|
||||
),
|
||||
DataColumn(
|
||||
label: Text(
|
||||
'显示名称',
|
||||
style: TextStyle(
|
||||
fontWeight: FontWeight.bold,
|
||||
color: WebTheme.getTextColor(context),
|
||||
),
|
||||
),
|
||||
),
|
||||
DataColumn(
|
||||
label: Text(
|
||||
'描述',
|
||||
style: TextStyle(
|
||||
fontWeight: FontWeight.bold,
|
||||
color: WebTheme.getTextColor(context),
|
||||
),
|
||||
),
|
||||
),
|
||||
DataColumn(
|
||||
label: Text(
|
||||
'权限',
|
||||
style: TextStyle(
|
||||
fontWeight: FontWeight.bold,
|
||||
color: WebTheme.getTextColor(context),
|
||||
),
|
||||
),
|
||||
),
|
||||
DataColumn(
|
||||
label: Text(
|
||||
'状态',
|
||||
style: TextStyle(
|
||||
fontWeight: FontWeight.bold,
|
||||
color: WebTheme.getTextColor(context),
|
||||
),
|
||||
),
|
||||
),
|
||||
DataColumn(
|
||||
label: Text(
|
||||
'优先级',
|
||||
style: TextStyle(
|
||||
fontWeight: FontWeight.bold,
|
||||
color: WebTheme.getTextColor(context),
|
||||
),
|
||||
),
|
||||
),
|
||||
DataColumn(
|
||||
label: Text(
|
||||
'操作',
|
||||
style: TextStyle(
|
||||
fontWeight: FontWeight.bold,
|
||||
color: WebTheme.getTextColor(context),
|
||||
),
|
||||
),
|
||||
),
|
||||
],
|
||||
rows: roles.map((role) => DataRow(
|
||||
cells: [
|
||||
DataCell(
|
||||
Container(
|
||||
padding: const EdgeInsets.symmetric(horizontal: 8, vertical: 4),
|
||||
decoration: BoxDecoration(
|
||||
color: _getRoleTypeColor(role).withOpacity(0.1),
|
||||
borderRadius: BorderRadius.circular(8),
|
||||
),
|
||||
child: Row(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
children: [
|
||||
Icon(
|
||||
_getRoleTypeIcon(role),
|
||||
size: 16,
|
||||
color: _getRoleTypeColor(role),
|
||||
),
|
||||
const SizedBox(width: 4),
|
||||
Text(
|
||||
role.roleName,
|
||||
style: TextStyle(
|
||||
fontWeight: FontWeight.w500,
|
||||
color: WebTheme.getTextColor(context),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
DataCell(
|
||||
Text(
|
||||
role.displayName,
|
||||
style: TextStyle(color: WebTheme.getTextColor(context)),
|
||||
),
|
||||
),
|
||||
DataCell(
|
||||
SizedBox(
|
||||
width: 200,
|
||||
child: Text(
|
||||
role.description ?? '无描述',
|
||||
style: TextStyle(color: WebTheme.getSecondaryTextColor(context)),
|
||||
maxLines: 2,
|
||||
overflow: TextOverflow.ellipsis,
|
||||
),
|
||||
),
|
||||
),
|
||||
DataCell(
|
||||
SizedBox(
|
||||
width: 150,
|
||||
child: Wrap(
|
||||
spacing: 4,
|
||||
runSpacing: 2,
|
||||
children: role.permissions.take(3).map((permission) =>
|
||||
Container(
|
||||
padding: const EdgeInsets.symmetric(horizontal: 6, vertical: 2),
|
||||
decoration: BoxDecoration(
|
||||
color: WebTheme.getTextColor(context).withOpacity(0.1),
|
||||
borderRadius: BorderRadius.circular(10),
|
||||
),
|
||||
child: Text(
|
||||
permission,
|
||||
style: TextStyle(
|
||||
fontSize: 10,
|
||||
color: WebTheme.getSecondaryTextColor(context),
|
||||
),
|
||||
),
|
||||
),
|
||||
).toList(),
|
||||
),
|
||||
),
|
||||
),
|
||||
DataCell(_buildStatusChip(context, role.enabled)),
|
||||
DataCell(
|
||||
Text(
|
||||
'优先级: ${role.priority}',
|
||||
style: TextStyle(color: WebTheme.getSecondaryTextColor(context)),
|
||||
),
|
||||
),
|
||||
DataCell(_buildActionButtons(context, role)),
|
||||
],
|
||||
)).toList(),
|
||||
),
|
||||
)
|
||||
else
|
||||
Container(
|
||||
padding: const EdgeInsets.all(32),
|
||||
child: Center(
|
||||
child: Column(
|
||||
children: [
|
||||
Icon(
|
||||
Icons.security_outlined,
|
||||
size: 64,
|
||||
color: WebTheme.getSecondaryTextColor(context).withOpacity(0.5),
|
||||
),
|
||||
const SizedBox(height: 16),
|
||||
Text(
|
||||
'暂无角色数据',
|
||||
style: TextStyle(
|
||||
fontSize: 16,
|
||||
color: WebTheme.getSecondaryTextColor(context),
|
||||
),
|
||||
),
|
||||
const SizedBox(height: 16),
|
||||
ElevatedButton.icon(
|
||||
onPressed: () => _showCreateRoleDialog(context),
|
||||
icon: const Icon(Icons.add),
|
||||
label: const Text('创建第一个角色'),
|
||||
style: ElevatedButton.styleFrom(
|
||||
backgroundColor: WebTheme.getTextColor(context),
|
||||
foregroundColor: WebTheme.getBackgroundColor(context),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
Widget _buildStatusChip(BuildContext context, bool active) {
|
||||
return Container(
|
||||
padding: const EdgeInsets.symmetric(horizontal: 8, vertical: 4),
|
||||
decoration: BoxDecoration(
|
||||
color: active ? Colors.green.withOpacity(0.1) : Colors.red.withOpacity(0.1),
|
||||
borderRadius: BorderRadius.circular(12),
|
||||
),
|
||||
child: Row(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
children: [
|
||||
Icon(
|
||||
active ? Icons.check_circle : Icons.cancel,
|
||||
size: 14,
|
||||
color: active ? Colors.green.shade700 : Colors.red.shade700,
|
||||
),
|
||||
const SizedBox(width: 4),
|
||||
Text(
|
||||
active ? '活跃' : '禁用',
|
||||
style: TextStyle(
|
||||
color: active ? Colors.green.shade700 : Colors.red.shade700,
|
||||
fontWeight: FontWeight.w500,
|
||||
fontSize: 12,
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
Color _getRoleTypeColor(AdminRole role) {
|
||||
final roleName = role.roleName;
|
||||
if (roleName.startsWith('SYSTEM_')) {
|
||||
return Colors.orange;
|
||||
} else if (roleName.startsWith('ADMIN')) {
|
||||
return Colors.red;
|
||||
} else if (roleName.startsWith('USER')) {
|
||||
return Colors.blue;
|
||||
} else {
|
||||
return Colors.grey;
|
||||
}
|
||||
}
|
||||
|
||||
IconData _getRoleTypeIcon(AdminRole role) {
|
||||
final roleName = role.roleName;
|
||||
if (roleName.startsWith('SYSTEM_')) {
|
||||
return Icons.admin_panel_settings;
|
||||
} else if (roleName.startsWith('ADMIN')) {
|
||||
return Icons.manage_accounts;
|
||||
} else if (roleName.startsWith('USER')) {
|
||||
return Icons.person;
|
||||
} else {
|
||||
return Icons.group;
|
||||
}
|
||||
}
|
||||
|
||||
Widget _buildActionButtons(BuildContext context, AdminRole role) {
|
||||
final isSystemRole = role.roleName.startsWith('SYSTEM_');
|
||||
|
||||
return Row(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
children: [
|
||||
// 编辑角色
|
||||
IconButton(
|
||||
icon: Icon(
|
||||
Icons.edit,
|
||||
size: 18,
|
||||
color: isSystemRole
|
||||
? WebTheme.getSecondaryTextColor(context).withOpacity(0.5)
|
||||
: WebTheme.getTextColor(context),
|
||||
),
|
||||
onPressed: isSystemRole ? null : () => _showEditRoleDialog(context, role),
|
||||
tooltip: isSystemRole ? '系统角色不可编辑' : '编辑角色',
|
||||
visualDensity: VisualDensity.compact,
|
||||
),
|
||||
// 查看权限
|
||||
IconButton(
|
||||
icon: Icon(Icons.visibility, size: 18, color: WebTheme.getTextColor(context)),
|
||||
onPressed: () => _showPermissionsDialog(context, role),
|
||||
tooltip: '查看权限',
|
||||
visualDensity: VisualDensity.compact,
|
||||
),
|
||||
// 删除
|
||||
if (!isSystemRole)
|
||||
IconButton(
|
||||
icon: const Icon(Icons.delete, size: 18),
|
||||
onPressed: () => _showDeleteConfirmDialog(context, role),
|
||||
tooltip: '删除角色',
|
||||
visualDensity: VisualDensity.compact,
|
||||
color: Colors.red.shade700,
|
||||
),
|
||||
],
|
||||
);
|
||||
}
|
||||
|
||||
void _showCreateRoleDialog(BuildContext context) {
|
||||
ScaffoldMessenger.of(context).showSnackBar(
|
||||
const SnackBar(content: Text('创建角色功能开发中...')),
|
||||
);
|
||||
}
|
||||
|
||||
void _showEditRoleDialog(BuildContext context, AdminRole role) {
|
||||
ScaffoldMessenger.of(context).showSnackBar(
|
||||
const SnackBar(content: Text('编辑角色功能开发中...')),
|
||||
);
|
||||
}
|
||||
|
||||
void _showPermissionsDialog(BuildContext context, AdminRole role) {
|
||||
showDialog(
|
||||
context: context,
|
||||
builder: (context) => AlertDialog(
|
||||
backgroundColor: WebTheme.getCardColor(context),
|
||||
title: Text(
|
||||
'${role.displayName} - 权限详情',
|
||||
style: TextStyle(color: WebTheme.getTextColor(context)),
|
||||
),
|
||||
content: SizedBox(
|
||||
width: 400,
|
||||
child: Column(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
Text(
|
||||
'权限列表:',
|
||||
style: TextStyle(
|
||||
fontWeight: FontWeight.bold,
|
||||
color: WebTheme.getTextColor(context),
|
||||
),
|
||||
),
|
||||
const SizedBox(height: 12),
|
||||
if (role.permissions.isNotEmpty)
|
||||
...role.permissions.map((permission) => Padding(
|
||||
padding: const EdgeInsets.only(bottom: 8),
|
||||
child: Row(
|
||||
children: [
|
||||
Icon(
|
||||
Icons.check_circle,
|
||||
size: 16,
|
||||
color: Colors.green,
|
||||
),
|
||||
const SizedBox(width: 8),
|
||||
Expanded(
|
||||
child: Text(
|
||||
permission,
|
||||
style: TextStyle(color: WebTheme.getTextColor(context)),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
))
|
||||
else
|
||||
Text(
|
||||
'无权限配置',
|
||||
style: TextStyle(color: WebTheme.getSecondaryTextColor(context)),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
actions: [
|
||||
TextButton(
|
||||
onPressed: () => Navigator.of(context).pop(),
|
||||
child: Text(
|
||||
'关闭',
|
||||
style: TextStyle(color: WebTheme.getTextColor(context)),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
void _showDeleteConfirmDialog(BuildContext context, AdminRole role) async {
|
||||
final confirmed = await showDialog<bool>(
|
||||
context: context,
|
||||
builder: (context) => AlertDialog(
|
||||
backgroundColor: WebTheme.getCardColor(context),
|
||||
title: Text(
|
||||
'确认删除',
|
||||
style: TextStyle(color: WebTheme.getTextColor(context)),
|
||||
),
|
||||
content: Text(
|
||||
'确定要删除角色 "${role.displayName}" 吗?此操作不可撤销。',
|
||||
style: TextStyle(color: WebTheme.getTextColor(context)),
|
||||
),
|
||||
actions: [
|
||||
TextButton(
|
||||
onPressed: () => Navigator.of(context).pop(false),
|
||||
child: Text(
|
||||
'取消',
|
||||
style: TextStyle(color: WebTheme.getSecondaryTextColor(context)),
|
||||
),
|
||||
),
|
||||
ElevatedButton(
|
||||
onPressed: () => Navigator.of(context).pop(true),
|
||||
style: ElevatedButton.styleFrom(
|
||||
backgroundColor: Colors.red,
|
||||
foregroundColor: Colors.white,
|
||||
),
|
||||
child: const Text('删除'),
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
|
||||
if (confirmed == true && context.mounted) {
|
||||
// TODO: 实现删除角色API调用
|
||||
ScaffoldMessenger.of(context).showSnackBar(
|
||||
const SnackBar(
|
||||
content: Text('删除角色功能开发中...'),
|
||||
backgroundColor: Colors.red,
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
70
AINoval/lib/screens/admin/widgets/stats_card.dart
Normal file
70
AINoval/lib/screens/admin/widgets/stats_card.dart
Normal file
@@ -0,0 +1,70 @@
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
class StatsCard extends StatelessWidget {
|
||||
final String title;
|
||||
final String value;
|
||||
final IconData icon;
|
||||
final Color? color;
|
||||
|
||||
const StatsCard({
|
||||
super.key,
|
||||
required this.title,
|
||||
required this.value,
|
||||
required this.icon,
|
||||
this.color,
|
||||
});
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final cardColor = color ?? Theme.of(context).colorScheme.primary;
|
||||
|
||||
return Card(
|
||||
elevation: 4,
|
||||
child: Container(
|
||||
padding: const EdgeInsets.all(20),
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
Row(
|
||||
mainAxisAlignment: MainAxisAlignment.spaceBetween,
|
||||
children: [
|
||||
Icon(
|
||||
icon,
|
||||
size: 32,
|
||||
color: cardColor,
|
||||
),
|
||||
Container(
|
||||
padding: const EdgeInsets.all(8),
|
||||
decoration: BoxDecoration(
|
||||
color: cardColor.withOpacity(0.1),
|
||||
borderRadius: BorderRadius.circular(8),
|
||||
),
|
||||
child: Icon(
|
||||
icon,
|
||||
size: 24,
|
||||
color: cardColor,
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
const SizedBox(height: 16),
|
||||
Text(
|
||||
value,
|
||||
style: Theme.of(context).textTheme.headlineMedium?.copyWith(
|
||||
fontWeight: FontWeight.bold,
|
||||
color: cardColor,
|
||||
),
|
||||
),
|
||||
const SizedBox(height: 8),
|
||||
Text(
|
||||
title,
|
||||
style: Theme.of(context).textTheme.bodyMedium?.copyWith(
|
||||
color: Theme.of(context).colorScheme.onSurface.withOpacity(0.7),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
386
AINoval/lib/screens/admin/widgets/subscription_plan_table.dart
Normal file
386
AINoval/lib/screens/admin/widgets/subscription_plan_table.dart
Normal file
@@ -0,0 +1,386 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_bloc/flutter_bloc.dart';
|
||||
import '../../../models/admin/subscription_models.dart';
|
||||
import '../../../blocs/subscription/subscription_bloc.dart';
|
||||
|
||||
class SubscriptionPlanTable extends StatelessWidget {
|
||||
final List<SubscriptionPlan> plans;
|
||||
|
||||
const SubscriptionPlanTable({
|
||||
super.key,
|
||||
required this.plans,
|
||||
});
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Card(
|
||||
elevation: 4,
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
// 标题栏
|
||||
Container(
|
||||
padding: const EdgeInsets.all(16),
|
||||
decoration: BoxDecoration(
|
||||
color: Theme.of(context).colorScheme.primary.withOpacity(0.1),
|
||||
borderRadius: const BorderRadius.only(
|
||||
topLeft: Radius.circular(8),
|
||||
topRight: Radius.circular(8),
|
||||
),
|
||||
),
|
||||
child: Row(
|
||||
mainAxisAlignment: MainAxisAlignment.spaceBetween,
|
||||
children: [
|
||||
Text(
|
||||
'订阅计划管理',
|
||||
style: Theme.of(context).textTheme.titleLarge?.copyWith(
|
||||
fontWeight: FontWeight.bold,
|
||||
),
|
||||
),
|
||||
Row(
|
||||
children: [
|
||||
ElevatedButton.icon(
|
||||
onPressed: () {
|
||||
ScaffoldMessenger.of(context).showSnackBar(
|
||||
const SnackBar(content: Text('创建订阅计划功能开发中...')),
|
||||
);
|
||||
},
|
||||
icon: const Icon(Icons.add),
|
||||
label: const Text('创建订阅计划'),
|
||||
),
|
||||
const SizedBox(width: 8),
|
||||
IconButton(
|
||||
icon: const Icon(Icons.refresh),
|
||||
onPressed: () => context.read<SubscriptionBloc>().add(LoadSubscriptionPlans()),
|
||||
tooltip: '刷新订阅计划列表',
|
||||
),
|
||||
const SizedBox(width: 8),
|
||||
Text(
|
||||
'总计: ${plans.length} 个计划',
|
||||
style: Theme.of(context).textTheme.bodyMedium?.copyWith(
|
||||
color: Theme.of(context).colorScheme.primary,
|
||||
fontWeight: FontWeight.w500,
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
// 数据表格或空状态
|
||||
if (plans.isNotEmpty)
|
||||
_buildPlansTable(context)
|
||||
else
|
||||
_buildEmptyState(context),
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
Widget _buildPlansTable(BuildContext context) {
|
||||
return SingleChildScrollView(
|
||||
scrollDirection: Axis.horizontal,
|
||||
child: DataTable(
|
||||
columnSpacing: 16,
|
||||
dataRowMinHeight: 48,
|
||||
dataRowMaxHeight: 80,
|
||||
headingRowHeight: 56,
|
||||
columns: const [
|
||||
DataColumn(
|
||||
label: Text(
|
||||
'计划名称',
|
||||
style: TextStyle(fontWeight: FontWeight.bold),
|
||||
),
|
||||
),
|
||||
DataColumn(
|
||||
label: Text(
|
||||
'价格',
|
||||
style: TextStyle(fontWeight: FontWeight.bold),
|
||||
),
|
||||
),
|
||||
DataColumn(
|
||||
label: Text(
|
||||
'计费周期',
|
||||
style: TextStyle(fontWeight: FontWeight.bold),
|
||||
),
|
||||
),
|
||||
DataColumn(
|
||||
label: Text(
|
||||
'积分',
|
||||
style: TextStyle(fontWeight: FontWeight.bold),
|
||||
),
|
||||
numeric: true,
|
||||
),
|
||||
DataColumn(
|
||||
label: Text(
|
||||
'状态',
|
||||
style: TextStyle(fontWeight: FontWeight.bold),
|
||||
),
|
||||
),
|
||||
DataColumn(
|
||||
label: Text(
|
||||
'推荐',
|
||||
style: TextStyle(fontWeight: FontWeight.bold),
|
||||
),
|
||||
),
|
||||
DataColumn(
|
||||
label: Text(
|
||||
'优先级',
|
||||
style: TextStyle(fontWeight: FontWeight.bold),
|
||||
),
|
||||
numeric: true,
|
||||
),
|
||||
DataColumn(
|
||||
label: Text(
|
||||
'操作',
|
||||
style: TextStyle(fontWeight: FontWeight.bold),
|
||||
),
|
||||
),
|
||||
],
|
||||
rows: plans.map((plan) => DataRow(
|
||||
cells: [
|
||||
DataCell(
|
||||
Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
mainAxisAlignment: MainAxisAlignment.center,
|
||||
children: [
|
||||
Text(
|
||||
plan.planName,
|
||||
style: const TextStyle(fontWeight: FontWeight.w500),
|
||||
),
|
||||
if (plan.description != null && plan.description!.isNotEmpty)
|
||||
Text(
|
||||
plan.description!,
|
||||
style: Theme.of(context).textTheme.bodySmall?.copyWith(
|
||||
color: Theme.of(context).textTheme.bodySmall?.color?.withOpacity(0.7),
|
||||
),
|
||||
maxLines: 1,
|
||||
overflow: TextOverflow.ellipsis,
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
DataCell(
|
||||
Container(
|
||||
padding: const EdgeInsets.symmetric(horizontal: 8, vertical: 4),
|
||||
decoration: BoxDecoration(
|
||||
color: Theme.of(context).colorScheme.primaryContainer,
|
||||
borderRadius: BorderRadius.circular(12),
|
||||
),
|
||||
child: Text(
|
||||
plan.formattedPrice,
|
||||
style: TextStyle(
|
||||
color: Theme.of(context).colorScheme.onPrimaryContainer,
|
||||
fontWeight: FontWeight.w600,
|
||||
fontSize: 12,
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
DataCell(
|
||||
Container(
|
||||
padding: const EdgeInsets.symmetric(horizontal: 8, vertical: 4),
|
||||
decoration: BoxDecoration(
|
||||
color: _getBillingCycleColor(plan.billingCycle).withOpacity(0.1),
|
||||
borderRadius: BorderRadius.circular(12),
|
||||
),
|
||||
child: Text(
|
||||
plan.billingCycleText,
|
||||
style: TextStyle(
|
||||
color: _getBillingCycleColor(plan.billingCycle),
|
||||
fontWeight: FontWeight.w500,
|
||||
fontSize: 12,
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
DataCell(
|
||||
Text(
|
||||
plan.creditsGranted?.toString() ?? '-',
|
||||
style: const TextStyle(fontWeight: FontWeight.w500),
|
||||
),
|
||||
),
|
||||
DataCell(_buildStatusChip(context, plan.active)),
|
||||
DataCell(
|
||||
plan.recommended
|
||||
? const Icon(Icons.star, color: Colors.amber, size: 18)
|
||||
: const Icon(Icons.star_border, color: Colors.grey, size: 18),
|
||||
),
|
||||
DataCell(
|
||||
Text(
|
||||
plan.priority.toString(),
|
||||
style: const TextStyle(fontWeight: FontWeight.w500),
|
||||
),
|
||||
),
|
||||
DataCell(_buildActionButtons(context, plan)),
|
||||
],
|
||||
)).toList(),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
Widget _buildEmptyState(BuildContext context) {
|
||||
return Container(
|
||||
padding: const EdgeInsets.all(32),
|
||||
child: Center(
|
||||
child: Column(
|
||||
children: [
|
||||
Icon(
|
||||
Icons.subscriptions_outlined,
|
||||
size: 64,
|
||||
color: Theme.of(context).colorScheme.onSurface.withOpacity(0.3),
|
||||
),
|
||||
const SizedBox(height: 16),
|
||||
Text(
|
||||
'暂无订阅计划',
|
||||
style: Theme.of(context).textTheme.bodyLarge?.copyWith(
|
||||
color: Theme.of(context).colorScheme.onSurface.withOpacity(0.6),
|
||||
),
|
||||
),
|
||||
const SizedBox(height: 16),
|
||||
ElevatedButton.icon(
|
||||
onPressed: () {
|
||||
ScaffoldMessenger.of(context).showSnackBar(
|
||||
const SnackBar(content: Text('创建订阅计划功能开发中...')),
|
||||
);
|
||||
},
|
||||
icon: const Icon(Icons.add),
|
||||
label: const Text('创建第一个订阅计划'),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
Widget _buildStatusChip(BuildContext context, bool active) {
|
||||
return Container(
|
||||
padding: const EdgeInsets.symmetric(horizontal: 8, vertical: 4),
|
||||
decoration: BoxDecoration(
|
||||
color: active ? Colors.green.withOpacity(0.1) : Colors.red.withOpacity(0.1),
|
||||
borderRadius: BorderRadius.circular(12),
|
||||
),
|
||||
child: Row(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
children: [
|
||||
Icon(
|
||||
active ? Icons.check_circle : Icons.cancel,
|
||||
size: 14,
|
||||
color: active ? Colors.green.shade700 : Colors.red.shade700,
|
||||
),
|
||||
const SizedBox(width: 4),
|
||||
Text(
|
||||
active ? '活跃' : '禁用',
|
||||
style: TextStyle(
|
||||
color: active ? Colors.green.shade700 : Colors.red.shade700,
|
||||
fontWeight: FontWeight.w500,
|
||||
fontSize: 12,
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
Color _getBillingCycleColor(BillingCycle cycle) {
|
||||
switch (cycle) {
|
||||
case BillingCycle.monthly:
|
||||
return Colors.blue;
|
||||
case BillingCycle.quarterly:
|
||||
return Colors.orange;
|
||||
case BillingCycle.yearly:
|
||||
return Colors.green;
|
||||
case BillingCycle.lifetime:
|
||||
return Colors.purple;
|
||||
}
|
||||
}
|
||||
|
||||
Widget _buildActionButtons(BuildContext context, SubscriptionPlan plan) {
|
||||
return Row(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
children: [
|
||||
// 编辑计划
|
||||
IconButton(
|
||||
icon: const Icon(Icons.edit, size: 18),
|
||||
onPressed: () {
|
||||
ScaffoldMessenger.of(context).showSnackBar(
|
||||
const SnackBar(content: Text('编辑订阅计划功能开发中...')),
|
||||
);
|
||||
},
|
||||
tooltip: '编辑计划',
|
||||
visualDensity: VisualDensity.compact,
|
||||
),
|
||||
// 启用/禁用
|
||||
IconButton(
|
||||
icon: Icon(
|
||||
plan.active ? Icons.pause : Icons.play_arrow,
|
||||
size: 18,
|
||||
),
|
||||
onPressed: () => _togglePlanStatus(context, plan),
|
||||
tooltip: plan.active ? '禁用计划' : '启用计划',
|
||||
visualDensity: VisualDensity.compact,
|
||||
color: plan.active ? Colors.orange.shade700 : Colors.green.shade700,
|
||||
),
|
||||
// 删除
|
||||
IconButton(
|
||||
icon: const Icon(Icons.delete, size: 18),
|
||||
onPressed: () => _deletePlan(context, plan),
|
||||
tooltip: '删除计划',
|
||||
visualDensity: VisualDensity.compact,
|
||||
color: Colors.red.shade700,
|
||||
),
|
||||
],
|
||||
);
|
||||
}
|
||||
|
||||
void _togglePlanStatus(BuildContext context, SubscriptionPlan plan) {
|
||||
if (plan.id != null) {
|
||||
context.read<SubscriptionBloc>().add(ToggleSubscriptionPlanStatus(
|
||||
planId: plan.id!,
|
||||
active: !plan.active,
|
||||
));
|
||||
|
||||
ScaffoldMessenger.of(context).showSnackBar(
|
||||
SnackBar(
|
||||
content: Text('${plan.active ? "禁用" : "启用"}计划操作已提交'),
|
||||
backgroundColor: Colors.blue,
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
void _deletePlan(BuildContext context, SubscriptionPlan plan) async {
|
||||
final confirmed = await showDialog<bool>(
|
||||
context: context,
|
||||
builder: (context) => AlertDialog(
|
||||
title: const Text('确认删除'),
|
||||
content: Text('确定要删除订阅计划 "${plan.planName}" 吗?此操作不可撤销。'),
|
||||
actions: [
|
||||
TextButton(
|
||||
onPressed: () => Navigator.of(context).pop(false),
|
||||
child: const Text('取消'),
|
||||
),
|
||||
ElevatedButton(
|
||||
onPressed: () => Navigator.of(context).pop(true),
|
||||
style: ElevatedButton.styleFrom(
|
||||
backgroundColor: Colors.red,
|
||||
foregroundColor: Colors.white,
|
||||
),
|
||||
child: const Text('删除'),
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
|
||||
if (confirmed == true && context.mounted && plan.id != null) {
|
||||
context.read<SubscriptionBloc>().add(DeleteSubscriptionPlan(plan.id!));
|
||||
|
||||
ScaffoldMessenger.of(context).showSnackBar(
|
||||
const SnackBar(
|
||||
content: Text('删除订阅计划操作已提交'),
|
||||
backgroundColor: Colors.red,
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
384
AINoval/lib/screens/admin/widgets/system_preset_card.dart
Normal file
384
AINoval/lib/screens/admin/widgets/system_preset_card.dart
Normal file
@@ -0,0 +1,384 @@
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
import '../../../models/preset_models.dart';
|
||||
import '../../../utils/web_theme.dart';
|
||||
|
||||
/// 系统预设卡片组件
|
||||
/// 显示系统预设的基本信息和操作按钮
|
||||
class SystemPresetCard extends StatelessWidget {
|
||||
final AIPromptPreset preset;
|
||||
final bool isSelected;
|
||||
final bool batchMode;
|
||||
final VoidCallback? onTap;
|
||||
final VoidCallback? onEdit;
|
||||
final VoidCallback? onDelete;
|
||||
final VoidCallback? onToggleVisibility;
|
||||
final VoidCallback? onViewStats;
|
||||
final VoidCallback? onViewDetails;
|
||||
final ValueChanged<bool>? onSelectionChanged;
|
||||
|
||||
const SystemPresetCard({
|
||||
Key? key,
|
||||
required this.preset,
|
||||
this.isSelected = false,
|
||||
this.batchMode = false,
|
||||
this.onTap,
|
||||
this.onEdit,
|
||||
this.onDelete,
|
||||
this.onToggleVisibility,
|
||||
this.onViewStats,
|
||||
this.onViewDetails,
|
||||
this.onSelectionChanged,
|
||||
}) : super(key: key);
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Card(
|
||||
margin: const EdgeInsets.only(bottom: 12),
|
||||
color: isSelected
|
||||
? Theme.of(context).colorScheme.primary.withOpacity(0.1)
|
||||
: WebTheme.getCardColor(context),
|
||||
child: InkWell(
|
||||
onTap: onTap,
|
||||
borderRadius: BorderRadius.circular(8),
|
||||
child: Padding(
|
||||
padding: const EdgeInsets.all(16),
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
_buildHeader(context),
|
||||
const SizedBox(height: 12),
|
||||
_buildContent(context),
|
||||
const SizedBox(height: 12),
|
||||
_buildFooter(context),
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
Widget _buildHeader(BuildContext context) {
|
||||
return Row(
|
||||
children: [
|
||||
if (batchMode) ...[
|
||||
Checkbox(
|
||||
value: isSelected,
|
||||
onChanged: (value) => onSelectionChanged?.call(value ?? false),
|
||||
),
|
||||
const SizedBox(width: 8),
|
||||
],
|
||||
|
||||
Expanded(
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
Text(
|
||||
preset.presetName ?? '未命名预设',
|
||||
style: TextStyle(
|
||||
fontSize: 16,
|
||||
fontWeight: FontWeight.bold,
|
||||
color: WebTheme.getTextColor(context),
|
||||
),
|
||||
),
|
||||
if (preset.presetDescription?.isNotEmpty == true) ...[
|
||||
const SizedBox(height: 4),
|
||||
Text(
|
||||
preset.presetDescription!,
|
||||
style: TextStyle(
|
||||
fontSize: 14,
|
||||
color: WebTheme.getTextColor(context).withOpacity(0.7),
|
||||
),
|
||||
maxLines: 2,
|
||||
overflow: TextOverflow.ellipsis,
|
||||
),
|
||||
],
|
||||
],
|
||||
),
|
||||
),
|
||||
|
||||
if (!batchMode) ...[
|
||||
_buildQuickAccessIndicator(context),
|
||||
PopupMenuButton<String>(
|
||||
onSelected: (value) => _handleMenuAction(value),
|
||||
itemBuilder: (context) => [
|
||||
const PopupMenuItem(
|
||||
value: 'edit',
|
||||
child: Row(
|
||||
children: [
|
||||
Icon(Icons.edit, size: 18),
|
||||
SizedBox(width: 8),
|
||||
Text('编辑'),
|
||||
],
|
||||
),
|
||||
),
|
||||
PopupMenuItem(
|
||||
value: 'visibility',
|
||||
child: Row(
|
||||
children: [
|
||||
Icon(
|
||||
preset.showInQuickAccess ? Icons.visibility_off : Icons.visibility,
|
||||
size: 18,
|
||||
),
|
||||
const SizedBox(width: 8),
|
||||
Text(preset.showInQuickAccess ? '隐藏快捷访问' : '显示在快捷访问'),
|
||||
],
|
||||
),
|
||||
),
|
||||
const PopupMenuItem(
|
||||
value: 'details',
|
||||
child: Row(
|
||||
children: [
|
||||
Icon(Icons.article, size: 18),
|
||||
SizedBox(width: 8),
|
||||
Text('查看内容'),
|
||||
],
|
||||
),
|
||||
),
|
||||
const PopupMenuItem(
|
||||
value: 'stats',
|
||||
child: Row(
|
||||
children: [
|
||||
Icon(Icons.analytics, size: 18),
|
||||
SizedBox(width: 8),
|
||||
Text('查看统计'),
|
||||
],
|
||||
),
|
||||
),
|
||||
const PopupMenuDivider(),
|
||||
const PopupMenuItem(
|
||||
value: 'delete',
|
||||
child: Row(
|
||||
children: [
|
||||
Icon(Icons.delete, size: 18, color: Colors.red),
|
||||
SizedBox(width: 8),
|
||||
Text('删除', style: TextStyle(color: Colors.red)),
|
||||
],
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
],
|
||||
],
|
||||
);
|
||||
}
|
||||
|
||||
Widget _buildQuickAccessIndicator(BuildContext context) {
|
||||
if (!preset.showInQuickAccess) return const SizedBox();
|
||||
|
||||
return Container(
|
||||
margin: const EdgeInsets.only(right: 8),
|
||||
padding: const EdgeInsets.symmetric(horizontal: 8, vertical: 4),
|
||||
decoration: BoxDecoration(
|
||||
color: Theme.of(context).colorScheme.primary.withOpacity(0.1),
|
||||
borderRadius: BorderRadius.circular(12),
|
||||
border: Border.all(
|
||||
color: Theme.of(context).colorScheme.primary.withOpacity(0.3),
|
||||
),
|
||||
),
|
||||
child: Row(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
children: [
|
||||
Icon(
|
||||
Icons.flash_on,
|
||||
size: 14,
|
||||
color: Theme.of(context).colorScheme.primary,
|
||||
),
|
||||
const SizedBox(width: 4),
|
||||
Text(
|
||||
'快捷',
|
||||
style: TextStyle(
|
||||
fontSize: 12,
|
||||
color: Theme.of(context).colorScheme.primary,
|
||||
fontWeight: FontWeight.w500,
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
Widget _buildContent(BuildContext context) {
|
||||
return Row(
|
||||
children: [
|
||||
_buildFeatureTypeChip(context),
|
||||
const SizedBox(width: 12),
|
||||
if (preset.presetTags?.isNotEmpty == true) ...[
|
||||
Expanded(
|
||||
child: Wrap(
|
||||
spacing: 6,
|
||||
runSpacing: 6,
|
||||
children: preset.presetTags!.take(3).map((tag) => _buildTag(context, tag)).toList(),
|
||||
),
|
||||
),
|
||||
] else
|
||||
const Expanded(child: SizedBox()),
|
||||
],
|
||||
);
|
||||
}
|
||||
|
||||
Widget _buildFeatureTypeChip(BuildContext context) {
|
||||
final featureType = preset.aiFeatureType;
|
||||
|
||||
return Container(
|
||||
padding: const EdgeInsets.symmetric(horizontal: 8, vertical: 4),
|
||||
decoration: BoxDecoration(
|
||||
color: _getFeatureTypeColor(featureType).withOpacity(0.1),
|
||||
borderRadius: BorderRadius.circular(8),
|
||||
border: Border.all(
|
||||
color: _getFeatureTypeColor(featureType).withOpacity(0.3),
|
||||
),
|
||||
),
|
||||
child: Text(
|
||||
_getFeatureTypeLabel(featureType),
|
||||
style: TextStyle(
|
||||
fontSize: 12,
|
||||
color: _getFeatureTypeColor(featureType),
|
||||
fontWeight: FontWeight.w500,
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
Widget _buildTag(BuildContext context, String tag) {
|
||||
return Container(
|
||||
padding: const EdgeInsets.symmetric(horizontal: 6, vertical: 2),
|
||||
decoration: BoxDecoration(
|
||||
color: WebTheme.getTextColor(context).withOpacity(0.1),
|
||||
borderRadius: BorderRadius.circular(6),
|
||||
),
|
||||
child: Text(
|
||||
tag,
|
||||
style: TextStyle(
|
||||
fontSize: 11,
|
||||
color: WebTheme.getTextColor(context).withOpacity(0.7),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
Widget _buildFooter(BuildContext context) {
|
||||
return Row(
|
||||
children: [
|
||||
Icon(
|
||||
Icons.access_time,
|
||||
size: 14,
|
||||
color: WebTheme.getTextColor(context).withOpacity(0.5),
|
||||
),
|
||||
const SizedBox(width: 4),
|
||||
Text(
|
||||
_formatDateTime(preset.createdAt),
|
||||
style: TextStyle(
|
||||
fontSize: 12,
|
||||
color: WebTheme.getTextColor(context).withOpacity(0.5),
|
||||
),
|
||||
),
|
||||
|
||||
const SizedBox(width: 16),
|
||||
Icon(
|
||||
Icons.play_circle_outline,
|
||||
size: 14,
|
||||
color: WebTheme.getTextColor(context).withOpacity(0.5),
|
||||
),
|
||||
const SizedBox(width: 4),
|
||||
Text(
|
||||
'使用 ${preset.useCount} 次',
|
||||
style: TextStyle(
|
||||
fontSize: 12,
|
||||
color: WebTheme.getTextColor(context).withOpacity(0.5),
|
||||
),
|
||||
),
|
||||
|
||||
const Spacer(),
|
||||
|
||||
if (preset.lastUsedAt != null) ...[
|
||||
Text(
|
||||
'最后使用: ${_formatDateTime(preset.lastUsedAt)}',
|
||||
style: TextStyle(
|
||||
fontSize: 12,
|
||||
color: WebTheme.getTextColor(context).withOpacity(0.5),
|
||||
),
|
||||
),
|
||||
] else
|
||||
Text(
|
||||
'从未使用',
|
||||
style: TextStyle(
|
||||
fontSize: 12,
|
||||
color: WebTheme.getTextColor(context).withOpacity(0.5),
|
||||
),
|
||||
),
|
||||
],
|
||||
);
|
||||
}
|
||||
|
||||
Color _getFeatureTypeColor(String featureType) {
|
||||
switch (featureType) {
|
||||
case 'CHAT':
|
||||
return Colors.blue;
|
||||
case 'SCENE_GENERATION':
|
||||
return Colors.green;
|
||||
case 'CONTINUATION':
|
||||
return Colors.orange;
|
||||
case 'SUMMARY':
|
||||
return Colors.purple;
|
||||
case 'OUTLINE':
|
||||
return Colors.teal;
|
||||
default:
|
||||
return Colors.grey;
|
||||
}
|
||||
}
|
||||
|
||||
String _getFeatureTypeLabel(String featureType) {
|
||||
switch (featureType) {
|
||||
case 'CHAT':
|
||||
return 'AI聊天';
|
||||
case 'SCENE_GENERATION':
|
||||
return '场景生成';
|
||||
case 'CONTINUATION':
|
||||
return '续写';
|
||||
case 'SUMMARY':
|
||||
return '总结';
|
||||
case 'OUTLINE':
|
||||
return '大纲';
|
||||
default:
|
||||
return featureType;
|
||||
}
|
||||
}
|
||||
|
||||
String _formatDateTime(DateTime? dateTime) {
|
||||
if (dateTime == null) return '';
|
||||
|
||||
final now = DateTime.now();
|
||||
final difference = now.difference(dateTime);
|
||||
|
||||
if (difference.inDays > 0) {
|
||||
return '${difference.inDays}天前';
|
||||
} else if (difference.inHours > 0) {
|
||||
return '${difference.inHours}小时前';
|
||||
} else if (difference.inMinutes > 0) {
|
||||
return '${difference.inMinutes}分钟前';
|
||||
} else {
|
||||
return '刚刚';
|
||||
}
|
||||
}
|
||||
|
||||
void _handleMenuAction(String action) {
|
||||
switch (action) {
|
||||
case 'edit':
|
||||
onEdit?.call();
|
||||
break;
|
||||
case 'visibility':
|
||||
onToggleVisibility?.call();
|
||||
break;
|
||||
case 'details':
|
||||
onViewDetails?.call();
|
||||
break;
|
||||
case 'stats':
|
||||
onViewStats?.call();
|
||||
break;
|
||||
case 'delete':
|
||||
onDelete?.call();
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
702
AINoval/lib/screens/admin/widgets/template_details_dialog.dart
Normal file
702
AINoval/lib/screens/admin/widgets/template_details_dialog.dart
Normal file
@@ -0,0 +1,702 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter/services.dart';
|
||||
|
||||
import '../../../models/prompt_models.dart';
|
||||
import '../../../utils/web_theme.dart';
|
||||
import '../../../widgets/common/dialog_container.dart';
|
||||
import '../../../widgets/common/dialog_header.dart';
|
||||
|
||||
/// 模板详情查看对话框
|
||||
class TemplateDetailsDialog extends StatefulWidget {
|
||||
final EnhancedUserPromptTemplate template;
|
||||
final Map<String, Object>? statistics;
|
||||
|
||||
const TemplateDetailsDialog({
|
||||
Key? key,
|
||||
required this.template,
|
||||
this.statistics,
|
||||
}) : super(key: key);
|
||||
|
||||
@override
|
||||
State<TemplateDetailsDialog> createState() => _TemplateDetailsDialogState();
|
||||
}
|
||||
|
||||
class _TemplateDetailsDialogState extends State<TemplateDetailsDialog> with TickerProviderStateMixin {
|
||||
late TabController _tabController;
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
_tabController = TabController(length: 3, vsync: this);
|
||||
}
|
||||
|
||||
@override
|
||||
void dispose() {
|
||||
_tabController.dispose();
|
||||
super.dispose();
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return DialogContainer(
|
||||
maxWidth: 900,
|
||||
height: 700,
|
||||
child: Column(
|
||||
children: [
|
||||
DialogHeader(
|
||||
title: '模板详情 - ${widget.template.name}',
|
||||
onClose: () => Navigator.of(context).pop(),
|
||||
),
|
||||
_buildTabs(),
|
||||
Expanded(
|
||||
child: TabBarView(
|
||||
controller: _tabController,
|
||||
children: [
|
||||
_buildBasicInfoTab(),
|
||||
_buildContentTab(),
|
||||
_buildStatisticsTab(),
|
||||
],
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
Widget _buildTabs() {
|
||||
return Container(
|
||||
decoration: BoxDecoration(
|
||||
border: Border(
|
||||
bottom: BorderSide(
|
||||
color: WebTheme.getBorderColor(context),
|
||||
),
|
||||
),
|
||||
),
|
||||
child: TabBar(
|
||||
controller: _tabController,
|
||||
labelColor: WebTheme.getTextColor(context),
|
||||
unselectedLabelColor: WebTheme.getTextColor(context).withOpacity(0.6),
|
||||
indicatorColor: WebTheme.getPrimaryColor(context),
|
||||
tabs: const [
|
||||
Tab(
|
||||
icon: Icon(Icons.info),
|
||||
text: '基础信息',
|
||||
),
|
||||
Tab(
|
||||
icon: Icon(Icons.code),
|
||||
text: '提示词内容',
|
||||
),
|
||||
Tab(
|
||||
icon: Icon(Icons.analytics),
|
||||
text: '统计信息',
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
Widget _buildBasicInfoTab() {
|
||||
return SingleChildScrollView(
|
||||
padding: const EdgeInsets.all(24),
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
_buildInfoSection(),
|
||||
const SizedBox(height: 24),
|
||||
_buildStatusSection(),
|
||||
const SizedBox(height: 24),
|
||||
_buildTagsSection(),
|
||||
const SizedBox(height: 24),
|
||||
_buildMetadataSection(),
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
Widget _buildInfoSection() {
|
||||
return Card(
|
||||
child: Padding(
|
||||
padding: const EdgeInsets.all(20),
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
Row(
|
||||
children: [
|
||||
const Icon(Icons.info, size: 20),
|
||||
const SizedBox(width: 8),
|
||||
Text(
|
||||
'基本信息',
|
||||
style: TextStyle(
|
||||
fontSize: 18,
|
||||
fontWeight: FontWeight.bold,
|
||||
color: WebTheme.getTextColor(context),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
const SizedBox(height: 16),
|
||||
_buildInfoRow('模板名称', widget.template.name),
|
||||
_buildInfoRow('模板ID', widget.template.id),
|
||||
_buildInfoRow('功能类型', _getFeatureTypeLabel(widget.template.featureType.toApiString())),
|
||||
_buildInfoRow('语言', _getLanguageLabel(widget.template.language)),
|
||||
_buildInfoRow('版本', (widget.template.version ?? 1).toString()),
|
||||
if (widget.template.description?.isNotEmpty == true)
|
||||
_buildInfoRow('描述', widget.template.description!, maxLines: 3),
|
||||
_buildInfoRow('作者ID', widget.template.authorId ?? '未知'),
|
||||
_buildInfoRow('创建时间', _formatDateTime(widget.template.createdAt)),
|
||||
_buildInfoRow('更新时间', _formatDateTime(widget.template.updatedAt)),
|
||||
],
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
Widget _buildStatusSection() {
|
||||
return Card(
|
||||
child: Padding(
|
||||
padding: const EdgeInsets.all(20),
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
Row(
|
||||
children: [
|
||||
const Icon(Icons.flag, size: 20),
|
||||
const SizedBox(width: 8),
|
||||
Text(
|
||||
'状态信息',
|
||||
style: TextStyle(
|
||||
fontSize: 18,
|
||||
fontWeight: FontWeight.bold,
|
||||
color: WebTheme.getTextColor(context),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
const SizedBox(height: 16),
|
||||
Wrap(
|
||||
spacing: 12,
|
||||
runSpacing: 12,
|
||||
children: [
|
||||
_buildStatusChip(
|
||||
label: '公开状态',
|
||||
value: widget.template.isPublic == true ? '已发布' : '私有',
|
||||
color: widget.template.isPublic == true ? Colors.green : Colors.grey,
|
||||
),
|
||||
_buildStatusChip(
|
||||
label: '认证状态',
|
||||
value: widget.template.isVerified == true ? '已认证' : '未认证',
|
||||
color: widget.template.isVerified == true ? Colors.blue : Colors.grey,
|
||||
),
|
||||
_buildStatusChip(
|
||||
label: '评分',
|
||||
value: widget.template.rating > 0 ? widget.template.rating.toStringAsFixed(1) : '无评分',
|
||||
color: _getRatingColor(widget.template.rating),
|
||||
),
|
||||
],
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
Widget _buildTagsSection() {
|
||||
if (widget.template.tags.isEmpty) {
|
||||
return const SizedBox.shrink();
|
||||
}
|
||||
|
||||
return Card(
|
||||
child: Padding(
|
||||
padding: const EdgeInsets.all(20),
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
Row(
|
||||
children: [
|
||||
const Icon(Icons.label, size: 20),
|
||||
const SizedBox(width: 8),
|
||||
Text(
|
||||
'标签',
|
||||
style: TextStyle(
|
||||
fontSize: 18,
|
||||
fontWeight: FontWeight.bold,
|
||||
color: WebTheme.getTextColor(context),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
const SizedBox(height: 16),
|
||||
Wrap(
|
||||
spacing: 8,
|
||||
runSpacing: 8,
|
||||
children: widget.template.tags.map((tag) => _buildTag(tag)).toList(),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
Widget _buildMetadataSection() {
|
||||
return Card(
|
||||
child: Padding(
|
||||
padding: const EdgeInsets.all(20),
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
Row(
|
||||
children: [
|
||||
const Icon(Icons.data_object, size: 20),
|
||||
const SizedBox(width: 8),
|
||||
Text(
|
||||
'元数据',
|
||||
style: TextStyle(
|
||||
fontSize: 18,
|
||||
fontWeight: FontWeight.bold,
|
||||
color: WebTheme.getTextColor(context),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
const SizedBox(height: 16),
|
||||
_buildInfoRow('使用次数', widget.template.usageCount.toString()),
|
||||
_buildInfoRow('收藏次数', (widget.template.favoriteCount ?? 0).toString()),
|
||||
if (widget.template.reviewedAt != null)
|
||||
_buildInfoRow('审核时间', _formatDateTime(widget.template.reviewedAt)),
|
||||
if (widget.template.reviewedBy?.isNotEmpty == true)
|
||||
_buildInfoRow('审核人', widget.template.reviewedBy!),
|
||||
if (widget.template.reviewComment?.isNotEmpty == true)
|
||||
_buildInfoRow('审核备注', widget.template.reviewComment!, maxLines: 2),
|
||||
],
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
Widget _buildContentTab() {
|
||||
return SingleChildScrollView(
|
||||
padding: const EdgeInsets.all(24),
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
// 系统提示词部分
|
||||
_buildPromptSection(
|
||||
title: '系统提示词 (System Prompt)',
|
||||
content: widget.template.systemPrompt.isNotEmpty
|
||||
? widget.template.systemPrompt
|
||||
: '未设置系统提示词',
|
||||
icon: Icons.settings,
|
||||
isEmpty: widget.template.systemPrompt.isEmpty,
|
||||
),
|
||||
const SizedBox(height: 24),
|
||||
|
||||
// 用户提示词部分
|
||||
_buildPromptSection(
|
||||
title: '用户提示词 (User Prompt)',
|
||||
content: widget.template.userPrompt.isNotEmpty
|
||||
? widget.template.userPrompt
|
||||
: '未设置用户提示词',
|
||||
icon: Icons.person,
|
||||
isEmpty: widget.template.userPrompt.isEmpty,
|
||||
),
|
||||
|
||||
const SizedBox(height: 24),
|
||||
|
||||
// 占位符提示
|
||||
_buildPlaceholderInfo(),
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
Widget _buildPromptSection({
|
||||
required String title,
|
||||
required String content,
|
||||
required IconData icon,
|
||||
bool isEmpty = false,
|
||||
}) {
|
||||
return Card(
|
||||
child: Padding(
|
||||
padding: const EdgeInsets.all(20),
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
Row(
|
||||
children: [
|
||||
Icon(icon, size: 20),
|
||||
const SizedBox(width: 8),
|
||||
Text(
|
||||
title,
|
||||
style: TextStyle(
|
||||
fontSize: 18,
|
||||
fontWeight: FontWeight.bold,
|
||||
color: WebTheme.getTextColor(context),
|
||||
),
|
||||
),
|
||||
const Spacer(),
|
||||
if (!isEmpty)
|
||||
IconButton(
|
||||
onPressed: () => _copyToClipboard(content),
|
||||
icon: const Icon(Icons.copy, size: 18),
|
||||
tooltip: '复制到剪贴板',
|
||||
),
|
||||
],
|
||||
),
|
||||
const SizedBox(height: 16),
|
||||
Container(
|
||||
width: double.infinity,
|
||||
constraints: const BoxConstraints(minHeight: 120),
|
||||
padding: const EdgeInsets.all(16),
|
||||
decoration: BoxDecoration(
|
||||
color: isEmpty
|
||||
? Colors.grey.withOpacity(0.05)
|
||||
: Colors.grey.withOpacity(0.1),
|
||||
borderRadius: BorderRadius.circular(8),
|
||||
border: Border.all(
|
||||
color: isEmpty
|
||||
? Colors.grey.withOpacity(0.1)
|
||||
: Colors.grey.withOpacity(0.2),
|
||||
),
|
||||
),
|
||||
child: SelectableText(
|
||||
content,
|
||||
style: TextStyle(
|
||||
fontFamily: 'monospace',
|
||||
fontSize: 14,
|
||||
height: 1.4,
|
||||
color: isEmpty
|
||||
? WebTheme.getSecondaryTextColor(context)
|
||||
: WebTheme.getTextColor(context),
|
||||
fontStyle: isEmpty ? FontStyle.italic : FontStyle.normal,
|
||||
),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
/// 构建占位符信息
|
||||
Widget _buildPlaceholderInfo() {
|
||||
return Card(
|
||||
child: Padding(
|
||||
padding: const EdgeInsets.all(20),
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
Row(
|
||||
children: [
|
||||
const Icon(Icons.code, size: 20),
|
||||
const SizedBox(width: 8),
|
||||
Text(
|
||||
'占位符说明',
|
||||
style: TextStyle(
|
||||
fontSize: 18,
|
||||
fontWeight: FontWeight.bold,
|
||||
color: WebTheme.getTextColor(context),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
const SizedBox(height: 16),
|
||||
Text(
|
||||
'提示词中可以使用占位符来动态插入内容,常用占位符包括:',
|
||||
style: TextStyle(
|
||||
fontSize: 14,
|
||||
color: WebTheme.getTextColor(context),
|
||||
),
|
||||
),
|
||||
const SizedBox(height: 12),
|
||||
_buildPlaceholderExample('{content}', '要处理的主要内容'),
|
||||
_buildPlaceholderExample('{context}', '上下文信息'),
|
||||
_buildPlaceholderExample('{requirement}', '具体要求'),
|
||||
_buildPlaceholderExample('{style}', '风格要求'),
|
||||
_buildPlaceholderExample('{length}', '长度要求'),
|
||||
],
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
Widget _buildPlaceholderExample(String placeholder, String description) {
|
||||
return Padding(
|
||||
padding: const EdgeInsets.symmetric(vertical: 4),
|
||||
child: Row(
|
||||
children: [
|
||||
Container(
|
||||
padding: const EdgeInsets.symmetric(horizontal: 8, vertical: 4),
|
||||
decoration: BoxDecoration(
|
||||
color: WebTheme.getPrimaryColor(context).withOpacity(0.1),
|
||||
borderRadius: BorderRadius.circular(4),
|
||||
border: Border.all(
|
||||
color: WebTheme.getPrimaryColor(context).withOpacity(0.3),
|
||||
),
|
||||
),
|
||||
child: Text(
|
||||
placeholder,
|
||||
style: TextStyle(
|
||||
fontFamily: 'monospace',
|
||||
fontSize: 12,
|
||||
color: WebTheme.getPrimaryColor(context),
|
||||
fontWeight: FontWeight.w500,
|
||||
),
|
||||
),
|
||||
),
|
||||
const SizedBox(width: 12),
|
||||
Expanded(
|
||||
child: Text(
|
||||
description,
|
||||
style: TextStyle(
|
||||
fontSize: 13,
|
||||
color: WebTheme.getSecondaryTextColor(context),
|
||||
),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
Widget _buildStatisticsTab() {
|
||||
final stats = widget.statistics ?? {};
|
||||
|
||||
return SingleChildScrollView(
|
||||
padding: const EdgeInsets.all(24),
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
_buildStatsCard('使用统计', [
|
||||
_buildStatRow('总使用次数', stats['usageCount']?.toString() ?? '0'),
|
||||
_buildStatRow('本月使用', stats['monthlyUsage']?.toString() ?? '0'),
|
||||
_buildStatRow('本周使用', stats['weeklyUsage']?.toString() ?? '0'),
|
||||
_buildStatRow('今日使用', stats['dailyUsage']?.toString() ?? '0'),
|
||||
], Icons.play_arrow),
|
||||
const SizedBox(height: 24),
|
||||
_buildStatsCard('用户反馈', [
|
||||
_buildStatRow('收藏次数', stats['favoriteCount']?.toString() ?? '0'),
|
||||
_buildStatRow('平均评分', stats['averageRating']?.toString() ?? '0.0'),
|
||||
_buildStatRow('评分人数', stats['ratingCount']?.toString() ?? '0'),
|
||||
_buildStatRow('反馈次数', stats['feedbackCount']?.toString() ?? '0'),
|
||||
], Icons.favorite),
|
||||
const SizedBox(height: 24),
|
||||
_buildStatsCard('性能数据', [
|
||||
_buildStatRow('平均响应时间', '${stats['averageResponseTime'] ?? 0}ms'),
|
||||
_buildStatRow('成功率', '${stats['successRate'] ?? 100}%'),
|
||||
_buildStatRow('错误次数', stats['errorCount']?.toString() ?? '0'),
|
||||
_buildStatRow('最后使用时间', _formatDateTime(stats['lastUsedAt'] as DateTime?)),
|
||||
], Icons.speed),
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
Widget _buildStatsCard(String title, List<Widget> children, IconData icon) {
|
||||
return Card(
|
||||
child: Padding(
|
||||
padding: const EdgeInsets.all(20),
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
Row(
|
||||
children: [
|
||||
Icon(icon, size: 20),
|
||||
const SizedBox(width: 8),
|
||||
Text(
|
||||
title,
|
||||
style: TextStyle(
|
||||
fontSize: 18,
|
||||
fontWeight: FontWeight.bold,
|
||||
color: WebTheme.getTextColor(context),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
const SizedBox(height: 16),
|
||||
...children,
|
||||
],
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
Widget _buildStatRow(String label, String value) {
|
||||
return Padding(
|
||||
padding: const EdgeInsets.only(bottom: 8),
|
||||
child: Row(
|
||||
mainAxisAlignment: MainAxisAlignment.spaceBetween,
|
||||
children: [
|
||||
Text(
|
||||
label,
|
||||
style: const TextStyle(fontWeight: FontWeight.w500),
|
||||
),
|
||||
Text(
|
||||
value,
|
||||
style: const TextStyle(fontSize: 16),
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
Widget _buildInfoRow(String label, String value, {int maxLines = 1}) {
|
||||
return Padding(
|
||||
padding: const EdgeInsets.only(bottom: 12),
|
||||
child: Row(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
SizedBox(
|
||||
width: 120,
|
||||
child: Text(
|
||||
'$label:',
|
||||
style: const TextStyle(fontWeight: FontWeight.w500),
|
||||
),
|
||||
),
|
||||
Expanded(
|
||||
child: Text(
|
||||
value,
|
||||
style: TextStyle(
|
||||
color: WebTheme.getTextColor(context).withOpacity(0.8),
|
||||
),
|
||||
maxLines: maxLines,
|
||||
overflow: maxLines > 1 ? TextOverflow.ellipsis : null,
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
Widget _buildStatusChip({
|
||||
required String label,
|
||||
required String value,
|
||||
required Color color,
|
||||
}) {
|
||||
return Container(
|
||||
padding: const EdgeInsets.symmetric(horizontal: 12, vertical: 8),
|
||||
decoration: BoxDecoration(
|
||||
color: color.withOpacity(0.1),
|
||||
borderRadius: BorderRadius.circular(8),
|
||||
border: Border.all(color: color.withOpacity(0.3)),
|
||||
),
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
Text(
|
||||
label,
|
||||
style: TextStyle(
|
||||
fontSize: 12,
|
||||
color: color,
|
||||
fontWeight: FontWeight.w500,
|
||||
),
|
||||
),
|
||||
const SizedBox(height: 2),
|
||||
Text(
|
||||
value,
|
||||
style: TextStyle(
|
||||
fontSize: 14,
|
||||
color: color,
|
||||
fontWeight: FontWeight.bold,
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
Widget _buildTag(String tag) {
|
||||
return Container(
|
||||
padding: const EdgeInsets.symmetric(horizontal: 10, vertical: 6),
|
||||
decoration: BoxDecoration(
|
||||
color: WebTheme.getPrimaryColor(context).withOpacity(0.1),
|
||||
borderRadius: BorderRadius.circular(16),
|
||||
border: Border.all(
|
||||
color: WebTheme.getPrimaryColor(context).withOpacity(0.3),
|
||||
),
|
||||
),
|
||||
child: Text(
|
||||
tag,
|
||||
style: TextStyle(
|
||||
fontSize: 13,
|
||||
color: WebTheme.getPrimaryColor(context),
|
||||
fontWeight: FontWeight.w500,
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
String _getFeatureTypeLabel(String? featureType) {
|
||||
switch (featureType) {
|
||||
case 'AI_CHAT':
|
||||
return 'AI聊天';
|
||||
case 'TEXT_EXPANSION':
|
||||
return '文本扩写';
|
||||
case 'TEXT_REFACTOR':
|
||||
return '文本润色';
|
||||
case 'TEXT_SUMMARY':
|
||||
return '文本总结';
|
||||
case 'SCENE_TO_SUMMARY':
|
||||
return '场景转摘要';
|
||||
case 'SUMMARY_TO_SCENE':
|
||||
return '摘要转场景';
|
||||
case 'NOVEL_GENERATION':
|
||||
return '小说生成';
|
||||
case 'PROFESSIONAL_FICTION_CONTINUATION':
|
||||
return '专业续写';
|
||||
case 'SCENE_BEAT_GENERATION':
|
||||
return '场景节拍生成';
|
||||
default:
|
||||
return featureType ?? '未知类型';
|
||||
}
|
||||
}
|
||||
|
||||
String _getLanguageLabel(String? language) {
|
||||
switch (language) {
|
||||
case 'zh':
|
||||
return '中文';
|
||||
case 'en':
|
||||
return 'English';
|
||||
case 'ja':
|
||||
return '日本語';
|
||||
case 'ko':
|
||||
return '한국어';
|
||||
default:
|
||||
return language ?? '中文';
|
||||
}
|
||||
}
|
||||
|
||||
Color _getRatingColor(double? rating) {
|
||||
if (rating == null) return WebTheme.getSecondaryTextColor(context);
|
||||
if (rating >= 4.5) return WebTheme.success;
|
||||
if (rating >= 3.5) return WebTheme.warning;
|
||||
if (rating >= 2.0) return WebTheme.error;
|
||||
return WebTheme.getSecondaryTextColor(context);
|
||||
}
|
||||
|
||||
String _formatDateTime(DateTime? dateTime) {
|
||||
if (dateTime == null) return '未设置';
|
||||
|
||||
final now = DateTime.now();
|
||||
final difference = now.difference(dateTime);
|
||||
|
||||
if (difference.inDays > 0) {
|
||||
return '${dateTime.year}-${dateTime.month.toString().padLeft(2, '0')}-${dateTime.day.toString().padLeft(2, '0')}';
|
||||
} else if (difference.inHours > 0) {
|
||||
return '${difference.inHours}小时前';
|
||||
} else if (difference.inMinutes > 0) {
|
||||
return '${difference.inMinutes}分钟前';
|
||||
} else {
|
||||
return '刚刚';
|
||||
}
|
||||
}
|
||||
|
||||
void _copyToClipboard(String text) {
|
||||
Clipboard.setData(ClipboardData(text: text));
|
||||
ScaffoldMessenger.of(context).showSnackBar(
|
||||
const SnackBar(
|
||||
content: Text('已复制到剪贴板'),
|
||||
duration: Duration(seconds: 2),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
533
AINoval/lib/screens/admin/widgets/template_review_dialog.dart
Normal file
533
AINoval/lib/screens/admin/widgets/template_review_dialog.dart
Normal file
@@ -0,0 +1,533 @@
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
import '../../../models/prompt_models.dart';
|
||||
import '../../../utils/logger.dart';
|
||||
|
||||
/// 模板审核对话框
|
||||
class TemplateReviewDialog extends StatefulWidget {
|
||||
final EnhancedUserPromptTemplate template;
|
||||
final Function(bool approved, String? comment) onReview;
|
||||
|
||||
const TemplateReviewDialog({
|
||||
Key? key,
|
||||
required this.template,
|
||||
required this.onReview,
|
||||
}) : super(key: key);
|
||||
|
||||
@override
|
||||
State<TemplateReviewDialog> createState() => _TemplateReviewDialogState();
|
||||
}
|
||||
|
||||
class _TemplateReviewDialogState extends State<TemplateReviewDialog> {
|
||||
final _reviewCommentController = TextEditingController();
|
||||
|
||||
String _reviewAction = 'approve'; // 'approve', 'reject'
|
||||
bool _setAsVerified = false;
|
||||
bool _isLoading = false;
|
||||
|
||||
static const Map<String, String> _actionLabels = {
|
||||
'approve': '通过审核',
|
||||
'reject': '拒绝',
|
||||
};
|
||||
|
||||
static const Map<String, Color> _actionColors = {
|
||||
'approve': Colors.green,
|
||||
'reject': Colors.red,
|
||||
};
|
||||
|
||||
@override
|
||||
void dispose() {
|
||||
_reviewCommentController.dispose();
|
||||
super.dispose();
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Dialog(
|
||||
child: Container(
|
||||
width: 700,
|
||||
constraints: const BoxConstraints(maxHeight: 800),
|
||||
padding: const EdgeInsets.all(24),
|
||||
child: Column(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
_buildHeader(),
|
||||
const SizedBox(height: 24),
|
||||
|
||||
Expanded(
|
||||
child: SingleChildScrollView(
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
_buildTemplateInfo(),
|
||||
const SizedBox(height: 24),
|
||||
_buildTemplateContent(),
|
||||
const SizedBox(height: 24),
|
||||
_buildReviewSection(),
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
|
||||
const SizedBox(height: 24),
|
||||
_buildActionButtons(),
|
||||
],
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
Widget _buildHeader() {
|
||||
return Row(
|
||||
children: [
|
||||
const Icon(Icons.rate_review, size: 24),
|
||||
const SizedBox(width: 8),
|
||||
const Text(
|
||||
'模板审核',
|
||||
style: TextStyle(fontSize: 20, fontWeight: FontWeight.bold),
|
||||
),
|
||||
const Spacer(),
|
||||
_buildStatusChip(),
|
||||
const SizedBox(width: 16),
|
||||
IconButton(
|
||||
onPressed: () => Navigator.of(context).pop(),
|
||||
icon: const Icon(Icons.close),
|
||||
),
|
||||
],
|
||||
);
|
||||
}
|
||||
|
||||
Widget _buildStatusChip() {
|
||||
String status;
|
||||
Color color;
|
||||
|
||||
if (widget.template.isVerified == true) {
|
||||
status = '已认证';
|
||||
color = Colors.green;
|
||||
} else if (widget.template.isPublic == true) {
|
||||
status = '已发布';
|
||||
color = Colors.blue;
|
||||
} else {
|
||||
status = '待审核';
|
||||
color = Colors.orange;
|
||||
}
|
||||
|
||||
return Container(
|
||||
padding: const EdgeInsets.symmetric(horizontal: 12, vertical: 6),
|
||||
decoration: BoxDecoration(
|
||||
color: color.withOpacity(0.1),
|
||||
borderRadius: BorderRadius.circular(16),
|
||||
border: Border.all(color: color.withOpacity(0.3)),
|
||||
),
|
||||
child: Text(
|
||||
status,
|
||||
style: TextStyle(
|
||||
fontSize: 12,
|
||||
color: color,
|
||||
fontWeight: FontWeight.w500,
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
Widget _buildTemplateInfo() {
|
||||
return Container(
|
||||
padding: const EdgeInsets.all(16),
|
||||
decoration: BoxDecoration(
|
||||
color: Theme.of(context).colorScheme.surface,
|
||||
borderRadius: BorderRadius.circular(8),
|
||||
border: Border.all(
|
||||
color: Theme.of(context).colorScheme.outline.withOpacity(0.2),
|
||||
),
|
||||
),
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
Row(
|
||||
children: [
|
||||
const Icon(Icons.info_outline, size: 20),
|
||||
const SizedBox(width: 8),
|
||||
const Text(
|
||||
'模板信息',
|
||||
style: TextStyle(fontWeight: FontWeight.w500),
|
||||
),
|
||||
],
|
||||
),
|
||||
const SizedBox(height: 12),
|
||||
|
||||
// 基本信息
|
||||
_buildInfoRow('模板名称', widget.template.name),
|
||||
if (widget.template.description?.isNotEmpty == true)
|
||||
_buildInfoRow('描述', widget.template.description!),
|
||||
_buildInfoRow('功能类型', _getFeatureTypeLabel(widget.template.featureType.toApiString())),
|
||||
if (widget.template.authorId?.isNotEmpty == true)
|
||||
_buildInfoRow('作者', widget.template.authorId!),
|
||||
_buildInfoRow('版本', (widget.template.version ?? 1).toString()),
|
||||
_buildInfoRow('语言', widget.template.language ?? 'zh'),
|
||||
_buildInfoRow('创建时间', _formatDateTime(widget.template.createdAt)),
|
||||
_buildInfoRow('使用次数', '${widget.template.usageCount} 次'),
|
||||
_buildInfoRow('收藏次数', '${widget.template.favoriteCount ?? 0} 次'),
|
||||
if (widget.template.rating > 0)
|
||||
_buildInfoRow('评分', widget.template.rating.toStringAsFixed(1)),
|
||||
|
||||
// 标签
|
||||
if (widget.template.tags.isNotEmpty) ...[
|
||||
const SizedBox(height: 8),
|
||||
Row(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
const Text(
|
||||
'标签:',
|
||||
style: TextStyle(fontWeight: FontWeight.w500),
|
||||
),
|
||||
const SizedBox(width: 8),
|
||||
Expanded(
|
||||
child: Wrap(
|
||||
spacing: 6,
|
||||
runSpacing: 6,
|
||||
children: widget.template.tags.map((tag) =>
|
||||
_buildTag(tag)).toList(),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
],
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
Widget _buildInfoRow(String label, String value) {
|
||||
return Padding(
|
||||
padding: const EdgeInsets.only(bottom: 8),
|
||||
child: Row(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
SizedBox(
|
||||
width: 80,
|
||||
child: Text(
|
||||
'$label:',
|
||||
style: const TextStyle(fontWeight: FontWeight.w500),
|
||||
),
|
||||
),
|
||||
Expanded(
|
||||
child: Text(value),
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
Widget _buildTag(String tag) {
|
||||
return Container(
|
||||
padding: const EdgeInsets.symmetric(horizontal: 8, vertical: 4),
|
||||
decoration: BoxDecoration(
|
||||
color: Theme.of(context).colorScheme.primary.withOpacity(0.1),
|
||||
borderRadius: BorderRadius.circular(12),
|
||||
border: Border.all(
|
||||
color: Theme.of(context).colorScheme.primary.withOpacity(0.3),
|
||||
),
|
||||
),
|
||||
child: Text(
|
||||
tag,
|
||||
style: TextStyle(
|
||||
fontSize: 12,
|
||||
color: Theme.of(context).colorScheme.primary,
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
Widget _buildTemplateContent() {
|
||||
return Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
const Text(
|
||||
'模板内容',
|
||||
style: TextStyle(fontSize: 16, fontWeight: FontWeight.bold),
|
||||
),
|
||||
const SizedBox(height: 12),
|
||||
|
||||
Container(
|
||||
width: double.infinity,
|
||||
padding: const EdgeInsets.all(16),
|
||||
decoration: BoxDecoration(
|
||||
color: Theme.of(context).colorScheme.surface,
|
||||
borderRadius: BorderRadius.circular(8),
|
||||
border: Border.all(
|
||||
color: Theme.of(context).colorScheme.outline.withOpacity(0.2),
|
||||
),
|
||||
),
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
if (widget.template.systemPrompt.isNotEmpty) ...[
|
||||
Text(
|
||||
'系统提示词:',
|
||||
style: TextStyle(
|
||||
fontWeight: FontWeight.w500,
|
||||
color: Theme.of(context).colorScheme.onSurface.withOpacity(0.7),
|
||||
),
|
||||
),
|
||||
const SizedBox(height: 8),
|
||||
Container(
|
||||
width: double.infinity,
|
||||
padding: const EdgeInsets.all(12),
|
||||
decoration: BoxDecoration(
|
||||
color: Theme.of(context).colorScheme.background,
|
||||
borderRadius: BorderRadius.circular(6),
|
||||
border: Border.all(
|
||||
color: Theme.of(context).colorScheme.outline.withOpacity(0.1),
|
||||
),
|
||||
),
|
||||
child: Text(
|
||||
widget.template.systemPrompt,
|
||||
style: const TextStyle(
|
||||
fontFamily: 'monospace',
|
||||
fontSize: 13,
|
||||
),
|
||||
),
|
||||
),
|
||||
const SizedBox(height: 16),
|
||||
],
|
||||
if (widget.template.userPrompt.isNotEmpty) ...[
|
||||
Text(
|
||||
'用户提示词:',
|
||||
style: TextStyle(
|
||||
fontWeight: FontWeight.w500,
|
||||
color: Theme.of(context).colorScheme.onSurface.withOpacity(0.7),
|
||||
),
|
||||
),
|
||||
const SizedBox(height: 8),
|
||||
Container(
|
||||
width: double.infinity,
|
||||
padding: const EdgeInsets.all(12),
|
||||
decoration: BoxDecoration(
|
||||
color: Theme.of(context).colorScheme.background,
|
||||
borderRadius: BorderRadius.circular(6),
|
||||
border: Border.all(
|
||||
color: Theme.of(context).colorScheme.outline.withOpacity(0.1),
|
||||
),
|
||||
),
|
||||
child: Text(
|
||||
widget.template.userPrompt,
|
||||
style: const TextStyle(
|
||||
fontFamily: 'monospace',
|
||||
fontSize: 13,
|
||||
),
|
||||
),
|
||||
),
|
||||
],
|
||||
],
|
||||
),
|
||||
),
|
||||
],
|
||||
);
|
||||
}
|
||||
|
||||
Widget _buildReviewSection() {
|
||||
return Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
const Text(
|
||||
'审核操作',
|
||||
style: TextStyle(fontSize: 16, fontWeight: FontWeight.bold),
|
||||
),
|
||||
const SizedBox(height: 16),
|
||||
|
||||
// 审核动作选择
|
||||
Container(
|
||||
padding: const EdgeInsets.all(16),
|
||||
decoration: BoxDecoration(
|
||||
color: Theme.of(context).colorScheme.surface,
|
||||
borderRadius: BorderRadius.circular(8),
|
||||
border: Border.all(
|
||||
color: Theme.of(context).colorScheme.outline.withOpacity(0.2),
|
||||
),
|
||||
),
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
const Text(
|
||||
'审核结果:',
|
||||
style: TextStyle(fontWeight: FontWeight.w500),
|
||||
),
|
||||
const SizedBox(height: 12),
|
||||
|
||||
Column(
|
||||
children: _actionLabels.entries.map((entry) {
|
||||
return RadioListTile<String>(
|
||||
title: Text(
|
||||
entry.value,
|
||||
style: TextStyle(
|
||||
color: _actionColors[entry.key],
|
||||
fontWeight: FontWeight.w500,
|
||||
),
|
||||
),
|
||||
value: entry.key,
|
||||
groupValue: _reviewAction,
|
||||
onChanged: (value) {
|
||||
setState(() {
|
||||
_reviewAction = value!;
|
||||
});
|
||||
},
|
||||
);
|
||||
}).toList(),
|
||||
),
|
||||
|
||||
if (_reviewAction == 'approve') ...[
|
||||
const SizedBox(height: 12),
|
||||
CheckboxListTile(
|
||||
title: const Text('同时设为认证模板'),
|
||||
subtitle: const Text('为该模板添加官方认证标识'),
|
||||
value: _setAsVerified,
|
||||
onChanged: (value) {
|
||||
setState(() {
|
||||
_setAsVerified = value ?? false;
|
||||
});
|
||||
},
|
||||
),
|
||||
],
|
||||
],
|
||||
),
|
||||
),
|
||||
|
||||
const SizedBox(height: 16),
|
||||
|
||||
// 审核评论
|
||||
TextFormField(
|
||||
controller: _reviewCommentController,
|
||||
decoration: InputDecoration(
|
||||
labelText: '审核备注',
|
||||
hintText: _getCommentHint(),
|
||||
border: const OutlineInputBorder(),
|
||||
alignLabelWithHint: true,
|
||||
),
|
||||
maxLines: 4,
|
||||
),
|
||||
],
|
||||
);
|
||||
}
|
||||
|
||||
String _getCommentHint() {
|
||||
switch (_reviewAction) {
|
||||
case 'approve':
|
||||
return '可以添加通过审核的说明(可选)';
|
||||
case 'reject':
|
||||
return '请说明拒绝的原因';
|
||||
case 'request_changes':
|
||||
return '请详细说明需要修改的内容';
|
||||
default:
|
||||
return '请输入审核备注';
|
||||
}
|
||||
}
|
||||
|
||||
Widget _buildActionButtons() {
|
||||
return Row(
|
||||
mainAxisAlignment: MainAxisAlignment.end,
|
||||
children: [
|
||||
TextButton(
|
||||
onPressed: _isLoading ? null : () => Navigator.of(context).pop(),
|
||||
child: const Text('取消'),
|
||||
),
|
||||
const SizedBox(width: 12),
|
||||
ElevatedButton(
|
||||
onPressed: _isLoading ? null : _submitReview,
|
||||
style: ElevatedButton.styleFrom(
|
||||
backgroundColor: _actionColors[_reviewAction],
|
||||
foregroundColor: Colors.white,
|
||||
),
|
||||
child: _isLoading
|
||||
? const SizedBox(
|
||||
width: 20,
|
||||
height: 20,
|
||||
child: CircularProgressIndicator(strokeWidth: 2),
|
||||
)
|
||||
: Text(_actionLabels[_reviewAction]!),
|
||||
),
|
||||
],
|
||||
);
|
||||
}
|
||||
|
||||
Future<void> _submitReview() async {
|
||||
if (_reviewAction == 'reject') {
|
||||
if (_reviewCommentController.text.trim().isEmpty) {
|
||||
ScaffoldMessenger.of(context).showSnackBar(
|
||||
const SnackBar(content: Text('拒绝审核时请填写审核备注')),
|
||||
);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
setState(() {
|
||||
_isLoading = true;
|
||||
});
|
||||
|
||||
try {
|
||||
final reviewComment = _reviewCommentController.text.trim();
|
||||
final approved = _reviewAction == 'approve';
|
||||
|
||||
await widget.onReview(approved, reviewComment.isEmpty ? null : reviewComment);
|
||||
|
||||
if (mounted) {
|
||||
Navigator.of(context).pop();
|
||||
}
|
||||
} catch (e) {
|
||||
AppLogger.e('TemplateReviewDialog', '提交模板审核失败', e);
|
||||
if (mounted) {
|
||||
ScaffoldMessenger.of(context).showSnackBar(
|
||||
SnackBar(content: Text('提交失败: $e')),
|
||||
);
|
||||
}
|
||||
} finally {
|
||||
if (mounted) {
|
||||
setState(() {
|
||||
_isLoading = false;
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
String _getFeatureTypeLabel(String? featureType) {
|
||||
switch (featureType) {
|
||||
case 'AI_CHAT':
|
||||
return 'AI聊天';
|
||||
case 'TEXT_EXPANSION':
|
||||
return '文本扩写';
|
||||
case 'TEXT_REFACTOR':
|
||||
return '文本润色';
|
||||
case 'TEXT_SUMMARY':
|
||||
return '文本总结';
|
||||
case 'SCENE_TO_SUMMARY':
|
||||
return '场景转摘要';
|
||||
case 'SUMMARY_TO_SCENE':
|
||||
return '摘要转场景';
|
||||
case 'NOVEL_GENERATION':
|
||||
return '小说生成';
|
||||
case 'PROFESSIONAL_FICTION_CONTINUATION':
|
||||
return '专业续写';
|
||||
case 'SCENE_BEAT_GENERATION':
|
||||
return '场景节拍生成';
|
||||
default:
|
||||
return featureType ?? '未知';
|
||||
}
|
||||
}
|
||||
|
||||
String _formatDateTime(DateTime? dateTime) {
|
||||
if (dateTime == null) return '';
|
||||
|
||||
final now = DateTime.now();
|
||||
final difference = now.difference(dateTime);
|
||||
|
||||
if (difference.inDays > 0) {
|
||||
return '${difference.inDays}天前';
|
||||
} else if (difference.inHours > 0) {
|
||||
return '${difference.inHours}小时前';
|
||||
} else if (difference.inMinutes > 0) {
|
||||
return '${difference.inMinutes}分钟前';
|
||||
} else {
|
||||
return '刚刚';
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,469 @@
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
import '../../../services/api_service/repositories/impl/admin_repository_impl.dart';
|
||||
import '../../../utils/logger.dart';
|
||||
import '../../../widgets/common/loading_indicator.dart';
|
||||
|
||||
/// 模板统计对话框
|
||||
class TemplateStatisticsDialog extends StatefulWidget {
|
||||
const TemplateStatisticsDialog({Key? key}) : super(key: key);
|
||||
|
||||
@override
|
||||
State<TemplateStatisticsDialog> createState() => _TemplateStatisticsDialogState();
|
||||
}
|
||||
|
||||
class _TemplateStatisticsDialogState extends State<TemplateStatisticsDialog> {
|
||||
final AdminRepositoryImpl _adminRepository = AdminRepositoryImpl();
|
||||
|
||||
bool _isLoading = true;
|
||||
String? _error;
|
||||
Map<String, dynamic>? _statistics;
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
_loadStatistics();
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Dialog(
|
||||
child: Container(
|
||||
width: 600,
|
||||
height: 500,
|
||||
padding: const EdgeInsets.all(24),
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
_buildHeader(),
|
||||
const SizedBox(height: 24),
|
||||
Expanded(
|
||||
child: _buildContent(),
|
||||
),
|
||||
const SizedBox(height: 16),
|
||||
_buildActionButtons(),
|
||||
],
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
Widget _buildHeader() {
|
||||
return Row(
|
||||
children: [
|
||||
const Icon(Icons.analytics, size: 24),
|
||||
const SizedBox(width: 8),
|
||||
const Text(
|
||||
'模板统计',
|
||||
style: TextStyle(fontSize: 20, fontWeight: FontWeight.bold),
|
||||
),
|
||||
const Spacer(),
|
||||
IconButton(
|
||||
onPressed: () => Navigator.of(context).pop(),
|
||||
icon: const Icon(Icons.close),
|
||||
),
|
||||
],
|
||||
);
|
||||
}
|
||||
|
||||
Widget _buildContent() {
|
||||
if (_isLoading) {
|
||||
return const Center(child: LoadingIndicator());
|
||||
}
|
||||
|
||||
if (_error != null) {
|
||||
return Center(
|
||||
child: Column(
|
||||
mainAxisAlignment: MainAxisAlignment.center,
|
||||
children: [
|
||||
Icon(
|
||||
Icons.error_outline,
|
||||
size: 64,
|
||||
color: Theme.of(context).colorScheme.error,
|
||||
),
|
||||
const SizedBox(height: 16),
|
||||
Text(
|
||||
'加载失败',
|
||||
style: Theme.of(context).textTheme.titleLarge,
|
||||
),
|
||||
const SizedBox(height: 8),
|
||||
Text(
|
||||
_error!,
|
||||
style: Theme.of(context).textTheme.bodyMedium?.copyWith(
|
||||
color: Theme.of(context).textTheme.bodyMedium?.color?.withOpacity(0.7),
|
||||
),
|
||||
textAlign: TextAlign.center,
|
||||
),
|
||||
const SizedBox(height: 24),
|
||||
ElevatedButton.icon(
|
||||
onPressed: _loadStatistics,
|
||||
icon: const Icon(Icons.refresh),
|
||||
label: const Text('重试'),
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
if (_statistics == null) {
|
||||
return const Center(
|
||||
child: Text('暂无统计数据'),
|
||||
);
|
||||
}
|
||||
|
||||
return SingleChildScrollView(
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
_buildOverviewSection(),
|
||||
const SizedBox(height: 24),
|
||||
_buildCategorySection(),
|
||||
const SizedBox(height: 24),
|
||||
_buildStatusSection(),
|
||||
const SizedBox(height: 24),
|
||||
_buildTopTemplatesSection(),
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
Widget _buildOverviewSection() {
|
||||
final overview = _statistics!['overview'] as Map<String, dynamic>? ?? {};
|
||||
|
||||
return Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
const Text(
|
||||
'总览',
|
||||
style: TextStyle(fontSize: 16, fontWeight: FontWeight.bold),
|
||||
),
|
||||
const SizedBox(height: 12),
|
||||
|
||||
GridView.count(
|
||||
shrinkWrap: true,
|
||||
physics: const NeverScrollableScrollPhysics(),
|
||||
crossAxisCount: 2,
|
||||
crossAxisSpacing: 16,
|
||||
mainAxisSpacing: 16,
|
||||
childAspectRatio: 2.5,
|
||||
children: [
|
||||
_buildStatCard('总模板数', '${overview['totalTemplates'] ?? 0}', Icons.article, Colors.blue),
|
||||
_buildStatCard('官方模板', '${overview['officialTemplates'] ?? 0}', Icons.verified, Colors.green),
|
||||
_buildStatCard('用户模板', '${overview['userTemplates'] ?? 0}', Icons.person, Colors.orange),
|
||||
_buildStatCard('已发布', '${overview['publishedTemplates'] ?? 0}', Icons.public, Colors.purple),
|
||||
],
|
||||
),
|
||||
],
|
||||
);
|
||||
}
|
||||
|
||||
Widget _buildStatCard(String title, String value, IconData icon, Color color) {
|
||||
return Container(
|
||||
padding: const EdgeInsets.all(16),
|
||||
decoration: BoxDecoration(
|
||||
color: color.withOpacity(0.1),
|
||||
borderRadius: BorderRadius.circular(8),
|
||||
border: Border.all(color: color.withOpacity(0.3)),
|
||||
),
|
||||
child: Row(
|
||||
children: [
|
||||
Icon(icon, size: 24, color: color),
|
||||
const SizedBox(width: 12),
|
||||
Expanded(
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
mainAxisAlignment: MainAxisAlignment.center,
|
||||
children: [
|
||||
Text(
|
||||
value,
|
||||
style: TextStyle(
|
||||
fontSize: 20,
|
||||
fontWeight: FontWeight.bold,
|
||||
color: color,
|
||||
),
|
||||
),
|
||||
Text(
|
||||
title,
|
||||
style: TextStyle(
|
||||
fontSize: 12,
|
||||
color: color.withOpacity(0.8),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
Widget _buildCategorySection() {
|
||||
final categories = _statistics!['byCategory'] as Map<String, dynamic>? ?? {};
|
||||
|
||||
return Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
const Text(
|
||||
'按功能类型分布',
|
||||
style: TextStyle(fontSize: 16, fontWeight: FontWeight.bold),
|
||||
),
|
||||
const SizedBox(height: 12),
|
||||
|
||||
...categories.entries.map((entry) {
|
||||
final label = _getFeatureTypeLabel(entry.key);
|
||||
final count = entry.value as int;
|
||||
return _buildProgressItem(label, count, _getMaxCount(categories));
|
||||
}).toList(),
|
||||
],
|
||||
);
|
||||
}
|
||||
|
||||
Widget _buildStatusSection() {
|
||||
final status = _statistics!['byStatus'] as Map<String, dynamic>? ?? {};
|
||||
|
||||
return Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
const Text(
|
||||
'按状态分布',
|
||||
style: TextStyle(fontSize: 16, fontWeight: FontWeight.bold),
|
||||
),
|
||||
const SizedBox(height: 12),
|
||||
|
||||
...status.entries.map((entry) {
|
||||
final label = _getStatusLabel(entry.key);
|
||||
final count = entry.value as int;
|
||||
final color = _getStatusColor(entry.key);
|
||||
return _buildProgressItem(label, count, _getMaxCount(status), color);
|
||||
}).toList(),
|
||||
],
|
||||
);
|
||||
}
|
||||
|
||||
Widget _buildProgressItem(String label, int count, int maxCount, [Color? color]) {
|
||||
final progress = maxCount > 0 ? count / maxCount : 0.0;
|
||||
final itemColor = color ?? Theme.of(context).colorScheme.primary;
|
||||
|
||||
return Padding(
|
||||
padding: const EdgeInsets.only(bottom: 12),
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
Row(
|
||||
mainAxisAlignment: MainAxisAlignment.spaceBetween,
|
||||
children: [
|
||||
Text(label),
|
||||
Text(
|
||||
'$count',
|
||||
style: TextStyle(
|
||||
fontWeight: FontWeight.bold,
|
||||
color: itemColor,
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
const SizedBox(height: 4),
|
||||
LinearProgressIndicator(
|
||||
value: progress,
|
||||
backgroundColor: itemColor.withOpacity(0.2),
|
||||
valueColor: AlwaysStoppedAnimation<Color>(itemColor),
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
Widget _buildTopTemplatesSection() {
|
||||
final topTemplates = _statistics!['topTemplates'] as List<dynamic>? ?? [];
|
||||
|
||||
return Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
const Text(
|
||||
'热门模板 Top 5',
|
||||
style: TextStyle(fontSize: 16, fontWeight: FontWeight.bold),
|
||||
),
|
||||
const SizedBox(height: 12),
|
||||
|
||||
if (topTemplates.isEmpty)
|
||||
const Text('暂无数据')
|
||||
else
|
||||
...topTemplates.asMap().entries.map((entry) {
|
||||
final index = entry.key;
|
||||
final template = entry.value as Map<String, dynamic>;
|
||||
return _buildTopTemplateItem(
|
||||
index + 1,
|
||||
template['templateName'] as String,
|
||||
template['useCount'] as int,
|
||||
template['averageRating'] as double?,
|
||||
);
|
||||
}).toList(),
|
||||
],
|
||||
);
|
||||
}
|
||||
|
||||
Widget _buildTopTemplateItem(int rank, String name, int useCount, double? rating) {
|
||||
return Container(
|
||||
margin: const EdgeInsets.only(bottom: 8),
|
||||
padding: const EdgeInsets.all(12),
|
||||
decoration: BoxDecoration(
|
||||
color: Theme.of(context).colorScheme.surface,
|
||||
borderRadius: BorderRadius.circular(8),
|
||||
border: Border.all(
|
||||
color: Theme.of(context).colorScheme.outline.withOpacity(0.2),
|
||||
),
|
||||
),
|
||||
child: Row(
|
||||
children: [
|
||||
Container(
|
||||
width: 24,
|
||||
height: 24,
|
||||
decoration: BoxDecoration(
|
||||
color: _getRankColor(rank),
|
||||
shape: BoxShape.circle,
|
||||
),
|
||||
child: Center(
|
||||
child: Text(
|
||||
'$rank',
|
||||
style: const TextStyle(
|
||||
color: Colors.white,
|
||||
fontSize: 12,
|
||||
fontWeight: FontWeight.bold,
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
const SizedBox(width: 12),
|
||||
Expanded(
|
||||
child: Text(
|
||||
name,
|
||||
style: const TextStyle(fontWeight: FontWeight.w500),
|
||||
),
|
||||
),
|
||||
Text(
|
||||
'使用 $useCount 次',
|
||||
style: TextStyle(
|
||||
fontSize: 12,
|
||||
color: Theme.of(context).colorScheme.onSurface.withOpacity(0.6),
|
||||
),
|
||||
),
|
||||
if (rating != null && rating > 0) ...[
|
||||
const SizedBox(width: 8),
|
||||
Icon(Icons.star, size: 16, color: Colors.amber),
|
||||
Text(
|
||||
rating.toStringAsFixed(1),
|
||||
style: TextStyle(
|
||||
fontSize: 12,
|
||||
color: Theme.of(context).colorScheme.onSurface.withOpacity(0.6),
|
||||
),
|
||||
),
|
||||
],
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
Widget _buildActionButtons() {
|
||||
return Row(
|
||||
mainAxisAlignment: MainAxisAlignment.end,
|
||||
children: [
|
||||
TextButton.icon(
|
||||
onPressed: _loadStatistics,
|
||||
icon: const Icon(Icons.refresh),
|
||||
label: const Text('刷新'),
|
||||
),
|
||||
const SizedBox(width: 12),
|
||||
ElevatedButton(
|
||||
onPressed: () => Navigator.of(context).pop(),
|
||||
child: const Text('关闭'),
|
||||
),
|
||||
],
|
||||
);
|
||||
}
|
||||
|
||||
Future<void> _loadStatistics() async {
|
||||
setState(() {
|
||||
_isLoading = true;
|
||||
_error = null;
|
||||
});
|
||||
|
||||
try {
|
||||
final statistics = await _adminRepository.getTemplateStatistics();
|
||||
setState(() {
|
||||
_statistics = statistics;
|
||||
_isLoading = false;
|
||||
});
|
||||
} catch (e) {
|
||||
AppLogger.e('TemplateStatisticsDialog', '加载模板统计失败', e);
|
||||
setState(() {
|
||||
_error = e.toString();
|
||||
_isLoading = false;
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
int _getMaxCount(Map<String, dynamic> data) {
|
||||
if (data.isEmpty) return 1;
|
||||
return data.values.cast<int>().reduce((a, b) => a > b ? a : b);
|
||||
}
|
||||
|
||||
String _getFeatureTypeLabel(String featureType) {
|
||||
switch (featureType) {
|
||||
case 'CHAT':
|
||||
return 'AI聊天';
|
||||
case 'SCENE_GENERATION':
|
||||
return '场景生成';
|
||||
case 'CONTINUATION':
|
||||
return '续写';
|
||||
case 'SUMMARY':
|
||||
return '总结';
|
||||
case 'OUTLINE':
|
||||
return '大纲';
|
||||
default:
|
||||
return featureType;
|
||||
}
|
||||
}
|
||||
|
||||
String _getStatusLabel(String status) {
|
||||
switch (status) {
|
||||
case 'PUBLISHED':
|
||||
return '已发布';
|
||||
case 'PENDING':
|
||||
return '待审核';
|
||||
case 'REJECTED':
|
||||
return '已拒绝';
|
||||
case 'VERIFIED':
|
||||
return '已认证';
|
||||
default:
|
||||
return status;
|
||||
}
|
||||
}
|
||||
|
||||
Color _getStatusColor(String status) {
|
||||
switch (status) {
|
||||
case 'PUBLISHED':
|
||||
return Colors.green;
|
||||
case 'PENDING':
|
||||
return Colors.orange;
|
||||
case 'REJECTED':
|
||||
return Colors.red;
|
||||
case 'VERIFIED':
|
||||
return Colors.blue;
|
||||
default:
|
||||
return Colors.grey;
|
||||
}
|
||||
}
|
||||
|
||||
Color _getRankColor(int rank) {
|
||||
switch (rank) {
|
||||
case 1:
|
||||
return Colors.amber;
|
||||
case 2:
|
||||
return Colors.grey[400]!;
|
||||
case 3:
|
||||
return Colors.orange[300]!;
|
||||
default:
|
||||
return Colors.blue;
|
||||
}
|
||||
}
|
||||
}
|
||||
236
AINoval/lib/screens/admin/widgets/user_edit_dialog.dart
Normal file
236
AINoval/lib/screens/admin/widgets/user_edit_dialog.dart
Normal file
@@ -0,0 +1,236 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import '../../../models/admin/admin_models.dart';
|
||||
|
||||
class UserEditDialog extends StatefulWidget {
|
||||
final AdminUser user;
|
||||
|
||||
const UserEditDialog({
|
||||
super.key,
|
||||
required this.user,
|
||||
});
|
||||
|
||||
@override
|
||||
State<UserEditDialog> createState() => _UserEditDialogState();
|
||||
}
|
||||
|
||||
class _UserEditDialogState extends State<UserEditDialog> {
|
||||
final _formKey = GlobalKey<FormState>();
|
||||
late final TextEditingController _emailController;
|
||||
late final TextEditingController _displayNameController;
|
||||
late String _selectedAccountStatus;
|
||||
|
||||
final List<String> _accountStatuses = [
|
||||
'ACTIVE',
|
||||
'SUSPENDED',
|
||||
'DISABLED',
|
||||
'PENDING_VERIFICATION',
|
||||
];
|
||||
|
||||
final Map<String, String> _statusLabels = {
|
||||
'ACTIVE': '活跃',
|
||||
'SUSPENDED': '暂停',
|
||||
'DISABLED': '禁用',
|
||||
'PENDING_VERIFICATION': '待验证',
|
||||
};
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
_emailController = TextEditingController(text: widget.user.email);
|
||||
_displayNameController = TextEditingController(text: widget.user.displayName ?? '');
|
||||
_selectedAccountStatus = widget.user.accountStatus;
|
||||
}
|
||||
|
||||
@override
|
||||
void dispose() {
|
||||
_emailController.dispose();
|
||||
_displayNameController.dispose();
|
||||
super.dispose();
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return AlertDialog(
|
||||
title: Text(
|
||||
'编辑用户信息',
|
||||
style: Theme.of(context).textTheme.titleLarge?.copyWith(
|
||||
fontWeight: FontWeight.bold,
|
||||
),
|
||||
),
|
||||
content: SizedBox(
|
||||
width: 400,
|
||||
child: Form(
|
||||
key: _formKey,
|
||||
child: Column(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
// 用户基本信息展示
|
||||
Container(
|
||||
padding: const EdgeInsets.all(12),
|
||||
decoration: BoxDecoration(
|
||||
color: Theme.of(context).colorScheme.surfaceVariant,
|
||||
borderRadius: BorderRadius.circular(8),
|
||||
),
|
||||
child: Row(
|
||||
children: [
|
||||
Icon(
|
||||
Icons.person,
|
||||
color: Theme.of(context).colorScheme.onSurfaceVariant,
|
||||
),
|
||||
const SizedBox(width: 8),
|
||||
Expanded(
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
Text(
|
||||
widget.user.username,
|
||||
style: Theme.of(context).textTheme.titleMedium?.copyWith(
|
||||
fontWeight: FontWeight.w600,
|
||||
),
|
||||
),
|
||||
Text(
|
||||
'ID: ${widget.user.id}',
|
||||
style: Theme.of(context).textTheme.bodySmall?.copyWith(
|
||||
color: Theme.of(context).textTheme.bodySmall?.color?.withOpacity(0.7),
|
||||
),
|
||||
),
|
||||
Text(
|
||||
'积分: ${widget.user.credits}',
|
||||
style: Theme.of(context).textTheme.bodyMedium?.copyWith(
|
||||
color: Theme.of(context).colorScheme.primary,
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
const SizedBox(height: 16),
|
||||
|
||||
// 邮箱输入
|
||||
TextFormField(
|
||||
controller: _emailController,
|
||||
decoration: const InputDecoration(
|
||||
labelText: '邮箱',
|
||||
prefixIcon: Icon(Icons.email),
|
||||
border: OutlineInputBorder(),
|
||||
),
|
||||
keyboardType: TextInputType.emailAddress,
|
||||
validator: (value) {
|
||||
if (value == null || value.isEmpty) {
|
||||
return '请输入邮箱';
|
||||
}
|
||||
if (!RegExp(r'^[\w-\.]+@([\w-]+\.)+[\w-]{2,4}$').hasMatch(value)) {
|
||||
return '请输入有效的邮箱地址';
|
||||
}
|
||||
return null;
|
||||
},
|
||||
),
|
||||
const SizedBox(height: 16),
|
||||
|
||||
// 显示名称输入
|
||||
TextFormField(
|
||||
controller: _displayNameController,
|
||||
decoration: const InputDecoration(
|
||||
labelText: '显示名称',
|
||||
prefixIcon: Icon(Icons.badge),
|
||||
border: OutlineInputBorder(),
|
||||
hintText: '可选,留空则使用用户名',
|
||||
),
|
||||
),
|
||||
const SizedBox(height: 16),
|
||||
|
||||
// 账户状态选择
|
||||
DropdownButtonFormField<String>(
|
||||
value: _selectedAccountStatus,
|
||||
decoration: const InputDecoration(
|
||||
labelText: '账户状态',
|
||||
prefixIcon: Icon(Icons.account_circle),
|
||||
border: OutlineInputBorder(),
|
||||
),
|
||||
items: _accountStatuses.map((status) {
|
||||
return DropdownMenuItem<String>(
|
||||
value: status,
|
||||
child: Row(
|
||||
children: [
|
||||
_getStatusIcon(status),
|
||||
const SizedBox(width: 8),
|
||||
Text(_statusLabels[status] ?? status),
|
||||
],
|
||||
),
|
||||
);
|
||||
}).toList(),
|
||||
onChanged: (value) {
|
||||
if (value != null) {
|
||||
setState(() {
|
||||
_selectedAccountStatus = value;
|
||||
});
|
||||
}
|
||||
},
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
actions: [
|
||||
TextButton(
|
||||
onPressed: () => Navigator.of(context).pop(),
|
||||
child: const Text('取消'),
|
||||
),
|
||||
ElevatedButton(
|
||||
onPressed: _handleSubmit,
|
||||
child: const Text('保存'),
|
||||
),
|
||||
],
|
||||
);
|
||||
}
|
||||
|
||||
Widget _getStatusIcon(String status) {
|
||||
switch (status) {
|
||||
case 'ACTIVE':
|
||||
return Icon(Icons.check_circle, color: Colors.green, size: 16);
|
||||
case 'SUSPENDED':
|
||||
return Icon(Icons.pause_circle, color: Colors.orange, size: 16);
|
||||
case 'DISABLED':
|
||||
return Icon(Icons.cancel, color: Colors.red, size: 16);
|
||||
case 'PENDING_VERIFICATION':
|
||||
return Icon(Icons.pending, color: Colors.blue, size: 16);
|
||||
default:
|
||||
return Icon(Icons.help, color: Colors.grey, size: 16);
|
||||
}
|
||||
}
|
||||
|
||||
void _handleSubmit() {
|
||||
if (_formKey.currentState?.validate() == true) {
|
||||
final email = _emailController.text.trim();
|
||||
final displayName = _displayNameController.text.trim();
|
||||
|
||||
// 检查是否有更改
|
||||
bool hasChanges = false;
|
||||
Map<String, dynamic> changes = {};
|
||||
|
||||
if (email != widget.user.email) {
|
||||
hasChanges = true;
|
||||
changes['email'] = email;
|
||||
}
|
||||
|
||||
if (displayName != (widget.user.displayName ?? '')) {
|
||||
hasChanges = true;
|
||||
changes['displayName'] = displayName.isEmpty ? null : displayName;
|
||||
}
|
||||
|
||||
if (_selectedAccountStatus != widget.user.accountStatus) {
|
||||
hasChanges = true;
|
||||
changes['accountStatus'] = _selectedAccountStatus;
|
||||
}
|
||||
|
||||
if (hasChanges) {
|
||||
Navigator.of(context).pop(changes);
|
||||
} else {
|
||||
Navigator.of(context).pop();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
484
AINoval/lib/screens/admin/widgets/user_management_table.dart
Normal file
484
AINoval/lib/screens/admin/widgets/user_management_table.dart
Normal file
@@ -0,0 +1,484 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_bloc/flutter_bloc.dart';
|
||||
import '../../../models/admin/admin_models.dart';
|
||||
import '../../../blocs/admin/admin_bloc.dart';
|
||||
import 'credit_operation_dialog.dart';
|
||||
import 'user_edit_dialog.dart';
|
||||
|
||||
class UserManagementTable extends StatelessWidget {
|
||||
final List<AdminUser> users;
|
||||
|
||||
const UserManagementTable({
|
||||
super.key,
|
||||
required this.users,
|
||||
});
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Card(
|
||||
elevation: 4,
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
// 标题栏
|
||||
Container(
|
||||
padding: const EdgeInsets.all(16),
|
||||
decoration: BoxDecoration(
|
||||
color: Theme.of(context).colorScheme.primary.withOpacity(0.1),
|
||||
borderRadius: const BorderRadius.only(
|
||||
topLeft: Radius.circular(8),
|
||||
topRight: Radius.circular(8),
|
||||
),
|
||||
),
|
||||
child: Row(
|
||||
mainAxisAlignment: MainAxisAlignment.spaceBetween,
|
||||
children: [
|
||||
Text(
|
||||
'用户管理',
|
||||
style: Theme.of(context).textTheme.titleLarge?.copyWith(
|
||||
fontWeight: FontWeight.bold,
|
||||
),
|
||||
),
|
||||
Row(
|
||||
children: [
|
||||
IconButton(
|
||||
icon: const Icon(Icons.refresh),
|
||||
onPressed: () => context.read<AdminBloc>().add(LoadUsers()),
|
||||
tooltip: '刷新用户列表',
|
||||
),
|
||||
const SizedBox(width: 8),
|
||||
Text(
|
||||
'总计: ${users.length} 用户',
|
||||
style: Theme.of(context).textTheme.bodyMedium?.copyWith(
|
||||
color: Theme.of(context).colorScheme.primary,
|
||||
fontWeight: FontWeight.w500,
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
// 数据表格
|
||||
if (users.isNotEmpty)
|
||||
SingleChildScrollView(
|
||||
scrollDirection: Axis.horizontal,
|
||||
child: DataTable(
|
||||
columnSpacing: 16,
|
||||
dataRowMinHeight: 48,
|
||||
dataRowMaxHeight: 80,
|
||||
headingRowHeight: 56,
|
||||
columns: const [
|
||||
DataColumn(
|
||||
label: Text(
|
||||
'用户名',
|
||||
style: TextStyle(fontWeight: FontWeight.bold),
|
||||
),
|
||||
),
|
||||
DataColumn(
|
||||
label: Text(
|
||||
'邮箱',
|
||||
style: TextStyle(fontWeight: FontWeight.bold),
|
||||
),
|
||||
),
|
||||
DataColumn(
|
||||
label: Text(
|
||||
'状态',
|
||||
style: TextStyle(fontWeight: FontWeight.bold),
|
||||
),
|
||||
),
|
||||
DataColumn(
|
||||
label: Text(
|
||||
'积分',
|
||||
style: TextStyle(fontWeight: FontWeight.bold),
|
||||
),
|
||||
numeric: true,
|
||||
),
|
||||
DataColumn(
|
||||
label: Text(
|
||||
'角色',
|
||||
style: TextStyle(fontWeight: FontWeight.bold),
|
||||
),
|
||||
),
|
||||
DataColumn(
|
||||
label: Text(
|
||||
'创建时间',
|
||||
style: TextStyle(fontWeight: FontWeight.bold),
|
||||
),
|
||||
),
|
||||
DataColumn(
|
||||
label: Text(
|
||||
'操作',
|
||||
style: TextStyle(fontWeight: FontWeight.bold),
|
||||
),
|
||||
),
|
||||
],
|
||||
rows: users.map((user) => DataRow(
|
||||
cells: [
|
||||
DataCell(
|
||||
Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
mainAxisAlignment: MainAxisAlignment.center,
|
||||
children: [
|
||||
Text(
|
||||
user.username,
|
||||
style: const TextStyle(fontWeight: FontWeight.w500),
|
||||
),
|
||||
if (user.displayName != null && user.displayName!.isNotEmpty)
|
||||
Text(
|
||||
user.displayName!,
|
||||
style: Theme.of(context).textTheme.bodySmall?.copyWith(
|
||||
color: Theme.of(context).textTheme.bodySmall?.color?.withOpacity(0.7),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
DataCell(
|
||||
SelectableText(
|
||||
user.email,
|
||||
style: Theme.of(context).textTheme.bodySmall,
|
||||
),
|
||||
),
|
||||
DataCell(_buildStatusChip(context, user.accountStatus)),
|
||||
DataCell(
|
||||
Container(
|
||||
padding: const EdgeInsets.symmetric(horizontal: 8, vertical: 4),
|
||||
decoration: BoxDecoration(
|
||||
color: Theme.of(context).colorScheme.primaryContainer,
|
||||
borderRadius: BorderRadius.circular(12),
|
||||
),
|
||||
child: Text(
|
||||
_formatCredits(user.credits),
|
||||
style: TextStyle(
|
||||
color: Theme.of(context).colorScheme.onPrimaryContainer,
|
||||
fontWeight: FontWeight.w600,
|
||||
fontSize: 12,
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
DataCell(
|
||||
SizedBox(
|
||||
width: 100,
|
||||
child: Wrap(
|
||||
spacing: 4,
|
||||
runSpacing: 2,
|
||||
children: user.roles.take(2).map((role) => Chip(
|
||||
label: Text(
|
||||
role,
|
||||
style: const TextStyle(fontSize: 10),
|
||||
),
|
||||
backgroundColor: Theme.of(context).colorScheme.secondaryContainer,
|
||||
labelStyle: TextStyle(
|
||||
color: Theme.of(context).colorScheme.onSecondaryContainer,
|
||||
),
|
||||
materialTapTargetSize: MaterialTapTargetSize.shrinkWrap,
|
||||
visualDensity: VisualDensity.compact,
|
||||
)).toList(),
|
||||
),
|
||||
),
|
||||
),
|
||||
DataCell(
|
||||
Text(
|
||||
user.createdAt.toString().substring(0, 10),
|
||||
style: Theme.of(context).textTheme.bodySmall,
|
||||
),
|
||||
),
|
||||
DataCell(_buildActionButtons(context, user)),
|
||||
],
|
||||
)).toList(),
|
||||
),
|
||||
)
|
||||
else
|
||||
Container(
|
||||
padding: const EdgeInsets.all(32),
|
||||
child: Center(
|
||||
child: Column(
|
||||
children: [
|
||||
Icon(
|
||||
Icons.people_outline,
|
||||
size: 64,
|
||||
color: Theme.of(context).colorScheme.onSurface.withOpacity(0.3),
|
||||
),
|
||||
const SizedBox(height: 16),
|
||||
Text(
|
||||
'暂无用户数据',
|
||||
style: Theme.of(context).textTheme.bodyLarge?.copyWith(
|
||||
color: Theme.of(context).colorScheme.onSurface.withOpacity(0.6),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
Widget _buildStatusChip(BuildContext context, String status) {
|
||||
Color backgroundColor;
|
||||
Color textColor;
|
||||
IconData icon;
|
||||
String label;
|
||||
|
||||
switch (status) {
|
||||
case 'ACTIVE':
|
||||
backgroundColor = Colors.green.withOpacity(0.1);
|
||||
textColor = Colors.green.shade700;
|
||||
icon = Icons.check_circle;
|
||||
label = '活跃';
|
||||
break;
|
||||
case 'SUSPENDED':
|
||||
backgroundColor = Colors.orange.withOpacity(0.1);
|
||||
textColor = Colors.orange.shade700;
|
||||
icon = Icons.pause_circle;
|
||||
label = '暂停';
|
||||
break;
|
||||
case 'DISABLED':
|
||||
backgroundColor = Colors.red.withOpacity(0.1);
|
||||
textColor = Colors.red.shade700;
|
||||
icon = Icons.cancel;
|
||||
label = '禁用';
|
||||
break;
|
||||
case 'PENDING_VERIFICATION':
|
||||
backgroundColor = Colors.blue.withOpacity(0.1);
|
||||
textColor = Colors.blue.shade700;
|
||||
icon = Icons.pending;
|
||||
label = '待验证';
|
||||
break;
|
||||
default:
|
||||
backgroundColor = Colors.grey.withOpacity(0.1);
|
||||
textColor = Colors.grey.shade700;
|
||||
icon = Icons.help;
|
||||
label = status;
|
||||
}
|
||||
|
||||
return Container(
|
||||
padding: const EdgeInsets.symmetric(horizontal: 8, vertical: 4),
|
||||
decoration: BoxDecoration(
|
||||
color: backgroundColor,
|
||||
borderRadius: BorderRadius.circular(12),
|
||||
),
|
||||
child: Row(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
children: [
|
||||
Icon(icon, size: 14, color: textColor),
|
||||
const SizedBox(width: 4),
|
||||
Text(
|
||||
label,
|
||||
style: TextStyle(
|
||||
color: textColor,
|
||||
fontWeight: FontWeight.w500,
|
||||
fontSize: 12,
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
String _formatCredits(int credits) {
|
||||
if (credits >= 1000000) {
|
||||
return '${(credits / 1000000).toStringAsFixed(1)}M';
|
||||
} else if (credits >= 1000) {
|
||||
return '${(credits / 1000).toStringAsFixed(1)}K';
|
||||
} else {
|
||||
return credits.toString();
|
||||
}
|
||||
}
|
||||
|
||||
Widget _buildActionButtons(BuildContext context, AdminUser user) {
|
||||
return Row(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
children: [
|
||||
// 编辑用户信息
|
||||
IconButton(
|
||||
icon: const Icon(Icons.edit, size: 18),
|
||||
onPressed: () => _showEditUserDialog(context, user),
|
||||
tooltip: '编辑用户信息',
|
||||
visualDensity: VisualDensity.compact,
|
||||
),
|
||||
// 添加积分
|
||||
IconButton(
|
||||
icon: const Icon(Icons.add_circle, size: 18),
|
||||
onPressed: () => _showCreditDialog(context, user, true),
|
||||
tooltip: '添加积分',
|
||||
visualDensity: VisualDensity.compact,
|
||||
color: Colors.green.shade700,
|
||||
),
|
||||
// 扣减积分
|
||||
IconButton(
|
||||
icon: const Icon(Icons.remove_circle, size: 18),
|
||||
onPressed: () => _showCreditDialog(context, user, false),
|
||||
tooltip: '扣减积分',
|
||||
visualDensity: VisualDensity.compact,
|
||||
color: Colors.red.shade700,
|
||||
),
|
||||
// 更多操作
|
||||
PopupMenuButton<String>(
|
||||
icon: const Icon(Icons.more_vert, size: 18),
|
||||
onSelected: (value) => _handleMenuAction(context, user, value),
|
||||
itemBuilder: (context) => [
|
||||
const PopupMenuItem(
|
||||
value: 'toggle_status',
|
||||
child: ListTile(
|
||||
leading: Icon(Icons.swap_horiz, size: 18),
|
||||
title: Text('切换状态'),
|
||||
contentPadding: EdgeInsets.zero,
|
||||
dense: true,
|
||||
),
|
||||
),
|
||||
const PopupMenuItem(
|
||||
value: 'assign_role',
|
||||
child: ListTile(
|
||||
leading: Icon(Icons.security, size: 18),
|
||||
title: Text('分配角色'),
|
||||
contentPadding: EdgeInsets.zero,
|
||||
dense: true,
|
||||
),
|
||||
),
|
||||
const PopupMenuItem(
|
||||
value: 'view_details',
|
||||
child: ListTile(
|
||||
leading: Icon(Icons.info, size: 18),
|
||||
title: Text('查看详情'),
|
||||
contentPadding: EdgeInsets.zero,
|
||||
dense: true,
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
],
|
||||
);
|
||||
}
|
||||
|
||||
void _showEditUserDialog(BuildContext context, AdminUser user) async {
|
||||
final result = await showDialog<Map<String, dynamic>>(
|
||||
context: context,
|
||||
builder: (context) => UserEditDialog(user: user),
|
||||
);
|
||||
|
||||
if (result != null && context.mounted) {
|
||||
context.read<AdminBloc>().add(UpdateUserInfo(
|
||||
userId: user.id,
|
||||
email: result['email'],
|
||||
displayName: result['displayName'],
|
||||
accountStatus: result['accountStatus'],
|
||||
));
|
||||
}
|
||||
}
|
||||
|
||||
void _showCreditDialog(BuildContext context, AdminUser user, bool isAdd) async {
|
||||
final result = await showDialog<Map<String, dynamic>>(
|
||||
context: context,
|
||||
builder: (context) => CreditOperationDialog(user: user, isAdd: isAdd),
|
||||
);
|
||||
|
||||
if (result != null && context.mounted) {
|
||||
final amount = result['amount'] as int;
|
||||
final reason = result['reason'] as String;
|
||||
|
||||
if (isAdd) {
|
||||
context.read<AdminBloc>().add(AddCreditsToUser(
|
||||
userId: user.id,
|
||||
amount: amount,
|
||||
reason: reason,
|
||||
));
|
||||
} else {
|
||||
context.read<AdminBloc>().add(DeductCreditsFromUser(
|
||||
userId: user.id,
|
||||
amount: amount,
|
||||
reason: reason,
|
||||
));
|
||||
}
|
||||
|
||||
// 显示成功消息
|
||||
ScaffoldMessenger.of(context).showSnackBar(
|
||||
SnackBar(
|
||||
content: Text('${isAdd ? "添加" : "扣减"}积分操作已提交'),
|
||||
backgroundColor: Colors.green,
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
void _handleMenuAction(BuildContext context, AdminUser user, String action) {
|
||||
switch (action) {
|
||||
case 'toggle_status':
|
||||
final newStatus = user.accountStatus == 'ACTIVE' ? 'SUSPENDED' : 'ACTIVE';
|
||||
context.read<AdminBloc>().add(UpdateUserStatus(
|
||||
userId: user.id,
|
||||
status: newStatus,
|
||||
));
|
||||
break;
|
||||
case 'assign_role':
|
||||
_showAssignRoleDialog(context, user);
|
||||
break;
|
||||
case 'view_details':
|
||||
_showUserDetailsDialog(context, user);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
void _showAssignRoleDialog(BuildContext context, AdminUser user) {
|
||||
// TODO: 实现角色分配对话框
|
||||
ScaffoldMessenger.of(context).showSnackBar(
|
||||
const SnackBar(content: Text('角色分配功能开发中...')),
|
||||
);
|
||||
}
|
||||
|
||||
void _showUserDetailsDialog(BuildContext context, AdminUser user) {
|
||||
showDialog(
|
||||
context: context,
|
||||
builder: (context) => AlertDialog(
|
||||
title: Text('用户详情 - ${user.username}'),
|
||||
content: SingleChildScrollView(
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
children: [
|
||||
_buildDetailRow('用户ID', user.id),
|
||||
_buildDetailRow('用户名', user.username),
|
||||
_buildDetailRow('邮箱', user.email),
|
||||
_buildDetailRow('显示名称', user.displayName ?? '-'),
|
||||
_buildDetailRow('账户状态', user.accountStatus),
|
||||
_buildDetailRow('积分余额', user.credits.toString()),
|
||||
_buildDetailRow('角色', user.roles.join(', ')),
|
||||
_buildDetailRow('创建时间', user.createdAt.toString()),
|
||||
_buildDetailRow('更新时间', user.updatedAt?.toString() ?? '-'),
|
||||
],
|
||||
),
|
||||
),
|
||||
actions: [
|
||||
TextButton(
|
||||
onPressed: () => Navigator.of(context).pop(),
|
||||
child: const Text('关闭'),
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
Widget _buildDetailRow(String label, String value) {
|
||||
return Padding(
|
||||
padding: const EdgeInsets.symmetric(vertical: 4),
|
||||
child: Row(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
SizedBox(
|
||||
width: 80,
|
||||
child: Text(
|
||||
'$label:',
|
||||
style: const TextStyle(fontWeight: FontWeight.w500),
|
||||
),
|
||||
),
|
||||
Expanded(
|
||||
child: SelectableText(value),
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
216
AINoval/lib/screens/admin/widgets/validation_results_dialog.dart
Normal file
216
AINoval/lib/screens/admin/widgets/validation_results_dialog.dart
Normal file
@@ -0,0 +1,216 @@
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
import '../../../models/public_model_config.dart';
|
||||
import '../../../utils/web_theme.dart';
|
||||
|
||||
class ValidationResultsDialog extends StatelessWidget {
|
||||
const ValidationResultsDialog({
|
||||
super.key,
|
||||
required this.config,
|
||||
});
|
||||
|
||||
final PublicModelConfigWithKeys config;
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final keys = config.apiKeyStatuses ?? const <ApiKeyWithStatus>[];
|
||||
final successCount = keys.where((k) => k.isValid == true).length;
|
||||
final total = keys.length;
|
||||
final bool allPass = total > 0 && successCount == total;
|
||||
final bool somePass = successCount > 0 && successCount < total;
|
||||
|
||||
return Dialog(
|
||||
backgroundColor: WebTheme.getCardColor(context),
|
||||
child: Container(
|
||||
width: 720,
|
||||
height: 520,
|
||||
padding: const EdgeInsets.all(20),
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
Row(
|
||||
children: [
|
||||
Text(
|
||||
'API Key 验证结果',
|
||||
style: TextStyle(
|
||||
fontSize: 18,
|
||||
fontWeight: FontWeight.w600,
|
||||
color: WebTheme.getTextColor(context),
|
||||
),
|
||||
),
|
||||
const SizedBox(width: 12),
|
||||
_statusChip(
|
||||
context,
|
||||
successCount == total && total > 0 ? '全部通过' : '$successCount/$total 通过',
|
||||
successCount == total && total > 0 ? Colors.green : Colors.orange,
|
||||
),
|
||||
const Spacer(),
|
||||
IconButton(
|
||||
onPressed: () => Navigator.of(context).pop(),
|
||||
icon: Icon(Icons.close, color: WebTheme.getTextColor(context)),
|
||||
tooltip: '关闭',
|
||||
),
|
||||
],
|
||||
),
|
||||
const SizedBox(height: 10),
|
||||
_buildSummaryBanner(context, allPass: allPass, somePass: somePass, total: total, successCount: successCount),
|
||||
const SizedBox(height: 12),
|
||||
Text(
|
||||
'${config.provider}:${config.modelId}${config.displayName != null && config.displayName!.isNotEmpty ? ' (${config.displayName})' : ''}',
|
||||
style: TextStyle(color: WebTheme.getSecondaryTextColor(context)),
|
||||
),
|
||||
const SizedBox(height: 16),
|
||||
Expanded(
|
||||
child: Container(
|
||||
decoration: BoxDecoration(
|
||||
color: WebTheme.getBackgroundColor(context),
|
||||
borderRadius: BorderRadius.circular(8),
|
||||
border: Border.all(color: WebTheme.getSecondaryBorderColor(context)),
|
||||
),
|
||||
child: keys.isEmpty
|
||||
? Center(
|
||||
child: Text('没有可显示的API Key', style: TextStyle(color: WebTheme.getSecondaryTextColor(context))),
|
||||
)
|
||||
: ListView.separated(
|
||||
itemCount: keys.length,
|
||||
separatorBuilder: (_, __) => Divider(height: 1, color: WebTheme.getSecondaryBorderColor(context)),
|
||||
itemBuilder: (context, index) {
|
||||
final item = keys[index];
|
||||
return _buildRow(context, item);
|
||||
},
|
||||
),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
Widget _buildSummaryBanner(BuildContext context, {required bool allPass, required bool somePass, required int total, required int successCount}) {
|
||||
late Color bg;
|
||||
late Color fg;
|
||||
late IconData icon;
|
||||
late String text;
|
||||
if (allPass) {
|
||||
bg = Colors.green.withOpacity(0.12);
|
||||
fg = Colors.green;
|
||||
icon = Icons.check_circle_rounded;
|
||||
text = '全部通过:$successCount/$total';
|
||||
} else if (somePass) {
|
||||
bg = Colors.orange.withOpacity(0.12);
|
||||
fg = Colors.orange;
|
||||
icon = Icons.error_outline_rounded;
|
||||
text = '部分通过:$successCount/$total';
|
||||
} else {
|
||||
bg = Colors.red.withOpacity(0.12);
|
||||
fg = Colors.red;
|
||||
icon = Icons.cancel_rounded;
|
||||
text = total == 0 ? '未配置任何API Key' : '全部失败:$successCount/$total';
|
||||
}
|
||||
return Container(
|
||||
padding: const EdgeInsets.symmetric(horizontal: 12, vertical: 10),
|
||||
decoration: BoxDecoration(
|
||||
color: bg,
|
||||
borderRadius: BorderRadius.circular(8),
|
||||
border: Border.all(color: fg.withOpacity(0.35)),
|
||||
),
|
||||
child: Row(
|
||||
children: [
|
||||
Icon(icon, color: fg, size: 18),
|
||||
const SizedBox(width: 8),
|
||||
Expanded(
|
||||
child: Text(
|
||||
text,
|
||||
style: TextStyle(color: fg, fontWeight: FontWeight.w600),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
Widget _buildRow(BuildContext context, ApiKeyWithStatus item) {
|
||||
final maskedKey = _maskKey(item.apiKey ?? '');
|
||||
final isOk = item.isValid == true;
|
||||
return Padding(
|
||||
padding: const EdgeInsets.symmetric(horizontal: 12, vertical: 10),
|
||||
child: Row(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
Container(
|
||||
width: 10,
|
||||
height: 10,
|
||||
margin: const EdgeInsets.only(top: 6),
|
||||
decoration: BoxDecoration(
|
||||
color: isOk ? Colors.green : Colors.red,
|
||||
shape: BoxShape.circle,
|
||||
),
|
||||
),
|
||||
const SizedBox(width: 10),
|
||||
Expanded(
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
Row(
|
||||
children: [
|
||||
Expanded(
|
||||
child: Text(
|
||||
maskedKey,
|
||||
style: TextStyle(
|
||||
color: WebTheme.getTextColor(context),
|
||||
fontFamily: 'monospace',
|
||||
),
|
||||
),
|
||||
),
|
||||
const SizedBox(width: 8),
|
||||
_statusChip(context, isOk ? '有效' : '无效', isOk ? Colors.green : Colors.red),
|
||||
],
|
||||
),
|
||||
if ((item.validationError ?? '').isNotEmpty) ...[
|
||||
const SizedBox(height: 6),
|
||||
Text(
|
||||
item.validationError!,
|
||||
style: TextStyle(color: Colors.redAccent),
|
||||
),
|
||||
],
|
||||
if ((item.note ?? '').isNotEmpty) ...[
|
||||
const SizedBox(height: 6),
|
||||
Text(
|
||||
'备注: ${item.note}',
|
||||
style: TextStyle(color: WebTheme.getSecondaryTextColor(context)),
|
||||
),
|
||||
],
|
||||
],
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
String _maskKey(String key) {
|
||||
if (key.isEmpty) return '(空)';
|
||||
if (key.length <= 8) return '****$key';
|
||||
final start = key.substring(0, 4);
|
||||
final end = key.substring(key.length - 4);
|
||||
return '$start••••••••$end';
|
||||
}
|
||||
|
||||
Widget _statusChip(BuildContext context, String text, Color color) {
|
||||
return Container(
|
||||
padding: const EdgeInsets.symmetric(horizontal: 8, vertical: 4),
|
||||
decoration: BoxDecoration(
|
||||
color: color.withOpacity(0.12),
|
||||
borderRadius: BorderRadius.circular(10),
|
||||
border: Border.all(color: color.withOpacity(0.4)),
|
||||
),
|
||||
child: Text(
|
||||
text,
|
||||
style: TextStyle(color: color, fontSize: 12),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user