Skip to content

GridView inside AnimatedCrossFade throws Exception #51077

@feinstein

Description

@feinstein

I have a GridView inside an AnimatedCrossFade, and I get this error:

The following assertion was thrown during performResize():
Vertical viewport was given unbounded height.

Viewports expand in the scrolling direction to fill their container. In this case, a vertical viewport was given an unlimited amount of vertical space in which to expand. This situation typically happens when a scrollable widget is nested inside another scrollable widget.

If this widget is always nested in a scrollable widget there is no need to use a viewport because there will always be enough vertical space for the children. In this case, consider using a Column instead. Otherwise, consider using the "shrinkWrap" property (or a ShrinkWrappingViewport) to size the height of the viewport to the sum of the heights of its children.

I don't want to use shrinkWrap as this cuts my widget's elevation shadows and BouncingScrollPhysics as per #51078. Things are working fine, but I still get the exception.

Is this something that's missing from the docs or is AnimatedCrossFade not compatible with GridView?

Here's a sample code that triggers the issue:

class MyScreen extends StatefulWidget {
  @override
  _MyScreenState createState() => _MyScreenState();
}

class _MyScreenState extends State<MyScreen> {
  @override
  Widget build(BuildContext context) {
    return Theme(
      data: Theme.of(context).copyWith(primaryColorBrightness: Brightness.dark),
      child: Scaffold(
        body: Column(
          crossAxisAlignment: CrossAxisAlignment.stretch,
          children: <Widget>[
            MediaQuery.removePadding(
              context: context,
              removeBottom: true,
              child: AppBar(
                elevation: 10,
                backgroundColor: Theme.of(context).canvasColor,
                automaticallyImplyLeading: false,
                flexibleSpace: Container(height: 300, color: Colors.blue,),
              ),
            ),
            Expanded(
              child: FutureBuilder<void>(
                future: Future.delayed(Duration(seconds: 15)),
                builder: (context, snapshot) {
                  return AnimatedCrossFade(
                    duration: Duration(milliseconds: 300),
                    crossFadeState:
                    snapshot.connectionState != ConnectionState.done
                        ? CrossFadeState.showFirst
                        : CrossFadeState.showSecond,
                    firstChild: GridView.count(
                          crossAxisCount: 3,
                          physics: BouncingScrollPhysics(),
                          padding: EdgeInsets.all(12),
                          crossAxisSpacing: 12,
                          mainAxisSpacing: 12,
                          children: <Widget>[
                            for (int i = 0; i < 20; ++i)
                              Loading(),
                          ],
                          shrinkWrap: true,
                    ),
                      secondChild: GridView.count(
                        crossAxisCount: 3,
                        physics: BouncingScrollPhysics(),
                        padding: EdgeInsets.all(12),
                        crossAxisSpacing: 12,
                        mainAxisSpacing: 12,
                        children: <Widget>[
                          for (int i = 0; i < 5; ++i)
                            Loading(),
                        ],
                        shrinkWrap: true,
                      ),
                  );
                },
              ),
            ),
          ],
        ),
      ),
    );
  }
}

class Loading extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Card(
      shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(16)),
      elevation: 8,
      clipBehavior: Clip.antiAlias,
      child: AspectRatio(
        aspectRatio: 1,
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            Container(
              width: 70,
              height: 70,
              color: Colors.white30,
            ),
            SizedBox(height: 4),
            Container(
              width: 70,
              height: 15,
              color: Colors.white30,
            ),
          ],
        ),
      ),
    );
  }
}

Flutter 1.12.13+hotfix.8 • channel stable • https://github.com/flutter/flutter.git
Framework • revision 0b8abb4 (8 days ago) • 2020-02-11 11:44:36 -0800
Engine • revision e1e6ced
Tools • Dart 2.7.0

Metadata

Metadata

Assignees

No one assigned

    Labels

    a: animationAnimation APIsframeworkflutter/packages/flutter repository. See also f: labels.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions