在flift Stack小部件内对齐项目中心

时间:2020-03-12 10:10:44

标签: flutter flutter-layout

我正在尝试如下创建窗口小部件。它可以正常工作两位数。 (如图像“ 20”上所示),但是如果数字增加,则显示的标签不会居中。它被移到右侧。就像第二张图片所示。我该如何解决?我已经尝试了很多方法,但尝试都失败了。请在下面查看我尝试过的代码。

enter image description here

enter image description here

    Stack(
          children: <Widget>[
            Container(
              width: profileImageSize,
              height: profileImageSize,
              decoration: new BoxDecoration(
                shape: BoxShape.circle,
                image: new DecorationImage(
                  fit: BoxFit.fill,
                  image: new NetworkImage("$profileImageUrl"),
                ),
              ),
            ),
            (this.countFollowers != null)
                ? Padding(
                    padding: const EdgeInsets.only(left: 15.0, top: 30.0),
                    child: SpriiFollowerCountLabel(countFollowers),
                  )
                : Container(),
          ],
        );

3 个答案:

答案 0 :(得分:0)

Stack(alignment: AlignmentDirectional.center,children: <Widget>[])

答案 1 :(得分:0)

代替

Padding(
    padding: const EdgeInsets.only(left: 15.0, top: 30.0),
    child: SpriiFollowerCountLabel(countFollowers),
)

使用

Container(
   alignment: Alignment.center,
   padding: const EdgeInsets.only(top: 30.0), 
   child: SpriiFollowerCountLabel(countFollowers),
)

但我建议使用

Container(
        width: profileImageSize,
        height: profileImageSize,
        decoration: new BoxDecoration(
          shape: BoxShape.circle,
          image: new DecorationImage(
            fit: BoxFit.fill,
            image: new NetworkImage("$profileImageUrl"),
          ),
        ),
        child: Stack(
          children: <Widget>[
            Container(
              alignment: Alignment.bottomCenter,
              child: SpriiFollowerCountLabel(countFollowers),
            )
          ],
        ),
      ),

答案 2 :(得分:0)

我通过使用容器的transform属性解决了这个问题,并删除了Stack小部件并用列替换了

Column(
      children: <Widget>[
        Container(
          width: profileImageSize,
          height: profileImageSize,
          decoration: new BoxDecoration(
            shape: BoxShape.circle,
            image: new DecorationImage(
              fit: BoxFit.fill,
              image: new NetworkImage("$profileImageUrl"),
            ),
          ),
        ),
        (this.countFollowers != null)
            ? Container(
                child: SpriiFollowerCountLabel(countFollowers),
                transform: Matrix4.translationValues(0.0, -10.0, 0.0),
              )
            : Container(),
      ],
    );