Calculating the size dynamically

To calculate the height or the width of a widget dynamically instead of using fixed values, we need to know first how much size is available.

This is where MediaQuery comes into action, we can get information about the current device size, as well as user preferences, and design your layout accordingly. MediaQuery provides a higher-level view of the current app’s screen size and can also give more detailed information about the device and its layout preferences.

We have to connect it to our context ( the metadata object with some information about our widget and its position in the tree ).



Example :

      Column(

        crossAxisAlignmentCrossAxisAlignment.start,

        children: [

          Container(

            childconst Text(

              "50% <-->",

            ),

            widthMediaQuery.of(context).size.width * 0.5,

            colorColors.redAccent,

          ),

          Container(

            childconst Text(

              "70% <-->",

            ),

            widthMediaQuery.of(context).size.width * 0.7,

            colorColors.orangeAccent,

          ),

          SizedBox(heightMediaQuery.of(context).size.height * 0.4),

          Container(

            childconst Text(

              "30% ↑ 90% <-->",

            ),

            heightMediaQuery.of(context).size.height * 0.3,

            widthMediaQuery.of(context).size.width * 0.9,

            colorColors.pink,

          ),

        ],

      )




MediaQuery Flutter

Exercise :

Try to do this using Image.network, MediaQuery and IconButton.


Images :
- dogs
- cat




Solution :

 

class _MyHomePageState extends State<MyHomePage> {

  var sizeheightwidthdog = 0.5cat = 0.5;

  @override

  Widget build(BuildContext context) {

    size = MediaQuery.of(context).size;

    height = size.height;

    width = size.width;

 

    return Scaffold(

      appBarAppBar(

        titleconst Text("Test"),

      ),

      bodyCenter(

        childColumn(

          children: [

            Image.network(

              "https://www.sciencemag.org/sites/default/files/styles/article_main_large/public/dogs_1280p_0.jpg?itok=5P-MAjr-",

              widthwidth * dog,

            ),

            dog == 1

                ? IconButton(

                    onPressed: () {

                      setState(() {

                        dog = 0.5;

                      });

                    },

                    iconconst Icon(Icons.zoom_out))

                : IconButton(

                    onPressed: () {

                      setState(() {

                        dog = 1;

                      });

                    },

                    iconconst Icon(Icons.zoom_in)),

            Image.network(

              "https://static.scientificamerican.com/sciam/cache/file/92E141F8-36E4-4331-BB2EE42AC8674DD3_source.jpg",

              widthwidth * cat,

            ),

            cat == 1

                ? IconButton(

                    onPressed: () {

                      setState(() {

                        cat = 0.5;

                      });

                    },

                    iconconst Icon(Icons.zoom_out))

                : IconButton(

                    onPressed: () {

                      setState(() {

                        cat = 1;

                      });

                    },

                    iconconst Icon(Icons.zoom_in)),

          ],

        ),

      ),

    );

  }

}