Search code examples
flutterdartstaggered-gridview

Dynamic height for StaggeredGridView is not working in flutter project


I wanted to build a StaggeredGridView with a banner video at the 0th index and 2 cards at each row. But the video is not filling the screen width, because the height for every row is the same.

Here is what I am doing.

static final videoPlayerController = VideoPlayerController.network(
  "https://commondatastorage.googleapis.com/gtv-videos-bucket/sample/BigBuckBunny.mp4");

static final chewieController = ChewieController(
videoPlayerController: videoPlayerController,
aspectRatio: 3 / 2,
autoPlay: true,
looping: true,
allowMuting: true,
allowFullScreen: false,);

final playerWidget = Chewie(
controller: chewieController,);

Code for building Widget:

Widget buildStaggeredListing(BuildContext context) {
return StaggeredGridView.countBuilder(
  crossAxisCount: 2,
  crossAxisSpacing: 4.0,
  itemCount: 10,
  itemBuilder: (BuildContext buildContext, int index) {
    if (index == 0) {
      return playerWidget;
    }
    return Card(
      child: Padding(
        padding: EdgeInsets.all(16.0),
        child: Text(
          index.toString(),
          style: TextStyle(fontSize: 22.0),
        ),
      ),
    );
  },
  staggeredTileBuilder: (int index) {
    if (index == 0) {
      return StaggeredTile.count(2, 1);
    }
    return StaggeredTile.count(1, 1);
  },
);

}

Screenshot: screenshot

Update: according to sagar acharya's answer, I used

if (index == 0) {
      return StaggeredTile.fit(1);
   }
    return StaggeredTile.fit(2);

and this is what is happening: screenshot2


Solution

  • just check if this code works:

    staggeredTileBuilder: (int index) {
        if (index == 0) {
          return StaggeredTile.fit(2);
        }
        return StaggeredTile.count(1,1);
      },