简体   繁体   English

如何 position FLUTTER 中容器右上角的图标/按钮?

[英]How to position an Icon/Button at the Top Right corner of a Container in FLUTTER?

So this is my current home-screen and I'm trying to position the close button on the top-right corner of each container, which will end up deleting these chatrooms and I'm having some trouble figuring out how to do that.所以这是我当前的主屏幕,我正在尝试 position 每个容器右上角的关闭按钮,这将最终删除这些聊天室,我在弄清楚如何做到这一点时遇到了一些麻烦。 Below the image is my code.图片下方是我的代码。

在此处输入图像描述

child: Container(
    decoration: BoxDecoration(
        color: Color(0xff240046),
      borderRadius: BorderRadius.circular(15)
    ),
    padding: EdgeInsets.symmetric(horizontal: 12, vertical: 18),
    margin: EdgeInsets.symmetric(vertical: 8),
    child: Row(
      children: [
        ClipRRect(
          borderRadius: BorderRadius.circular(30),
          child: profilePicUrl.isNotEmpty ? Image.network(
            profilePicUrl,
            height: 40,
            width: 40,
          ) : null,
        ),
        SizedBox(width: 12),
        Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            Text(
              name,
              style: TextStyle(
                  color: Color(0xff7b2cbf),
                  fontSize: 16),
            ),
            SizedBox(height: 3),
            Text(widget.lastMessage,
            style: TextStyle(
              color: Color(0xffd4d4d4),
            ),)
          ],
        ),
        Column(
          children: [
            Align(
              alignment: Alignment.topRight,
              child: Icon(
                  Icons.close,

              ),
            )
          ],
        ),

      ],
    ),
  ),

So.. how do I do it?那么..我该怎么做? Thanks in advance.提前致谢。

first of all the approach you are using is not readable, any ways to do it on top right, set mainAxisAlignment to MainAxisAlignment.start, then set crossAxisAlignment to CrossAxisAlignment.end.首先,您使用的方法不可读,任何方法都在右上角,将 mainAxisAlignment 设置为 MainAxisAlignment.start,然后将 crossAxisAlignment 设置为 CrossAxisAlignment.end。 it looks like this:-它看起来像这样:-

  Column(
      mainAxisAlignement : MainAxisAlignment.start,
      crossAxisAlignement : CrossAxisAlignment.end,
      children: [
        Align(
          alignment: Alignment.topRight,
          child: Icon(
              Icons.close,

          ),
        )
      ],
    ),

now theres a best way to achieve this, its ListTile.现在有一种实现此目的的最佳方法,即 ListTile。 it has leading property for left side, and title and subtitle, and then for right side widgets it has trailing property.它具有左侧的前导属性、标题和副标题,然后对于右侧的小部件,它具有尾随属性。

it looks like this:-它看起来像这样:-

ListTile(
        leading:  ClipRRect(
      borderRadius: BorderRadius.circular(30),
      child: profilePicUrl.isNotEmpty ? Image.network(
        profilePicUrl,
        height: 40,
        width: 40,
      ) : null,
    ),
    title:   Text(
          'name',
          style: TextStyle(
              color: Color(0xff7b2cbf),
              fontSize: 16),
        ),
    subtitle:Text('(widget.lastMessage)',
    style: TextStyle(
          color: Color(0xffd4d4d4),
        ), ),
     trailing: Icon(
              Icons.close,

          ),
        ),

Plase try with Stack请尝试使用 Stack

Container(
        decoration: BoxDecoration(
            color: Color(0xff240046),
            borderRadius: BorderRadius.circular(15)
        ),
        padding: EdgeInsets.symmetric(horizontal: 12, vertical: 18),
        margin: EdgeInsets.symmetric(vertical: 8),
        child: Stack(
          children: [
            Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                Text(
                  name,
                  style: TextStyle(
                      color: Color(0xff7b2cbf),
                      fontSize: 16),
                ),
                SizedBox(height: 3),
                Text(widget.lastMessage,
                  style: TextStyle(
                    color: Color(0xffd4d4d4),
                  ),)
              ],
            ),
            Align(
              alignment: Alignment.topRight,
              child: Icon(
                Icons.close,

              ),
            ),
          ],
        ),
      )

ouput:输出: 在此处输入图像描述

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

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