大小样式调整

This commit is contained in:
mmc
2026-03-04 14:00:40 +08:00
parent cd6b9a59ff
commit 7715d0b9fd

View File

@@ -8,6 +8,7 @@ import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:syncfusion_flutter_gauges/gauges.dart'; import 'package:syncfusion_flutter_gauges/gauges.dart';
import 'package:flutter/animation.dart'; import 'package:flutter/animation.dart';
// 占位导入(请替换为你项目的实际路径)
import '../../../../core/app/app_user_cubit.dart'; import '../../../../core/app/app_user_cubit.dart';
import '../../../../core/di/injection.dart'; import '../../../../core/di/injection.dart';
import '../../../../core/network/net_message_dispatcher.dart'; import '../../../../core/network/net_message_dispatcher.dart';
@@ -120,8 +121,8 @@ class _RunningStatusPageState extends State<RunningStatusPage> {
return SizedBox(height: height); return SizedBox(height: height);
} }
// ====================== 图表卡片(统一样式) ====================== // ====================== 图表卡片(统一样式:标题字体缩小+标题与切换按钮同行) ======================
Widget _chartCard({required String title, required Widget child}) { Widget _chartCard({required String title, required Widget child, required bool isGaugeMode, required Function() onGaugeTap, required Function() onChartTap}) {
return Container( return Container(
decoration: BoxDecoration( decoration: BoxDecoration(
color: Colors.white, color: Colors.white,
@@ -132,9 +133,46 @@ class _RunningStatusPageState extends State<RunningStatusPage> {
child: Column( child: Column(
crossAxisAlignment: CrossAxisAlignment.start, crossAxisAlignment: CrossAxisAlignment.start,
children: [ children: [
Text( // 标题与切换按钮同行布局
title, Row(
style: const TextStyle(fontSize: 18, fontWeight: FontWeight.bold, color: Color(0xFF1677FF)), mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Text(
title,
style: const TextStyle(
fontSize: 16, // 标题字体缩小(原18)
fontWeight: FontWeight.bold,
color: Color(0xFF1677FF),
),
),
Row(
children: [
GestureDetector(
onTap: onGaugeTap,
child: Text(
"仪表盘",
style: TextStyle(
color: isGaugeMode ? const Color(0xFF1677FF) : Colors.grey,
fontSize: 14,
fontWeight: isGaugeMode ? FontWeight.bold : FontWeight.normal,
),
),
),
const SizedBox(width: 16),
GestureDetector(
onTap: onChartTap,
child: Text(
"折线图",
style: TextStyle(
color: !isGaugeMode ? const Color(0xFF1677FF) : Colors.grey,
fontSize: 14,
fontWeight: !isGaugeMode ? FontWeight.bold : FontWeight.normal,
),
),
),
],
),
],
), ),
const SizedBox(height: 16), const SizedBox(height: 16),
child, child,
@@ -143,38 +181,37 @@ class _RunningStatusPageState extends State<RunningStatusPage> {
); );
} }
// ====================== 带动画的圆形仪表盘(核心修改) ====================== // ====================== 带动画的圆形仪表盘(尺寸缩小:原220→180,绘制尺寸280→240) ======================
Widget _circularGauge({ Widget _circularGauge({
required double value, required double value,
required double maxValue, required double maxValue,
required String unit, required String unit,
required String label,
List<double> majorTicks = const [0, 10, 20, 30, 40, 50, 60, 70, 80, 90, 100], List<double> majorTicks = const [0, 10, 20, 30, 40, 50, 60, 70, 80, 90, 100],
}) { }) {
return AnimatedCircularGauge(value: value, maxValue: maxValue, unit: unit, majorTicks: majorTicks); return AnimatedCircularGauge(value: value, maxValue: maxValue, unit: unit, majorTicks: majorTicks);
} }
// ====================== 罗盘式航向角仪表盘 ====================== // ====================== 罗盘式航向角仪表盘(尺寸同步缩小) ======================
Widget _compassGauge({required double yaw}) { Widget _compassGauge({required double yaw}) {
return SizedBox( return SizedBox(
height: 250, height: 200, // 原250→200
child: Stack( child: Stack(
alignment: Alignment.center, alignment: Alignment.center,
children: [ children: [
CustomPaint( CustomPaint(
painter: CompassGaugePainter(yaw: yaw), painter: CompassGaugePainter(yaw: yaw),
size: const Size(280, 280), size: const Size(240, 240), // 原280→240
), ),
Text( Text(
"${yaw.toStringAsFixed(0)}°", "${yaw.toStringAsFixed(0)}°",
style: const TextStyle(fontSize: 24, fontWeight: FontWeight.bold, color: Colors.black), style: const TextStyle(fontSize: 20, fontWeight: FontWeight.bold, color: Colors.black), // 字体同步缩小
), ),
], ],
), ),
); );
} }
// ====================== 折线图(统一样式) ====================== // ====================== 折线图(修复溢出:限制宽度+裁剪+适配父容器) ======================
Widget _styledLineChart({ Widget _styledLineChart({
required String title, required String title,
required List<LineChartBarData> lines, required List<LineChartBarData> lines,
@@ -202,67 +239,69 @@ class _RunningStatusPageState extends State<RunningStatusPage> {
], ],
), ),
), ),
// 用Expanded适配宽度,避免溢出 // 修复溢出:用ConstrainedBox限制宽度+ClipRect裁剪+Expanded适配
Expanded( Expanded(
child: SizedBox( child: ConstrainedBox(
height: 200, constraints: const BoxConstraints(maxWidth: double.infinity),
child: LineChart( child: ClipRect(
LineChartData( child: SizedBox(
minY: yAxisMin, height: 180, // 原200→180,缩小折线图高度
maxY: yAxisMax, child: LineChart(
gridData: FlGridData( LineChartData(
show: true, minY: yAxisMin,
horizontalInterval: (yAxisMax - yAxisMin) / leftTicks.length, maxY: yAxisMax,
getDrawingHorizontalLine: (value) => const FlLine(color: Color(0xFFE5E5E5), strokeWidth: 1), minX: 0,
drawVerticalLine: false, maxX: bottomLabels.length - 1.toDouble(), // 固定X轴范围,防止溢出
), gridData: FlGridData(
titlesData: FlTitlesData( show: true,
leftTitles: AxisTitles( horizontalInterval: (yAxisMax - yAxisMin) / leftTicks.length,
sideTitles: SideTitles( getDrawingHorizontalLine: (value) => const FlLine(color: Color(0xFFE5E5E5), strokeWidth: 1),
showTitles: true, drawVerticalLine: false,
interval: (yAxisMax - yAxisMin) / leftTicks.length,
getTitlesWidget: (value, meta) {
if (leftTicks.contains(value)) {
return Text(value.toStringAsFixed(0), style: const TextStyle(fontSize: 12, color: Colors.grey));
}
return const SizedBox.shrink();
},
// 限制左侧标签宽度
reservedSize: 40,
), ),
), titlesData: FlTitlesData(
bottomTitles: AxisTitles( leftTitles: AxisTitles(
sideTitles: SideTitles( sideTitles: SideTitles(
showTitles: true, showTitles: true,
interval: bottomInterval, interval: (yAxisMax - yAxisMin) / leftTicks.length,
// 限制底部标签高度和宽度 getTitlesWidget: (value, meta) {
reservedSize: 30, if (leftTicks.contains(value)) {
getTitlesWidget: (value, meta) { return Text(value.toStringAsFixed(0), style: const TextStyle(fontSize: 12, color: Colors.grey));
int idx = value.toInt(); }
if (idx >= 0 && idx < bottomLabels.length) { return const SizedBox.shrink();
return Container( },
width: 40, reservedSize: 40,
alignment: Alignment.center, ),
child: Text( ),
bottomLabels[idx], bottomTitles: AxisTitles(
style: const TextStyle(fontSize: 12, color: Colors.grey), sideTitles: SideTitles(
overflow: TextOverflow.ellipsis, showTitles: true,
), interval: bottomInterval,
); reservedSize: 30,
} getTitlesWidget: (value, meta) {
return const SizedBox.shrink(); int idx = value.toInt();
}, if (idx >= 0 && idx < bottomLabels.length) {
return Container(
width: 40,
alignment: Alignment.center,
child: Text(
bottomLabels[idx],
style: const TextStyle(fontSize: 12, color: Colors.grey),
overflow: TextOverflow.ellipsis,
),
);
}
return const SizedBox.shrink();
},
),
),
rightTitles: const AxisTitles(sideTitles: SideTitles(showTitles: false)),
topTitles: const AxisTitles(sideTitles: SideTitles(showTitles: false)),
), ),
borderData: FlBorderData(show: false),
lineBarsData: lines,
extraLinesData: ExtraLinesData(horizontalLines: []),
), ),
rightTitles: const AxisTitles(sideTitles: SideTitles(showTitles: false)),
topTitles: const AxisTitles(sideTitles: SideTitles(showTitles: false)),
), ),
borderData: FlBorderData(show: false),
lineBarsData: lines,
// 设置图表边距,避免内容贴边
minX: 0,
maxX: bottomLabels.length - 1.toDouble(),
extraLinesData: ExtraLinesData(horizontalLines: []),
), ),
), ),
), ),
@@ -316,240 +355,217 @@ class _RunningStatusPageState extends State<RunningStatusPage> {
padding: const EdgeInsets.all(16), padding: const EdgeInsets.all(16),
child: Column( child: Column(
children: [ children: [
// 电压仪表盘/折线图 // 电压仪表盘/折线图(使用新的chartCard,传入切换回调)
_chartCard( _chartCard(
title: "电压 (V)", title: "电压 (V)",
child: Column( isGaugeMode: _voltageGaugeMode,
children: [ onGaugeTap: () => setState(() => _voltageGaugeMode = true),
Row( onChartTap: () => setState(() => _voltageGaugeMode = false),
mainAxisAlignment: MainAxisAlignment.end, child: _voltageGaugeMode
children: [ ? _circularGauge(value: status.voltage, maxValue: 250, unit: "V", majorTicks: const [0, 50, 100, 150, 200, 250])
GestureDetector( : SizedBox(
onTap: () => setState(() => _voltageGaugeMode = true), height: 180, // 原200→180
child: Text( child: _styledLineChart(
"仪表盘", title: "电压",
style: TextStyle( lines: [_lineData(_voltageHistory, const Color(0xFF4CAF50))],
color: _voltageGaugeMode ? const Color(0xFF1677FF) : Colors.grey, yAxisMax: 250,
fontSize: 14, leftTicks: const [0, 50, 100, 150, 200, 250],
fontWeight: _voltageGaugeMode ? FontWeight.bold : FontWeight.normal,
),
),
), ),
const SizedBox(width: 16), ),
GestureDetector(
onTap: () => setState(() => _voltageGaugeMode = false),
child: Text(
"折线图",
style: TextStyle(
color: !_voltageGaugeMode ? const Color(0xFF1677FF) : Colors.grey,
fontSize: 14,
fontWeight: !_voltageGaugeMode ? FontWeight.bold : FontWeight.normal,
),
),
),
],
),
const SizedBox(height: 16),
_voltageGaugeMode
? _circularGauge(value: status.voltage, maxValue: 250, unit: "V", label: "电压", majorTicks: const [0, 50, 100, 150, 200, 250])
: SizedBox(
height: 200,
child: _styledLineChart(
title: "电压",
lines: [_lineData(_voltageHistory, const Color(0xFF4CAF50))],
yAxisMax: 250,
leftTicks: const [0, 50, 100, 150, 200, 250],
),
),
],
),
), ),
_gap(), _gap(),
// 芯片温度仪表盘/折线图 // 芯片温度仪表盘/折线图
_chartCard( _chartCard(
title: "芯片温度 (°C)", title: "芯片温度 (°C)",
child: Column( isGaugeMode: _chipTempGaugeMode,
children: [ onGaugeTap: () => setState(() => _chipTempGaugeMode = true),
Row( onChartTap: () => setState(() => _chipTempGaugeMode = false),
mainAxisAlignment: MainAxisAlignment.end, child: _chipTempGaugeMode
children: [ ? _circularGauge(value: status.chipTemp, maxValue: 100, unit: "°C", majorTicks: const [0, 10, 20, 30, 40, 50, 60, 70, 80, 90, 100])
GestureDetector( : SizedBox(
onTap: () => setState(() => _chipTempGaugeMode = true), height: 180,
child: Text( child: _styledLineChart(
"仪表盘", title: "芯片温度",
style: TextStyle( lines: [_lineData(_chipTempHistory, const Color(0xFF4CAF50))],
color: _chipTempGaugeMode ? const Color(0xFF1677FF) : Colors.grey, yAxisMax: 100,
fontSize: 14, leftTicks: const [0, 20, 40, 60, 80, 100],
fontWeight: _chipTempGaugeMode ? FontWeight.bold : FontWeight.normal,
),
),
), ),
const SizedBox(width: 16), ),
GestureDetector(
onTap: () => setState(() => _chipTempGaugeMode = false),
child: Text(
"折线图",
style: TextStyle(
color: !_chipTempGaugeMode ? const Color(0xFF1677FF) : Colors.grey,
fontSize: 14,
fontWeight: !_chipTempGaugeMode ? FontWeight.bold : FontWeight.normal,
),
),
),
],
),
const SizedBox(height: 16),
_chipTempGaugeMode
? _circularGauge(
value: status.chipTemp,
maxValue: 100,
unit: "°C",
label: "芯片温度",
majorTicks: const [0, 10, 20, 30, 40, 50, 60, 70, 80, 90, 100],
)
: SizedBox(
height: 200,
child: _styledLineChart(
title: "芯片温度",
lines: [_lineData(_chipTempHistory, const Color(0xFF4CAF50))],
yAxisMax: 100,
leftTicks: const [0, 20, 40, 60, 80, 100],
),
),
],
),
), ),
_gap(), _gap(),
// 割刀速度仪表盘/折线图 // 割刀速度仪表盘/折线图
_chartCard( _chartCard(
title: "割刀速度 (rpm)", title: "割刀速度 (rpm)",
isGaugeMode: _knifeSpeedGaugeMode,
onGaugeTap: () => setState(() => _knifeSpeedGaugeMode = true),
onChartTap: () => setState(() => _knifeSpeedGaugeMode = false),
child: _knifeSpeedGaugeMode
? _circularGauge(
value: double.tryParse(status.knifeCuttingSpeed) ?? 0,
maxValue: 3000,
unit: "rpm",
majorTicks: const [0, 500, 1000, 1500, 2000, 2500, 3000],
)
: SizedBox(
height: 180,
child: _styledLineChart(
title: "割刀速度",
lines: [_lineData(_knifeHistory, const Color(0xFF4CAF50))],
yAxisMax: 3000,
yAxisMin: -3000,
leftTicks: const [-3000, -2000, -1000, 0, 1000, 2000, 3000],
),
),
),
_gap(),
// 测量速度对比(无切换按钮,使用原布局+标题字体缩小)
Container(
decoration: BoxDecoration(
color: Colors.white,
borderRadius: BorderRadius.circular(12),
boxShadow: [BoxShadow(color: Colors.grey.withOpacity(0.1), blurRadius: 4, offset: const Offset(0, 2))],
),
padding: const EdgeInsets.all(16),
child: Column( child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [ children: [
Row( Text(
mainAxisAlignment: MainAxisAlignment.end, "左右轮测量速度对比 (rpm)",
children: [ style: const TextStyle(
GestureDetector( fontSize: 16, // 标题字体缩小
onTap: () => setState(() => _knifeSpeedGaugeMode = true), fontWeight: FontWeight.bold,
child: Text( color: Color(0xFF1677FF),
"仪表盘", ),
style: TextStyle(
color: _knifeSpeedGaugeMode ? const Color(0xFF1677FF) : Colors.grey,
fontSize: 14,
fontWeight: _knifeSpeedGaugeMode ? FontWeight.bold : FontWeight.normal,
),
),
),
const SizedBox(width: 16),
GestureDetector(
onTap: () => setState(() => _knifeSpeedGaugeMode = false),
child: Text(
"折线图",
style: TextStyle(
color: !_knifeSpeedGaugeMode ? const Color(0xFF1677FF) : Colors.grey,
fontSize: 14,
fontWeight: !_knifeSpeedGaugeMode ? FontWeight.bold : FontWeight.normal,
),
),
),
],
), ),
const SizedBox(height: 16), const SizedBox(height: 16),
_knifeSpeedGaugeMode SizedBox(
? _circularGauge( height: 180,
value: double.tryParse(status.knifeCuttingSpeed) ?? 0, child: _styledLineChart(
maxValue: 3000, title: "左右轮测量速度对比",
unit: "rpm", lines: [_lineData(_leftMeasureHistory, const Color(0xFF3F51B5)), _lineData(_rightMeasureHistory, const Color(0xFF8BC34A))],
label: "割刀速度", yAxisMax: 3000,
majorTicks: const [0, 500, 1000, 1500, 2000, 2500, 3000], yAxisMin: -3000,
) leftTicks: const [-3000, -2000, -1000, 0, 1000, 2000, 3000],
: SizedBox( ),
height: 200, ),
child: _styledLineChart(
title: "割刀速度",
lines: [_lineData(_knifeHistory, const Color(0xFF4CAF50))],
yAxisMax: 3000,
yAxisMin: -3000,
leftTicks: const [-3000, -2000, -1000, 0, 1000, 2000, 3000],
),
),
], ],
), ),
), ),
_gap(), _gap(),
// 测量速度对比(左轮 + 右轮在一张图)
_chartCard( // 目标速度对比
title: "左右轮测量速度对比 (rpm)", Container(
child: SizedBox( decoration: BoxDecoration(
height: 200, color: Colors.white,
child: _styledLineChart( borderRadius: BorderRadius.circular(12),
title: "左右轮测量速度对比", boxShadow: [BoxShadow(color: Colors.grey.withOpacity(0.1), blurRadius: 4, offset: const Offset(0, 2))],
lines: [ ),
_lineData(_leftMeasureHistory, const Color(0xFF3F51B5)), // 左轮 padding: const EdgeInsets.all(16),
_lineData(_rightMeasureHistory, const Color(0xFF8BC34A)), // 右轮 child: Column(
], crossAxisAlignment: CrossAxisAlignment.start,
yAxisMax: 3000, children: [
yAxisMin: -3000, Text(
leftTicks: const [-3000, -2000, -1000, 0, 1000, 2000, 3000], "左右轮目标速度对比 (rpm)",
), style: const TextStyle(fontSize: 16, fontWeight: FontWeight.bold, color: Color(0xFF1677FF)),
),
const SizedBox(height: 16),
SizedBox(
height: 180,
child: _styledLineChart(
title: "左右轮目标速度对比",
lines: [_lineData(_leftTargetHistory, const Color(0xFF3F51B5)), _lineData(_rightTargetHistory, const Color(0xFF8BC34A))],
yAxisMax: 3000,
yAxisMin: -3000,
leftTicks: const [-3000, -2000, -1000, 0, 1000, 2000, 3000],
),
),
],
), ),
), ),
_gap(),
// 目标速度对比(左轮 + 右轮在一张图)
_chartCard(
title: "左右轮目标速度对比 (rpm)",
child: SizedBox(
height: 200,
child: _styledLineChart(
title: "左右轮目标速度对比",
lines: [
_lineData(_leftTargetHistory, const Color(0xFF3F51B5)), // 左轮
_lineData(_rightTargetHistory, const Color(0xFF8BC34A)), // 右轮
],
yAxisMax: 3000,
yAxisMin: -3000,
leftTicks: const [-3000, -2000, -1000, 0, 1000, 2000, 3000],
),
),
),
_gap(), _gap(),
// 电流对比 // 电流对比
_chartCard( Container(
title: "电流对比 (A)", decoration: BoxDecoration(
child: SizedBox( color: Colors.white,
height: 200, borderRadius: BorderRadius.circular(12),
child: _styledLineChart( boxShadow: [BoxShadow(color: Colors.grey.withOpacity(0.1), blurRadius: 4, offset: const Offset(0, 2))],
title: "电流对比", ),
lines: [_lineData(_leftCurrentHistory, const Color(0xFF3F51B5)), _lineData(_rightCurrentHistory, const Color(0xFF8BC34A))], padding: const EdgeInsets.all(16),
yAxisMax: 100, child: Column(
leftTicks: const [0, 20, 40, 60, 80, 100], crossAxisAlignment: CrossAxisAlignment.start,
), children: [
Text(
"电流对比 (A)",
style: const TextStyle(fontSize: 16, fontWeight: FontWeight.bold, color: Color(0xFF1677FF)),
),
const SizedBox(height: 16),
SizedBox(
height: 180,
child: _styledLineChart(
title: "电流对比",
lines: [_lineData(_leftCurrentHistory, const Color(0xFF3F51B5)), _lineData(_rightCurrentHistory, const Color(0xFF8BC34A))],
yAxisMax: 100,
leftTicks: const [0, 20, 40, 60, 80, 100],
),
),
],
), ),
), ),
_gap(), _gap(),
// 电机温度对比 // 电机温度对比
_chartCard( Container(
title: "电机温度对比 (°C)", decoration: BoxDecoration(
child: SizedBox( color: Colors.white,
height: 200, borderRadius: BorderRadius.circular(12),
child: _styledLineChart( boxShadow: [BoxShadow(color: Colors.grey.withOpacity(0.1), blurRadius: 4, offset: const Offset(0, 2))],
title: "电机温度对比", ),
lines: [_lineData(_leftTempHistory, const Color(0xFF3F51B5)), _lineData(_rightTempHistory, const Color(0xFF8BC34A))], padding: const EdgeInsets.all(16),
yAxisMax: 100, child: Column(
leftTicks: const [0, 20, 40, 60, 80, 100], crossAxisAlignment: CrossAxisAlignment.start,
), children: [
Text(
"电机温度对比 (°C)",
style: const TextStyle(fontSize: 16, fontWeight: FontWeight.bold, color: Color(0xFF1677FF)),
),
const SizedBox(height: 16),
SizedBox(
height: 180,
child: _styledLineChart(
title: "电机温度对比",
lines: [_lineData(_leftTempHistory, const Color(0xFF3F51B5)), _lineData(_rightTempHistory, const Color(0xFF8BC34A))],
yAxisMax: 100,
leftTicks: const [0, 20, 40, 60, 80, 100],
),
),
],
), ),
), ),
_gap(),
// 航向角仪表盘 // 航向角仪表盘(标题字体缩小+尺寸缩小)
_chartCard( Container(
title: "航向角 (°)", decoration: BoxDecoration(
child: _compassGauge(yaw: status.yaw), color: Colors.white,
borderRadius: BorderRadius.circular(12),
boxShadow: [BoxShadow(color: Colors.grey.withOpacity(0.1), blurRadius: 4, offset: const Offset(0, 2))],
),
padding: const EdgeInsets.all(16),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
"航向角 (°)",
style: const TextStyle(fontSize: 16, fontWeight: FontWeight.bold, color: Color(0xFF1677FF)),
),
const SizedBox(height: 16),
_compassGauge(yaw: status.yaw),
],
),
), ),
_gap(), _gap(),
], ],
@@ -580,7 +596,7 @@ class _RunningStatusPageState extends State<RunningStatusPage> {
children: [ children: [
const Padding( const Padding(
padding: EdgeInsets.symmetric(vertical: 12), padding: EdgeInsets.symmetric(vertical: 12),
child: Text("电机参数", style: TextStyle(fontSize: 18, fontWeight: FontWeight.bold)), child: Text("电机参数", style: TextStyle(fontSize: 16, fontWeight: FontWeight.bold)), // 标题字体缩小
), ),
Table( Table(
border: TableBorder.all(color: const Color(0xFFE5E5E5)), border: TableBorder.all(color: const Color(0xFFE5E5E5)),
@@ -631,7 +647,7 @@ class _RunningStatusPageState extends State<RunningStatusPage> {
children: [ children: [
const Padding( const Padding(
padding: EdgeInsets.symmetric(vertical: 12), padding: EdgeInsets.symmetric(vertical: 12),
child: Text("其他参数", style: TextStyle(fontSize: 18, fontWeight: FontWeight.bold)), child: Text("其他参数", style: TextStyle(fontSize: 16, fontWeight: FontWeight.bold)), // 标题字体缩小
), ),
Table( Table(
border: TableBorder.all(color: const Color(0xFFE5E5E5)), border: TableBorder.all(color: const Color(0xFFE5E5E5)),
@@ -831,7 +847,7 @@ class _RunningStatusPageState extends State<RunningStatusPage> {
} }
} }
// ====================== 带动画的圆形仪表盘 Widget ====================== // ====================== 带动画的圆形仪表盘 Widget(尺寸缩小:原220→180,绘制尺寸280→240) ======================
class AnimatedCircularGauge extends StatefulWidget { class AnimatedCircularGauge extends StatefulWidget {
final double value; final double value;
final double maxValue; final double maxValue;
@@ -852,64 +868,56 @@ class _AnimatedCircularGaugeState extends State<AnimatedCircularGauge> with Sing
@override @override
void initState() { void initState() {
super.initState(); super.initState();
// 初始化动画控制器,时长800ms(可调整)
_animationController = AnimationController(vsync: this, duration: const Duration(milliseconds: 800)); _animationController = AnimationController(vsync: this, duration: const Duration(milliseconds: 800));
// 初始值动画
_previousValue = widget.value; _previousValue = widget.value;
_valueAnimation = _valueAnimation = Tween<double>(begin: 0, end: widget.value).animate(CurvedAnimation(parent: _animationController, curve: Curves.easeOutCubic))
Tween<double>(begin: 0, end: widget.value).animate( ..addListener(() {
CurvedAnimation( setState(() {});
parent: _animationController, });
curve: Curves.easeOutCubic, // 缓出曲线,动画更自然
),
)..addListener(() {
setState(() {}); // 动画值变化时刷新UI
});
_animationController.forward(); _animationController.forward();
} }
@override @override
void didUpdateWidget(covariant AnimatedCircularGauge oldWidget) { void didUpdateWidget(covariant AnimatedCircularGauge oldWidget) {
super.didUpdateWidget(oldWidget); super.didUpdateWidget(oldWidget);
// 数值变化时触发新的动画
if (widget.value != oldWidget.value) { if (widget.value != oldWidget.value) {
_previousValue = _valueAnimation.value; // 从当前值开始动画 _previousValue = _valueAnimation.value;
_valueAnimation = _valueAnimation =
Tween<double>(begin: _previousValue, end: widget.value).animate(CurvedAnimation(parent: _animationController, curve: Curves.easeOutCubic)) Tween<double>(begin: _previousValue, end: widget.value).animate(CurvedAnimation(parent: _animationController, curve: Curves.easeOutCubic))
..addListener(() { ..addListener(() {
setState(() {}); setState(() {});
}); });
_animationController.reset(); // 重置动画 _animationController.reset();
_animationController.forward(); // 执行动画 _animationController.forward();
} }
} }
@override @override
void dispose() { void dispose() {
_animationController.dispose(); // 释放动画控制器 _animationController.dispose();
super.dispose(); super.dispose();
} }
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
return SizedBox( return SizedBox(
height: 220, height: 180, // 原220→180
child: Stack( child: Stack(
alignment: Alignment.center, alignment: Alignment.center,
children: [ children: [
CustomPaint( CustomPaint(
painter: CircularGaugePainter( painter: CircularGaugePainter(value: _valueAnimation.value, maxValue: widget.maxValue, majorTicks: widget.majorTicks),
value: _valueAnimation.value, // 使用动画插值后的数值 size: const Size(240, 240), // 原280→240
maxValue: widget.maxValue,
majorTicks: widget.majorTicks,
),
size: const Size(280, 280),
), ),
Positioned( Positioned(
bottom: 20, bottom: 20,
child: Text( child: Text(
"${_valueAnimation.value.toStringAsFixed(2)} ${widget.unit}", "${_valueAnimation.value.toStringAsFixed(2)} ${widget.unit}",
style: const TextStyle(fontSize: 20, fontWeight: FontWeight.w500, color: Colors.grey), style: const TextStyle(
fontSize: 18, // 原20→18
fontWeight: FontWeight.w500,
color: Colors.grey,
),
), ),
), ),
], ],
@@ -918,22 +926,20 @@ class _AnimatedCircularGaugeState extends State<AnimatedCircularGauge> with Sing
} }
} }
// ====================== 圆形仪表盘绘制器(带动画支持) ====================== // ====================== 圆形仪表盘绘制器(尺寸同步缩小) ======================
// ====================== 圆形仪表盘绘制器(修复widget关键字错误) ======================
class CircularGaugePainter extends CustomPainter { class CircularGaugePainter extends CustomPainter {
final double value; final double value;
final double maxValue; final double maxValue;
final List<double> majorTicks; // 直接通过成员变量获取,而非widget final List<double> majorTicks;
CircularGaugePainter({required this.value, required this.maxValue, required this.majorTicks}); CircularGaugePainter({required this.value, required this.maxValue, required this.majorTicks});
@override @override
void paint(Canvas canvas, Size size) { void paint(Canvas canvas, Size size) {
// 画布中心和半径(留出边距)
final center = Offset(size.width / 2, size.height / 2); final center = Offset(size.width / 2, size.height / 2);
final radius = size.width / 2 - 20; final radius = size.width / 2 - 20;
// 1. 绘制背景圆弧(浅蓝底色,270°范围:从135°到405°,对应左下到右下的半圆) // 背景圆弧
final bgArcPaint = Paint() final bgArcPaint = Paint()
..color = const Color(0xFFE3F2FD) ..color = const Color(0xFFE3F2FD)
..style = PaintingStyle.stroke ..style = PaintingStyle.stroke
@@ -942,8 +948,8 @@ class CircularGaugePainter extends CustomPainter {
final bgRect = Rect.fromCircle(center: center, radius: radius); final bgRect = Rect.fromCircle(center: center, radius: radius);
canvas.drawArc(bgRect, 135 * math.pi / 180, 270 * math.pi / 180, false, bgArcPaint); canvas.drawArc(bgRect, 135 * math.pi / 180, 270 * math.pi / 180, false, bgArcPaint);
// 2. 绘制进度圆弧(浅蓝色,根据动画后的数值比例绘制) // 进度圆弧
final progressRatio = math.min(value / maxValue, 1.0); // 限制最大为1 final progressRatio = math.min(value / maxValue, 1.0);
final progressPaint = Paint() final progressPaint = Paint()
..color = const Color(0xFFBBDEFB) ..color = const Color(0xFFBBDEFB)
..style = PaintingStyle.stroke ..style = PaintingStyle.stroke
@@ -951,42 +957,38 @@ class CircularGaugePainter extends CustomPainter {
..strokeCap = StrokeCap.round; ..strokeCap = StrokeCap.round;
canvas.drawArc(bgRect, 135 * math.pi / 180, 270 * math.pi / 180 * progressRatio, false, progressPaint); canvas.drawArc(bgRect, 135 * math.pi / 180, 270 * math.pi / 180 * progressRatio, false, progressPaint);
// 3. 绘制刻度(主刻度+副刻度) // 刻度
final tickPaint = Paint()..color = Colors.grey.shade400; final tickPaint = Paint()..color = Colors.grey.shade400;
final majorTickPaint = Paint() final majorTickPaint = Paint()
..color = Colors.grey.shade600 ..color = Colors.grey.shade600
..strokeWidth = 2; ..strokeWidth = 2;
// 总刻度数(每1°一个副刻度,每10°一个主刻度)
const totalTicks = 270; const totalTicks = 270;
for (int i = 0; i < totalTicks; i++) { for (int i = 0; i < totalTicks; i++) {
// 计算刻度角度(从135°开始,到405°结束)
final angle = 135 * math.pi / 180 + (i * math.pi * 270 / 180) / totalTicks; final angle = 135 * math.pi / 180 + (i * math.pi * 270 / 180) / totalTicks;
// 刻度起始和结束位置
final tickStart = center + Offset(math.cos(angle), math.sin(angle)) * (radius - 4); final tickStart = center + Offset(math.cos(angle), math.sin(angle)) * (radius - 4);
final tickEnd = center + Offset(math.cos(angle), math.sin(angle)) * radius; final tickEnd = center + Offset(math.cos(angle), math.sin(angle)) * radius;
// 副刻度(短刻度)
tickPaint.strokeWidth = 1; tickPaint.strokeWidth = 1;
canvas.drawLine(tickStart, tickEnd, tickPaint); canvas.drawLine(tickStart, tickEnd, tickPaint);
// 主刻度(长刻度,匹配majorTicks的数值)
final tickValue = (i / totalTicks) * maxValue; final tickValue = (i / totalTicks) * maxValue;
// 修复:直接使用this.majorTicks(去掉widget.)
if (majorTicks.any((tick) => (tickValue - tick).abs() < 0.1)) { if (majorTicks.any((tick) => (tickValue - tick).abs() < 0.1)) {
final majorTickStart = center + Offset(math.cos(angle), math.sin(angle)) * (radius - 8); final majorTickStart = center + Offset(math.cos(angle), math.sin(angle)) * (radius - 8);
canvas.drawLine(majorTickStart, tickEnd, majorTickPaint); canvas.drawLine(majorTickStart, tickEnd, majorTickPaint);
// 绘制主刻度数值
final textValue = tickValue.toStringAsFixed(0); final textValue = tickValue.toStringAsFixed(0);
final textPainter = TextPainter( final textPainter = TextPainter(
text: TextSpan( text: TextSpan(
text: textValue, text: textValue,
style: const TextStyle(fontSize: 14, color: Colors.grey, fontWeight: FontWeight.w500), style: const TextStyle(
fontSize: 12, // 原14→12,刻度数值缩小
color: Colors.grey,
fontWeight: FontWeight.w500,
),
), ),
textDirection: TextDirection.ltr, textDirection: TextDirection.ltr,
)..layout(); )..layout();
// 数值位置(在主刻度外侧)
final textOffset = center + Offset(math.cos(angle), math.sin(angle)) * (radius - 20); final textOffset = center + Offset(math.cos(angle), math.sin(angle)) * (radius - 20);
final textX = textOffset.dx - textPainter.width / 2; final textX = textOffset.dx - textPainter.width / 2;
final textY = textOffset.dy - textPainter.height / 2; final textY = textOffset.dy - textPainter.height / 2;
@@ -994,38 +996,30 @@ class CircularGaugePainter extends CustomPainter {
} }
} }
// 4. 计算指针角度(使用动画后的数值) // 指针角度
final needleAngle = 135 * math.pi / 180 + 270 * math.pi / 180 * progressRatio; final needleAngle = 135 * math.pi / 180 + 270 * math.pi / 180 * progressRatio;
// 5. 绘制三角形指针(细尖角指向刻度,宽底边在中心) // 三角形指针
final needlePaint = Paint() final needlePaint = Paint()
..color = Colors.grey.shade600 ..color = Colors.grey.shade600
..style = PaintingStyle.fill; ..style = PaintingStyle.fill;
// 调整三角形顶点和底边位置:细尖角指向刻度,宽底边在中心
final needlePath = Path() final needlePath = Path()
// 1. 移动画笔到指针的细尖角(指向刻度的一端)
..moveTo(center.dx + (radius - 10) * math.cos(needleAngle), center.dy + (radius - 10) * math.sin(needleAngle)) ..moveTo(center.dx + (radius - 10) * math.cos(needleAngle), center.dy + (radius - 10) * math.sin(needleAngle))
// 2. 绘制第一条边:从尖角到中心左侧的底边端点
..lineTo( ..lineTo(
center.dx + 8 * math.cos(needleAngle - math.pi / 2), // 中心左侧8px center.dx + 6 * math.cos(needleAngle - math.pi / 2), // 原8→6,指针底边宽度缩小
center.dy + 8 * math.sin(needleAngle - math.pi / 2), center.dy + 6 * math.sin(needleAngle - math.pi / 2),
) )
// 3. 绘制第二条边:从中心左侧端点到中心右侧的底边端点 ..lineTo(center.dx + 6 * math.cos(needleAngle + math.pi / 2), center.dy + 6 * math.sin(needleAngle + math.pi / 2))
..lineTo(
center.dx + 8 * math.cos(needleAngle + math.pi / 2), // 中心右侧8px
center.dy + 8 * math.sin(needleAngle + math.pi / 2),
)
// 4. 闭合路径:回到尖角,形成三角形
..close(); ..close();
canvas.drawPath(needlePath, needlePaint); canvas.drawPath(needlePath, needlePaint);
// 6. 绘制指针中心圆点(增强视觉效果) // 中心圆点
final centerDotPaint = Paint() final centerDotPaint = Paint()
..color = Colors.white ..color = Colors.white
..style = PaintingStyle.fill; ..style = PaintingStyle.fill;
canvas.drawCircle(center, 4, centerDotPaint); canvas.drawCircle(center, 3, centerDotPaint); // 原4→3,圆点缩小
} }
@override @override
@@ -1034,7 +1028,7 @@ class CircularGaugePainter extends CustomPainter {
} }
} }
// ====================== 罗盘式航向角仪表盘绘制器 ====================== // ====================== 罗盘式航向角仪表盘绘制器(尺寸缩小) ======================
class CompassGaugePainter extends CustomPainter { class CompassGaugePainter extends CustomPainter {
final double yaw; final double yaw;
@@ -1049,8 +1043,7 @@ class CompassGaugePainter extends CustomPainter {
final outerPaint = Paint() final outerPaint = Paint()
..color = const Color(0xFF1677FF) ..color = const Color(0xFF1677FF)
..style = PaintingStyle.stroke ..style = PaintingStyle.stroke
..strokeWidth = 12; ..strokeWidth = 10; // 原12→10,外圈宽度缩小
canvas.drawCircle(center, radius, outerPaint); canvas.drawCircle(center, radius, outerPaint);
// 刻度 // 刻度
@@ -1066,13 +1059,11 @@ class CompassGaugePainter extends CustomPainter {
final y2 = center.dy + radius * math.sin(angle); final y2 = center.dy + radius * math.sin(angle);
canvas.drawLine(Offset(x1, y1), Offset(x2, y2), tickPaint); canvas.drawLine(Offset(x1, y1), Offset(x2, y2), tickPaint);
// 主刻度(45°间隔)
if (i % 9 == 0) { if (i % 9 == 0) {
final x3 = center.dx + (radius - 20) * math.cos(angle); final x3 = center.dx + (radius - 20) * math.cos(angle);
final y3 = center.dy + (radius - 20) * math.sin(angle); final y3 = center.dy + (radius - 20) * math.sin(angle);
canvas.drawLine(Offset(x1, y1), Offset(x3, y3), tickPaint..strokeWidth = 4); canvas.drawLine(Offset(x1, y1), Offset(x3, y3), tickPaint..strokeWidth = 4);
// 方向文字
final direction = i == 0 final direction = i == 0
? "N" ? "N"
: i == 9 : i == 9
@@ -1086,7 +1077,7 @@ class CompassGaugePainter extends CustomPainter {
final textPainter = TextPainter( final textPainter = TextPainter(
text: TextSpan( text: TextSpan(
text: direction, text: direction,
style: const TextStyle(fontSize: 18, fontWeight: FontWeight.bold, color: Colors.black), style: const TextStyle(fontSize: 16, fontWeight: FontWeight.bold, color: Colors.black), // 原18→16
), ),
textDirection: TextDirection.ltr, textDirection: TextDirection.ltr,
)..layout(); )..layout();
@@ -1095,12 +1086,11 @@ class CompassGaugePainter extends CustomPainter {
textPainter.paint(canvas, Offset(textX, textY)); textPainter.paint(canvas, Offset(textX, textY));
} }
// 角度值
final angleValue = (i * 10).toString(); final angleValue = (i * 10).toString();
final textPainter = TextPainter( final textPainter = TextPainter(
text: TextSpan( text: TextSpan(
text: angleValue, text: angleValue,
style: const TextStyle(fontSize: 14, color: Colors.black54), style: const TextStyle(fontSize: 12, color: Colors.black54), // 原14→12
), ),
textDirection: TextDirection.ltr, textDirection: TextDirection.ltr,
)..layout(); )..layout();