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

Flutter中为LinearPercentIndicator添加边框无效的问题求助

解决LinearPercentIndicator添加边框无效的问题

你当前的写法里,Container的边框被LinearPercentIndicator的背景色遮挡了——因为进度条撑满了Container的内部空间,和边框完全贴合,导致看不到边框。以下两种方法可以解决:

方法一:给Container添加内边距(推荐)

通过给Container设置padding,让进度条和边框之间留出空隙,边框就会显示出来:

Container(
  width: Get.width * 0.8,
  padding: EdgeInsets.all(2), // 添加入边距,数值可根据需求调整
  decoration: BoxDecoration(
    border: Border.all(color: Colors.black),
  ),
  child: LinearPercentIndicator(
    lineHeight: Get.height * 0.04,
    percent: percent / 100,
    backgroundColor: Colors.grey,
    progressColor: AppColors.orangeTextColor,
    center: Text(
      "${percent}%",
      style: TextStyle(
        fontSize: 12.0,
        fontWeight: FontWeight.w600,
        color: Colors.black,
      ),
    ),
  ),
)

方法二:用Stack分层实现

如果不需要空隙,想让进度条和边框紧贴,可使用Stack将边框容器和进度条分层,让边框在底层,进度条在上层并缩小尺寸避免遮挡:

Stack(
  alignment: Alignment.center,
  children: [
    // 底层边框容器
    Container(
      width: Get.width * 0.8,
      height: Get.height * 0.04,
      decoration: BoxDecoration(
        border: Border.all(color: Colors.black),
      ),
    ),
    // 上层进度条,尺寸略小于边框容器
    LinearPercentIndicator(
      width: Get.width * 0.8 - 4, // 减去边框宽度的2倍(左右各2)
      lineHeight: Get.height * 0.04 - 4, // 上下各留出2的空隙
      percent: percent / 100,
      backgroundColor: Colors.grey,
      progressColor: AppColors.orangeTextColor,
      center: Text(
        "${percent}%",
        style: TextStyle(
          fontSize: 12.0,
          fontWeight: FontWeight.w600,
          color: Colors.black,
        ),
      ),
    ),
  ],
)

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.19 02:15:41