user2511882
user2511882

Reputation: 9152

Stack within ListView fails Flutter

I am trying to add a Stack within a ListView in a SafeArea. However, there is a runtime exception as seen below:

BoxConstraints forces an infinite height.
I/flutter (18717): These invalid constraints were provided to RenderConstrainedBox's layout() function by the following
I/flutter (18717): function, which probably computed the invalid constraints in question:
I/flutter (18717):   RenderConstrainedBox.performLayout (package:flutter/src/rendering/proxy_box.dart:259:13)
I/flutter (18717): The offending constraints were:
I/flutter (18717):   BoxConstraints(w=411.4, h=Infinity)

My code:

    @override
  Widget build(BuildContext context) {
    SizeConfig().init(context);

    return new Scaffold(
        body: SafeArea(
            top: true,
            child: ListView(  shrinkWrap: true,
        children: <Widget>[_buildFrostedRow(context)])));
  }


  Stack _buildFrostedRow(BuildContext context) {
    return Stack(
    children: <Widget>[
      new ConstrainedBox(
          constraints: const BoxConstraints.expand(), child: _showCardRow()),
      new Center(
        child: new ClipRect(
          child: new BackdropFilter(
            filter: new ImageFilter.blur(sigmaX: 10.0, sigmaY: 10.0),
            child: new Container(
              width: SizeConfig.safeBlockHorizontal * 90,
              height: SizeConfig.safeBlockVertical * 20,
              decoration: new BoxDecoration(
                  color: Colors.grey.shade200.withOpacity(0.5)),
              child: new Center(
                child: new Text('Frosted',
                    style: Theme.of(context).textTheme.display3),
              ),
            ),
          ),
        ),
      ),
    ],
  );
  }

What am I missing here?

Upvotes: 0

Views: 108

Answers (2)

Er. Divyesh Shani
Er. Divyesh Shani

Reputation: 76

Just wrap your Stack with Constrained box or Fixed Heigh Container. Because, Listview and Stack has infinite height

Upvotes: 0

Phat Huynh
Phat Huynh

Reputation: 902

You need to limit height to Stack (put it into Container and set height or something like that). Because if you didn't, the ListView cannot calculate layout.

return new Scaffold(
        body: SafeArea(
            top: true,
            child: ListView(  shrinkWrap: true,
        children: <Widget>[Container(height:200, // can change
                           child: _buildFrostedRow(context))])));

Upvotes: 3

Related Questions