簡體   English   中英

如何在 flutter 中創建類似使用列表視圖的類似按鈕?

[英]How to create a like button like using listview in flutter?

我正在 flutter 中創建一個博客應用程序,並堅持使用類似按鈕的功能。 我想做喜歡(如果用戶按下 flutter 列表視圖中的心形按鈕,它將變為紅色,當再次按下時它將 go 處於原始狀態)但是當我單擊“喜歡”按鈕時,所有按鈕的顏色都會改變列表顯示。 這是我的代碼片段。

body: ListView.builder(
      itemCount: 10,
      physics: BouncingScrollPhysics(),
      itemBuilder: (context, index) {
        return Column(
          children: <Widget>[
            Padding(
              padding: const EdgeInsets.fromLTRB(3.0, 3.0, 3.0, 0.0),
              child: singleItem(index),
            ),
            Divider(height: 0.5, color: Constants.greyColor),
          ],
        );
      },
    ),
Widget singleItem(int index) {
return ListTile(
  leading: CircleAvatar(
    radius: 25.0,
    foregroundColor: Constants.orangeColor,
    backgroundColor: Constants.orangeColor,
    backgroundImage:
        NetworkImage("https://png.pngtree.com/svg/20161113/ef1b24279e.png"),
  ),
  trailing:
      Text("Jul23", style: TextStyle(color: Constants.ligthGreyColor)),
  title: Text("Jea @jeaBooty.jul23",
      style: TextStyle(
          color: Constants.slightBlackColor,
          fontSize: 16.0,
          fontWeight: FontWeight.w600)),
  subtitle: Container(
    margin: const EdgeInsets.symmetric(vertical: 8.0),
    child: Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      mainAxisAlignment: MainAxisAlignment.center,
      children: <Widget>[
        Text("Happy Birthday, hope you will have a wonderful Day"),
        Container(
          child: Row(
            crossAxisAlignment: CrossAxisAlignment.center,
            children: <Widget>[
              IconButton(
                onPressed: () {},
                icon: Icon(Constants.commentIcon,
                    color: Constants.ligthGreyColor, size: 15.0),
              ),
              SizedBox(width: 50.0),
              IconButton(
                onPressed: () {
                  if (!_isLike) {
                    setState(() {
                      _isLike = true;
                      color = Constants.orangeColor;
                    });
                  } else {
                    setState(() {
                      _isLike = false;
                      color = Constants.ligthGreyColor;
                    });
                  }
                },
                icon: Icon(Constants.crownIcon, color: color, size: 15.0),
              ),
            ],
          ),
        ),
      ],
    ),
  ),
);

}

我想正如評論中提到的 George Herbet 一樣,似乎所有索引都使用了相同的變量_isLikecolor

也許您可以將isLike的isLike保留在List中,即List<bool> _likes ,然后使用索引訪問當前的,例如

...
IconButton(
   onPressed: () {
       setState(() {
           _likes[index] = !_likes[index];
       });
       }
   },
   icon: Icon(Constants.crownIcon, color: _likes[index] ? Constants.orangeColor : 
Constants.ligthGreyColor, size: 15.0),
),
...

定義一個 bool 類型的 List 變量,如下所示:

List<bool> _likes = List.filled(length,true);

然后使用以下參數更改 IconButton:

    IconButton(
icon: _likes[index] 
 ? Icon(Icons.favorite_border,
        color: Colors.grey,)
 : Icon(Icons.favorite,
        color: Colors.red,) ,
onPressed: () {
  setState(() {
               _likes[index] = !_likes[index];
                                              });
           },)

您可以使用 LikeButton 小部件。 這會保留索引並具有 animation。

Align(
  alignment: Alignment.topRight,
  child: LikeButton()
)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM