How to set Custom height for Widget in GridView in Flutter?

Cover Image for How to set Custom height for Widget in GridView in Flutter?
Matheus Mello
Matheus Mello
published a few days ago. updated a few hours ago

How to set Custom height for Widget in GridView in Flutter? 📏📏📏

So you want to customize the height of widgets in a GridView in Flutter, but no matter what you do, you end up with square widgets? Don't worry, you're not alone! Many developers face this issue when working with GridViews in Flutter. In this blog post, we'll explore the common problems and provide easy solutions to help you achieve the desired custom height for your widgets. Let's get started! 💪💪💪

The Problem 🤔🤔🤔

Let's take a look at the code snippet you provided:

class MyHomePage extends StatefulWidget {
  // ...
}

class _MyHomePageState extends State<MyHomePage> {
  List<String> widgetList = ['A', 'B', 'C'];

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
      ),
      body: Container(
        child: GridView.count(
          crossAxisCount: 2,
          controller: ScrollController(keepScrollOffset: false),
          shrinkWrap: true,
          scrollDirection: Axis.vertical,
          children: widgetList.map((String value) {
            return Container(
              height: 250.0,
              color: Colors.green,
              margin: EdgeInsets.all(1.0),
              child: Center(
                child: Text(
                  value,
                  style: TextStyle(fontSize: 50.0, color: Colors.white),
                ),
              ),
            );
          }).toList(),
        ),
      ),
    );
  }
}

By setting the height property of the Container to 250.0, you expected to get widgets with a custom height of 250.0. However, the output you're getting is a GridView with square widgets. Not what you were aiming for, right? 😫😫😫

The Solution 👍👍👍

To achieve a custom height for your widgets in the GridView, you need to wrap your GridView.count with an Expanded widget. The Expanded widget, when used inside a ListView or GridView, allows its child widget to occupy the remaining space in the scrollable area. By doing this, you ensure that the custom height you set for your widgets is respected. 🙌🙌🙌

Here's the updated code with the solution:

class MyHomePage extends StatefulWidget {
  // ...
}

class _MyHomePageState extends State<MyHomePage> {
  List<String> widgetList = ['A', 'B', 'C'];

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
      ),
      body: Container(
        child: Expanded(
          child: GridView.count(
            crossAxisCount: 2,
            controller: ScrollController(keepScrollOffset: false),
            shrinkWrap: true,
            scrollDirection: Axis.vertical,
            children: widgetList.map((String value) {
              return Container(
                height: 250.0,
                color: Colors.green,
                margin: EdgeInsets.all(1.0),
                child: Center(
                  child: Text(
                    value,
                    style: TextStyle(fontSize: 50.0, color: Colors.white),
                  ),
                ),
              );
            }).toList(),
          ),
        ),
      ),
    );
  }
}

By wrapping the GridView.count with an Expanded widget, you ensure that the custom height of 250.0 is applied to each widget in the GridView. Run the code now, and voila! You should see your desired output with custom height widgets in the GridView. 🎉🎉🎉

Conclusion 🎯🎯🎯

Setting a custom height for widgets in a GridView in Flutter is easily achievable by using the Expanded widget. By following the solutions provided in this blog post, you can now customize the height of widgets in your GridViews with ease. Happy coding! 😊😊😊

Have you faced any other challenges with Flutter development? Let us know in the comments below, and we'll be happy to help you out! 🔥🔥🔥


More Stories

Cover Image for How can I echo a newline in a batch file?

How can I echo a newline in a batch file?

updated a few hours ago
batch-filenewlinewindows

🔥 💻 🆒 Title: "Getting a Fresh Start: How to Echo a Newline in a Batch File" Introduction: Hey there, tech enthusiasts! Have you ever found yourself in a sticky situation with your batch file output? We've got your back! In this exciting blog post, we

Matheus Mello
Matheus Mello
Cover Image for How do I run Redis on Windows?

How do I run Redis on Windows?

updated a few hours ago
rediswindows

# Running Redis on Windows: Easy Solutions for Redis Enthusiasts! 🚀 Redis is a powerful and popular in-memory data structure store that offers blazing-fast performance and versatility. However, if you're a Windows user, you might have stumbled upon the c

Matheus Mello
Matheus Mello
Cover Image for Best way to strip punctuation from a string

Best way to strip punctuation from a string

updated a few hours ago
punctuationpythonstring

# The Art of Stripping Punctuation: Simplifying Your Strings 💥✂️ Are you tired of dealing with pesky punctuation marks that cause chaos in your strings? Have no fear, for we have a solution that will strip those buggers away and leave your texts clean an

Matheus Mello
Matheus Mello
Cover Image for Purge or recreate a Ruby on Rails database

Purge or recreate a Ruby on Rails database

updated a few hours ago
rakeruby-on-railsruby-on-rails-3

# Purge or Recreate a Ruby on Rails Database: A Simple Guide 🚀 So, you have a Ruby on Rails database that's full of data, and you're now considering deleting everything and starting from scratch. Should you purge the database or recreate it? 🤔 Well, my

Matheus Mello
Matheus Mello