How to Build Responsive Apps with Flutter

SHARJEEL UR REHMAN
5 min readSep 9, 2023

--

https://youtu.be/n-eiP3xchsw

Have you ever wondered how to create apps that look and work great on any device, from a small phone to a large tablet, or even a desktop computer? If so, you are not alone. Many developers face the challenge of creating apps that can adapt to different screen sizes and orientations, without compromising on performance or user experience. This is where Flutter comes in.

Flutter is a framework for building cross-platform applications with a single code-base. It allows you to create beautiful and responsive UIs with widgets, animations, and themes. In this article, I will show you how to use Flutter to create apps that are responsive and adaptive, meaning that they can adjust to the available screen space and device type.

Responsive and adaptive apps are essential for providing a consistent and seamless user experience across different devices. They can also help you reach a wider audience and increase user retention and engagement. By using Flutter, you can save time and effort by writing one codebase that works on multiple platforms, without sacrificing quality or functionality.

In this article, you will learn:

  • The difference between responsive and adaptive apps
  • How to use LayoutBuilder and MediaQuery to create responsive layouts
  • How to use platform-specific features and widgets to create adaptive UIs
  • Some tips and tricks for improving your Flutter development skills

Responsive vs Adaptive Apps

Responsive app is one that has its layout tuned for the available screen size. It can change its layout depending on the width, height, aspect ratio, or orientation of the screen. For example, a responsive app might show a list of items in a row on a wide screen, but in a column on a narrow screen.

An adaptive app is one that has its UI tailored for the device type. It can change its UI components depending on the input method, platform conventions, or device features. For example, an adaptive app might show a bottom navigation bar on a mobile device, but a side drawer on a desktop device.

How can we achieve this kind of responsiveness and adaptiveness with Flutter? Let’s find out in the next sections.

Creating Responsive Layouts with Layout-builder and Media-query

LayoutBuilder is a widget that provides you with the constraints of its parent widget. You can use these constraints to decide what layout to display based on the available space. For example, you can use LayoutBuilder to show a different number of columns in a grid view depending on the screen width.

MediaQuery is a class that provides you with information about the device’s screen size, orientation, brightness, text scale factor, etc. You can use these values to adjust your layout based on the device’s characteristics. For example, you can use MediaQuery to show or hide some widgets based on the device’s orientation.

Here is an example of how to use LayoutBuilder and MediaQuery to create responsive layouts in Flutter:

import 'package:flutter/material.

class ResponsiveLayout extends StatelessWidget {
@override
Widget build(BuildContext context) {
// Get the screen size from MediaQuery
final screenSize = MediaQuery.of(context).size;

// Get the orientation from MediaQuery
final orientation = MediaQuery.of(context).

// Use LayoutBuilder to get the parent widget's constraints
return LayoutBuilder(
builder: (context, constraints) {
// Decide the number of columns based on the screen width
int columns;
if (constraints.maxWidth < 600) {
columns = 2;
} else if (constraints.maxWidth < 900) {
columns = 3;
} else {
columns = 4;
}

// Decide whether to show or hide the title based on the orientation
bool showTitle = orientation == Orientation.portrait;

// Return a Scaffold widget with the responsive layout
return Scaffold(
appBar: AppBar(
title: Text('Responsive Layout'),
),
body: GridView.count(
crossAxisCount: columns,
children: List.generate(16, (index) {
return Center(
child: Text(
'Item $index',
style: Theme.of(context).textTheme.
),
);
}),
),
floatingActionButton: showTitle
? FloatingActionButton(
onPressed: () {},
child: Icon(Icons.add),
)
: null,
);
},
);
}
}

In this example, we use MediaQuery to get the screen size and orientation, and use LayoutBuilder to get the parent widget’s constraints. We use these values to decide the number of columns in the grid view and whether to show or hide the floating action button. We return a Scaffold widget with the responsive layout.

Creating Adaptive UIs with Platform-specific Features and Widgets

Flutter allows you to access some platform-specific features through plugins or channels. You can use these features to enhance your app’s functionality or appearance on different platforms. For example, you can use the url_launcher plugin to open URLs in the browser or the app_settings plugin to open the device’s settings app.

Flutter also provides some widgets that automatically adapt to the platform’s conventions or guidelines. You can use these widgets to create UIs that look native on different platforms. For example, you can use the PlatformAppBar widget from the flutter_platform_widgets package to show an app bar that follows the Material Design or Cupertino style. Here is an example of how to use platform-specific features and widgets to create adaptive UIs in Flutter:

mport 'package:flutter/material.
import 'package:flutter_platform_
import 'package:url_launcher/url_
import 'package:app_settings/app_

class AdaptiveUI extends StatelessWidget {
@override
Widget build(BuildContext context) {
// Use PlatformAppBar widget to show an adaptive app bar
return PlatformScaffold(
appBar: PlatformAppBar(
title: Text('Adaptive UI'),
trailingActions: [
// Use IconButton widget to show an icon button
IconButton(
icon: Icon(Icons.settings),
onPressed: () {
// Use app_settings plugin to open the device's settings app
AppSettings.openAppSettings();
},
),
],
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
// Use PlatformText widget to show an adaptive text
PlatformText(
'This is an adaptive UI',
style: Theme.of(context).textTheme.
),
SizedBox(height: 16),
// Use PlatformButton widget to show an adaptive button
PlatformButton(
child: PlatformText('Open Flutter website'),
onPressed: () {
// Use url_launcher plugin to open the Flutter website in the browser
launch('https://flutter.dev');
},
),
],
),
),
);
}

In this example, we use the flutter_platform_widgets package to show some widgets that adapt to the platform’s style. We use the PlatformAppBar widget to show an app bar that follows the Material Design or Cupertino style. We use the PlatformText widget to show a text that follows the platform’s font and size. We use the PlatformButton widget to show a button that follows the platform’s shape and color.

We also use some plugins to access some platform-specific features. We use the url_launcher plugin to open URLs in the browser. We use the app_settings plugin to open the device’s settings app.

Tips and Tricks for Improving Your Flutter Development Skill

  • Use null safety to avoid null errors and crashes in your code.
  • Use Flutter packages to add functionality or features to your app without reinventing the wheel.
  • Use data classes to simplify your data models and reduce boilerplate code.
  • Use linting rules to enforce consistent coding style and best practices in your project.
  • Use snippets to speed up your coding process and avoid typos.

These are some of the tips and tricks that I found useful for improving my Flutter development skills.

--

--

SHARJEEL UR REHMAN
SHARJEEL UR REHMAN

Written by SHARJEEL UR REHMAN

Experienced Flutter dev with 3 yrs exp. Specializes in mobile apps. Skilled in , Clean Architecture, Bloc,Focuses on scalable, maintainable code.

Responses (1)