'How to create responsive grid in flutter app?

What is best practice to make a flutter app responsive? In the code below is a StraggeredGridView with hardcoded values.

Should I write a method that is calculating the values, depending on the screen size, or are there any other ways to do so?

Thanks for your help!

  StaggeredGridView.count(
    crossAxisCount: 2,
    crossAxisSpacing: 20,
    mainAxisSpacing: 20,
    padding: EdgeInsets.symmetric(horizontal: 20.0, vertical: 20),
    //shrinkWrap: true,
    children: <Widget>[
      _buildTile(
          Padding(
              padding: const EdgeInsets.all(20.0),
            child: Column(children: <Widget>[
              Material(
                  color: Color.fromRGBO(123, 228, 193, 0.5),
                  child: Padding(
                    padding: const EdgeInsets.all(15.0),
                    child: Image.asset(
                      'assets/siren.png',
                      height: 40,
                    ),
                  )),
              Padding(padding: EdgeInsets.only(bottom: 10.0)),
              Text('NOTRUF', style: tileFontStyle),
            ]),
          ),),


Solution 1:[1]

use responsive_grid package

it works like the Bootstrap grid system

Solution 2:[2]

What I've been doing lately is that I get the dimensions of an app when it starts and store them as constants that i use all over the app. check out this as i found it very helpful.

You can also use some widgets that does the scaling on their own like layoutBuilder more on that here

Solution 3:[3]

You can make it responsive just like how you do it in CSS.

Use mathematics! but how?

Wrap your widget with layout builder and use constraints.maxWidth to play with the crossAxisCount attribute:

crossAxisCount:(constraints.maxWidth ~/ 250).toInt()

Solution 4:[4]

Use the MediaQuery.of(context).orientation == Orientation.landscape on crossAxisCount. Then use ternary operator or if/else statement to state crossAxisCount values. See example below. Worked for me.

crossAxisCount: (MediaQuery.of(context).orientation == Orientation.landscape) ? 4 : 2

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Mohamed Ali
Solution 2 Abbas.M
Solution 3 Bermjly Team
Solution 4 Darkhorse