Bram  Vanbilsen
Bram Vanbilsen

Reputation: 6505

Calculate height of Text widget at build

I'm trying to add padding under a SliverFixedExtentList to fill the screen by using a SliverPadding and add a Text.rich widget at the bottom of the screen. Calculating the height of the list was rather easy because I'm setting the extent. But I should also keep the height of the Text.rich widget in mind.

I'm uncertain about how to calculate the height of the Text.rich widget at build time, is this possible? If not, what would be a better approach?

I've been using the following code:

  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,
            ),
          ],
        )
      ),
    );
  }

Upvotes: 0

Views: 2877

Answers (1)

R&#233;mi Rousselet
R&#233;mi Rousselet

Reputation: 276957

No. It's not possible.

The whole point of the widget system is to make widgets autonomous. Under no circumstances should they know at build time the size of their children. The only exception is when that size is predefined (such as with PreferredSizeWidget).

If you are certain you need it. Then what you want to create is not a widget, but a RenderBox. Which is one of the lower layers of flutter.

Upvotes: 3

Related Questions