Files
EasyDesignCollection/lib/render/bar_painter.dart
T
呆头鹅 339b96c73f feat(V1.0.1): 泳道/轨道支持多动作(同执行器多条不重叠);圆周弧可拖拽端点;依赖标签沿弧避让
- 泳道模型:layer 兼作泳道/轨道编号,同一泳道可放多个互不重叠动作(角区间重叠校验)
- 条形:上下拖拽换道;圆周:Shift+径向拖拽换轨;冲突时拒绝并提示
- 泳道标题合并显示(/ 分隔,≤2 行);左侧列表按泳道分组
- 圆周动作弧支持拖拽两端改起止角(弱吸附 + 径向虚线参考),整体拖拽旋转
- 圆周依赖标签沿圆弧切向排布,落入环间空隙,避免压住动作弧
- 版本 1.0.1+2;帮助/README 补充泳道说明;重新构建 dist
2026-09-18 17:54:35 +08:00

576 lines
24 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.
/// 条形(时间轴)视图绘制器。
///
/// 职责:把 [Project] 中的动作按“双轴”条形图渲染到 Flutter 画布上——
/// 横轴同时表达相位角度与等价的绝对时间,纵轴按工位顺序排列动作行。
///
/// 架构位置:
/// - 全部几何计算委托给 [BarLayout](`lib/layout/layout.dart`),
/// 本绘制器只负责取几何结果并落笔,不自行推导坐标。
/// - 颜色一律取自 [Tokens],因此与圆形视图及 SVG 导出保持同一视觉体系。
/// - 关键路径信息([criticalActions]、[criticalDeps])由上层分析后传入,
/// 本类不参与计算,仅负责高亮呈现。
///
/// 绘制顺序(见 [paint]):
/// 相位带 → 时间/相位标尺 → 循环边界 → 动作行 → 依赖连线。
library;
import 'dart:math' as math;
import 'dart:ui' as ui;
import 'package:flutter/material.dart' hide Action;
import '../layout/layout.dart';
import '../model/models.dart';
import '../model/units.dart';
import '../theme/tokens.dart';
/// 条形时间轴视图的 [CustomPainter] 实现。
///
/// 坐标约定:横轴 `x` 表示 0°–360° 相位(并线性映射到 0–cycle 时间),
/// 纵轴 `y` 表示动作行。角度到像素的换算由 [BarLayout.xFor] 完成。
class BarPainter extends CustomPainter {
/// 创建条形视图绘制器。
///
/// 参数:
/// - [project]:待绘制的工程数据(动作、工位、依赖、视图开关)。
/// - [tokens]:当前主题令牌,决定所有颜色。
/// - [criticalActions]:关键路径上的动作 id 集合,用于描边高亮。
/// - [criticalDeps]:关键路径上的依赖集合,键格式 `fromId->toId`。
/// - [selectedId]:当前选中动作 id,可为空。
///
/// 无副作用;实例不缓存布局,每次 [paint] 都重新计算。
BarPainter({
required this.project,
required this.tokens,
required this.criticalActions,
required this.criticalDeps,
required this.selectedIds,
this.guideAngle,
});
/// 待绘制的工程数据源。
final Project project;
/// 当前主题令牌。
final Tokens tokens;
/// 关键路径上的动作 id 集合。
final Set<String> criticalActions;
/// 关键路径上的依赖边集合,元素格式为 `fromId->toId`。
final Set<String> criticalDeps;
/// 当前选中的动作 id 集合(支持多选)。
final Set<String> selectedIds;
/// 拖拽相位分区边界时的参考角(度);非空时绘制一条竖直虚线参考。
final double? guideAngle;
/// 标尺横线所在的 y 坐标(画布坐标)。
static const double axisTop = 138;
/// 相位色带的顶部 y 坐标。
static const double phaseBandTop = 92;
/// 绘制整张条形视图。
///
/// 参数 [canvas]:目标画布;[size]:可用尺寸(用于构建 [BarLayout])。
/// 副作用:向 [canvas] 写入若干图元,不修改任何模型状态。
/// 图层顺序遵循“先背景后前景”:相位带、标尺、边界、动作行、依赖。
@override
void paint(Canvas canvas, Size size) {
final BarLayout layout = BarLayout(project, size);
final UnitSettings u = project.units;
final double cycle = project.cycle.durationMs;
if (project.viewState.showPhaseBands) _phaseBands(canvas, layout);
if (project.viewState.showRuler) _ruler(canvas, layout, u, cycle);
_boundaries(canvas, layout);
_rows(canvas, layout, u, cycle);
if (project.viewState.showDeps) _deps(canvas, layout, u);
// 分区拖拽参考线绘制在最上层,保证清晰可见。
if (guideAngle != null) _guide(canvas, layout, u);
}
/// 绘制相位分区拖拽时的竖直虚线参考线,并在顶部标注当前角度。
void _guide(Canvas canvas, BarLayout layout, UnitSettings u) {
final double deg = guideAngle!;
final double x = layout.xFor(deg.clamp(0, 360));
final double bottom = layout.yFor(layout.ordered.length) - 4;
final Paint p = Paint()
..color = tokens.accent
..strokeWidth = 1.4;
// 虚线:从相位带一直画到图表底部。
const double dash = 6;
const double gap = 6;
double y = phaseBandTop - 8;
while (y < bottom) {
canvas.drawLine(Offset(x, y), Offset(x, math.min(y + dash, bottom)), p);
y += dash + gap;
}
_text(canvas, '${deg.toStringAsFixed(1)}°', Offset(x, phaseBandTop - 18),
color: tokens.accent, size: 11, align: TextAlign.center, weight: FontWeight.w700);
}
/// 过滤出有效(跨度 > 0)的相位分区。
///
/// 相位分区来自 [Project.phaseZones],可在工艺参数面板中编辑;
/// 这里只做基本合法性过滤,保证异常数据不会画出负宽度色带。
List<PhaseZone> _phaseZones() {
return project.phaseZones.where((PhaseZone z) => z.endAngle > z.startAngle).toList();
}
/// 绘制相位色带。
///
/// 算法:把每个相位分区映射到横轴像素,绘制一个圆角矩形,填充强调色的
/// 低透明度版本并描边;宽度足够(> 64px)时居中标注分区名称。
/// 若分区跨越 360°(结束角 > 360),则在左侧补画从 0° 起的续段,
/// 对应“跨循环”语义。
void _phaseBands(Canvas canvas, BarLayout layout) {
for (final PhaseZone z in _phaseZones()) {
final double x0 = layout.xFor(z.startAngle);
final double x1 = layout.xFor(z.endAngle);
final Rect r = Rect.fromLTWH(x0, phaseBandTop, math.max(x1 - x0, 2), 32);
canvas.drawRRect(
RRect.fromRectAndRadius(r, const Radius.circular(6)),
Paint()..color = tokens.accent.withValues(alpha: 0.08),
);
canvas.drawRRect(
RRect.fromRectAndRadius(r, const Radius.circular(6)),
Paint()
..style = PaintingStyle.stroke
..color = tokens.line2,
);
// 名称居中且按色带宽度截断,保证不越出色块。
if (r.width > 26) {
_textClipped(canvas, z.name, Offset(r.center.dx, r.center.dy), r.width - 10,
color: tokens.sub, size: 11);
}
if (z.endAngle > 360) {
final double wx = layout.xFor(z.endAngle - 360) - layout.plotLeft;
canvas.drawRRect(
RRect.fromRectAndRadius(Rect.fromLTWH(layout.plotLeft, phaseBandTop, wx, 32), const Radius.circular(6)),
Paint()..color = tokens.accent.withValues(alpha: 0.08),
);
}
}
}
/// 绘制双轴标尺。
///
/// 每 15° 一个刻度,30° 为主刻度(更长);每 60° 标注文字:
/// 下排为相位(度,强调色),上排为等价的绝对时间(由 [tvalStr] 格式化)。
/// 参数 [u] 提供单位设置,[cycle] 为一个完整循环的毫秒数。
void _ruler(Canvas canvas, BarLayout layout, UnitSettings u, double cycle) {
canvas.drawLine(Offset(layout.plotLeft, axisTop), Offset(layout.plotRight, axisTop),
Paint()..color = tokens.line2..strokeWidth = 1);
for (int d = 0; d <= 360; d += 15) {
final double x = layout.xFor(d.toDouble());
final bool major = d % 30 == 0;
canvas.drawLine(
Offset(x, axisTop),
Offset(x, axisTop + (major ? 28 : 14)),
Paint()
..color = major ? tokens.line2 : tokens.line
..strokeWidth = 1,
);
if (d % 60 == 0) {
_text(canvas, pfmt(d.toDouble(), u, dec: 0), Offset(x, axisTop + 44),
color: tokens.accent, size: 10, align: TextAlign.center);
_text(canvas, tvalStr(d / 360 * cycle, u, dec: u.timeUnit == TimeUnit.ms ? 0 : 1),
Offset(x, axisTop + 22), color: tokens.dim, size: 10, align: TextAlign.center);
}
}
_text(canvas, '时间 / ${timeUnitLabel(u.timeUnit)}', Offset(layout.plotLeft - 14, axisTop + 22),
color: tokens.dim, size: 10, align: TextAlign.right);
_text(canvas, '相位 / ${phaseUnitLabel(u.phaseUnit)}', Offset(layout.plotLeft - 14, axisTop + 44),
color: tokens.accent, size: 10, align: TextAlign.right);
}
/// 绘制循环起止边界线(0° 与 360°)。
///
/// 0° 使用 [Tokens.ok]、360° 使用 [Tokens.warn] 的虚线竖线表示,
/// 并在下方标注“循环开始 / 循环结束”。虚线由 [_dashedLine] 手工分段绘制。
void _boundaries(Canvas canvas, BarLayout layout) {
for (final double d in <double>[0, 360]) {
final double x = layout.xFor(d);
final Color col = d == 0 ? tokens.ok : tokens.warn;
final Paint p = Paint()
..color = col.withValues(alpha: 0.7)
..strokeWidth = 1.4;
_dashedLine(canvas, Offset(x, axisTop + 28), Offset(x, layout.yFor(layout.ordered.length) - 4), p);
_text(
canvas,
d == 0 ? '循环开始 0°' : '循环结束 360°',
Offset(d == 0 ? x + 8 : x - 8, axisTop + 62),
color: col,
size: 10,
align: d == 0 ? TextAlign.left : TextAlign.right,
);
}
}
/// 在两点之间绘制固定 dash/gap 的虚线。
///
/// 参数 [a]/[b]:线段端点;[p]:画笔(颜色与线宽由调用方决定)。
/// 通过沿单位方向向量步进生成短线段,不依赖 dashPathEffect。
void _dashedLine(Canvas canvas, Offset a, Offset b, Paint p) {
const double dash = 6;
const double gap = 6;
final double total = (b - a).distance;
final Offset dir = (b - a) / total;
double t = 0;
while (t < total) {
final double end = math.min(t + dash, total);
canvas.drawLine(a + dir * t, a + dir * end, p);
t = end + gap;
}
}
/// 绘制所有动作行。
///
/// 每行包含:
/// - 隔行背景(偶数行填充 [Tokens.panel2] 半透明色,便于横向阅读);
/// - 左侧序号色块,以及 `序号. id 名称`、`工位 · 驱动` 文本;
/// - 行底分隔线;
/// - 主条块(带阴影、选中描边、关键路径描边);
/// - 跨循环延续段(当 [Action.wrap] 且开启自动拆分时);
/// - 条块内或条块外的名称与时长标注。
///
/// 参数 [layout] 提供行列几何,[u]/[cycle] 用于时长文本换算。
void _rows(Canvas canvas, BarLayout layout, UnitSettings u, double cycle) {
final List<int> lanes = layout.lanes;
for (int li = 0; li < lanes.length; li++) {
final int lane = lanes[li];
final double y = layout.yFor(li);
final List<Action> acts = layout.actionsInLane(lane);
// 隔泳道背景色带,弱化边界。
if (li.isEven) {
canvas.drawRRect(
RRect.fromRectAndRadius(
Rect.fromLTWH(20, y - 8, layout.plotRight - 20, BarLayout.rowVisual),
const Radius.circular(10),
),
Paint()..color = tokens.panel2.withValues(alpha: 0.5),
);
}
// 泳道标题(合并显示,最多两行,用 / 分隔)。
_laneHeader(canvas, layout, li, acts, y);
// 泳道底分隔线。
canvas.drawLine(
Offset(layout.plotLeft, y + BarLayout.rowVisual - 4),
Offset(layout.plotRight, y + BarLayout.rowVisual - 4),
Paint()
..color = tokens.line.withValues(alpha: 0.5)
..strokeWidth = 1,
);
// 该泳道内的所有动作条(互不重叠)。
for (final Action a in acts) {
_bar(canvas, layout, a, u, cycle);
}
}
}
/// 绘制一条泳道的标题:序号徽标 + 合并动作名(≤2 行,用 / 分隔)+ 副标题。
void _laneHeader(Canvas canvas, BarLayout layout, int li, List<Action> acts, double y) {
final double labelMax = BarLayout.leftGutter - 68;
final Color c0 = acts.isEmpty ? tokens.accent : Color(acts.first.color);
canvas.drawRRect(
RRect.fromRectAndRadius(Rect.fromLTWH(32, y + 8, 18, 18), const Radius.circular(5)),
Paint()..color = c0,
);
_text(canvas, '${li + 1}', Offset(41, y + 17),
color: tokens.chip, size: 10, align: TextAlign.center, weight: FontWeight.w700);
if (acts.isEmpty) return;
// 多个动作合并为「A1 上料 / A2 定位」,最多两行。
final String joined = acts.map((Action a) => '${a.id} ${a.name}').join(' / ');
final TextPainter tp = TextPainter(
text: TextSpan(
text: joined,
style: TextStyle(
color: tokens.text,
fontSize: 11,
fontWeight: FontWeight.w600,
fontFamily: 'Microsoft YaHei UI',
),
),
textDirection: ui.TextDirection.ltr,
maxLines: 2,
ellipsis: '…',
)..layout(maxWidth: labelMax);
tp.paint(canvas, Offset(60, y + 2));
final Station? st = project.stationById(acts.first.stationId);
final String sub = '${st?.name ?? acts.first.stationId} · ${acts.first.driver}'
'${acts.length > 1 ? ' · 共 ${acts.length} 个' : ''}';
_textMax(canvas, sub, Offset(60, y + 32), labelMax, color: tokens.dim, size: 10);
}
/// 绘制单个动作条块(投影 / 填充 / 描边 / 跨圈尾段 / 文字标注)。
void _bar(Canvas canvas, BarLayout layout, Action a, UnitSettings u, double cycle) {
final Color c = Color(a.color);
final Rect r = layout.rectFor(a);
final bool sel = selectedIds.contains(a.id);
final bool crit = criticalActions.contains(a.id);
canvas.drawRRect(
RRect.fromRectAndRadius(r.shift(const Offset(1.5, 2)), const Radius.circular(8)),
Paint()..color = tokens.shadow.withValues(alpha: tokens.shadowOp),
);
canvas.drawRRect(
RRect.fromRectAndRadius(r, const Radius.circular(8)),
Paint()..color = c.withValues(alpha: 0.92),
);
canvas.drawRRect(
RRect.fromRectAndRadius(r, const Radius.circular(8)),
Paint()
..style = PaintingStyle.stroke
..strokeWidth = 1
..color = tokens.selOutline.withValues(alpha: sel ? 0.9 : 0.25),
);
if (crit) {
canvas.drawRRect(
RRect.fromRectAndRadius(r, const Radius.circular(8)),
Paint()
..style = PaintingStyle.stroke
..strokeWidth = 2
..color = tokens.crit,
);
}
if (a.wrap && u.autoSplitWrap) {
final double wx = layout.xFor(a.endAngle - 360) - layout.plotLeft;
final Rect tail = Rect.fromLTWH(layout.plotLeft, r.top, math.max(wx, 2), r.height);
canvas.drawRRect(
RRect.fromRectAndRadius(tail, const Radius.circular(8)),
Paint()..color = c.withValues(alpha: tokens.tailOp),
);
_text(canvas, '跨循环延续 ${(a.endAngle - 360).toStringAsFixed(0)}°',
Offset(layout.plotLeft + wx + 18, r.center.dy), color: tokens.warn, size: 10);
}
final double durMs = a.durationAngle / 360 * cycle;
if (r.width > 150) {
_text(canvas, a.name, Offset(r.left + 10, r.center.dy - 5),
color: tokens.arcText, size: 11, weight: FontWeight.w700);
_text(canvas, tfmt(durMs, u), Offset(r.left + 10, r.center.dy + 7),
color: tokens.arcText.withValues(alpha: 0.8), size: 10);
} else if (r.right + 170 > layout.plotRight) {
_text(canvas, '${a.name} ${tfmt(durMs, u)}', Offset(r.left - 10, r.center.dy),
color: c, size: 11, weight: FontWeight.w600, align: TextAlign.right);
} else {
_text(canvas, '${a.name} ${tfmt(durMs, u)}', Offset(r.right + 10, r.center.dy),
color: c, size: 11, weight: FontWeight.w600);
}
_text(canvas, 'Δ${a.durationAngle.toStringAsFixed(0)}°', Offset(layout.plotRight - 4, r.center.dy),
color: tokens.dim, size: 10, align: TextAlign.right);
}
/// 绘制动作依赖连线(正交折线)。
///
/// 算法要点:
/// - 建立“动作 id → 行号”索引,用于判断两行相对位置。
/// - FS 依赖从源条块右端出发;SS 依赖从源条块左端出发。
/// - 相邻行(行距 ≤ 1)走短折线;跨多行时在行区间外侧选一条“泳道”
/// 作为竖直通道,泳道位置按区间内条块的最右/最左边界外扩,
/// 并叠加 `(k % 3) * 16` 的错位,避免多条线完全重叠。
/// - 关键路径依赖先画一层加粗低透明度的“光晕”,再画主线。
/// - 末端绘制箭头与类型标签(FS 顺序 / SS +偏移角)。
///
/// 参数 [layout] 提供条块矩形;[u] 当前未直接使用,保留以对齐调用接口。
void _deps(Canvas canvas, BarLayout layout, UnitSettings u) {
final List<Action> ordered = layout.ordered;
// 以「泳道序号」判断依赖的相邻关系(同泳道多个动作不再误判)。
final Map<String, int> index = <String, int>{
for (final Action a in project.actions) a.id: layout.laneIndexOf(a.layer),
};
int k = 0;
for (final Action to in ordered) {
for (final Dependency d in to.deps) {
final Action? from = project.actionById(d.fromId);
if (from == null) continue;
final int ia = index[from.id] ?? 0;
final int ib = index[to.id] ?? 0;
final Color col = d.type == DepType.fs ? tokens.fs : tokens.ss;
final bool crit = criticalDeps.contains('${d.fromId}->${d.toId}');
final Rect ra = layout.rectFor(from);
final Rect rb = layout.rectFor(to);
final double sy = ra.center.dy;
final double ty = rb.center.dy;
final double sx = d.type == DepType.fs ? ra.right : ra.left;
final double ex = d.type == DepType.fs ? sx - 9 : sx + 9;
final double tx = rb.left + 14;
final double dir = ty > sy ? 1 : -1;
final double gapY = ty - dir * 23;
final double tipY = ty - dir * 15;
final Path p = Path();
double chipX;
final double chipY = gapY;
// 相邻行:短折线,直接从源端折向目标条块上方。
if ((ib - ia).abs() <= 1) {
p.moveTo(ex, sy + dir * 13);
p.lineTo(ex, gapY);
p.lineTo(tx, gapY);
p.lineTo(tx, tipY - dir * 12);
chipX = (ex + tx) / 2;
} else {
// 跨多行:扫描区间内所有条块边界,据此在外侧选一条竖直泳道。
double maxRight = 0;
double minLeft = double.infinity;
final List<int> lanes = layout.lanes;
for (int r = math.min(ia, ib) + 1; r < math.max(ia, ib); r++) {
if (r < 0 || r >= lanes.length) continue;
for (final Action aa in layout.actionsInLane(lanes[r])) {
final Rect rr = layout.rectFor(aa);
maxRight = math.max(maxRight, rr.right);
minLeft = math.min(minLeft, rr.left);
}
}
double lane = math.max(math.max(sx, tx), maxRight) + 28 + (k % 3) * 16;
if (lane > layout.plotRight - 16) {
lane = math.min(math.min(sx, tx), minLeft) - 28 - (k % 3) * 16;
}
final double srcGapY = sy + dir * 27;
p.moveTo(ex, sy + dir * 13);
p.lineTo(ex, srcGapY);
p.lineTo(lane, srcGapY);
p.lineTo(lane, gapY);
p.lineTo(tx, gapY);
p.lineTo(tx, tipY - dir * 12);
chipX = (lane + tx) / 2;
}
// 关键路径依赖:先铺一层加粗低透明度光晕,再画主线。
if (crit) {
canvas.drawPath(
p,
Paint()
..style = PaintingStyle.stroke
..strokeWidth = 9
..strokeCap = StrokeCap.round
..color = col.withValues(alpha: 0.18),
);
}
canvas.drawPath(
p,
Paint()
..style = PaintingStyle.stroke
..strokeWidth = crit ? 2.2 : 1.7
..strokeCap = StrokeCap.round
..color = col.withValues(alpha: 0.95),
);
_arrow(canvas, Offset(tx, tipY), Offset(0, dir), col);
final String label = d.type == DepType.fs ? 'FS 顺序' : 'SS +${d.offsetAngle.toStringAsFixed(0)}°';
// 标签上移,避免压住箭头(箭头在更靠近目标条块的一侧)。
_chip(canvas, Offset(chipX, chipY - 11), label, col, crit);
k++;
}
}
}
/// 在 [tip] 处绘制指向 [dir] 方向的实心三角箭头。
///
/// 参数 [col] 为填充色;[dir] 只需提供方向(本函数不归一化,直接按分量使用)。
void _arrow(Canvas canvas, Offset tip, Offset dir, Color col) {
const double s = 13;
final Offset b = tip - dir * s;
final Offset n = Offset(-dir.dy, dir.dx) * s * 0.5;
final Path p = Path()
..moveTo(tip.dx, tip.dy)
..lineTo(b.dx + n.dx, b.dy + n.dy)
..lineTo(b.dx - n.dx, b.dy - n.dy)
..close();
canvas.drawPath(p, Paint()..color = col);
}
/// 绘制依赖类型标签胶囊。
///
/// 参数 [center]:胶囊中心;[label]:文字;[col]:边框与文字色;
/// [crit]:为真时在左侧追加星形关键标记,并把胶囊加宽。
void _chip(Canvas canvas, Offset center, String label, Color col, bool crit) {
final TextPainter tp = _tp(label, 10.5, col, FontWeight.w600);
final double w = tp.width + 18 + (crit ? 18 : 0);
const double h = 18;
final Rect r = Rect.fromCenter(center: center, width: w, height: h);
canvas.drawRRect(
RRect.fromRectAndRadius(r, const Radius.circular(6)),
Paint()..color = tokens.chip,
);
canvas.drawRRect(
RRect.fromRectAndRadius(r, const Radius.circular(6)),
Paint()
..style = PaintingStyle.stroke
..strokeWidth = 1
..color = col,
);
if (crit) {
canvas.drawCircle(Offset(r.left + 9, center.dy), 7, Paint()..color = tokens.crit);
_text(canvas, '★', Offset(r.left + 9, center.dy), color: tokens.onAccent, size: 10, align: TextAlign.center);
tp.paint(canvas, Offset(r.left + 20, center.dy - tp.height / 2));
} else {
tp.paint(canvas, Offset(r.left + 9, center.dy - tp.height / 2));
}
}
/// 构造并布局一个 [TextPainter]。
///
/// 统一使用微软雅黑字体;参数 [s] 文本、[size] 字号、[color] 颜色、
/// [weight] 字重、[align] 对齐方式。返回已完成 layout 的实例。
TextPainter _tp(String s, double size, Color color, FontWeight weight, {TextAlign align = TextAlign.left}) {
final TextPainter tp = TextPainter(
text: TextSpan(
text: s,
style: TextStyle(color: color, fontSize: size, fontWeight: weight, fontFamily: 'Microsoft YaHei UI'),
),
textDirection: ui.TextDirection.ltr,
textAlign: align,
)..layout();
return tp;
}
/// 便捷文本绘制。
///
/// 在 [at] 处按 [align] 对齐绘制字符串 [s](默认左对齐、垂直居中)。
/// [color] 缺省时使用 [Tokens.text]。
/// 在 [at] 处绘制左对齐的单行文字,并按 [maxWidth] 截断(超出显示省略号)。
void _textMax(Canvas canvas, String s, Offset at, double maxWidth,
{Color? color, double size = 12, FontWeight weight = FontWeight.normal}) {
if (maxWidth <= 4) return;
final TextPainter tp = TextPainter(
text: TextSpan(
text: s,
style: TextStyle(color: color ?? tokens.text, fontSize: size, fontWeight: weight, fontFamily: 'Microsoft YaHei UI'),
),
textDirection: ui.TextDirection.ltr,
maxLines: 1,
ellipsis: '…',
)..layout(maxWidth: maxWidth);
tp.paint(canvas, Offset(at.dx, at.dy - tp.height / 2));
}
/// 在 [center] 处绘制居中的单行文字,并按 [maxWidth] 截断(超出显示省略号)。
void _textClipped(Canvas canvas, String s, Offset center, double maxWidth,
{Color? color, double size = 11, FontWeight weight = FontWeight.w600}) {
if (maxWidth <= 4) return;
final TextPainter tp = TextPainter(
text: TextSpan(
text: s,
style: TextStyle(color: color ?? tokens.text, fontSize: size, fontWeight: weight, fontFamily: 'Microsoft YaHei UI'),
),
textDirection: ui.TextDirection.ltr,
maxLines: 1,
ellipsis: '…',
)..layout(maxWidth: maxWidth);
tp.paint(canvas, Offset(center.dx - tp.width / 2, center.dy - tp.height / 2));
}
void _text(Canvas canvas, String s, Offset at,
{Color? color, double size = 12, FontWeight weight = FontWeight.normal, TextAlign align = TextAlign.left}) {
final TextPainter tp = _tp(s, size, color ?? tokens.text, weight, align: align);
double dx = at.dx;
if (align == TextAlign.center) dx -= tp.width / 2;
if (align == TextAlign.right) dx -= tp.width;
tp.paint(canvas, Offset(dx, at.dy - tp.height / 2));
}
/// 始终返回 `true`。
///
/// 绘制完全依赖每次重建的布局与外部状态,且本绘制器不做缓存,
/// 因此保守地要求每次重绘。
@override
bool shouldRepaint(covariant BarPainter old) => true;
}