【问题标题】:How can I put an icon in the corner of a container ? (just get the icon)如何将图标放在容器的角落? (只需获取图标)
【发布时间】:2021-11-24 09:09:35
【问题描述】:

示例:

现实:

我希望该图标是“状态”类型的标签,但是如果使用 Positioned,它不会让我把它一直放在左边。

有没有什么方法可以得到想要的结果或者有什么其他的解决方案?

return ConstrainedBox(
  constraints: new BoxConstraints(
    minHeight: 50, //exemple
    minWidth: 100, //exemple
  ),
  child: Container(
    decoration: BoxDecoration(
      color: Colors.orange,
    ),
    child: Stack(
      children: [
        Positioned(
          left: 0,
          top: 0,
          child: Transform.rotate(
            angle: -45 * pi / 180,
            child: Icon(
              Icons.arrow_drop_up,
              size: 30,
            ),
          ),
        ),
        Center(
          child: Padding(
            padding: const EdgeInsets.all(8.0),
            child: Text(
              "0001",
              textAlign: TextAlign.center,
              style: TextStyle(
                fontSize: 30,
                color: Colors.white,
              ),
            ),
          ),
        ),
      ],
    ),
  ),
);

【问题讨论】:

    标签: flutter icons


    【解决方案1】:

    你的做法是对的,但是Icon 周围有一些额外的空格,绿色分配图标的大小。

    这就是为什么您需要对lefttopPositioned 小部件上使用基于iconSize 的负值。 会的。

    Positioned(
      left: -((12 * iconSize) / 30),
      top: -((13 * iconSize) / 30),
      child: Transform.rotate(
        angle: -45 * pi / 180,
        child: Icon(
          Icons.arrow_drop_up,
          size: iconSize,
        ),
      ),
    ),
    

    感谢@Amir 的价值,我正在回答这个问题以获得响应式视图。

    【讨论】:

    • 非常感谢!它有效,我只想知道你为什么将它除以 30?
    • 这是一个简单的算术逻辑来做出响应,我忘记了这个方法的名称,但30 是图标的大小,适用于1213
    【解决方案2】:

    我更改了lefttop 数字,图标转到左上角:

     Positioned(
         left: -12,
         top: -13,  
         child: ...
    ),
    

    结果图片:

    【讨论】:

    • 非常感谢,它也很有用,对我的价值观有所帮助!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-09-17
    • 2016-10-11
    • 2021-10-24
    相关资源
    最近更新 更多