// 应用图标组件:绘制一个与应用程序图标一致的紧凑“时钟 / 周期表盘”标记。 // // 架构位置:属于纯 UI 装饰组件,仅依赖主题令牌(Tokens)取色, // 不持有任何业务状态;在工具栏与启动画面等处复用。 import 'dart:math' as math; import 'package:flutter/material.dart'; import '../theme/tokens.dart'; /// 应用图标:一个简洁的时钟 / 周期表盘标记,呼应产品“节拍”主题。 class AppLogo extends StatelessWidget { /// 创建应用图标;[size] 为正方形边长,默认 30。 const AppLogo({super.key, this.size = 30}); /// 图标边长(逻辑像素)。 final double size; /// 使用 [_LogoPainter] 在方形画布上绘制图标。 @override Widget build(BuildContext context) { final Tokens t = context.tok; return CustomPaint( size: Size.square(size), painter: _LogoPainter(t), ); } } /// 图标绘制器:按主题令牌绘制圆角底板、双色圆弧表盘与指针。 class _LogoPainter extends CustomPainter { /// 创建绘制器,[t] 为当前主题令牌。 _LogoPainter(this.t); /// 当前主题令牌,提供配色。 final Tokens t; /// 依次绘制:圆角底板 → 双色圆环弧 → 时针分针 → 中心圆点。 @override void paint(Canvas canvas, Size size) { // 以最短边为基准计算尺寸,保证图标始终为正方形。 final double s = size.shortestSide; final Offset c = Offset(size.width / 2, size.height / 2); // 圆角方形底板:半透明填充 + 主题色描边。 final RRect badge = RRect.fromRectAndRadius( Rect.fromLTWH(0, 0, s, s), Radius.circular(s * 0.28), ); canvas.drawRRect(badge, Paint()..color = t.accent.withValues(alpha: 0.16)); canvas.drawRRect( badge, Paint() ..style = PaintingStyle.stroke ..strokeWidth = 1.2 ..color = t.accent, ); // 表盘圆环:用两段不同颜色的圆弧表示进度/周期。 final double r = s * 0.30; final Paint ring = Paint() ..style = PaintingStyle.stroke ..strokeWidth = s * 0.075 ..strokeCap = StrokeCap.round; ring.color = t.accent; canvas.drawArc(Rect.fromCircle(center: c, radius: r), -math.pi / 2, math.pi * 0.55, false, ring); ring.color = t.accent2; canvas.drawArc(Rect.fromCircle(center: c, radius: r), -math.pi / 2 + math.pi * 0.62, math.pi * 0.5, false, ring); // 指针:一根主色长针 + 一根强调色短针,从中心向外绘制。 final Paint hand = Paint() ..strokeCap = StrokeCap.round ..strokeWidth = s * 0.085; hand.color = t.text; canvas.drawLine(c, c.translate(0, -r * 0.62), hand); hand.color = t.accent; canvas.drawLine(c, c.translate(r * 0.5, r * 0.16), hand); // 中心轴点:外圈文字色、内圈背景色,形成中空圆点。 canvas.drawCircle(c, s * 0.055, Paint()..color = t.text); canvas.drawCircle(c, s * 0.022, Paint()..color = t.bg); } /// 仅当主题令牌变化时重绘。 @override bool shouldRepaint(covariant _LogoPainter old) => old.t != t; }