Flutter:堆栈中的底部中心小部件

时间:2019-03-07 23:57:30

标签: dart flutter flutter-layout

我想实现这张照片。为简化起见,我创建了一个包含两个容器的堆栈。我想使用Align小部件将小容器的底部居中,但无法正常工作!它始终保留在顶部

Stack(
              overflow: Overflow.visible,
              children: <Widget>[
                Container(
                  width: MediaQuery.of(context).size.width,
                  height: 170,
                  decoration: BoxDecoration(
                      color: Colors.white,
                      borderRadius: BorderRadius.only(
                          bottomLeft: Radius.circular(0),
                          bottomRight: Radius.circular(0))),
                  // child: Image.network(tutorImage),
                ),
                Align(
                  alignment: Alignment.bottomCenter,
                  child: Container(
                    width: 60,
                    height: 60,
                    color: Colors.black,
                  ),
                )
              ],
            ),

enter image description here

3 个答案:

答案 0 :(得分:7)

您可以在堆栈中使用Positioned.fill和Align:

Column(
  children: <Widget>[
    Stack(
      overflow: Overflow.visible,
      children: <Widget>[
        Container(
          width: MediaQuery.of(context).size.width,
          height: 170,
          decoration: BoxDecoration(
              color: Colors.white, borderRadius: BorderRadius.only(bottomLeft: Radius.circular(0), bottomRight: Radius.circular(0))),
          // child: Image.network(tutorImage),
        ),
        Positioned.fill(
          child: Align(
            alignment: Alignment.bottomCenter,
            child: Container(
              width: 60,
              height: 60,
              color: Colors.black,
            ),
          ),
        )
      ],
    ),
  ],
);

答案 1 :(得分:0)

怎么样?

Stack(
      overflow: Overflow.visible,
      children: <Widget>[
        Container(
          width: MediaQuery.of(context).size.width,
          height: 170,
          decoration: BoxDecoration(
              color: Colors.white,
              borderRadius: BorderRadius.only(
                  bottomLeft: Radius.circular(0),
                  bottomRight: Radius.circular(0))),
          // child: Image.network(tutorImage),
        ),
        Positioned(
          bottom: -30,
          left: 0,
          right: 0,
          child: Center(
            child: Container(
              width: 60,
              height: 60,
              color: Colors.black,
            ),
          ),
        )
      ],
    ),

答案 2 :(得分:0)

尝试将您的容器包装到Column中,并使用轴对齐属性。

Stack(
          overflow: Overflow.visible,
          children: <Widget>[
            Container(
              //width: MediaQuery.of(context).size.width, In my test this line causes bad behavior
              height: 170,
              decoration: BoxDecoration(
                  color: Colors.white,
                  borderRadius: BorderRadius.only(
                      bottomLeft: Radius.circular(0),
                      bottomRight: Radius.circular(0))),
              // child: Image.network(tutorImage),
            ),
            Column(
              mainAxisAlignment: MainAxisAlignment.end, // start at end/bottom of column 
              crossAxisAlignment: CrossAxisAlignment.center, 
              children: <Widget>[
                Center( //  center in horizontal axis
                  child: Container(
                    width: 60,
                    height: 60,
                    color: Colors.black,
                  ),
                ),
              ],
            ),
          ],
        ),