简体   繁体   English

如何在 Flutter 中创建一个赞按钮?

[英]How to create a Like button in Flutter?

This is my custom widget这是我的自定义小部件

Widget productlist({@required String img, @required IconButton likebtn}) {
return Card(
  shape: RoundedRectangleBorder(
    borderRadius: BorderRadius.circular(20.0),
  ),
  elevation: 10.0,
  child: InkWell(
    onTap: () {},
    child: Container(
      height: 190.0,
      width: MediaQuery.of(context).size.width * 0.43,
      child: Stack(
        children: [
          Image(
            height: 95.0,
            width: 200.0,
            fit: BoxFit.fill,
            image: AssetImage(img),
          ),
          Positioned(
            top: 89.0,
            left: 115.0,
            child: likebtn,
          ),
          Positioned(
            top: 97.0,
            left: 5.0,
            child: Text(
              "Name",
              style: TextStyle(
                fontSize: 24.0,
              ),
            ),
          ),
          Positioned(
            top: 128.0,
            left: 5.0,
            child: Text(
              "Description",
              style: TextStyle(
                fontSize: 16.0,
                fontWeight: FontWeight.w600,
                color: Colors.grey,
              ),
            ),
          ),
        ],
      ),
    ),
  ),
);

} }

Now I am calling this custom widget like this in my body..现在我在我的身体里像这样调用这个自定义小部件..

productlist(
                img: "assets/images/demo6.png",
                likebtn: IconButton(
                  onPressed: () {
                    setState(() {
                      liked = !liked;
                    });
                  },
                  icon: Icon(liked
                      ? Icons.thumb_up_alt_outlined
                      : Icons.thumb_up_alt_rounded),
                ),
              ),
              productlist(
                img: "assets/images/demo7.png",
                likebtn: IconButton(
                  onPressed: () {
                    setState(() {
                      liked = !liked;
                    });
                  },
                  icon: Icon(liked
                      ? Icons.thumb_up_alt_outlined
                      : Icons.thumb_up_alt_rounded),
                ),
              ),

I have declared a bool value so I can use it but since I am using same bool value for all so if I press like button in one widget then it works in other widget too.我已经声明了一个布尔值,所以我可以使用它,但是因为我对所有人都使用相同的布尔值,所以如果我在一个小部件中按下类似按钮,那么它也可以在其他小部件中使用。 For example if I like one photo then all photos gets liked.. Do I have to declare different bool values for all widgets differently like list maybe??例如,如果我喜欢一张照片,那么所有照片都会被喜欢。我是否必须为所有小部件声明不同的布尔值,比如列表? What if I have 100 widgets like this?如果我有 100 个这样的小部件怎么办? In that situation 100 bool values would be mess code!!在那种情况下,100 个布尔值将是混乱代码!

Make sure all your photos have a unique ID and then create a List for all the liked photos确保您所有的照片都有一个唯一的 ID ,然后为所有喜欢的照片创建一个列表

 List<String> likedPhotos = [];

Photo Icon照片图标

Icon: likedPhotos.contains(photo.id)? LikedIcon : UnLikedIcon

OnLikedPhotoTap OnLikedPhotoTap

likedPhotos.add(photo.id)

OnUnlikePhotoTap OnUnlikePhotoTap

likedPhotos.remove(photo.id)

You were right, in this case you need 100 bool values to do so, and I guess also need 100 img to store too.你是对的,在这种情况下,你需要 100 个 bool 值才能这样做,我想也需要 100 个 img 来存储。
In fact, we can do it in another way: Using List.事实上,我们可以通过另一种方式做到这一点:使用 List。
First, create a class to save data:首先,创建一个 class 来保存数据:

class Product {
  String img;
  bool liked;

  Post({this.img, this.liked});
}

Then, use a list to store all products:然后,使用列表来存储所有产品:

List<Product> list = [
  Product('assets/images/demo6.png', false),
  Product('assets/images/demo7.png', false),
  Product('assets/images/demo8.png', false)
]

Finaly, show the list, I prefer using List.map:最后,显示列表,我更喜欢使用 List.map:

list.map((item) => productlist(
                img: item.img,
                likebtn: IconButton(
                  onPressed: () {
                    setState(() {
                      item.liked = !item.liked;
                    });
                  },
                  icon: Icon(item.liked
                      ? Icons.thumb_up_alt_outlined
                      : Icons.thumb_up_alt_rounded),
                ),
              )
)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM