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

Flutter中如何设置RatingBar为不可点击状态?提交反馈后禁用评分

如何在Flutter中禁用RatingBar的点击交互

要实现默认不可点击或提交反馈后禁用RatingBar,核心是利用flutter_rating_bar包提供的ignoreGestures属性,配合状态变量动态控制交互权限。

实现步骤

  1. 添加状态变量
    在你的State类中定义一个布尔变量,用来标记评分是否已禁用:
bool _isRatingDisabled = false; // 默认不禁用,若要默认不可点击则设为true
  1. 修改RatingBar配置
    给RatingBar添加ignoreGestures属性,并根据状态变量控制onRatingUpdate的可用性:
RatingBar(
  itemSize: 35,
  initialRating: 0,
  glowColor: Colors.transparent,
  direction: Axis.horizontal,
  allowHalfRating: false,
  tapOnlyMode: false,
  itemCount: 5,
  itemPadding: const EdgeInsets.symmetric(horizontal: 0.0),
  // 关键:禁用手势交互
  ignoreGestures: _isRatingDisabled,
  ratingWidget: RatingWidget(
    full: Image.asset(img_star_rating_fill, width: 25.w, height: 25.h),
    half: Image.asset(img_star_rating_fill, width: 25.w, height: 25.h),
    empty: Image.asset(img_star_rating_empty, width: 25.w, height: 25.h),
  ),
  // 仅在未禁用时响应评分更新
  onRatingUpdate: _isRatingDisabled 
      ? null 
      : (value) {
          setState(() {
            _ratingValue = value;
            printData('Rating to consultation booking ID', _ratingValue.toString());
          });
          controller
              .callRateConsultationAPI(
                  widget.i,
                  controller.pastBookingList[widget.i].id.toString(),
                  value.toString())
              .then((_) {
            // API调用成功后,禁用评分
            setState(() {
              _isRatingDisabled = true;
            });
          });
        },
)

关键说明

  • ignoreGestures: true会完全阻止用户的点击、滑动等手势操作,RatingBar变为纯展示状态。
  • 同时将onRatingUpdate设为null是双重保险,避免意外触发回调。
  • 如果需要默认不可点击,只需把初始的_isRatingDisabled设为true即可。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.16 09:01:00