首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何控制RatingBar?

如何控制RatingBar?
EN

Stack Overflow用户
提问于 2021-04-29 00:28:11
回答 1查看 25关注 0票数 0

我正在尝试控制一个RatingBar,但我在这方面有点困难。问题是评级栏没有更新。下面是我的代码:

代码语言:javascript
复制
 Widget _buildBody(videoid) {
    double rating = 3;
    return Container(
      color: Colors.red,
      child: Stack(
        children: [
          Padding(
            padding: const EdgeInsets.fromLTRB(3, 7, 0, 0),
            child: Align(
              alignment: Alignment.topLeft,
              child: RatingBarIndicator(
                rating: rating,
                itemBuilder: (context, index) => InkWell(
                  onTap: (){
                    setState(() {
                      rating=index.toDouble()+1;
                    });
               },
                                  child: Icon(
                    Icons.star,
                    color: Colors.amber,
                  ),
                ),
                itemCount: 5,
                itemSize: 31.0,
                direction: Axis.horizontal,
              ),
            ),
          ),
          Align(
            alignment: Alignment.topRight,
            child: TextButton(
              onPressed: () {
                letuservoting = true;

                setState(() {
                  rating = 0;
                  israting = false;
                });

                dislike(idovvideo, _rating);
              },
              child: Text(
                "Clear",
                style: TextStyle(color: Colors.white, fontSize: 20),
              ),
            ),
          ),
        ],
      ),
    );
  }

我将等级设置为3,然后在明文按钮上我想将其设置为0,然后当用户点击2时,它应该显示2星。目前,无论我做什么,它都只显示这3颗星。我怎么才能修复它呢?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-04-29 01:03:33

最好不要在小部件内部赋值变量,因为它会不断重建并重新赋值变量,在你的例子中,rating变量会一直重新赋值为3。试着把它放在你的类中,在build方法之外。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/67304203

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档