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

问题描述 投票:0回答:3

我正在尝试如下创建窗口小部件。它可以正常工作两位数。 (如图像“ 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(),
          ],
        );
flutter flutter-layout
3个回答
0
投票
Stack(alignment: AlignmentDirectional.center,children: <Widget>[])

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),
            )
          ],
        ),
      ),

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(),
      ],
    );
© www.soinside.com 2019 - 2024. All rights reserved.