Flutter. How to collapse GridView.builder when it empty (do not have any elements)

Issue

I have a GridView.builder with shrinkWrap to true. It fills dynamically, but the problem that at start it empty but takes place, like it has elements (sort of reserving place) depends on the size of maxCrossAxisExtent.
Is it possible change this? Empty space not very good..
I looked docs and google but didnt find any answer.

This is the code

class _PhotosState extends State<Photos> {
  List<File> photos = [];

  final ImagePicker _picker = ImagePicker();

  Widget getGrid() {
    return Container(
      child: GridView.builder(
        padding: EdgeInsets.all(0),
        shrinkWrap: true,
        physics:  NeverScrollableScrollPhysics(),
        gridDelegate: SliverGridDelegateWithMaxCrossAxisExtent(

          maxCrossAxisExtent: 200,
          //childAspectRatio: 3 / 2,
          // crossAxisSpacing: 10,
          // mainAxisSpacing: 10,
          //mainAxisExtent: 200,
        ),
        itemCount: photos.length,
        itemBuilder: (BuildContext ctx, index) {
          return Container(
            color: Colors.blueGrey,
          );
        },
      ),
    );
  }

The empty GridView with empty space
empty space when grid is empty

One element added to GridView
one element added

Solution

try

  Widget getGrid() {
    return photos.length<1?SizedBox():
           Container(
      child: GridView.builder(

Answered By – Yeasin Sheikh

Answer Checked By – Robin (FlutterFixes Admin)

Leave a Reply

Your email address will not be published. Required fields are marked *