Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flutter MediaQuery the body size

I am trying to create 3 sections in the body, but I would like that these 3 always cover the entire screen.

Container(
        height: MediaQuery.of(context).size.height / 3,
        width: double.infinity,
        color: Colors.red,
      ),

Result Screen

MediaQuery is taking the entire screen as reference, that is why the last section overflow the same height as the appbar.

Is there any way to use the body as a reference for MediaQuery?

like image 655
Happeh Avatar asked Oct 05 '18 15:10

Happeh


People also ask

What is mediaquery in flutter?

Actually, the MediaQuery establishes a Widget sub-tree in which media queries resolve to the given data. For example, we can read the MediaQueryData.size property from the MediaQueryData returned by MediaQuery.of function and adjusts our Flutter app’s size accordingly.

How to get screen size of device using mediaquery?

MediaQuery is one of the best and simple method to get screen size of width and height. Here MediaQuery.of (context).size.width is used to get device screen’s width and MediaQuery.of (context).size.height is used to get height. ? Another example with height 75% (0.75) and width 50% (0.5) of screen size.

How to get a size object with both dimensions in flutter?

If you want to get a Size object with both dimensions you can get with with just MediaQuery.of (context).size. Very important is to use import ‘package:flutter/material.dart’; to be able to use MediaQuery, too. To avoid another head bang onto the wall there is a bit of a trick for the height value that we need to know.

How to fix flutter widgets with different screen sizes?

Luckily there is a relatively quick fix for this problem in Flutter. We can retrieve the screen size (width or height) and calculate for each widget what ratio shall have relative to the device size. Obviously, this will result in different values for different devices with different screen sizes.


2 Answers

Try using Column with children wrapped by Expanded , each child will have the same size.

  Column(
        children: <Widget>[
          Expanded(
            child: Container(color: Colors.white),
          ),
          Expanded(
            child: Container(color: Colors.red),
          ),
          Expanded(
            child: Container(color: Colors.green),
          ),
        ],
      );
like image 143
diegoveloper Avatar answered Oct 19 '22 10:10

diegoveloper


actually yes when you get the height of container inside body

(MediaQuery.of(context).size.height -
 appBaa.preferredSize.height - MediaQuery.of(context).padding.top) * 0.3,

here we give the %30 of body to this container, you must have a reference of appbar in this case its appBaa and get the height of status bar by padding

like image 24
Omar Falah Avatar answered Oct 19 '22 11:10

Omar Falah