Professional Documents
Culture Documents
Developing For Multiple Screen Sizes and Orientations in Flutter (Fragments in Flutter) - by Deven Joshi - Flutter Community - Medium
Developing For Multiple Screen Sizes and Orientations in Flutter (Fragments in Flutter) - by Deven Joshi - Flutter Community - Medium
Developing For Multiple Screen Sizes and Orientations in Flutter (Fragments in Flutter) - by Deven Joshi - Flutter Community - Medium
Save
https://medium.com/flutter-community/developing-for-multiple-screen-sizes-and-orientations-in-flutter-fragments-in-flutter-a4c51b849434 1/26
3/18/23, 1:37 PM Developing for Multiple Screen Sizes and Orientations in Flutter (Fragments in Flutter) | by Deven Joshi | Flutter Community | Med…
Mobile applications need to support a wide range of device sizes, pixel densities and
orientations. Apps need to be able to scale well, handle orientation changes and persist
data through all these. Flutter gives you the capability to choose the way to tackle these
challenges instead of only giving one particular solution.
https://medium.com/flutter-community/developing-for-multiple-screen-sizes-and-orientations-in-flutter-fragments-in-flutter-a4c51b849434 2/26
3/18/23, 1:37 PM Developing for Multiple Screen Sizes and Orientations in Flutter (Fragments in Flutter) | by Deven Joshi | Flutter Community | Med…
Meaning we have to define one layout file for phones, one for tablets and then both
orientations for each device type. These layouts are then instantiated according to
whichever device is running it. We then check for which layout is active(mobile/tablet)
and initialise accordingly.
For most applications, a master-detail flow is used for handling larger screen sizes,
which uses Fragments. We’ll go into what master-detail flow is in a while.
Let us first look at handling orientation and then handling screen sizes for Flutter.
The example below creates a rudimentary profile page in both orientations and builds
the layout differently depending on the orientation to maximise the use of the width of
the screen. The complete source code will be hosted on GitHub (Link given at the end
of this article).
https://medium.com/flutter-community/developing-for-multiple-screen-sizes-and-orientations-in-flutter-fragments-in-flutter-a4c51b849434 4/26
3/18/23, 1:37 PM Developing for Multiple Screen Sizes and Orientations in Flutter (Fragments in Flutter) | by Deven Joshi | Flutter Community | Med…
https://medium.com/flutter-community/developing-for-multiple-screen-sizes-and-orientations-in-flutter-fragments-in-flutter-a4c51b849434 5/26
3/18/23, 1:37 PM Developing for Multiple Screen Sizes and Orientations in Flutter (Fragments in Flutter) | by Deven Joshi | Flutter Community | Med…
Here we have a simple screen which has different layouts for portrait as well as
landscape. Let’s try to understand how we actually switch layouts in Flutter by creating
the example above.
In concept, we work very similarly to the Android way of doing things. We have two
layouts (not layout files, as Flutter doesn't have those), one for portrait and one for
landscape orientation. When the device changes orientation we rebuild our layout.
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(),
body: OrientationBuilder(
builder: (context, orientation) {
return orientation == Orientation.portrait
? _buildVerticalLayout()
: _buildHorizontalLayout();
},
),
);
}
The OrientationBuilder has a builder function to build our layout. The builder function
is called when the orientation changes. The possible values being Orientation.portrait
or Orientation.landscape.
In this example, we check if the screen is in portrait mode and construct a vertical
layout if that is the case, else we construct a horizontal layout for the screen.
https://medium.com/flutter-community/developing-for-multiple-screen-sizes-and-orientations-in-flutter-fragments-in-flutter-a4c51b849434 6/26
3/18/23, 1:37 PM Developing for Multiple Screen Sizes and Orientations in Flutter (Fragments in Flutter) | by Deven Joshi | Flutter Community | Med…
We can also check the orientation at any point in the code (inside or outside the
OrientationBuilder) using
MediaQuery.of(context).orientation
Note: For that time when we’re lazy and/or only portrait will do, use
SystemChrome.setPreferredOrientations(DeviceOrientation.portraitUp);
First, let’s take a look at the most common use case of it.
Let’s go back to the “Master-Detail Flow” we were going to talk about. When it comes to
apps, you will see a common pattern where you have a Master list of items and when
you click on a list item, you get redirected to a different Detail screen. Take the
example of Gmail, where we have a list of emails and when we click on one, a detail
view opens with the content of the mail.
https://medium.com/flutter-community/developing-for-multiple-screen-sizes-and-orientations-in-flutter-fragments-in-flutter-a4c51b849434 7/26
3/18/23, 1:37 PM Developing for Multiple Screen Sizes and Orientations in Flutter (Fragments in Flutter) | by Deven Joshi | Flutter Community | Med…
https://medium.com/flutter-community/developing-for-multiple-screen-sizes-and-orientations-in-flutter-fragments-in-flutter-a4c51b849434 8/26
3/18/23, 1:37 PM Developing for Multiple Screen Sizes and Orientations in Flutter (Fragments in Flutter) | by Deven Joshi | Flutter Community | Med…
https://medium.com/flutter-community/developing-for-multiple-screen-sizes-and-orientations-in-flutter-fragments-in-flutter-a4c51b849434 9/26
3/18/23, 1:37 PM Developing for Multiple Screen Sizes and Orientations in Flutter (Fragments in Flutter) | by Deven Joshi | Flutter Community | Med…
This app simply holds a list of numbers and displays a number prominently when
tapped on. We have a master list of numbers and a detail view that displays a number
when clicked. Just like the emails.
If we used the same layout in tablets, it would be a rather large waste of space. So what
can we do to solve it? We can have both the master list and detail view on the same
screen as we have the available screen space.
Let’s see how Android tackles this. Android creates reusable components called
Fragments out of the master list and the detail view. A Fragment can be defined
https://medium.com/flutter-community/developing-for-multiple-screen-sizes-and-orientations-in-flutter-fragments-in-flutter-a4c51b849434 10/26
3/18/23, 1:37 PM Developing for Multiple Screen Sizes and Orientations in Flutter (Fragments in Flutter) | by Deven Joshi | Flutter Community | Med…
separately from the screen and just added into the screen without repeating the code
twice.
Search Medium
So Fragment A is the master list fragment and B is the detail fragment. In mobiles or
smaller width layouts, a click on a list item would navigate to a separate page whereas
in tablets it would remain on the same page and change the detail fragment. We can
also do a tablet-like interface when a phone is rotated to landscape.
All we need to do is define two widgets. One for the master list, one for the detail view.
These are, in effect, fragments. We simply check if the device has enough width to
handle both the list and detail part. If it does, we use both widgets. If the device does
not have enough width to support both, we only show the list and navigate to a
separate screen to show the detail content.
We first need to check the device’s width to see if we can use the larger layout instead
of the smaller one. To get the width, we use
MediaQuery.of(context).size.width
The Size gives us the height and width of the device in dps.
https://medium.com/flutter-community/developing-for-multiple-screen-sizes-and-orientations-in-flutter-fragments-in-flutter-a4c51b849434 11/26
3/18/23, 1:37 PM Developing for Multiple Screen Sizes and Orientations in Flutter (Fragments in Flutter) | by Deven Joshi | Flutter Community | Med…
Let’s set the minimum width to 600 dp for switching to the second layout.
Summing up:
1. We create two widgets, one containing the master list and one containing the detail
view.
2. We create two screens. On the first screen, we check if the device has enough
width to handle both widgets.
3. If there is enough width, we add both widgets on one page. If there is not, we
navigate to a second page when a list item is tapped which only has a detail view.
6.4K 17
Let’s code it
Let’s code the demo that I’ve included at the top of this section where we have a list of
numbers and the detail view displays that number. First we make two widgets.
https://medium.com/flutter-community/developing-for-multiple-screen-sizes-and-orientations-in-flutter-fragments-in-flutter-a4c51b849434 12/26
3/18/23, 1:37 PM Developing for Multiple Screen Sizes and Orientations in Flutter (Fragments in Flutter) | by Deven Joshi | Flutter Community | Med…
https://medium.com/flutter-community/developing-for-multiple-screen-sizes-and-orientations-in-flutter-fragments-in-flutter-a4c51b849434 13/26
3/18/23, 1:37 PM Developing for Multiple Screen Sizes and Orientations in Flutter (Fragments in Flutter) | by Deven Joshi | Flutter Community | Med…
ListWidget(
this.count,
this.onItemSelected,
);
@override
_ListWidgetState createState() => _ListWidgetState();
}
In the list, we take how many items we want to display as well as a callback when an
item is clicked. This callback is important as it decides whether to simply change the
detail view on a larger screen or navigate to a different page on a smaller screen.
https://medium.com/flutter-community/developing-for-multiple-screen-sizes-and-orientations-in-flutter-fragments-in-flutter-a4c51b849434 14/26
3/18/23, 1:37 PM Developing for Multiple Screen Sizes and Orientations in Flutter (Fragments in Flutter) | by Deven Joshi | Flutter Community | Med…
We simply display cards for each index and surround it with an InkWell to respond to
taps.
https://medium.com/flutter-community/developing-for-multiple-screen-sizes-and-orientations-in-flutter-fragments-in-flutter-a4c51b849434 15/26
3/18/23, 1:37 PM Developing for Multiple Screen Sizes and Orientations in Flutter (Fragments in Flutter) | by Deven Joshi | Flutter Community | Med…
https://medium.com/flutter-community/developing-for-multiple-screen-sizes-and-orientations-in-flutter-fragments-in-flutter-a4c51b849434 16/26
3/18/23, 1:37 PM Developing for Multiple Screen Sizes and Orientations in Flutter (Fragments in Flutter) | by Deven Joshi | Flutter Community | Med…
DetailWidget(this.data);
@override
_DetailWidgetState createState() => _DetailWidgetState();
}
Notice that these are not screens. These are simply widgets we are going to use on the
screens.
https://medium.com/flutter-community/developing-for-multiple-screen-sizes-and-orientations-in-flutter-fragments-in-flutter-a4c51b849434 17/26
3/18/23, 1:37 PM Developing for Multiple Screen Sizes and Orientations in Flutter (Fragments in Flutter) | by Deven Joshi | Flutter Community | Med…
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(),
body: OrientationBuilder(builder: (context, orientation) {
https://medium.com/flutter-community/developing-for-multiple-screen-sizes-and-orientations-in-flutter-fragments-in-flutter-a4c51b849434 18/26
3/18/23, 1:37 PM Developing for Multiple Screen Sizes and Orientations in Flutter (Fragments in Flutter) | by Deven Joshi | Flutter Community | Med…
This is the main page of the app. We have two variables: selectedValue for storing the
selected list item, and isLargeScreen is a simple boolean which stores if the screen is
large enough to display both the list and detail widgets.
We first check if the width is large enough to display our layout using
https://medium.com/flutter-community/developing-for-multiple-screen-sizes-and-orientations-in-flutter-fragments-in-flutter-a4c51b849434 19/26
3/18/23, 1:37 PM Developing for Multiple Screen Sizes and Orientations in Flutter (Fragments in Flutter) | by Deven Joshi | Flutter Community | Med…
Container(),
If the screen is large, we add a detail widget, and if it is not, we return an empty
container. We use the Expanded widgets around it to fill the screen or divide the screen
into proportions in case of a larger screen. So Expanded allows each widget to fill half
of the screen or even a certain percentage by setting the Flex property.
if (isLargeScreen) {
selectedValue = value;
setState(() {});
} else {
Navigator.push(context, MaterialPageRoute(
builder: (context) {
return DetailPage(value);
},
));
}
Meaning, if the larger layout is used, we don’t need to go to a different screen as the
detail widget is on the page itself. If the screen is smaller, we need to navigate to a
different page as only the list is displayed on the current screen.
And finally,
https://medium.com/flutter-community/developing-for-multiple-screen-sizes-and-orientations-in-flutter-fragments-in-flutter-a4c51b849434 20/26
3/18/23, 1:37 PM Developing for Multiple Screen Sizes and Orientations in Flutter (Fragments in Flutter) | by Deven Joshi | Flutter Community | Med…
https://medium.com/flutter-community/developing-for-multiple-screen-sizes-and-orientations-in-flutter-fragments-in-flutter-a4c51b849434 21/26
3/18/23, 1:37 PM Developing for Multiple Screen Sizes and Orientations in Flutter (Fragments in Flutter) | by Deven Joshi | Flutter Community | Med…
DetailPage(this.data);
@override
_DetailPageState createState() => _DetailPageState();
}
It only holds one detail widget on the page, and is used for displaying the data on
smaller screens.
Now we have a functioning app that adapts to screens of different sizes and their
orientation.
https://medium.com/flutter-community/developing-for-multiple-screen-sizes-and-orientations-in-flutter-fragments-in-flutter-a4c51b849434 22/26
3/18/23, 1:37 PM Developing for Multiple Screen Sizes and Orientations in Flutter (Fragments in Flutter) | by Deven Joshi | Flutter Community | Med…
https://medium.com/flutter-community/developing-for-multiple-screen-sizes-and-orientations-in-flutter-fragments-in-flutter-a4c51b849434 23/26
3/18/23, 1:37 PM Developing for Multiple Screen Sizes and Orientations in Flutter (Fragments in Flutter) | by Deven Joshi | Flutter Community | Med…
2. If you want to have a design for tablets only, instead of checking for width from
MediaQuery, get the size and use it to get actual width instead of width in that specific
orientation. When we used the width from MediaQuery directly, it will get the get the
width in that orientation only. So in landscape mode, the length of the phone is
considered width.
https://github.com/deven98/FlutterAdaptiveLayouts
https://medium.com/flutter-community/developing-for-multiple-screen-sizes-and-orientations-in-flutter-fragments-in-flutter-a4c51b849434 24/26
3/18/23, 1:37 PM Developing for Multiple Screen Sizes and Orientations in Flutter (Fragments in Flutter) | by Deven Joshi | Flutter Community | Med…
Find more content in the Flutter Community and follow us for notifications on Twitter.
Technology
https://medium.com/flutter-community/developing-for-multiple-screen-sizes-and-orientations-in-flutter-fragments-in-flutter-a4c51b849434 25/26
3/18/23, 1:37 PM Developing for Multiple Screen Sizes and Orientations in Flutter (Fragments in Flutter) | by Deven Joshi | Flutter Community | Med…
https://medium.com/flutter-community/developing-for-multiple-screen-sizes-and-orientations-in-flutter-fragments-in-flutter-a4c51b849434 26/26