我正在试着把Chip放在CircleAvatar的右上角,类似于下图,但似乎无法移动它

enter image description here

SizedBox(
                      width: 50,
                      height: 50,
                      child: Stack(
                        children: [
                          CircleAvatar(
                            radius: 50,
                            backgroundColor: Colors.indigo,
                            child: Stack(
                              children: const [
                                Align(
                                  alignment: Alignment.topRight,
                                  child: Chip(
                                      label: Text('7'),
                                      side: BorderSide(
                                        color: Colors.white,
                                        width: 1,
                                      )),
                                )
                              ],
                            ),
                          ),
                        ],
                      ),
                    ),

The above code is producing this enter image description here

推荐答案

您不需要嵌套Stack,并且您在放置小部件时错过了继承性.并将materialTapTargetSize设置为MaterialTapTargetSize.shrinkWrap,这将移除芯片周围的默认填充.并迫使它缩小到它的大小

您的代码中存在错误:

Stack
 | CircularAvatar
   | Stack          <-- Not needed
     | Align

正确的代码:

Stack
  | CircularAvatar
  | Align           <-- Should be in same hierarchy as CircularAvatar

try 以下代码:

SizedBox(
        height: 100,
        width: 100,
        child: Stack(
          children: const [
            CircleAvatar(
              backgroundColor: Colors.amber,
              radius: 50,
              backgroundImage: NetworkImage(
                  'https://th.bing.com/th/id/OIP.KZ9jKGoLM_wXMX6aHCB6oAHaEY?pid=ImgDet&rs=1'),
            ),
            Align(
              alignment: Alignment.topRight,
              child: Chip(
                materialTapTargetSize: MaterialTapTargetSize.shrinkWrap,
                label: Text(
                  '78',
                  style: TextStyle(
                    color: Colors.white,
                  ),
                ),
                padding: EdgeInsets.zero,
                labelPadding: EdgeInsets.symmetric(horizontal: 10),
                backgroundColor: Colors.black
              ),
            )
          ],
        ),
      ),

输出:

enter image description here

Flutter相关问答推荐

SupabaseFlutter 集成问题:无法更新行

Android Studio将不会构建iOS模拟器应用程序,因为plist.info有问题

在Fighter中,我如何在窗口的顶部和底部排列Widget?

将侧边菜单和底部导航栏放在同一Flutter 应用程序中

我怎样才能go 掉这个底部导航栏上的白色背景

从图标启动器打开时,VSCode未检测到Web设备

Riverpod Provider.family 和 HookConsumerWidget 不刷新 UI

使用 Firebase Realtime 实现高效流式传输

如何解决需要一个标识符,但得到的是:.try 在:之前插入一个标识符.dart 错误

这是使用导航功能处理从一页到另一页的移动的最佳方式

Flutter ListView 在 Column 中工作但在 Row 中不工作

当我将主题更改为深色然后返回浅色模式时,Getx 会Flutter 更改主题,它不会加载我的自定义主题

在 Dart 中使用隔离时访问外部范围内的变量

如何在 Flutter 中创建 Facebook 创建帖子屏幕?

扩展磁贴尾随图标在与一个磁贴交互时更新列表中的所有内容.如何只更改展开图块的图标?

如何判断 text.contains() 是否包含多个值

NoSuchMethodError:方法 '[]' 在 null 上被调用.我的信息流中的错误

Flutter Desktop,如何获取windows用户配置文件名称?

如何做到这一点,当我们在 textified 中输入 ab 然后将 applebanana 显示为一个单词?飘飘然

Flutter Firebase 可以在真实设备上运行,而不是在 Android 模拟器上运行