代码之家  ›  专栏  ›  技术社区  ›  Ray Li Farhana Naaz Ansari

居中对齐堆栈中的两个小部件

  •  0
  • Ray Li Farhana Naaz Ansari  · 技术社区  · 6 年前

    来自Android开发,在Flutter中“Android:layout_gravity=“center_vertical”的等价物是什么?

    我有两个盒子包裹在一个堆栈中,我想垂直居中对齐。但是,最大项目的大小不是固定的,因此我无法使用任何固定值。这带来了一些困难,因为我无法确定一个孩子与另一个孩子的关系。在安卓系统中,你所要做的就是将子项的“布局重力”设置为“中心垂直”。

    这就是我目前所拥有的。图标的位置是固定的,这不好。

    class CardItem extends StatelessWidget {
      @override
      Widget build(BuildContext context) {
        return new SizedBox(
            width: double.infinity,
            child: Stack(
              children: <Widget>[
                Padding(
                  padding: EdgeInsets.fromLTRB(72.0, 6.0, 12.0, 6.0),
                  child: ConstrainedBox(
                    constraints: BoxConstraints(
                      minHeight: 150.0,
                    ),
                    child: Material(
                      elevation: 8.0,
                      borderRadius: BorderRadius.circular(12.0),
                      color: Colors.white,
                      child: InkWell(
                        onTap: () => {},
                        child: Padding(
                          padding: EdgeInsets.fromLTRB(76.0, 24.0, 6.0, 24.0),
                          child: Column(
                            mainAxisAlignment: MainAxisAlignment.center,
                            crossAxisAlignment: CrossAxisAlignment.stretch,
                            children: <Widget>[
                              Text('Computer',
                                  style: Theme.of(context).textTheme.headline),
                              Text('电脑',
                                  style: Theme.of(context).textTheme.subhead.apply(fontWeightDelta: -2)
                              ),
                            ],
                          ),
                        ),
                      ),
                    ),
                  )
                ),
                Align(
                  alignment: Alignment.centerLeft,
                  child: Padding(
                    padding: EdgeInsets.fromLTRB(12.0, 0.0, 0.0, 0.0),
                    child: SizedBox.fromSize(
                      size: Size.fromRadius(60.0),
                      child: Material(
                        elevation: 12.0,
                        shape: CircleBorder(),
                        child: Image.asset('image.png'),
                      ),
                    ),
                  ),
                ),
              ],
            )
        );
      }
    }
    
    0 回复  |  直到 6 年前
        1
  •  1
  •   buckleyJohnson    4 年前

    将其包装在Align小部件中。。。

    对齐( 对齐:对齐。居中 孩子:定位( 前10名, 孩子:文本('hi'), ), ),