How can a container perfectly wrap a gridview flutter

Viewed 89

I am creating an app and I need a GridView in it. It didn't work with return GridView so I had to wrap it with a container. The problem is that if I don't define a precise height to the container, the code doesn't work. So I want the Container to perfectly match the size of the GridView whatever its height is. I tried to use the Wrap widget but didn't succeed. Can you help?

return Container(
      //height: 10000, I want the height to adapt with the GridView
      child: GridView.count(
          scrollDirection: Axis.vertical,
          crossAxisCount: 2,
          children: List.generate(List.length, (index) {
            return Padding(
              padding: const EdgeInsets.all(8.0),
              child: Container(
                decoration: BoxDecoration(
                  borderRadius: BorderRadius.circular(20),
                  color: Colors.black,
                  // image: [index].img,
                  boxShadow: [
                    BoxShadow(
                      color: Colors.grey.withOpacity(0.8),
                      spreadRadius: 2,
                      blurRadius: 2,
                      offset: Offset(0, 3), // changes position of shadow
                    ),
                  ],
                ),
              ),
            );
          })),
    );
1 Answers

Without the rest of code it's a little difficult to know what's causing this.

However, I recommend that you place your GridView widget into a Expanded widget.

In this way, you don't need wrap the GridView with a Container.

Expanded(
    child:GridView.count(
      scrollDirection: Axis.vertical,
      crossAxisCount: 2,
      children: List.generate(4, (index) {
        return Padding(
          padding: const EdgeInsets.all(8.0),
          child: Container(
            decoration: BoxDecoration(
              borderRadius: BorderRadius.circular(20),
              color: Colors.black,
              // image: [index].img,
              boxShadow: [
                BoxShadow(
                  color: Colors.grey.withOpacity(0.8),
                  spreadRadius: 2,
                  blurRadius: 2,
                  offset: Offset(0, 3), // changes position of shadow
                ),
              ],
            ),
          ),
        );
      },
    ),
  ),
),
Related