43c99fc4b6
- 原创应用标志「环序/Cycle-Step」:tools/gen-icon.py 重写 + 工具栏 Logo 同源,小尺寸简化版 - 条形时序视图:标题/副标题精简、双刻度值置于分区与刻度线之间、时间默认内嵌、依赖线箭头收窄、同泳道箭头接色块外侧、图注自动避让 - 圆周时序视图:去掉色块描边、环宽与间隙优化、度数外侧标时间、方向箭头外移;固定顺时针(移除逆时针) - 左侧栏:编辑工位/编辑相位分区入口;双击画布色块或动作行进入编辑;至少保留一个动作/泳道 - 右侧栏:移除重复的编辑操作、导出与单位卡片;文件浏览器新增「桌面」快捷入口 - AI 对话框:字号优化并新增「编辑示例」;节拍统计移除无依据的预估产能 - 文档:README 与 docs/HELP.md 全面更新;dist 同步重建
305 lines
12 KiB
Dart
305 lines
12 KiB
Dart
/// AI 生成时序图对话框。
|
|
///
|
|
/// 交互流程:填写 DeepSeek API Key → 选择模型(Flash / Pro)与推理强度
|
|
/// (高 / 最高)→ 在较大的自然语言输入框中描述工艺流程 → 点击「生成」,
|
|
/// 由 [AiClient] 调用大模型并解析为 [Project],成功后载入工作台。
|
|
///
|
|
/// 安全:API Key 仅保存在内存(静态字段,便于本次会话内复用),
|
|
/// 不写入工程文件,也不会被日志输出。
|
|
library;
|
|
|
|
import 'package:flutter/material.dart';
|
|
|
|
import '../ai/ai_client.dart';
|
|
import '../model/models.dart';
|
|
import '../state/app_controller.dart';
|
|
import '../theme/tokens.dart';
|
|
import 'dialogs.dart';
|
|
|
|
class AiGenerateDialog extends StatefulWidget {
|
|
const AiGenerateDialog({super.key, required this.controller});
|
|
|
|
/// 全局应用控制器。
|
|
final AppController controller;
|
|
|
|
@override
|
|
State<AiGenerateDialog> createState() => _AiGenerateDialogState();
|
|
}
|
|
|
|
class _AiGenerateDialogState extends State<AiGenerateDialog> {
|
|
/// 会话内复用的 API Key(不持久化)。
|
|
static String _sessionKey = '';
|
|
|
|
/// 示例描述:与输入框灰色占位一致,可一键填入后直接编辑。
|
|
static const String _examplePrompt =
|
|
'一条装配线,循环 2.4 秒。上料后定位夹紧,夹紧完成再伺服下压 0.6 秒;'
|
|
'下压完成后并行进行超声波焊接与视觉检测;检测完成分选下料,最后清洁吹气。'
|
|
'共 3 个工位,请给出动作、相位与依赖关系。';
|
|
|
|
late final TextEditingController _key;
|
|
late final TextEditingController _prompt;
|
|
|
|
String _model = kAiModels.first.id;
|
|
String _effort = 'high';
|
|
bool _obscure = true;
|
|
bool _busy = false;
|
|
String? _error;
|
|
|
|
@override
|
|
void initState() {
|
|
super.initState();
|
|
_key = TextEditingController(text: _sessionKey);
|
|
_prompt = TextEditingController();
|
|
}
|
|
|
|
@override
|
|
void dispose() {
|
|
_key.dispose();
|
|
_prompt.dispose();
|
|
super.dispose();
|
|
}
|
|
|
|
Future<void> _generate() async {
|
|
setState(() {
|
|
_busy = true;
|
|
_error = null;
|
|
});
|
|
try {
|
|
_sessionKey = _key.text.trim();
|
|
final Project p = await AiClient.generate(
|
|
prompt: _prompt.text,
|
|
settings: AiSettings(apiKey: _sessionKey, model: _model, effort: _effort),
|
|
);
|
|
widget.controller.loadProject(p, dirty: true);
|
|
widget.controller.setStatus('AI 已生成时序图(${p.actions.length} 个动作)');
|
|
if (mounted) Navigator.pop(context);
|
|
} catch (e) {
|
|
if (mounted) setState(() => _error = '$e');
|
|
} finally {
|
|
if (mounted) setState(() => _busy = false);
|
|
}
|
|
}
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
final Tokens t = context.tok;
|
|
return Dialog(
|
|
backgroundColor: t.dialog,
|
|
shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(14)),
|
|
child: scaledDialogBox(
|
|
context,
|
|
880,
|
|
620,
|
|
Padding(
|
|
padding: const EdgeInsets.all(18),
|
|
child: Column(
|
|
crossAxisAlignment: CrossAxisAlignment.start,
|
|
children: <Widget>[
|
|
// 标题
|
|
Row(
|
|
children: <Widget>[
|
|
Icon(Icons.auto_awesome, size: 21, color: t.accent2),
|
|
const SizedBox(width: 8),
|
|
Text('AI 生成时序图', style: TextStyle(color: t.text, fontSize: 17, fontWeight: FontWeight.w700)),
|
|
const Spacer(),
|
|
Text('自然语言 → 时序图', style: TextStyle(color: t.dim, fontSize: 11.5)),
|
|
],
|
|
),
|
|
const Divider(height: 22),
|
|
|
|
// API Key
|
|
Text('DeepSeek API Key', style: TextStyle(color: t.sub, fontSize: 12.5)),
|
|
const SizedBox(height: 6),
|
|
TextField(
|
|
controller: _key,
|
|
obscureText: _obscure,
|
|
style: TextStyle(color: t.text, fontSize: 12.5),
|
|
decoration: InputDecoration(
|
|
isDense: true,
|
|
hintText: 'sk-...',
|
|
hintStyle: TextStyle(color: t.dim, fontSize: 12.5),
|
|
filled: true,
|
|
fillColor: t.panel2,
|
|
prefixIcon: Icon(Icons.key, size: 16, color: t.dim),
|
|
suffixIcon: IconButton(
|
|
onPressed: () => setState(() => _obscure = !_obscure),
|
|
icon: Icon(_obscure ? Icons.visibility_off : Icons.visibility, size: 16, color: t.dim),
|
|
tooltip: _obscure ? '显示' : '隐藏',
|
|
),
|
|
border: OutlineInputBorder(borderRadius: BorderRadius.circular(8)),
|
|
),
|
|
),
|
|
const SizedBox(height: 14),
|
|
|
|
// 模型 + 推理强度
|
|
Row(
|
|
crossAxisAlignment: CrossAxisAlignment.start,
|
|
children: <Widget>[
|
|
Expanded(
|
|
child: Column(
|
|
crossAxisAlignment: CrossAxisAlignment.start,
|
|
children: <Widget>[
|
|
Text('模型', style: TextStyle(color: t.sub, fontSize: 12.5)),
|
|
const SizedBox(height: 6),
|
|
Container(
|
|
height: 38,
|
|
padding: const EdgeInsets.symmetric(horizontal: 10),
|
|
decoration: BoxDecoration(
|
|
color: t.panel2,
|
|
borderRadius: BorderRadius.circular(8),
|
|
border: Border.all(color: t.line2),
|
|
),
|
|
child: DropdownButton<String>(
|
|
value: _model,
|
|
isExpanded: true,
|
|
underline: const SizedBox.shrink(),
|
|
dropdownColor: t.dialog,
|
|
style: TextStyle(color: t.text, fontSize: 12.5),
|
|
icon: Icon(Icons.keyboard_arrow_down, size: 18, color: t.sub),
|
|
items: kAiModels
|
|
.map((AiModelOption m) =>
|
|
DropdownMenuItem<String>(value: m.id, child: Text(m.label)))
|
|
.toList(),
|
|
onChanged: (String? v) {
|
|
if (v != null) setState(() => _model = v);
|
|
},
|
|
),
|
|
),
|
|
],
|
|
),
|
|
),
|
|
const SizedBox(width: 12),
|
|
Expanded(
|
|
child: Column(
|
|
crossAxisAlignment: CrossAxisAlignment.start,
|
|
children: <Widget>[
|
|
Text('推理强度', style: TextStyle(color: t.sub, fontSize: 12.5)),
|
|
const SizedBox(height: 6),
|
|
Row(
|
|
children: <Widget>[
|
|
Expanded(child: _effortChip(t, '高', 'high')),
|
|
const SizedBox(width: 8),
|
|
Expanded(child: _effortChip(t, '最高', 'max')),
|
|
],
|
|
),
|
|
],
|
|
),
|
|
),
|
|
],
|
|
),
|
|
const SizedBox(height: 14),
|
|
|
|
// 自然语言输入(大输入框)
|
|
Row(
|
|
children: <Widget>[
|
|
Text('用自然语言描述工艺流程', style: TextStyle(color: t.sub, fontSize: 12.5)),
|
|
const Spacer(),
|
|
// 一键把灰色示例填入输入框,便于直接修改后生成。
|
|
TextButton.icon(
|
|
onPressed: _busy
|
|
? null
|
|
: () => setState(() => _prompt.text = _examplePrompt),
|
|
icon: const Icon(Icons.edit_note, size: 17),
|
|
label: const Text('编辑示例', style: TextStyle(fontSize: 12)),
|
|
style: TextButton.styleFrom(
|
|
foregroundColor: t.accent,
|
|
padding: const EdgeInsets.symmetric(horizontal: 8),
|
|
minimumSize: const Size(0, 28),
|
|
),
|
|
),
|
|
],
|
|
),
|
|
const SizedBox(height: 6),
|
|
Expanded(
|
|
child: Container(
|
|
decoration: BoxDecoration(
|
|
color: t.panel2,
|
|
borderRadius: BorderRadius.circular(10),
|
|
border: Border.all(color: t.line2),
|
|
),
|
|
padding: const EdgeInsets.symmetric(horizontal: 12, vertical: 10),
|
|
child: TextField(
|
|
controller: _prompt,
|
|
maxLines: null,
|
|
expands: true,
|
|
textAlignVertical: TextAlignVertical.top,
|
|
style: TextStyle(color: t.text, fontSize: 12.5, height: 1.6),
|
|
decoration: InputDecoration.collapsed(
|
|
hintText: '例如:$_examplePrompt',
|
|
hintStyle: TextStyle(color: t.dim, fontSize: 12.5, height: 1.6),
|
|
),
|
|
),
|
|
),
|
|
),
|
|
if (_error != null) ...<Widget>[
|
|
const SizedBox(height: 10),
|
|
Container(
|
|
width: double.infinity,
|
|
padding: const EdgeInsets.all(10),
|
|
decoration: BoxDecoration(
|
|
color: t.danger.withValues(alpha: 0.12),
|
|
borderRadius: BorderRadius.circular(8),
|
|
border: Border.all(color: t.danger.withValues(alpha: 0.5)),
|
|
),
|
|
child: Text(_error!, style: TextStyle(color: t.danger, fontSize: 12, height: 1.5)),
|
|
),
|
|
],
|
|
const SizedBox(height: 14),
|
|
|
|
// 操作按钮
|
|
Row(
|
|
children: <Widget>[
|
|
Expanded(
|
|
child: Text(
|
|
'API Key 仅保存在本机内存,不写入工程文件。',
|
|
style: TextStyle(color: t.dim, fontSize: 11),
|
|
),
|
|
),
|
|
TextButton(onPressed: _busy ? null : () => Navigator.pop(context), child: const Text('取消')),
|
|
const SizedBox(width: 8),
|
|
FilledButton.icon(
|
|
onPressed: _busy ? null : _generate,
|
|
icon: _busy
|
|
? const SizedBox(
|
|
width: 15,
|
|
height: 15,
|
|
child: CircularProgressIndicator(strokeWidth: 2),
|
|
)
|
|
: const Icon(Icons.auto_awesome, size: 16),
|
|
label: Text(_busy ? '生成中…' : '生成'),
|
|
style: FilledButton.styleFrom(
|
|
backgroundColor: t.accent2,
|
|
foregroundColor: Colors.white,
|
|
shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(9)),
|
|
padding: const EdgeInsets.symmetric(horizontal: 18, vertical: 12),
|
|
),
|
|
),
|
|
],
|
|
),
|
|
],
|
|
),
|
|
),
|
|
),
|
|
);
|
|
}
|
|
|
|
/// 推理强度分段按钮。
|
|
Widget _effortChip(Tokens t, String label, String value) {
|
|
final bool active = _effort == value;
|
|
return InkWell(
|
|
onTap: () => setState(() => _effort = value),
|
|
borderRadius: BorderRadius.circular(8),
|
|
child: Container(
|
|
height: 38,
|
|
alignment: Alignment.center,
|
|
decoration: BoxDecoration(
|
|
color: active ? t.accent.withValues(alpha: 0.18) : t.panel2,
|
|
borderRadius: BorderRadius.circular(8),
|
|
border: Border.all(color: active ? t.accent : t.line2),
|
|
),
|
|
child: Text(label, style: TextStyle(color: active ? t.accent : t.sub, fontSize: 12.5)),
|
|
),
|
|
);
|
|
}
|
|
}
|