Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't set a GridView in a Column

Tags:

flutter

dart

I need to set many items into a grid view. But I need to write something above the grid view and that's why I need a column which contains the texts and the grid view. But If I set some text and below the grid view then it doesn't work. Here is my code:

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Hamim Shop',
      home: Scaffold(
        body: Container(
          padding: const EdgeInsets.all(8.0),
          child: Column(
            children: [
              Text('Hamim Shop'),
              GridView.count(
                crossAxisCount: 3,
                children: List.generate(choices.length, (index) {
                  return Center(
                    child: ChoiceCard(choice: choices[index]),
                  );
                }),
              ),
            ],
          ),
        ),
      ),
    );
  }
}

Edited from Here:

After adding shrinkWrap: true this error is showed

Choice Card:

class ChoiceCard extends StatelessWidget {
  const ChoiceCard({Key key, this.choice}) : super(key: key);

  final Choice choice;

  @override
  Widget build(BuildContext context) {
    return Card(
        color: Colors.white,
        child: Center(
          child: Column(
              mainAxisSize: MainAxisSize.min,
              crossAxisAlignment: CrossAxisAlignment.center,
              children: [
                Expanded(child: Icon(choice.icon, size: 150)),
                Text(choice.title),
              ]),
        ));
  }
}

...

class Choice {
  const Choice({this.title, this.icon});
  final String title;
  final IconData icon;
}

const List<Choice> choices = const [
  const Choice(title: 'Car', icon: Icons.directions_car),
  .....
];
like image 889
Santo Shakil Avatar asked Dec 17 '25 11:12

Santo Shakil


1 Answers

Use shrinkwrap:true & physics:NeverScrollableScrollPhysics()

physics: Scroll physics that does not allow the user to scroll. Means only Column+SingleChildScrollView Scrolling work.

shrinkwrap:

If you do not set the shrinkWrap property, your GridView will be as big as its parent.

If you set it to true, the GridView will wrap its content and be as big as its children allow it to be.

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Hamim Shop',
      home: Scaffold(
        body: Container(
          padding: const EdgeInsets.all(8.0),
          child: SingleChildScrollView(
            child: Column(
              children: [
                Text('Hamim Shop'),
                GridView.count(
                  shrinkWrap: true,
                  physics: NeverScrollableScrollPhysics(),
                  crossAxisCount: 3,
                  children: List.generate(choices.length, (index) {
                    return Center(
                      child: ChoiceCard(choice: choices[index]),
                    );
                  }),
                ),
              ],
            ),
          ),
        ),
      ),
    );
  }
}

class ChoiceCard extends StatelessWidget {
  const ChoiceCard({Key key, this.choice}) : super(key: key);

  final Choice choice;

  @override
  Widget build(BuildContext context) {
    return Card(
        color: Colors.white,
        child: Center(
          child: Column(
              mainAxisSize: MainAxisSize.max,
              crossAxisAlignment: CrossAxisAlignment.center,
              children: [
                Expanded(child: Icon(choice.icon, size: 150)),
                Text(choice.title),
              ]),
        ));
  }
}
class Choice {
  const Choice({this.title, this.icon});
  final String title;
  final IconData icon;
}

const List<Choice> choices = const [
const Choice(title: 'Car', icon: Icons.directions_car),
....
];
like image 81
Jitesh Mohite Avatar answered Dec 20 '25 08:12

Jitesh Mohite



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!