-1

I am using Gridview.builder to show dynamic item count in a row with the help of crossAxisCount. if crossAxisCount is 3 then 3 items are showing on grid. But It is showing top and bottom space(item space,not thein between space of items) for every item. Please help me how to remove the space. Thank you

code:

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';

class Order extends StatefulWidget {
  @override
  State<StatefulWidget> createState() => _OrderState();
}

class _OrderState extends State<Order> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Column(
        children: [
          Expanded(
            child: SizedBox(
              width: MediaQuery.of(context).size.width,
              // height: MediaQuery.of(context).size.height,
              child: GridView.builder(
                shrinkWrap: true,
                gridDelegate: const SliverGridDelegateWithFixedCrossAxisCount(
                    crossAxisCount: 3),
                padding: EdgeInsets.zero,

                itemCount: 9,
                itemBuilder: (BuildContext context, int index) {
                  return Container(
                    decoration: BoxDecoration(
                      border: Border.all(color: Colors.red),
                      borderRadius: BorderRadius.circular(10),
                    ),

                    margin: const EdgeInsets.fromLTRB(10, 20, 10, 10),
                    child: Row(
                      mainAxisAlignment: MainAxisAlignment.spaceBetween,
                      children: const [
                        Text("hello"),
                        Text("hello"),

                      ],
                    ),
                  );
                },
              ),
            ),
          ),
        ],
      ),
    );
  }
}

screenshot: enter image description here

image

[![enter image description here][3]][3]

Actually trying for this design

kartheeki j
  • 2,206
  • 5
  • 27
  • 51

3 Answers3

0

Try add childAspectRatio in side SliverGridDelegateWithFixedCrossAxisCount():

             gridDelegate: const SliverGridDelegateWithFixedCrossAxisCount(
                crossAxisCount: 3,
               childAspectRatio: 1/3
             ),
Xuuan Thuc
  • 2,340
  • 1
  • 5
  • 22
0

The question is not so clear.

If problem is space between square, just remova margin: const EdgeInsets.fromLTRB(10, 20, 10, 10).

If problem is why your text in the center and look like container have space on top and bottom of the text, that's not the problem. Cause the Grid item was fixed sized of child (Container) and a Row inside, children of Row was fill the size of parent as default.

Example: just add alignment, the spaces is gone enter image description here

Updated: update answer, check this example below

import 'package:flutter/material.dart';

void main() {
  runApp(MaterialApp(
    home: Scaffold(
      appBar: AppBar(),
      body: Column(
        children: [
          Row(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: const [
              Expanded(child: Ticket(title: '#1', height: 200)),
              Expanded(child: Ticket(title: '#3', height: 400)),
              Expanded(child: Ticket(title: '#4', height: 300)),
            ],
          ),
        ]
      ),
    ),
  ));
}

class Ticket extends StatelessWidget {
  const Ticket({required this.height, required this.title, Key? key})
      : super(key: key);
  final String title;
  final double height;
  @override
  Widget build(BuildContext context) {
    return Padding(
      padding: const EdgeInsets.all(4),
      child: Container(
        decoration: BoxDecoration(
          border: Border.all(),
        ),
        child: Column(
          children: [
            Text(title),
            Placeholder(fallbackHeight: height),
          ],
        ),
      ),
    );
  }
}

enter image description here

Tuan
  • 2,033
  • 1
  • 9
  • 16
0

Try below code, use MainAxisAlignment.spaceAround refer layout

 Column(
    children: [
      Expanded(
        child: SizedBox(
          width: MediaQuery.of(context).size.width,
          // height: MediaQuery.of(context).size.height,
          child: GridView.builder(
            shrinkWrap: true,
            gridDelegate: const SliverGridDelegateWithFixedCrossAxisCount(
              crossAxisCount: 3,
              crossAxisSpacing: 1,
              mainAxisSpacing: 1,
            ),
            padding: EdgeInsets.zero,
            itemCount: 9,
            itemBuilder: (BuildContext context, int index) {
              return Container(
                decoration: BoxDecoration(
                  border: Border.all(color: Colors.red),
                  borderRadius: BorderRadius.circular(10),
                ),
                margin: const EdgeInsets.all(10),
                child: Row(
                  mainAxisAlignment: MainAxisAlignment.spaceAround,
                  children: const [
                    Text("hello"),
                    Text("hello"),
                  ],
                ),
              );
            },
          ),
        ),
      ),
    ],
  ),
Ravindra S. Patil
  • 11,757
  • 3
  • 13
  • 40