Ad

Is There A Way To Get Fixed Size For Child Inside GridView?

- 1 answer

Is there a way to have a fixed size for the child inside GridView? I wanted to have the red color be same height. I tried using Expanded or specified the size for the Container, but it just won't work. GridView just makes the child same size.

Note: I tried specify the height for the red Container. It didn't work.

This is what I got.

enter image description here

This is what I did.

    return GridView.count(
      crossAxisCount: 2,
      children: List.generate(drinksData.length, (index) {
        return Container(
          padding: EdgeInsets.all(20.0),
          child: Column(
            children: [
              Expanded(
                child: Container(
                  color: Colors.red,
                ),
              ),
              Text(drinksData[index].label),
            ],
          ),
        );
      }),
    );
Ad

Answer

GridView.count(
     crossAxisCount: 2,
     children: List.generate(drinksData.length, (index) {
       return Container(
         padding: const EdgeInsets.all(20.0),
         child: Column(
           children: [
             Expanded(
               child: Container(
                // height:90,//or whatever size you want!
                 color: Colors.red,
               ),
             ),
             SizedBox(
               height: 45,
                 child: Text(drinksData[index])
             ),
           ],
         ),
       );
     }),
   )
   ```
Ad
source: stackoverflow.com
Ad