You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何用syncfusion_flutter_gauge实现预期自定义Flutter仪表盘?

自定义Syncfusion Flutter仪表盘实现问题

我尝试用syncfusion_flutter_gauge包创建自定义仪表盘,但最终效果和预期不符。以下是我的代码、已实现效果和期望效果,请求帮忙调整代码实现目标样式。

现有代码

SfRadialGauge(
  axes: <RadialAxis>[
    RadialAxis(
      minimum: 0,
      maximum: 1000,
      showTicks: false,
      showLabels: false,
      axisLineStyle: const AxisLineStyle(
        dashArray: [75, 6],
        thickness: 5,
        gradient: SweepGradient(
          colors: [
            AppColors.redColor,
            AppColors.orangeColor,
            AppColors.yellowColor,
            AppColors.greenColor,
            AppColors.darkGreen,
          ],
        ),
      ),
      canScaleToFit: true,
      startAngle: 170,
      endAngle: 10,
      ranges: <GaugeRange>[
        GaugeRange(
          startValue: 0,
          endValue: 679,
          gradient: SweepGradient(
            colors: [AppColors.redColor.withOpacity(0.3)],
          ),
          startWidth: 25,
          endWidth: 25,
        ),
        GaugeRange(
          startValue: 680,
          startWidth: 25,
          endWidth: 25,
          endValue: 718,
          gradient: SweepGradient(
            colors: [AppColors.orangeColor.withOpacity(0.3)],
          ),
        ),
        GaugeRange(
          startValue: 719,
          startWidth: 25,
          endWidth: 25,
          gradient: SweepGradient(
            colors: [AppColors.yellowColor.withOpacity(0.3)],
          ),
          endValue: 759,
        ),
        GaugeRange(
          startValue: 760,
          startWidth: 25,
          endWidth: 25,
          endValue: 900,
          gradient: SweepGradient(
            colors: [AppColors.greenColor.withOpacity(0.3)],
          ),
        ),
        GaugeRange(
          startValue: 901,
          endValue: 1000,
          startWidth: 25,
          endWidth: 25,
          gradient: SweepGradient(
            colors: [AppColors.darkGreen.withOpacity(0.3)],
          ),
        ),
      ],
      pointers: const <GaugePointer>[
        MarkerPointer(
          value: 90,
          markerType: MarkerType.triangle,
          markerOffset: 17,
          color: AppColors.darkTextColor,
        ),
      ],
      annotations: <GaugeAnnotation>[
        GaugeAnnotation(
          widget: Column(
            mainAxisAlignment: MainAxisAlignment.center,
            children: [
              Text(
                "650",
                style: const TextStyle(
                  fontSize: 28,
                  fontWeight: FontWeight.bold,
                  color: AppColors.darkTextColor,
                ),
              ),
              Row(
                mainAxisAlignment: MainAxisAlignment.center,
                children: [
                  Text(
                    "Excellent",
                    style: AppConstants.semiBold18TextStyle.copyWith(
                      fontSize: 10,
                      color: AppColors.primaryColor,
                    ),
                  ),
                  const SizedBox(width: 4),
                  const Icon(
                    Icons.info_outline,
                    color: AppColors.primaryColor,
                    size: 14,
                  )
                ],
              )
            ],
          ),
          angle: 90,
          positionFactor: 0,
        )
      ],
    ),
  ],
)

已实现效果

已实现效果

期望效果

期望效果


修改后的代码及说明

对比期望效果,调整后的代码如下:

SfRadialGauge(
  axes: <RadialAxis>[
    RadialAxis(
      minimum: 0,
      maximum: 1000,
      showTicks: false,
      showLabels: false,
      // 隐藏原有虚线轴线,改用小范围模拟顶部刻度
      axisLineStyle: const AxisLineStyle(thickness: 0),
      canScaleToFit: true,
      startAngle: 170,
      endAngle: 10,
      ranges: <GaugeRange>[
        // 背景半透明范围
        GaugeRange(
          startValue: 0,
          endValue: 679,
          color: AppColors.redColor.withOpacity(0.3),
          startWidth: 22,
          endWidth: 22,
        ),
        GaugeRange(
          startValue: 680,
          endValue: 718,
          color: AppColors.orangeColor.withOpacity(0.3),
          startWidth: 22,
          endWidth: 22,
        ),
        GaugeRange(
          startValue: 719,
          endValue: 759,
          color: AppColors.yellowColor.withOpacity(0.3),
          startWidth: 22,
          endWidth: 22,
        ),
        GaugeRange(
          startValue: 760,
          endValue: 900,
          color: AppColors.greenColor.withOpacity(0.3),
          startWidth: 22,
          endWidth: 22,
        ),
        GaugeRange(
          startValue: 901,
          endValue: 1000,
          color: AppColors.darkGreen.withOpacity(0.3),
          startWidth: 22,
          endWidth: 22,
        ),
        // 顶部彩色短刻度
        GaugeRange(
          startValue: 0,
          endValue: 679,
          color: AppColors.redColor,
          startWidth: 3,
          endWidth: 3,
          offset: 12,
        ),
        GaugeRange(
          startValue: 680,
          endValue: 718,
          color: AppColors.orangeColor,
          startWidth: 3,
          endWidth: 3,
          offset: 12,
        ),
        GaugeRange(
          startValue: 719,
          endValue: 759,
          color: AppColors.yellowColor,
          startWidth: 3,
          endWidth: 3,
          offset: 12,
        ),
        GaugeRange(
          startValue: 760,
          endValue: 900,
          color: AppColors.greenColor,
          startWidth: 3,
          endWidth: 3,
          offset: 12,
        ),
        GaugeRange(
          startValue: 901,
          endValue: 1000,
          color: AppColors.darkGreen,
          startWidth: 3,
          endWidth: 3,
          offset: 12,
        ),
      ],
      pointers: const <GaugePointer>[
        MarkerPointer(
          value: 650, // 修正指针值与中心数值一致
          markerType: MarkerType.triangle,
          markerOffset: 15,
          color: AppColors.darkTextColor,
          markerHeight: 12,
          markerWidth: 12,
        ),
      ],
      annotations: <GaugeAnnotation>[
        GaugeAnnotation(
          widget: Column(
            mainAxisAlignment: MainAxisAlignment.center,
            children: [
              const Text(
                "650",
                style: TextStyle(
                  fontSize: 28,
                  fontWeight: FontWeight.bold,
                  color: AppColors.darkTextColor,
                ),
              ),
              Row(
                mainAxisAlignment: MainAxisAlignment.center,
                children: [
                  Text(
                    "Excellent",
                    style: AppConstants.semiBold18TextStyle.copyWith(
                      fontSize: 10,
                      color: AppColors.primaryColor,
                    ),
                  ),
                  const SizedBox(width: 4),
                  const Icon(
                    Icons.info_outline,
                    color: AppColors.primaryColor,
                    size: 14,
                  )
                ],
              )
            ],
          ),
          angle: 90,
          positionFactor: 0,
        )
      ],
    ),
  ],
)

关键修改点:

  1. 顶部刻度实现:隐藏原轴线,添加5个小宽度的GaugeRange并向上偏移,模拟期望的彩色短刻度
  2. 指针修正:将指针值改为650,与中心显示数值匹配,调整偏移量让指针对齐刻度
  3. 背景范围优化:缩小背景范围宽度,简化单颜色范围的样式写法
  4. 细节调整:微调指针的大小,让整体视觉更贴近期望效果

内容的提问来源于stack exchange,提问作者Ahmed Ashraf

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.18 19:35:52