Flutter set Container view layout width height in Percentage Responsive

Flutter has a inbuilt package named as MediaQuery. MediaQuery class is used to get current media screen dimensions known as device screen size. There is a widget named as FractionallySizedBox in flutter which directly supports width height in percentage format but there is a loop in FractionallySizedBox widget. This widget can only be used as single child alone and you cannot use it with multiple children. So here comes our this tutorial with solution for making each child element width height in percentage format. We are using MediaQuery package functions to get current device screen width height then we would multiply them with percentage value. So in this tutorial we would set Container view layout width height in Percentage Responsive View format in flutter android iOS app using MediaQuery.

Contents in this project set Container view layout width height in Percentage Responsive Format in Flutter Android iOS App:

1. Import material.dart package in your app’s main.dart file.

2. Create void main runApp() method and call our main MyApp view class.

3. Create a class named as BoxScreen extends with StatelessWidget. This is our view class. We cannot directly call MediaQuery class in our main class because it has to define under MaterialApp widget. So the only way to define Media Query under Material App is to create this in another class then call in our main class.

  • MediaQuery.of(context).size.width : To get current device width.
  • MediaQuery.of(context).size.height : To get current device height.
  • deviceHeight * 0.30  : Here 0.30 means 30% of total device height.
  • deviceWidth * 0.30 : Here 0.30 means 30% of total device width.

4. Create our main View class named as MyApp extends with StatelessWidget.

  • BoxScreen() : Calling our box screen class and it will automatically draw complete above widget  on screen.

5. Complete source code for main.dart file:

Screenshot:

Flutter set Container view layout width height in Percentage Responsive

Leave a Reply

Your email address will not be published.