Original address: medium.com/flutter-cla…

Original author: medium.com/hiashutosh

Published: May 27, 2021-3 minutes to read

If you are coming to this page directly, make sure that before starting this page, you first go through the journey to Flutter Web: 2.

In this article, we’ll discuss how to make applications responsive on mobile and on the web while maintaining a single code base.

Here are some of the widgets that Flutter provides for building a responsive layout.

  • CustomSingle Layout
  • CustomMultiChildLayout
  • The right box
  • Decimal matrix diagram (FractionallySizedBox)
  • Layout generator
  • Media queries
  • Media query data
  • Direction builder
  • Aspect ratio

These widgets will help you make your application responsive on devices of different sizes, but the same user interface on each device doesn’t look intuitive. What if you want to display different user interfaces on devices of different sizes?

Here is a screenshot of the Rally project’s responsive user interface, which has different user interfaces for mobile and desktop views.

Rally desktop interface

Rally mobile version of the interface

So, in this case, you need to use a breakpoint that will change the UI when the device width exceeds or falls below that breakpoint. For example, if my breakpoint is 700px, then the mobile UI is displayed when the device width is ≤700, the tablet UI is displayed when the device width is >700 and the device width is <=1200, and the desktop UI is displayed when the device width is >1200.

If you don’t want to define your own breakpoints, you can use this package, which has many breakpoints.

Here’s how to do this with a helper class.

import 'package:flutter/material.dart';

/// list of all devices
enum DeviceType {
  desktop,
  tablet,
  handset,
}

/// breakpoints for desktop, tablet and handset
final desktop = 1200;
final tablet = 900;
final handset = 600;

DeviceType _displayTypeOf(BuildContext context) {
  /// Use shortestSide to detect device type regardless of orientation
  double deviceWidth = MediaQuery.of(context).size.shortestSide;

  if (deviceWidth > desktop) {
    return DeviceType.desktop;
  }
  if (deviceWidth > tablet) {
    return DeviceType.tablet;
  } else {
    returnDeviceType.handset; }}bool isDeviceDesktop(BuildContext context) {
  return _displayTypeOf(context) == DeviceType.desktop;
}

bool isDeviceTab(BuildContext context) {
  return _displayTypeOf(context) == DeviceType.tablet;
}

bool isDeviceMobile(BuildContext context) {
  return _displayTypeOf(context) == DeviceType.handset;
}
Copy the code

Let’s see how to use the helper class above.

Container(
  child: isDeviceDesktop(context)
      ? _buildDesktopUI()
      : isDeviceTab()
          ? _buildTabletUI()
          : _buildMobileUI(),
)
Copy the code

See these sample projects for your reference.

  1. aqi_monitor
  2. flutter_gallery

If you find this article useful, please click on the clap icon 👏 and share it with your friends as this will inspire me to write more articles. If you have any questions about Flutter, please feel free to contact me on social media platforms.

Linktr. Ee/hiahutoshsi…


Translation via www.DeepL.com/Translator (free version)