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?
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.
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.
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.
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.
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),
),
],
);
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With