Home > Software design >  Is there a way to get fixed size for child inside GridView?
Is there a way to get fixed size for child inside GridView?

Time:11-03

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),
            ],
          ),
        );
      }),
    );

CodePudding user response:

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])
             ),
           ],
         ),
       );
     }),
   )
   ```
  • Related