为 like_button flutter 添加敲击声

Adding tap sound to like_button flutter

我添加了 LikeButton 包:https://pub.dev/packages/like_button

在 RawMaterialButton() 内部,但 onPressed: () {} 函数被忽略。

起作用的是来自 LikeButton 的 onTap: 行为,但这不会像 RawMaterialButton() 通常那样产生“反馈敲击声”。

我想要 RawMaterialButton() 在点击 LikeButton() 时使用 onPressed() 通常产生的“反馈敲击声”

代码是这样的:

RawMaterialButton(
            elevation: 5,
            onPressed: () {
              // this is ignored
               print(isLiked);
            },
            shape: CircleBorder(),
            fillColor: Colors.white,
            constraints: BoxConstraints.tightFor(
              width: MediaQuery.of(context).size.width * 0.17,
              height: MediaQuery.of(context).size.width * 0.17,
            ),
            child: LikeButton(
            onTap: 
                // doesn't produce feedback tap sound
                (isLiked) {
                setState(() {
                  isLiked = !isLiked;
                });

                print(isLiked);
                return Future.value(!isLiked);
              }, 
              size: MediaQuery.of(context).size.width * 0.17,
              isLiked: isLiked,
              likeBuilder: (bool isLiked) {
                return Builder(builder: (context) {
                  if (isLiked) {
                    return  Icon(
                        Icons.favorite_rounded,
                        color: Colors.red,
                        size: 4.25 *
                            MediaQuery.of(context).size.height *
                            0.01,
                     );
                  } else {
                    return Icon(
                      Icons.favorite_outline_rounded,
                      color: Colors.grey,
                      size:
                          4.25 * MediaQuery.of(context).size.height * 0.01,
                    );
                  }
                });
              },

              
            ), 

          ),

尝试用 Inkwell 包裹你的按钮,像这样空点击功能

child: Inkwell(onTap:(){},
               child:LikeButton(

如果这不起作用或 Inkwell 阻止操作传递给子按钮

你还有选择权

1-访问库的源代码并将负责水龙头的小部件更改为Inkwell

2- 使用 this package and more about how to use this package in the docs and post

在 LikeButton 的 onTap 中播放自定义声音

我通过添加解决了这个问题:

Feedback.forTap(context);

进入 LikeButton(onTap:

需要这样做的是 LikeButton 处理默认情况下不产生反馈声音的 GestureDetector()。

参考:https://api.flutter.dev/flutter/material/Feedback-class.html