颤动:在堆栈小部件中删除的图像顶部

时间:2020-05-30 07:59:00

标签: flutter flutter-layout

我正在尝试在小部件树中使用堆栈:

  body: Center(
    child: SingleChildScrollView(
      child: Stack(
        overflow: Overflow.visible,
        children: <Widget>[
          Container(
            // card view
            alignment: Alignment.center,
            height: 200,
            margin: EdgeInsets.only(
                top: 20.0, bottom: 50.0, left: 50.0, right: 50.0),
            decoration: BoxDecoration(
              color: color_transparent_black,
              borderRadius: BorderRadius.circular(14.0),
            ),
          ),
          Positioned(
            top: -60,
            left: 0,
            right: 0,
            bottom: 0,
            child: Align(
              alignment: Alignment.topCenter,
              child: Container(
                width: width * 0.3,
                height: height * 0.2,
                child: CircleAvatar(
                  backgroundColor: Colors.transparent,
                  child: Image.asset("assets/images/ic_setting.png"),
                ),
              ),
            ),
          ),
        ],
      ),
    ),
  ),
);

结果是这样的:

enter image description here

为什么设置图标的顶部已被删除?

2 个答案:

答案 0 :(得分:1)

我已解决问题:

 body: Center(
    child: SingleChildScrollView(
      child: Stack(
        // overflow: Overflow.visible,
        children: <Widget>[
          Container(
            // card view
            alignment: Alignment.center,
            height: 200,
            margin: EdgeInsets.only(
                top: 80.0, bottom: 50.0, left: 50.0, right: 50.0),
            decoration: BoxDecoration(
              color: color_transparent_black,
              borderRadius: BorderRadius.circular(14.0),
            ),

          ),
          FractionalTranslation(
            translation: Offset(0.0, 0.0),
            child: Align(
              alignment: Alignment.topCenter,
              child: Container(
                width: width * 0.3,
                height: height * 0.2,
                child: CircleAvatar(
                  backgroundColor: Colors.transparent,
                  child: Image.asset("assets/images/ic_setting.png"),
                ),
              ),
            ),
          ),
        ],
      ),
    ),
  ),
);

它是结果:

enter image description here

我将最高边距增加到top: 80.0,并用 FractionalTranslation 替换Positioned。实际上,它也可以与 Positioned 一起使用。

答案 1 :(得分:0)

最可能是因为您为top: -60小部件设置了Positioned

编辑1:

通过删除SingleChildScrollView

停止剪辑

您可以使用Stack小部件本身的alignment: Alignment.topCenter属性将Stack的所有子项居中。

 child: Center(
     child: Stack(
         overflow: Overflow.visible,
         alignment: Alignment.topCenter,
         children: <Widget>[
              Container(
                   height: 200,
                              ...
              ),
              Positioned(
                   top: -60,
                   ...
                   child: Container(
                               ...
                   ),
              ),
      ),
 ]),