在构建时计算Text小部件的高度

时间:2018-04-02 20:02:26

标签: dart flutter

我尝试在SliverFixedExtentList下添加填充以使用SliverPadding填充屏幕,并在屏幕底部添加Text.rich小部件。计算列表的高度相当容易,因为我设置了范围。但我也应该记住Text.rich小部件的高度。

我不确定如何在构建时计算Text.rich小部件的高度,这可能吗?如果没有,那会是更好的方法吗?

我一直在使用以下代码:

  Widget build(BuildContext context) {
    double maxNonScrollableListSize = (MediaQuery.of(context).size.height - listPadding.bottom - listPadding.top - bottomFooterPadding.bottom - fontSize - 2);
    double totalChildHeight = childCount * itemExtent;
    double footerPadding = maxNonScrollableListSize > totalChildHeight ? maxNonScrollableListSize - totalChildHeight : 5.0;
    return new Scaffold(
      body: new Container(
        color: new Color.fromRGBO(50, 120, 176, 1.0),
        child: new CustomScrollView(
          slivers: <Widget>[
            new SliverPadding(
              padding: listPadding,
              sliver: new SliverFixedExtentList(
                itemExtent: itemExtent,
                delegate: new SliverChildBuilderDelegate(
                  (BuildContext context, int index) {
                    return new Card(
                      child: new Container(
                        child: new Center(child: new Text("Card $index")),
                      ),
                    );
                  },
                  childCount: childCount,
                ),
              ),
            ),
            new SliverPadding(
              padding: new EdgeInsets.only(bottom: footerPadding),
            ),
            new SliverToBoxAdapter(
              child: new Column(
                mainAxisSize: MainAxisSize.max,
                mainAxisAlignment: MainAxisAlignment.end,
                children: <Widget>[
                  new Container(child: new Text("ok", style: new TextStyle(fontSize: fontSize),))
                ],
              ),
            ),
            new SliverPadding(
              padding: bottomFooterPadding,
            ),
          ],
        )
      ),
    );
  }

1 个答案:

答案 0 :(得分:3)

没有。这是不可能的。

小部件系统的重点是使小部件自治。在任何情况下,他们都不应该在建造时知道他们孩子的大小。唯一的例外是预定义该大小(例如使用PreferredSizeWidget)。

如果您某些,则需要它。那么你想要创建的不是一个小部件,而是一个RenderBox。这是较低层次的颤动之一。