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

Flutter中如何创建带停靠操作按钮的自定义进度条?

环形滑块效果实现方案

针对你尝试复现环形滑块效果时遇到的问题,以下是基于sleek_circular_slider的自定义配置方案,以及备选组件的使用方法:

一、自定义sleek_circular_slider匹配目标效果

通过调整组件的外观参数,可以实现类似图片中的带刻度、分段颜色的环形滑块:

SleekCircularSlider(
  min: 0,
  max: 100,
  initialValue: 30,
  // 自定义滑块外观
  appearance: CircularSliderAppearance(
    customWidths: CustomSliderWidths(
      trackWidth: 12,
      progressBarWidth: 12,
      handlerSize: 16,
    ),
    customColors: CustomSliderColors(
      trackColor: Colors.grey.shade300,
      // 设置分段渐变颜色,匹配图片中的多色效果
      progressBarColors: [Colors.blue, Colors.lightGreen, Colors.orange],
      hideShadow: true,
    ),
    // 中间数值显示配置
    infoProperties: InfoProperties(
      mainLabelStyle: TextStyle(
        fontSize: 24,
        fontWeight: FontWeight.bold,
        color: Colors.black,
      ),
      modifier: (value) => '${value.toInt()}%',
    ),
    // 控制滑块角度范围(示例为半圆,整圆设angleRange为360)
    startAngle: 180,
    angleRange: 180,
    // 刻度线配置
    ticks: CircularSliderTicks(
      ticks: 10,
      enabled: true,
      length: 8,
      color: Colors.grey.shade600,
      thickness: 2,
    ),
  ),
  onChange: (value) {
    // 处理数值变化逻辑
  },
)

二、备选组件:circular_slider

如果sleek_circular_slider无法满足需求,可以尝试更灵活的circular_slider库:

  1. 添加依赖到pubspec.yaml:
dependencies:
  circular_slider: ^1.0.0
  1. 示例实现代码:
CircularSlider(
  100, // 最大值
  30, // 当前值
  height: 200,
  width: 200,
  primarySectors: 10, // 主刻度数量
  secondarySectors: 4, // 每个主刻度下的副刻度数量
  baseColor: Colors.grey.shade300,
  selectionColor: Colors.blue,
  handlerColor: Colors.white,
  handlerOutterRadius: 16,
  onSelectionChange: (start, end) {
    // 处理数值变更
  },
  child: Center(
    child: Text(
      '30%',
      style: TextStyle(
        fontSize: 24,
        fontWeight: FontWeight.bold,
      ),
    ),
  ),
)

关键调整要点

  • 分段颜色:通过progressBarColors设置渐变数组,模拟图片中的多色分段效果
  • 刻度显示:启用ticks参数并调整长度、厚度、数量,匹配图片刻度样式
  • 角度范围:通过startAngle和angleRange控制滑块是半圆还是整圆
  • 中间数值:利用组件自带的信息配置或自定义子组件显示当前数值

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.20 13:21:15