The Design

I came across this design and found appropriate to explain how to create responsive layouts using Flutter SDK.

What’s the Problem?

When we talk about Flutter, we consider the amazing fact that with Flutter we write code once and run on any platform(Android, iOS, Web, Desktop, Embedded, any many more to come…). This impresses and makes you move to Flutter, no matter which mobile platform you come from.

Source: giphy.com

But, how many times has it happened that you have created a screen for iPhone XS and when you run it on Android Phones, you see the most famous exception — RenderFlowException, because the sizes like 24.0, 16.0, etc. that worked for iPhone doesn't render exactly for Android. This is because both the hardware have different screen configurations like devicePixelRatio and many others.

What is the End Result?

Well, the end product is — we need a codebase that handles portrait, the landscape for mobile and tablets and multiple mobile phone resolutions. Now, when you are targeting these many devices you already covered Desktop and Web.

How to Achieve this?

Here, in this article, I am trying to cover possible solutions but do not forget to check out the official documentation of Flutter, which covers the responsive widgets that can help in creating the ultimate layout. But, there are some more hacks that you need to use to solve our problem.

Strategy -1 Use a mechanism where text and image sizes can be changed based on the width, height, and orientation of the screen. I am obliged to mention the post from Daniele Cambi for his article on using the width and height of the screen to decide the text multipliers, which will make text sizes to not grow or shrink when there is a small screen size phone. Strategy -2 Provide two or more layouts for portrait, landscape and also considering the width of the screen.

Let’s Begin !!! (Strategy — 1 )

I have created a very simple welcome screen mostly dealing with Column, Row and Expanded widgets. We need to have size_config.dart which will give us some multipliers like imageSizeMultiplier , textMultiplier , heightMultiplier , widthMultiplier . These all have meaningful names, that you can easily understand.

You need an entry point for SizeConfig to be initialized. For me, it will be when you load your first widget.

Whenever there is layout change or orientation change, we can get new values for textMultiplier , imageMultiplier , heightMultiplier and widthMultiplier .

In the above code, if we are in landscape mode, maxHeight it becomes screen width and maxWidth becomes the screen height. _blockSizeWidth and _blocSizeHeight are the width and height of the block which is 1/100th of the screen.

It’s all up to you to decide when you consider the width of the screen to be mobile’s screen or any other screen. Here, in my case, I have considered the tablet’s portrait and landscape to be landscape category and mobile as portrait only when the screen width is less than 450.

Text Multiplier

Let us assume the textMultiplier is 8.01234… something. So, you have to first use the Text widget for one mobile size you usually work on and see what font size looks good (apart from the fact many companies have designers tell you that). Suppose the fontSize is 24, then divide fontSize by textMultiplier , let’s call this X. X is the number that multiplied with textMultiplier will give the required fontSize for every possible screen out there.

Not to forgot, FittedBox is not wrong to use while using textMultiplier . I have used a combination of both. It depends on how much space Text has to Fit.

I recommend using ThemeData, TextTheme, and TextStyles in styling.dart as I did for this project.

Image Size Multiplier

Like textMultiplier , imageSizeMultiplier will be used which will scale images according to the screen size.

I mage can also be made responsive to a certain limit by using AspectRatio , BoxFit but again it depends on the space it has to grow and shrink

Certainly, in the project, I required imageMultiplier for Icons and not the Image as I wanted the image to take maximum size possible because it is in Expanded widget. Have a look at the two code snippets below.

Expanded used as Icon parent is in Row to give equal horizontal space.

Portrait & Landscape iPhone Xr Row -> Expanded Example

Height Multiplier & Width Multiplier