首页 > 解决方案 > 在堆栈中按基线对齐图标

问题描述

我需要将一个图标的基线居中,在堆栈的中心:

这个图片

我试试这个

class Screen extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(title: Text("Tst")),
        body: Stack(
          children: <Widget>[
            Container(
              color: Colors.white,
            ),
            Positioned(
                bottom: context.size.height / 2,
                width: context.size.width,
                child: Align(
                  alignment: Alignment.bottomCenter,
                  child: Icon(
                    Icons.add_location,
                    size: 100,
                  ),
                )),
          ],
        ));
  }
}

但是收到错误:

“在构建过程中无法获取大小。” “这个渲染对象的大小还没有确定,因为框架还在”

标签: flutterflutter-layout

解决方案


"Cannot get size during build." "The size of this render object has not yet been determined because the framework is still in the process of building widget"

The error is very obvious. You can not calculate the size of the widget before it is built.

A better alternative is to use Center widget in this case as suggested by hayi:

Center(
  child: Align(
    alignment: Alignment.bottomCenter,
    child: Icon(
      Icons.add_location,
      size: 100,
    ),
  )),

The above code is just an example.

Hope this helps


推荐阅读