· Flutter Deep Dives · 3 min read

Building a responsive grid layout with `MediaQuery` in Flutter

One of the crucial aspects of creating a modern and user-friendly mobile app is making sure it ensures a consistent and visually appealing experience across different screen sizes and orientations. In Flutter, we can achieve this by using MediaQuery to create a responsive grid layout.

What is MediaQuery?

MediaQuery is a class in Flutter that provides access to information regarding the current media or device specifications, such as the size of the screen or the device’s orientation. We can leverage this information to design our grid layout that adapts to different screen sizes.

Creating the Grid Layout

Let’s go through an example of building a responsive grid layout using MediaQuery in Flutter.

  1. Importing Required Packages

First, ensure that you have imported the necessary packages for Flutter layout design:

import 'package:flutter/material.dart';
  1. Defining the Screen Layout

Now, define the layout of your screen using the build method in your Flutter widget:

class MyGridScreen extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Responsive Grid Layout'),
      ),
      body: _buildGrid(context),
    );
  }

  Widget _buildGrid(BuildContext context) {
    final screenWidth = MediaQuery.of(context).size.width;

    return GridView.builder(
      gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
        crossAxisCount: _calculateCrossAxisCount(screenWidth),
      ),
      itemCount: 10, // Replace with your desired number of grid items.
      itemBuilder: (BuildContext context, int index) {
        // Replace with your grid item widget.
        return Container(
          child: Text('Grid Item $index'),
        );
      },
    );
  }

  int _calculateCrossAxisCount(double screenWidth) {
    // Adjust the values according to your preferences.
    if (screenWidth < 600) {
      return 2;
    } else if (screenWidth < 900) {
      return 3;
    } else {
      return 4;
    }
  }
}

Let’s break down the code:

  • _buildGrid method retrieves the width of the screen using MediaQuery and determines the number of columns to be displayed in the grid layout by calling _calculateCrossAxisCount.
  • _calculateCrossAxisCount method is used to set different column counts based on the screen width. Adjust these values to suit your design requirements.
  • GridView.builder is used to create a grid view with a dynamic number of grid items. Customize the itemCount property as per your needs.
  • Inside the itemBuilder, you can replace the Container widget with your desired widget for each grid item.
  1. Displaying the Grid Layout

To display the grid layout, simply call MyGridScreen widget in the runApp method:

void main() {
  runApp(
    MaterialApp(
      home: MyGridScreen(),
    ),
  );
}

Conclusion

In this blog post, we explored how to create a responsive grid layout using MediaQuery in Flutter. By leveraging the device’s screen size information, we can design layouts that adapt and look great on different devices. With this knowledge, you can now create visually appealing and user-friendly grid-based experiences in your Flutter apps.

#flutter #responsivedesign

Share:
Back to Blog