Flutter how to set a column that wrap gridview height

146

There is a property inside GridView called : shrinkWrap, see shrinkWrap property

Here you can find simple example

class SimpleExample extends StatelessWidget {
  const SimpleExample({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Container(
      child: SingleChildScrollView(
        child: Column(
          children: [
            GridView.builder(
              physics: NeverScrollableScrollPhysics(),
              itemCount: 30,
              shrinkWrap: true,
              gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
                  crossAxisCount: 2,
                  childAspectRatio: 22 / 30,
                  mainAxisSpacing: 5,
                  crossAxisSpacing: 3),
              itemBuilder: (ctx, i) {
                return Container(
                  color: Colors.grey,
                  child: Center(
                    child: Text("Item $i"),
                  ),
                );
              },
            ),
          ],
        ),
      ),
    );
  }
}
Share:
146
Admin
Author by

Admin

Updated on January 03, 2023

Comments

  • Admin
    Admin over 1 year

    i trying to make the height of column into dynamically because the more product i have i need more height for the column, down there was my code

    Container(
              width: double.infinity,
              height: 1000,
              child: GridView.builder(
                physics: NeverScrollableScrollPhysics(),
                itemCount: productsList.length,
                gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
                    crossAxisCount: 2,
                    childAspectRatio: 22 / 30,
                    mainAxisSpacing: 5,
                    crossAxisSpacing: 3),
                itemBuilder: (ctx, i) {
                  return ChangeNotifierProvider.value(
                    value: productsList[i],
                    child: FeedsProduct(),
                  );
                },
              ),
            ),
    

    how can i set the container height dynamically?

    • MohammedAli
      MohammedAli about 2 years
      Just Column wrap with Expanded Widget
  • Admin
    Admin about 2 years
    by the way if i put itemcount 30 did it max it my item only for 30 even in database got 40?
  • Omar Alshyokh
    Omar Alshyokh about 2 years
    I added 30 item count just to show you an example. you have to change the item count with your list.length
  • Admin
    Admin about 2 years
    ah forgot about it okok