Files
EasyDesignCollection/lib/ui/arch_panel.dart
T

1566 lines
55 KiB
Dart
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
/// 架构图模块侧栏内容:
/// - 左侧(工具):手绘识别、绘图辅助(F8 / F10)、网格吸附与粒度、
/// 参数化草图、连线走线方式、图形库、颜色、示例场景、操作提示;
/// - 右侧(属性):选中对象(支持多选)信息与操作。
///
/// 外层容器(宽度 / 边框 / 标题栏)由 `app_shell.dart` 负责,本文件只提供主体。
library;
import 'dart:convert';
import 'dart:io' show File;
import 'dart:math' as math;
import 'dart:typed_data';
import 'dart:ui' show PathMetric, Tangent;
import 'package:flutter/material.dart';
import '../arch/arch_models.dart';
import '../arch/arch_painter.dart';
import '../library/symbol_library.dart';
import '../persistence/android_docs.dart';
import '../sketch/sketch_models.dart';
import '../sketch/sketch_solver.dart';
import '../state/app_controller.dart';
import '../theme/tokens.dart';
import 'dialogs.dart';
/// 尺寸改值对话框(识别后图形 / 曲线 / 图元;右栏与画布标签共用逻辑)。
Future<void> _editShapeDim(
BuildContext context,
AppController c,
ArchShape s,
ArchDimKind k,
) async {
final double cur = archShapeDimValue(s, k);
final String? input = await showTextEditDialog(
context,
title: '编辑${archDimKindLabel(k)}(当前 ${cur.toStringAsFixed(1)} mm)',
initial: cur.toStringAsFixed(1),
);
if (input == null) return;
final double? v = double.tryParse(input.trim());
if (v == null || v <= 0) {
c.setStatus('数值无效:请输入正数');
return;
}
c.setArchShapeDim(s.id, k, v);
}
/// 图元预览画笔(100×100 归一化盒 → 控件尺寸;跳过文字保证小尺寸清晰)。
class _SymPreviewPainter extends CustomPainter {
/// 构造。
_SymPreviewPainter(this.symbol, this.color);
final LibSymbol symbol;
final Color color;
/// 绘制。
@override
void paint(Canvas canvas, Size size) {
if (size.width <= 0 || size.height <= 0) return;
canvas.save();
canvas.scale(size.width / 100, size.height / 100);
const Rect box = Rect.fromLTWH(0, 0, 100, 100);
final Paint stroke = Paint()
..style = PaintingStyle.stroke
..strokeWidth = 100 / size.width * 0.9
..strokeCap = StrokeCap.round
..strokeJoin = StrokeJoin.round
..color = color;
for (final SymPrim p in symbol.prims) {
if (p.kind == SymPrimKind.text) continue;
final Path? path = symPrimPath(p, box);
if (path == null) continue;
canvas.drawPath(path, stroke);
}
canvas.restore();
}
/// 是否需要重绘。
@override
bool shouldRepaint(covariant _SymPreviewPainter old) =>
old.symbol.id != symbol.id || old.color != color;
}
/// 小节标题。
Widget _sec(Tokens t, String s) => Text(
s,
maxLines: 1,
overflow: TextOverflow.ellipsis,
style: TextStyle(
color: t.text2,
fontSize: 11.5,
letterSpacing: 1.2,
fontWeight: FontWeight.w600,
),
);
/// 架构图左侧栏主体。
class ArchLeftBody extends StatelessWidget {
/// 构造左侧栏主体。
const ArchLeftBody({super.key, required this.controller});
/// 全局应用控制器。
final AppController controller;
/// 构建:手绘模式 → 网格 → 连线 → 图形库 → 颜色 → 示例 → 提示。
@override
Widget build(BuildContext context) {
final Tokens t = context.tok;
final AppController c = controller;
final ArchDoc doc = c.arch;
return Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
_sec(t, '手绘识别'),
const SizedBox(height: 8),
Text(
'笔迹一律扶正为标准图形(矩形 / 圆 / 箭头 / 折线 / 曲线):支持两笔组合、'
'圆柱拼接与图元库匹配(库里有什么就能画什么);识别不了的保留为墨迹,'
'可用「橡皮」擦除。文字请双击图形 / 空白处直接输入。',
style: TextStyle(color: t.dim, fontSize: 10, height: 1.5),
),
const SizedBox(height: 6),
Row(
children: <Widget>[
Expanded(
child: Text('松手自动识别',
style: TextStyle(color: t.sub, fontSize: 11)),
),
Switch(
value: c.archAutoRecognize,
onChanged: c.setArchAutoRecognize,
materialTapTargetSize: MaterialTapTargetSize.shrinkWrap,
),
],
),
if (!c.archAutoRecognize)
Text(
'已关闭:笔迹先保留为墨迹(${c.arch.strokes.length} 笔待识别),'
'点顶部「识别」按钮统一确认(平板手写推荐)。',
style: TextStyle(color: t.dim, fontSize: 10, height: 1.5),
),
const SizedBox(height: 14),
_sec(t, '绘图辅助(F8 / F10)'),
const SizedBox(height: 8),
Container(
padding: const EdgeInsets.fromLTRB(10, 6, 6, 8),
decoration: BoxDecoration(
color: t.info,
borderRadius: BorderRadius.circular(10),
border: Border.all(color: t.line),
),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Row(
children: <Widget>[
Icon(Icons.straighten, size: 15, color: t.accent),
const SizedBox(width: 6),
Expanded(
child: Text('正交移动(F8)',
style: TextStyle(
color: t.text,
fontSize: 12,
fontWeight: FontWeight.w600,
)),
),
Switch(
value: c.archOrtho,
onChanged: c.setArchOrtho,
materialTapTargetSize: MaterialTapTargetSize.shrinkWrap,
),
],
),
Row(
children: <Widget>[
Icon(Icons.polyline_outlined, size: 15, color: t.accent),
const SizedBox(width: 6),
Expanded(
child: Text('极轴追踪(F10)',
style: TextStyle(
color: t.text,
fontSize: 12,
fontWeight: FontWeight.w600,
)),
),
Switch(
value: c.archPolar,
onChanged: c.setArchPolar,
materialTapTargetSize: MaterialTapTargetSize.shrinkWrap,
),
],
),
if (c.archPolar)
Padding(
padding: const EdgeInsets.only(bottom: 6),
child: Wrap(
spacing: 6,
runSpacing: 6,
crossAxisAlignment: WrapCrossAlignment.center,
children: <Widget>[
Text('极轴角度', style: TextStyle(color: t.sub, fontSize: 10.5)),
for (final double a in <double>[5, 10, 15, 22.5, 30, 45])
_chip(
t,
'${a == a.roundToDouble() ? a.round() : a}°',
c.archPolarDeg == a,
() => c.setArchPolarDeg(a),
),
],
),
),
Row(
children: <Widget>[
Icon(Icons.square_foot, size: 15, color: t.dim),
const SizedBox(width: 6),
Expanded(
child: Text('四边标尺',
style: TextStyle(
color: t.text,
fontSize: 12,
fontWeight: FontWeight.w600,
)),
),
Switch(
value: c.archRulers,
onChanged: c.setArchRulers,
materialTapTargetSize: MaterialTapTargetSize.shrinkWrap,
),
],
),
Text('正交:移动只走水平 / 垂直;极轴:移动方向吸附到角度倍数;'
'标尺上的滑动虚线指示鼠标位置。',
style: TextStyle(color: t.dim, fontSize: 10, height: 1.5)),
],
),
),
const SizedBox(height: 14),
_sec(t, '网格'),
const SizedBox(height: 8),
Container(
padding: const EdgeInsets.fromLTRB(10, 6, 6, 8),
decoration: BoxDecoration(
color: t.info,
borderRadius: BorderRadius.circular(10),
border: Border.all(color: t.line),
),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Row(
children: <Widget>[
Icon(Icons.grid_4x4, size: 15, color: t.accent),
const SizedBox(width: 6),
Expanded(
child: Text('吸附网格',
style: TextStyle(
color: t.text,
fontSize: 12,
fontWeight: FontWeight.w600,
)),
),
Switch(
value: doc.gridSnap,
onChanged: (bool v) => c.setArchGrid(snap: v),
materialTapTargetSize: MaterialTapTargetSize.shrinkWrap,
),
],
),
Row(
children: <Widget>[
Text('粒度',
style: TextStyle(color: t.sub, fontSize: 10.5)),
const SizedBox(width: 8),
for (final double g in <double>[10, 20, 40, 80])
Padding(
padding: const EdgeInsets.only(right: 6),
child: _chip(
t,
'${g.round()}',
doc.gridSize == g,
() => c.setArchGrid(size: g),
),
),
],
),
Text('识别、移动与缩放时会优先对位到网格。',
style: TextStyle(color: t.dim, fontSize: 10, height: 1.5)),
],
),
),
const SizedBox(height: 14),
_sec(t, '连线'),
const SizedBox(height: 6),
Text('画路径即出连线,一律横平竖直(正交);端点自动吸附图元边框,'
'选中后拖拽路径上的角点可调整走线位置。',
style: TextStyle(color: t.dim, fontSize: 10, height: 1.5)),
const SizedBox(height: 14),
_sec(t, '图形库'),
const SizedBox(height: 8),
Wrap(
spacing: 6,
runSpacing: 6,
children: <Widget>[
_libBtn(t, Icons.crop_square, '矩形', ArchShapeType.rect),
_libBtn(t, Icons.rounded_corner, '圆角矩形', ArchShapeType.roundRect),
_libBtn(t, Icons.panorama_fish_eye, '椭圆', ArchShapeType.ellipse),
_libBtn(t, Icons.circle_outlined, '圆形', ArchShapeType.circle),
_libBtn(t, Icons.change_history, '三角形', ArchShapeType.triangle),
_libBtn(t, Icons.diamond_outlined, '菱形', ArchShapeType.diamond),
_libBtn(t, Icons.hexagon_outlined, '六边形', ArchShapeType.hexagon),
_libBtn(t, Icons.storage, '圆柱', ArchShapeType.cylinder),
_libBtn(t, Icons.cloud_outlined, '云', ArchShapeType.cloud),
_libBtn(t, Icons.text_fields, '文本', ArchShapeType.text),
],
),
const SizedBox(height: 10),
Text('IT / 系统设计',
style: TextStyle(color: t.dim, fontSize: 10)),
const SizedBox(height: 6),
Wrap(
spacing: 6,
runSpacing: 6,
children: <Widget>[
_libBtn(t, Icons.dns_outlined, '服务器', ArchShapeType.server),
_libBtn(t, Icons.desktop_windows_outlined, '客户端',
ArchShapeType.client),
_libBtn(t, Icons.smartphone, '移动端', ArchShapeType.mobile),
_libBtn(t, Icons.security, '防火墙', ArchShapeType.firewall),
_libBtn(t, Icons.lan_outlined, '交换机', ArchShapeType.netSwitch),
_libBtn(t, Icons.view_stream_outlined, '消息队列',
ArchShapeType.queue),
],
),
const SizedBox(height: 10),
_SymbolLibPanel(controller: c),
const SizedBox(height: 14),
_sec(t, '颜色'),
const SizedBox(height: 8),
Wrap(
spacing: 8,
runSpacing: 8,
children: <Widget>[
for (final int col in kArchPalette) _colorDot(t, c, col),
],
),
Text('影响新绘制的图形;选中图形后点色块可直接换色(支持多选)。',
style: TextStyle(color: t.dim, fontSize: 10, height: 1.5)),
const SizedBox(height: 14),
SizedBox(
width: double.infinity,
child: TextButton.icon(
onPressed: c.archClear,
icon: const Icon(Icons.delete_sweep_outlined, size: 15),
label: const Text('清空画布'),
style: TextButton.styleFrom(
foregroundColor: t.danger,
visualDensity: VisualDensity.compact,
textStyle: const TextStyle(
fontSize: 11.5,
fontFamily: 'Microsoft YaHei UI',
fontFamilyFallback: <String>[
'Microsoft YaHei',
'Segoe UI',
'sans-serif',
],
),
),
),
),
const SizedBox(height: 14),
Container(
padding: const EdgeInsets.all(10),
decoration: BoxDecoration(
color: t.info,
borderRadius: BorderRadius.circular(10),
border: Border.all(color: t.line),
),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Text('操作提示',
style: TextStyle(
color: t.warn, fontSize: 11, fontWeight: FontWeight.w700)),
const SizedBox(height: 6),
Text('手绘:按住拖动 → 松手自动识别',
style: TextStyle(color: t.sub, fontSize: 10, height: 1.6)),
Text('空白处左键拖拽 = 框选;中键 / 右键拖拽 = 平移',
style: TextStyle(color: t.sub, fontSize: 10, height: 1.6)),
Text('触屏:单指平移 · 长按框选 · 双指捏合缩放',
style: TextStyle(color: t.sub, fontSize: 10, height: 1.6)),
Text('双击图形 / 连线编辑文字 · Del 删除选中 · Ctrl+A 全选',
style: TextStyle(color: t.sub, fontSize: 10, height: 1.6)),
],
),
),
],
);
}
/// 粒度小圆片。
Widget _chip(Tokens t, String label, bool active, VoidCallback onTap) {
return InkWell(
onTap: onTap,
borderRadius: BorderRadius.circular(7),
child: Container(
padding: const EdgeInsets.symmetric(horizontal: 9, vertical: 4),
decoration: BoxDecoration(
color: active ? t.accent.withValues(alpha: 0.16) : t.panel2,
borderRadius: BorderRadius.circular(7),
border: Border.all(color: active ? t.accent : t.line),
),
child: Text(
label,
style: TextStyle(
color: active ? t.accent : t.sub,
fontSize: 10.5,
fontWeight: active ? FontWeight.w700 : FontWeight.w500,
),
),
),
);
}
/// 图形库按钮:点击后在视口中心插入一个标准图形。
Widget _libBtn(Tokens t, IconData icon, String label, ArchShapeType type) {
return InkWell(
onTap: () => _insert(type),
borderRadius: BorderRadius.circular(9),
child: Container(
width: 82,
height: 56,
decoration: BoxDecoration(
color: t.panel2,
borderRadius: BorderRadius.circular(9),
border: Border.all(color: t.line),
),
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Icon(icon, size: 18, color: t.sub),
const SizedBox(height: 3),
Text(label, style: TextStyle(color: t.sub, fontSize: 10.5)),
],
),
),
);
}
/// 在视口中心插入标准图形(位置按网格吸附)。
void _insert(ArchShapeType type) {
final AppController c = controller;
final ArchDoc doc = c.arch;
final Size vp = c.archViewport;
final Size d = archDefaultSize(type);
final Offset center = vp.width < 10 || vp.height < 10
? const Offset(400, 300)
: Offset(
(vp.width / 2 - doc.panX) / doc.zoom,
(vp.height / 2 - doc.panY) / doc.zoom,
);
Rect r = Rect.fromCenter(center: center, width: d.width, height: d.height);
if (doc.gridSnap && doc.gridSize > 1) {
final Offset tl = archSnapPoint(doc, r.topLeft);
r = Rect.fromLTWH(tl.dx, tl.dy, r.width, r.height);
}
final int color = c.archColor;
final ArchShape s = ArchShape(
id: archNewId('S', doc.allIds),
type: type,
rect: r,
radius: type == ArchShapeType.roundRect ? 16 : 0,
fill: type == ArchShapeType.text ? 0 : archFillOf(color),
stroke: color,
sw: 1.8,
text: type == ArchShapeType.text ? '文本' : '',
);
c.addArchShape(s);
c.selectArch(s.id);
}
/// 颜色圆点。
Widget _colorDot(Tokens t, AppController c, int col) {
final bool active = c.archColor == col;
return InkWell(
onTap: () => c.setArchColor(col),
borderRadius: BorderRadius.circular(16),
child: Container(
width: 26,
height: 26,
decoration: BoxDecoration(
color: Color(col),
shape: BoxShape.circle,
border: Border.all(
color: active ? t.accent : t.line,
width: active ? 2.5 : 1,
),
),
),
);
}
}
/// 架构图右侧栏主体:选中对象属性(支持多选)。
class ArchRightBody extends StatelessWidget {
/// 构造右侧栏主体。
const ArchRightBody({super.key, required this.controller});
/// 全局应用控制器。
final AppController controller;
/// 构建:草图模式 → 草图面板;否则选中对象卡片 + 统计。
@override
Widget build(BuildContext context) {
final Tokens t = context.tok;
final AppController c = controller;
if (c.sketchMode) return _sketchBody(context, c);
final ArchDoc doc = c.arch;
final List<String> sel = c.archSelIds;
final ArchShape? s = sel.length == 1 ? doc.shapeById(sel.first) : null;
final ArchConnector? cn =
sel.length == 1 ? doc.connectorById(sel.first) : null;
final ArchStroke? st = sel.length == 1 ? doc.strokeById(sel.first) : null;
return Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
_sec(t, '选中对象'),
const SizedBox(height: 8),
if (sel.length > 1)
_card(t, <Widget>[
Text(
'已选中 ${sel.length} 个对象',
style: TextStyle(
color: t.text,
fontSize: 12,
fontWeight: FontWeight.w700,
),
),
Text('拖动任一选中图形可整体移动;改色作用于选中的图形。',
style: TextStyle(color: t.dim, fontSize: 10.5, height: 1.5)),
const SizedBox(height: 8),
Row(
children: <Widget>[
_mini(t, '全选', c.selectAllArch),
const SizedBox(width: 6),
_mini(t, '取消选择', () => c.selectArch(null)),
],
),
const SizedBox(height: 8),
Text('对齐 / 均布(以选中范围为参照;均布需 ≥3 个)',
style: TextStyle(color: t.dim, fontSize: 10.5)),
const SizedBox(height: 6),
Wrap(
spacing: 4,
runSpacing: 4,
children: <Widget>[
_alignBtn(t, Icons.align_horizontal_left, '左对齐', ArchAlign.left, c),
_alignBtn(t, Icons.align_horizontal_center, '横向居中', ArchAlign.hCenter, c),
_alignBtn(t, Icons.align_horizontal_right, '右对齐', ArchAlign.right, c),
_alignBtn(t, Icons.align_vertical_top, '顶对齐', ArchAlign.top, c),
_alignBtn(t, Icons.align_vertical_center, '纵向居中', ArchAlign.vCenter, c),
_alignBtn(t, Icons.align_vertical_bottom, '底对齐', ArchAlign.bottom, c),
_alignBtn(t, Icons.horizontal_distribute, '横向均布', ArchAlign.distributeH, c),
_alignBtn(t, Icons.vertical_distribute, '纵向均布', ArchAlign.distributeV, c),
],
),
const SizedBox(height: 6),
_mini(t, '删除(Del)', c.removeArchSelection, danger: true),
])
else if (s != null)
_card(t, <Widget>[
Row(
children: <Widget>[
Icon(_iconOf(s.type), size: 15, color: Color(s.stroke)),
const SizedBox(width: 6),
Expanded(
child: Text(
'${archShapeTypeLabel(s.type)} · ${s.id}',
style: TextStyle(
color: t.text,
fontSize: 12,
fontWeight: FontWeight.w700,
),
),
),
],
),
if (!s.isLinear)
Text(
'尺寸 ${s.rect.width.round()} × ${s.rect.height.round()} px',
style: TextStyle(color: t.dim, fontSize: 10.5),
),
if (s.isLinear)
Text(
'${s.type == ArchShapeType.line && s.a != null ? 2 : s.pts.length} 个折点'
'${s.arrow ? ' · 箭头' : ''}',
style: TextStyle(color: t.dim, fontSize: 10.5),
),
if (s.text.isNotEmpty)
Text(
'文字:${s.text.replaceAll('\n', ' / ')}',
maxLines: 2,
overflow: TextOverflow.ellipsis,
style: TextStyle(color: t.sub, fontSize: 11),
),
const SizedBox(height: 8),
Row(
children: <Widget>[
Text('尺寸 · mm(点数值直接修改)',
style: TextStyle(color: t.dim, fontSize: 10)),
const Spacer(),
InkWell(
onTap: () {
final List<ArchDimKind> kinds = archShapeDimKinds(s);
final bool all = kinds.every(s.dims.contains);
for (final ArchDimKind k in kinds) {
if (all == s.dims.contains(k)) {
c.toggleArchShapeDim(s.id, k);
}
}
},
borderRadius: BorderRadius.circular(6),
child: Padding(
padding: const EdgeInsets.symmetric(
horizontal: 5, vertical: 2),
child: Text(
archShapeDimKinds(s)
.every(s.dims.contains)
? '清除标注'
: '标注全部',
style: TextStyle(
color: t.accent,
fontSize: 10,
fontWeight: FontWeight.w600,
),
),
),
),
],
),
const SizedBox(height: 4),
for (final ArchDimKind k in archShapeDimKinds(s))
Padding(
padding: const EdgeInsets.only(bottom: 3),
child: Row(
children: <Widget>[
SizedBox(
width: 34,
child: Text(archDimKindLabel(k),
style: TextStyle(color: t.dim, fontSize: 11)),
),
InkWell(
onTap: () => _editShapeDim(context, c, s, k),
borderRadius: BorderRadius.circular(5),
child: Container(
padding: const EdgeInsets.symmetric(
horizontal: 6, vertical: 2),
decoration: BoxDecoration(
color: t.panel2,
borderRadius: BorderRadius.circular(5),
border: Border.all(color: t.line),
),
child: Text(
'${archShapeDimValue(s, k).toStringAsFixed(1)} mm',
style: TextStyle(
color: t.accent2,
fontSize: 11,
fontWeight: FontWeight.w600,
),
),
),
),
const Spacer(),
InkWell(
onTap: () => c.toggleArchShapeDim(s.id, k),
borderRadius: BorderRadius.circular(6),
child: Padding(
padding: const EdgeInsets.all(3),
child: Icon(
s.dims.contains(k)
? Icons.label
: Icons.label_outline,
size: 14,
color: s.dims.contains(k) ? t.accent : t.dim,
),
),
),
],
),
),
const SizedBox(height: 8),
Row(
children: <Widget>[
_mini(t, '编辑文字', () => _editText(context, c, s)),
const SizedBox(width: 6),
_mini(t, '上移一层', () => c.moveArchZ(s, 1)),
const SizedBox(width: 6),
_mini(t, '下移一层', () => c.moveArchZ(s, -1)),
],
),
const SizedBox(height: 6),
Row(
children: <Widget>[
if (s.type == ArchShapeType.polyline ||
s.type == ArchShapeType.curve) ...<Widget>[
_mini(
t,
s.type == ArchShapeType.polyline ? '平滑为曲线' : '折线化',
() => _togglePolyCurve(c, s),
),
const SizedBox(width: 6),
],
_mini(t, '删除(Del)', c.removeArchSelection, danger: true),
],
),
])
else if (cn != null)
_card(t, <Widget>[
Text(
'连线 · ${cn.id}',
style: TextStyle(
color: t.text,
fontSize: 12,
fontWeight: FontWeight.w700,
),
),
Text(
cn.label.isEmpty ? '(无标签)' : '标签:${cn.label}',
style: TextStyle(color: t.sub, fontSize: 11),
),
const SizedBox(height: 4),
Text(
'走线:一律正交(横平竖直);拖拽路径上的角点可调整走线位置。',
style: TextStyle(color: t.dim, fontSize: 10.5, height: 1.5),
),
const SizedBox(height: 6),
Row(
children: <Widget>[
_mini(t, '编辑标签', () => _editConnLabel(context, c, cn)),
const SizedBox(width: 6),
_mini(t, '删除(Del)', c.removeArchSelection, danger: true),
],
),
])
else if (st != null)
_card(t, <Widget>[
Text(
'手绘墨迹 · ${st.id}',
style: TextStyle(
color: t.text,
fontSize: 12,
fontWeight: FontWeight.w700,
),
),
Text(
'${st.pts.length} 个采样点(识别不了的笔迹保留为墨迹)',
style: TextStyle(color: t.sub, fontSize: 11),
),
const SizedBox(height: 8),
Row(
children: <Widget>[
_mini(t, '删除(Del)', c.removeArchSelection, danger: true),
],
),
])
else
_card(t, <Widget>[
Text(
'未选中任何对象',
style: TextStyle(
color: t.text,
fontSize: 12,
fontWeight: FontWeight.w600,
),
),
Text(
'点选图形 / 连线 / 墨迹查看属性;空白拖拽框选,双击图形编辑文字。',
style: TextStyle(color: t.dim, fontSize: 10.5, height: 1.5),
),
const SizedBox(height: 8),
_mini(t, '全选(Ctrl+A)', c.selectAllArch),
]),
const SizedBox(height: 14),
Row(
children: <Widget>[
_sec(t, '图层'),
const Spacer(),
InkWell(
onTap: c.addArchLayer,
borderRadius: BorderRadius.circular(7),
child: Container(
padding:
const EdgeInsets.symmetric(horizontal: 8, vertical: 3),
decoration: BoxDecoration(
color: t.panel2,
borderRadius: BorderRadius.circular(7),
border: Border.all(color: t.line),
),
child: Row(
mainAxisSize: MainAxisSize.min,
children: <Widget>[
Icon(Icons.add, size: 13, color: t.sub),
const SizedBox(width: 3),
Text('新建', style: TextStyle(color: t.sub, fontSize: 10.5)),
],
),
),
),
],
),
const SizedBox(height: 8),
for (final ArchLayer ly in doc.layers) _layerRow(context, t, c, ly),
const SizedBox(height: 14),
_sec(t, '统计'),
const SizedBox(height: 6),
Text(
'元件 ${doc.shapes.length} · 连线 ${doc.connectors.length} · 墨迹 ${doc.strokes.length}',
style: TextStyle(color: t.sub, fontSize: 11),
),
const SizedBox(height: 4),
Text(
'架构图保存为独立文件(*.arch.json),不与时序图互通。',
style: TextStyle(color: t.dim, fontSize: 10, height: 1.5),
),
],
);
}
/// 弹窗编辑图形文字(画布内也可双击图形直接编辑)。
Future<void> _editText(
BuildContext context,
AppController c,
ArchShape s,
) async {
final String? r =
await showTextEditDialog(context, title: '编辑图形文字', initial: s.text);
if (r != null && r != s.text) {
c.updateArchShape(s.copyWith(text: r), status: '编辑文字');
}
}
/// 弹窗编辑连线标签(画布内也可双击连线直接编辑)。
Future<void> _editConnLabel(
BuildContext context,
AppController c,
ArchConnector cn,
) async {
final String? r =
await showTextEditDialog(context, title: '编辑连线标签', initial: cn.label);
if (r != null && r != cn.label) {
c.updateArchConnector(cn.copyWith(label: r), status: '编辑连线标签');
}
}
/// 折线 ⇄ 曲线互转(同一组通过点)。
void _togglePolyCurve(AppController c, ArchShape s) {
if (s.type == ArchShapeType.polyline) {
c.updateArchShape(s.copyWith(type: ArchShapeType.curve),
status: '已平滑为曲线');
} else if (s.type == ArchShapeType.curve) {
c.updateArchShape(s.copyWith(type: ArchShapeType.polyline),
status: '已折线化');
}
}
/// 图层条目:可见 / 锁定 / 颜色 / 名称(单击设为活动、双击改名)/ 删除。
Widget _layerRow(
BuildContext context,
Tokens t,
AppController c,
ArchLayer ly,
) {
final ArchDoc doc = c.arch;
final bool active = doc.activeLayer == ly.id;
return Container(
margin: const EdgeInsets.only(bottom: 4),
padding: const EdgeInsets.symmetric(horizontal: 6, vertical: 2),
decoration: BoxDecoration(
color: active ? t.accent.withValues(alpha: 0.12) : t.panel2,
borderRadius: BorderRadius.circular(8),
border: Border.all(color: active ? t.accent : t.line),
),
child: Row(
children: <Widget>[
_layerMiniIcon(
t,
ly.visible ? Icons.visibility_outlined : Icons.visibility_off_outlined,
ly.visible ? t.sub : t.dim,
() => c.toggleArchLayerVisible(ly.id),
ly.visible ? '隐藏图层' : '显示图层',
),
_layerMiniIcon(
t,
ly.locked ? Icons.lock_outline : Icons.lock_open_outlined,
ly.locked ? t.warn : t.sub,
() => c.toggleArchLayerLocked(ly.id),
ly.locked ? '解锁图层' : '锁定图层',
),
InkWell(
onTap: () => _pickLayerColor(context, c, ly),
borderRadius: BorderRadius.circular(8),
child: Padding(
padding: const EdgeInsets.all(3),
child: Container(
width: 15,
height: 15,
decoration: BoxDecoration(
color: Color(ly.color),
shape: BoxShape.circle,
border: Border.all(color: t.line),
),
),
),
),
const SizedBox(width: 4),
Expanded(
child: GestureDetector(
onTap: () => c.setArchActiveLayer(ly.id),
onDoubleTap: () => _renameLayer(context, c, ly),
child: Text(
ly.name,
maxLines: 1,
overflow: TextOverflow.ellipsis,
style: TextStyle(
color: active ? t.accent : t.text,
fontSize: 11.5,
fontWeight: active ? FontWeight.w700 : FontWeight.w500,
),
),
),
),
if (doc.layers.length > 1)
_layerMiniIcon(
t,
Icons.close,
t.dim,
() => c.removeArchLayer(ly.id),
'删除图层(对象移入首层)',
),
],
),
);
}
/// 图层行内小图标按钮。
Widget _layerMiniIcon(
Tokens t,
IconData icon,
Color color,
VoidCallback onTap,
String tip,
) {
return Tooltip(
message: tip,
child: InkWell(
onTap: onTap,
borderRadius: BorderRadius.circular(6),
child: Padding(
padding: const EdgeInsets.all(4),
child: Icon(icon, size: 14, color: color),
),
),
);
}
/// 重命名图层(弹窗)。
Future<void> _renameLayer(
BuildContext context,
AppController c,
ArchLayer ly,
) async {
final String? r =
await showTextEditDialog(context, title: '重命名图层', initial: ly.name);
if (r != null && r.trim().isNotEmpty && r != ly.name) {
c.updateArchLayer(ly.copyWith(name: r.trim()), status: '图层已重命名');
}
}
/// 选择图层颜色(调色板弹窗)。
Future<void> _pickLayerColor(
BuildContext context,
AppController c,
ArchLayer ly,
) async {
final int? picked = await showDialog<int>(
context: context,
builder: (BuildContext ctx) {
final Tokens tt = ctx.tok;
return AlertDialog(
backgroundColor: tt.dialog,
title: Text('图层颜色',
style: TextStyle(
color: tt.text, fontSize: 15, fontWeight: FontWeight.w700)),
content: Wrap(
spacing: 10,
runSpacing: 10,
children: <Widget>[
for (final int col in kArchPalette)
InkWell(
onTap: () => Navigator.of(ctx).pop(col),
borderRadius: BorderRadius.circular(18),
child: Container(
width: 30,
height: 30,
decoration: BoxDecoration(
color: Color(col),
shape: BoxShape.circle,
border: Border.all(
color: col == ly.color ? tt.accent : tt.line,
width: col == ly.color ? 2.5 : 1,
),
),
),
),
],
),
actions: <Widget>[
TextButton(
onPressed: () => Navigator.of(ctx).pop(),
child: const Text('取消')),
],
);
},
);
if (picked != null) {
c.updateArchLayer(ly.copyWith(color: picked), status: '图层颜色已更新');
}
}
/// 卡片容器。
/// 草图模式的右侧栏:选中对象 / 统计 / 提示。
Widget _sketchBody(BuildContext context, AppController c) {
final Tokens t = context.tok;
final SketchDoc d = c.sketch;
final List<String> sel = c.sketchSelIds;
final int consCount =
d.constraints.where((SketchConstraint k) => !k.internal).length;
return Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
_sec(t, '草图 · 选中对象'),
const SizedBox(height: 8),
_card(t, <Widget>[
Text(
sel.isEmpty ? '未选中任何草图对象' : '已选中 ${sel.length} 个对象',
style: TextStyle(
color: t.text,
fontSize: 12,
fontWeight: FontWeight.w700,
),
),
Text(
'点 / 实体 / 尺寸都可选中;拖动点或图形会按约束实时求解,'
'双击尺寸标签可直接改值。',
style: TextStyle(color: t.dim, fontSize: 10.5, height: 1.5),
),
const SizedBox(height: 8),
Row(
children: <Widget>[
_mini(t, '全选', c.selectAllSketch),
const SizedBox(width: 6),
_mini(t, '取消选择', () => c.selectSketchIds(const <String>[])),
const SizedBox(width: 6),
_mini(t, '删除(Del)', c.deleteSketchSelection, danger: true),
],
),
]),
const SizedBox(height: 14),
_sec(t, '草图统计'),
const SizedBox(height: 8),
_card(t, <Widget>[
Text('点 ${d.points.length} · 实体 ${d.entities.length}',
style: TextStyle(color: t.sub, fontSize: 11.5)),
Text('约束 $consCount · 尺寸 ${d.dims.length}',
style: TextStyle(color: t.sub, fontSize: 11.5)),
Text('剩余自由度 ${sketchDofCount(d)}',
style: TextStyle(color: t.sub, fontSize: 11.5)),
const SizedBox(height: 4),
Text(
sketchDofCount(d) == 0
? '已全约束:几何被尺寸 / 约束完全驱动。'
: '欠约束:可继续添加约束 / 尺寸,或直接拖动试解。',
style: TextStyle(color: t.dim, fontSize: 10.5, height: 1.5),
),
]),
],
);
}
/// 属性卡片容器。
Widget _card(Tokens t, List<Widget> children) {
return Container(
width: double.infinity,
padding: const EdgeInsets.all(10),
decoration: BoxDecoration(
color: t.panel2,
borderRadius: BorderRadius.circular(10),
border: Border.all(color: t.line),
),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: children,
),
);
}
/// 小按钮。
Widget _mini(
Tokens t,
String label,
VoidCallback onTap, {
bool danger = false,
bool active = false,
}) {
return InkWell(
onTap: onTap,
borderRadius: BorderRadius.circular(8),
child: Container(
padding: const EdgeInsets.symmetric(horizontal: 10, vertical: 6),
decoration: BoxDecoration(
color: danger
? t.danger.withValues(alpha: 0.12)
: (active ? t.accent.withValues(alpha: 0.16) : t.panel),
borderRadius: BorderRadius.circular(8),
border: Border.all(
color: danger
? t.danger.withValues(alpha: 0.45)
: (active ? t.accent : t.line),
),
),
child: Text(
label,
style: TextStyle(
color: danger ? t.danger : (active ? t.accent : t.sub),
fontSize: 11,
),
),
),
);
}
/// 对齐 / 均布小按钮(图标 + 悬浮提示)。
Widget _alignBtn(
Tokens t,
IconData icon,
String tip,
ArchAlign mode,
AppController c,
) {
return Tooltip(
message: tip,
child: InkWell(
onTap: () => c.alignArchSelection(mode),
borderRadius: BorderRadius.circular(7),
child: Container(
width: 30,
height: 28,
decoration: BoxDecoration(
color: t.panel,
borderRadius: BorderRadius.circular(7),
border: Border.all(color: t.line),
),
child: Icon(icon, size: 15, color: t.sub),
),
),
);
}
/// 图形类型对应的小图标。
IconData _iconOf(ArchShapeType type) {
switch (type) {
case ArchShapeType.rect:
return Icons.crop_square;
case ArchShapeType.roundRect:
return Icons.rounded_corner;
case ArchShapeType.ellipse:
return Icons.panorama_fish_eye;
case ArchShapeType.circle:
return Icons.circle_outlined;
case ArchShapeType.triangle:
return Icons.change_history;
case ArchShapeType.diamond:
return Icons.diamond_outlined;
case ArchShapeType.cylinder:
return Icons.storage;
case ArchShapeType.cloud:
return Icons.cloud_outlined;
case ArchShapeType.hexagon:
return Icons.hexagon_outlined;
case ArchShapeType.text:
return Icons.text_fields;
case ArchShapeType.line:
return Icons.trending_flat;
case ArchShapeType.polyline:
return Icons.polyline;
case ArchShapeType.curve:
return Icons.gesture;
case ArchShapeType.server:
return Icons.dns_outlined;
case ArchShapeType.client:
return Icons.desktop_windows_outlined;
case ArchShapeType.mobile:
return Icons.smartphone;
case ArchShapeType.firewall:
return Icons.security;
case ArchShapeType.netSwitch:
return Icons.lan_outlined;
case ArchShapeType.queue:
return Icons.view_stream_outlined;
case ArchShapeType.symbol:
return Icons.extension_outlined;
}
}
}
// ===================== 图元库面板(可折叠 · Visio 式) =====================
/// 图元库面板:按分类折叠展示(默认全部收起,只展开选中的库),
/// 点图元即插入画布;用户图元可删除;支持导入与「从选中图形存为图元」。
class _SymbolLibPanel extends StatefulWidget {
/// 构造。
const _SymbolLibPanel({required this.controller});
/// 全局应用控制器。
final AppController controller;
/// 创建状态。
@override
State<_SymbolLibPanel> createState() => _SymbolLibPanelState();
}
/// 面板状态:记录当前展开的分类(同时只允许一个)。
class _SymbolLibPanelState extends State<_SymbolLibPanel> {
String? _openCategory;
/// 构建。
@override
Widget build(BuildContext context) {
final Tokens t = context.tok;
final AppController c = widget.controller;
final Map<String, List<LibSymbol>> groups = <String, List<LibSymbol>>{};
for (final LibSymbol s in SymbolCache.all) {
groups.putIfAbsent(s.category, () => <LibSymbol>[]).add(s);
}
final List<String> cats = groups.keys.toList();
return Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Row(
children: <Widget>[
Text('图元库(可扩展)',
style: TextStyle(color: t.dim, fontSize: 10)),
const Spacer(),
InkWell(
onTap: () => _symImport(context, c),
borderRadius: BorderRadius.circular(6),
child: Padding(
padding:
const EdgeInsets.symmetric(horizontal: 6, vertical: 2),
child: Text('导入图元…',
style: TextStyle(
color: t.accent,
fontSize: 10.5,
fontWeight: FontWeight.w600)),
),
),
],
),
const SizedBox(height: 4),
for (final String cat in cats) ...<Widget>[
InkWell(
onTap: () => setState(() =>
_openCategory = _openCategory == cat ? null : cat),
borderRadius: BorderRadius.circular(7),
child: Container(
padding:
const EdgeInsets.symmetric(horizontal: 8, vertical: 5),
margin: const EdgeInsets.only(bottom: 3),
decoration: BoxDecoration(
color: _openCategory == cat
? t.accent.withValues(alpha: 0.10)
: t.panel2,
borderRadius: BorderRadius.circular(7),
border: Border.all(
color: _openCategory == cat
? t.accent.withValues(alpha: 0.5)
: t.line,
),
),
child: Row(
children: <Widget>[
Icon(
_openCategory == cat
? Icons.expand_less
: Icons.expand_more,
size: 15,
color: _openCategory == cat ? t.accent : t.sub,
),
const SizedBox(width: 6),
Expanded(
child: Text(
'$cat(${groups[cat]!.length})',
style: TextStyle(
color: _openCategory == cat ? t.accent : t.sub,
fontSize: 11,
fontWeight: _openCategory == cat
? FontWeight.w700
: FontWeight.w500,
),
),
),
],
),
),
),
if (_openCategory == cat)
Padding(
padding: const EdgeInsets.only(left: 6, right: 2, bottom: 4),
child: Wrap(
spacing: 6,
runSpacing: 6,
children: <Widget>[
for (final LibSymbol sym in groups[cat]!) _symCard(t, c, sym),
],
),
),
],
const SizedBox(height: 4),
SizedBox(
width: double.infinity,
child: OutlinedButton.icon(
onPressed:
c.archSelIds.isEmpty ? null : () => _symFromSelection(context, c),
icon: const Icon(Icons.add_box_outlined, size: 15),
label: const Text('从选中图形存为图元'),
style: OutlinedButton.styleFrom(
visualDensity: VisualDensity.compact,
textStyle: const TextStyle(
fontSize: 11.5,
fontFamily: 'Microsoft YaHei UI',
fontFamilyFallback: <String>[
'Microsoft YaHei',
'Segoe UI',
'sans-serif',
],
),
),
),
),
],
);
}
}
/// 图元卡片(预览 + 名称;用户图元带删除角标)。
Widget _symCard(Tokens t, AppController c, LibSymbol sym) {
final bool user = !SymbolLibrary.isBuiltinId(sym.id);
return InkWell(
onTap: () => _insertSymbolShape(c, sym),
borderRadius: BorderRadius.circular(9),
child: Container(
width: 82,
height: 64,
decoration: BoxDecoration(
color: t.panel2,
borderRadius: BorderRadius.circular(9),
border: Border.all(color: t.line),
),
child: Stack(
children: <Widget>[
Positioned.fill(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
SizedBox(
width: 34,
height: 30,
child: CustomPaint(
painter: _SymPreviewPainter(sym, t.sub),
),
),
const SizedBox(height: 2),
Padding(
padding: const EdgeInsets.symmetric(horizontal: 3),
child: Text(
sym.name,
maxLines: 1,
overflow: TextOverflow.ellipsis,
style: TextStyle(color: t.sub, fontSize: 9.5),
),
),
],
),
),
if (user)
Positioned(
right: 0,
top: 0,
child: InkWell(
onTap: () => _symDelete(c, sym),
borderRadius: BorderRadius.circular(8),
child: Padding(
padding: const EdgeInsets.all(3),
child: Icon(Icons.close, size: 12, color: t.dim),
),
),
),
],
),
),
);
}
/// 插入图元库符号(保持图元纵横比,位置在视口中心、可吸附网格)。
void _insertSymbolShape(AppController c, LibSymbol sym) {
final ArchDoc doc = c.arch;
final Size vp = c.archViewport;
final Offset center = vp.width < 10 || vp.height < 10
? const Offset(400, 300)
: Offset(
(vp.width / 2 - doc.panX) / doc.zoom,
(vp.height / 2 - doc.panY) / doc.zoom,
);
final Rect nat = symbolNaturalBounds(sym);
final double scale = 140 / math.max(nat.width, nat.height);
Rect r = Rect.fromCenter(
center: center,
width: nat.width * scale,
height: nat.height * scale,
);
if (doc.gridSnap && doc.gridSize > 1) {
final Offset tl = archSnapPoint(doc, r.topLeft);
r = Rect.fromLTWH(tl.dx, tl.dy, r.width, r.height);
}
final int color = c.archColor;
final ArchShape s = ArchShape(
id: archNewId('S', doc.allIds),
type: ArchShapeType.symbol,
symbolId: sym.id,
rect: r,
fill: archFillOf(color),
stroke: color,
sw: 1.8,
);
c.addArchShape(s);
c.selectArch(s.id);
}
/// 删除用户图元(内置图元不可删)。
Future<void> _symDelete(AppController c, LibSymbol sym) async {
await SymbolLibrary.deleteUser(sym.id);
await SymbolCache.refresh();
c.setStatus('已删除图元「${sym.name}」');
}
/// 导入图元文件(`*.symbol.json`;桌面文件浏览器 / 安卓公共目录)。
Future<void> _symImport(BuildContext context, AppController c) async {
try {
String content;
if (AndroidDocs.isAvailable) {
final String? name = await showDialog<String>(
context: context,
builder: (_) => const AndroidFileDialog(
saveMode: false,
defaultName: '',
suffix: '.symbol.json',
),
);
if (name == null) return;
final Uint8List? bytes = await AndroidDocs.readFile(name);
if (bytes == null) {
c.setStatus('导入失败:未找到文件 $name');
return;
}
content = utf8.decode(bytes);
} else {
final String? path = await pickFile(
context,
save: false,
suffix: '.symbol.json',
);
if (path == null) return;
content = await File(path).readAsString();
}
final LibSymbol sym =
LibSymbol.fromJson(jsonDecode(content) as Map<String, dynamic>);
if (sym.id.isEmpty || sym.prims.isEmpty) {
c.setStatus('导入失败:不是有效的图元文件');
return;
}
await SymbolLibrary.saveUser(sym);
await SymbolCache.refresh();
c.setStatus('已导入图元「${sym.name}」(${sym.category})');
} catch (e) {
c.setStatus('导入失败:$e');
}
}
/// 把当前选中的图形保存为图元(可多选;几何按选中范围归一化到 100×100)。
Future<void> _symFromSelection(BuildContext context, AppController c) async {
final List<ArchShape> sel = <ArchShape>[
for (final String id in c.archSelIds)
if (c.arch.shapeById(id) != null) c.arch.shapeById(id)!,
];
if (sel.isEmpty) {
c.setStatus('先选中要保存为图元的图形(可多选)');
return;
}
final String? name = await showTextEditDialog(
context,
title: '保存为图元(名称)',
initial: '自定义图元',
);
if (name == null || name.trim().isEmpty) return;
final LibSymbol sym = _symbolFromShapes(sel, name.trim());
await SymbolLibrary.saveUser(sym);
await SymbolCache.refresh();
c.setStatus('已保存图元「${sym.name}」到图元库(可点击插入 / 手绘识别)');
}
/// 由图形集合生成图元(采样轮廓为折线 + 文本)。
LibSymbol _symbolFromShapes(List<ArchShape> shapes, String name) {
Rect union = shapes.first.bounds;
for (final ArchShape s in shapes) {
union = union.expandToInclude(s.bounds);
}
final double scale = 100 / math.max(union.width, union.height);
final Offset c0 = union.center;
Offset norm(Offset p) =>
Offset((p.dx - c0.dx) * scale + 50, (p.dy - c0.dy) * scale + 50);
final List<SymPrim> prims = <SymPrim>[];
for (final ArchShape s in shapes) {
final Path path = archShapePath(s);
for (final PathMetric m in path.computeMetrics()) {
final int count =
math.max(6, (m.length * scale / 5).ceil()).clamp(6, 160);
final List<double> flat = <double>[];
for (int i = 0; i <= count; i++) {
final Tangent? t = m.getTangentForOffset(m.length * i / count);
if (t == null) continue;
final Offset n2 = norm(t.position);
flat.add(n2.dx);
flat.add(n2.dy);
}
if (flat.length >= 4) {
prims.add(SymPrim(kind: SymPrimKind.polyline, pts: flat));
}
}
if (s.text.isNotEmpty) {
final Offset n2 = norm(s.rect.center);
prims.add(SymPrim(
kind: SymPrimKind.text,
pts: <double>[n2.dx - s.text.length * 2.5, n2.dy - 5],
label: s.text,
));
}
}
return LibSymbol(
id: 'user-${DateTime.now().millisecondsSinceEpoch}',
name: name,
category: '自定义',
prims: prims,
pins: const <SymPin>[],
);
}