1

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

Md. Yeasin Sheikh
  • 54,221
  • 7
  • 29
  • 56
Parafin
  • 89
  • 3
  • 7
  • @YeasinSheikh thanks for editing my post! I use it like so SliverToBoxAdapter(child:Photos()) The Photos is my StatefullWidget – Parafin Aug 27 '21 at 09:07
  • let me know if my answer solve in your case. – Md. Yeasin Sheikh Aug 27 '21 at 09:08
  • Thnks for answer @gtxtreme what do you mean by FutureBuilder? It is some widget right? I new to flutter and cant get it.. I do not use FutureBuilder at this place. Can you detalize please if possible? – Parafin Aug 27 '21 at 09:16
  • Thanks @YeasinSheikh it works, I marked your answer as right and asked new question below your answer)) – Parafin Aug 27 '21 at 09:17
  • I will try to answer your question asap after reproducing the errors and testing myself – Md. Yeasin Sheikh Aug 27 '21 at 09:20

1 Answers1

0

try

  Widget getGrid() {
    return photos.length<1?SizedBox():
           Container(
      child: GridView.builder(
Md. Yeasin Sheikh
  • 54,221
  • 7
  • 29
  • 56
  • Thanks @YeasinSheikh it works, but it sort of workaroud ? If it not very hard can you please explain where this empty space comes from and why if I use shrinkWrap actually? – Parafin Aug 27 '21 at 09:11
  • Based on my observation `SliverGridDelegateWithMaxCrossAxisExtent` will always take `maxCrossAxisExtent`in this case 200 width because of its default aspect ratio is 1x1. It is taking height. I assume it's splitting the width before assigning child. Maybe it is just a bug that I'm not sure of it. If you look at the source code, I think it is coming from `getLayout`() – Md. Yeasin Sheikh Aug 27 '21 at 09:47
  • Thanks for you help and time @Yeasin! – Parafin Aug 27 '21 at 12:11