我正在try 开发一个聊天应用程序,用户可以通过双击消息来"喜欢"消息.我希望心形图标出现在卡片的右上角.作为参考,我有点想知道iMessage的react 是什么样子的.

The problem is that the corner of the icon is being clipped by the container constraints and I can't figure out how to not make it look like this: enter image description here

class ChatBubble extends StatefulWidget {
  final String? messageText;
  final DateTime? timeStamp;

  // I want to use this as a way to identify individual messages
  final String? messageId;
  final bool? isSelf;
  bool? isLiked;
  ChatBubble(
      {super.key,
      required this.messageText,
      required this.isSelf,
      this.isLiked = false,
      this.timeStamp,
      this.messageId});

  @override
  State<ChatBubble> createState() => _ChatBubbleState();
}

class _ChatBubbleState extends State<ChatBubble> {
  @override
  Widget build(BuildContext context) {
    return Align(
      alignment: !widget.isSelf! ? Alignment.centerLeft : Alignment.centerRight,
      child: Padding(
        padding: !widget.isSelf!
            ? EdgeInsets.only(left: AppPadding.globalSidePadding.scale(context))
            : EdgeInsets.only(
                right: AppPadding.globalSidePadding.scale(context)),
        child: GestureDetector(
          onDoubleTap: () {
            if (widget.isSelf == false) {
              setState(() {
                widget.isLiked ??= false;
                widget.isLiked = !widget.isLiked!;
              });
            }
          },
          child: LayoutBuilder(builder: (context, constraints) {
            return Padding(
              padding: EdgeInsets.only(
                  top: AppPadding.transactionCardTopPadding.scale(context)),
              child: Stack(
                children: [
                  Card(
                    color: !widget.isSelf! ? Colors.white : AppColors.pink,
                    shape: const RoundedRectangleBorder(
                        borderRadius: BorderRadius.all(Radius.circular(
                            AppNums.globalElevatedButtonBorderRadius))),
                    child: Container(
                      constraints: BoxConstraints(
                          maxWidth: constraints.maxWidth * 0.7),
                      child: Padding(
                        padding: EdgeInsets.symmetric(
                            vertical: AppPadding.cardTopPadding
                                
                            horizontal: AppPadding.chatMessageCardSidePadding
                                
                        child: Text(widget.messageText!,
                            style: Fonts.notoSansSerif
                                .copyWith(
                                    fontSize: 10,
                                    fontWeight: FontWeight.normal,
                                    color: !widget.isSelf!
                                        ? Colors.black
                                        : Colors.white)
                                ),
                      ),
                    ),
                  ),
                  if (widget.isLiked == true)
                    Positioned(
                        top: -5,
                        right: -5,
                        child: LikedMessage(
                            reactionCameFromSelf:
                                widget.isLiked! && !widget.isSelf!)),
                ],
              ),
            );
          }),
        ),
      ),
    );
  }
}

class LikedMessage extends StatelessWidget {
  final bool? reactionCameFromSelf;
  const LikedMessage({super.key, required this.reactionCameFromSelf});

  @override
  Widget build(BuildContext context) {
    return CircleAvatar(
        radius: 20,
        backgroundColor: reactionCameFromSelf! ? Colors.grey : AppColors.pink,
        foregroundColor: reactionCameFromSelf! ? AppColors.pink : Colors.white,
        child: const Icon(Icons.favorite));
  }
}

这里的棘手之处在于,每个气泡都被包裹在Align中,以控制卡片是显示在屏幕的左侧还是右侧,这取决于发送消息的人.

我try 了使用Stack小工具的不同组合,但再次try ,因为从技术上讲,每张卡片的底层小工具跨越整个屏幕宽度,心形图标将出现在远离卡片的地方.

我在这方面哪里错了?

推荐答案

StackclipBehavior属性设置为Clip.none.

Stack(
  clipBehavior: Clip.none,
  // ...
)

enter image description here

Flutter相关问答推荐

在Flutter 中压缩图片返回空

Flutter 中的多页表现

无效参数(S):隔离消息中的非法参数:try 旋转图像时对象不可发送

Flutter /dart 列表.第一个子类列表中的Where()错误

我怎样才能在Ffltter中显示html布局链接?

如何在Flutter 安全存储器中存储对象值?

TextField 中的富文本并获取 RenderParagraph

即使小部件比Flutter 中的屏幕更宽,也始终显示滚动条

自定义绘画未显示 - Flutter

Flutter firebase_auth 和 firebase_core 依赖错误

通过在父窗口小部件功能上设置状态来浏览屏幕

如何停止折线图的抖动

如何在 Flutter 中的 BackdropFilter.blur 中制作一个 100x100 的清晰孔

我想在一个屏幕上有两个相同区域的列表视图,我该如何设置?

如何从 showModalBottomSheet 中的 topRight 和 topLeft 移除平边

Flutter - 使用 pin 进行身份验证

在 FAB 上添加表格小部件单击 FLUTTER

阴影效果只出现在一张卡片的左边 在Flutter 中?

在 Flutter 应用程序中全局使用 assets 文件夹

这在Flutter 的 pubspec 文件中是什么意思?