【问题标题】:Why CircleAvator's position can't align exactly under The IconButton?为什么 CircleAvator 的位置在 IconButton 下不能完全对齐?
【发布时间】:2020-09-30 04:32:49
【问题描述】:

创建了一个 IconButton 并以响应方式定位它。然后用 CircleAvator 小部件包装它。 我期待 circleavator 将被放置在 IconButton 下并且也会响应响应,但 CircleAvator 不会在按钮下对齐,甚至不会响应响应。 这是我的代码-

Positioned(
                    left: (_width / 2.4) - (_height / 3.5 * 0.30),
                    top: (_height * 0.5) / 2.35,
                    child: CircleAvatar(
                      backgroundColor: colorBlack,
                      radius: 50.0,

                      child: IconButton(
                          icon: Icon(Icons.check_circle),
                          iconSize: _height / 3.5 * 0.5,
                          color: loading ? Colors.teal : Colors.red,
                          onPressed: doConversion),
                    ),
                  ),

这是我的设备输出 - enter image description here

【问题讨论】:

    标签: flutter flutter-positioned iconbutton


    【解决方案1】:

    要堆叠 2 个小部件,实际上有一个更简单的解决方案。只需使用Stack 小部件。 您可以使用alignment 将元素定位在堆栈中。

     Stack(
            alignment: Alignment.center,
            children: <Widget>[
              CircleAvatar(
                backgroundColor: Colors.black,
                radius: 50.0,
              ),
              IconButton(
                  icon: Icon(Icons.check_circle),
                  iconSize: 100,
                  color: Colors.red,
                  onPressed: () {}),
            ],
          )
    

    【讨论】:

      【解决方案2】:

      实际上,当使用CircleAvatar() 作为父级时,它会将子小部件覆盖到一定大小,并且当子小部件的大小超过一定数量(约 70%)时,它无法按预期包裹子小部件并开始移动孩子到右下角。而且IconButton() 有一个大约6 或8 的预设填充,您可以通过像padding: EdgeInsets.all(0.0), 那样自己定义填充属性来将其设置为0。因此,除了@Benedikt J Schlegel 的回答之外,我建议您再尝试 2 次:

      1. IconButton() 填充设置为 0,并且永远不要将超过父级 170% 的大小传递给子级:

                Positioned(
                    left: (_width / 2.4) - (_height / 3.5 * 0.30),
                    top: (_height * 0.5) / 2.35,
                  child: CircleAvatar(
                    backgroundColor: Colors.black,
                    radius: 100.0,
        
                    child: Padding(
                      padding: const EdgeInsets.all(8.0),
                      child: IconButton(
                          icon: Icon(Icons.check_circle),
                          padding: EdgeInsets.all(0.0),
                          iconSize: 170,
                          color: Colors.red,
                          onPressed: (){}
                      ),
                    ),
                  ),
                ),
        
      2. 使用Container() 作为IconButton() 的父级:

          Positioned(
            left: (_width / 2.4) - (_height / 3.5 * 0.30),
            top: (_height * 0.5) / 2.35,
            child: Container(
        
              decoration: BoxDecoration(
                color: Colors.black,
                shape: BoxShape.circle,
              ),
        
              child: IconButton(
                  icon: Icon(Icons.check_circle),
                  padding: EdgeInsets.all(0.0),
                  iconSize: 100,
                  color: Colors.red,
                  onPressed: (){}
              ),
            ),
          ),
        

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2019-05-09
        • 2016-04-10
        • 1970-01-01
        • 2019-10-07
        • 2018-09-23
        • 2012-10-22
        • 2020-01-25
        相关资源
        最近更新 更多