DEV Community

Cover image for Building Responsive UI in Flutter: Make Your Apps Shine on Any Screen Size
مصعب يوسف
مصعب يوسف

Posted on Originally published at ms3byoussef.medium.com on

Building Responsive UI in Flutter: Make Your Apps Shine on Any Screen Size

Building Responsive UI in Flutter: Make Your Apps Shine on Any Screen Size

Creating a visually appealing and consistent user interface across different devices can be a challenging task, but it’s crucial for providing a great user experience. With Flutter, building responsive UIs for phones, tablets, and desktops is made easier thanks to its powerful framework and various community packages. In this article, we’ll explore four popular packages that help you build responsive UIs and show you how to create layouts that look great on any screen size. We’ll also cover how to add side menus, handle different breakpoints, and ensure a consistent user experience across all devices.

Understanding Responsive Design in Flutter

Responsive design is about creating UIs that adjust smoothly to various screen sizes and orientations, ensuring an excellent user experience no matter the device. In Flutter, you can use built-in features like MediaQuery, layout widgets like Flexible and Expanded, or rely on third-party packages to simplify your development process.

Responsive design in Flutter is not just about adjusting the size of widgets but also about creating layouts that provide the best experience for users on each device. This includes making sure that elements are appropriately spaced, fonts are legible, and navigation is intuitive. By understanding these core principles, you can create apps that users love across all devices.

Here, we’ll focus on four powerful packages that make building responsive UIs in Flutter easier:

  1. flutter_screenutil
  2. responsive_builder
  3. responsive_framework
  4. sizer

Let’s dive into each package and learn how they can help you create a beautiful responsive UI.

1. flutter_screenutil

flutter_screenutilis a widely-used package that helps you create responsive and adaptive user interfaces by providing a way to define widget dimensions and fonts relative to the device's screen size. It simplifies the process of designing consistent layouts for different devices.

This package is particularly useful when you want to maintain consistent sizes for fonts, padding, and other layout elements across different screen sizes. By defining a base design size, flutter_screenutil automatically scales elements to fit the current device's screen.

Example: Responsive Layout for Mobile, Tablet, and Desktop with Side Menu

import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';

void main() {
  runApp(MyApp());
}
class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return ScreenUtilInit(
      designSize: Size(360, 690),
      builder: () => MaterialApp(
        home: ResponsiveHomePage(),
      ),
    );
  }
}
class ResponsiveHomePage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Responsive Home', style: TextStyle(fontSize: 20.sp)),
      ),
      drawer: ResponsiveBuilder(
        builder: (context, sizingInformation) {
          if (sizingInformation.deviceScreenType == DeviceScreenType.tablet ||
              sizingInformation.deviceScreenType == DeviceScreenType.desktop) {
            return Drawer(
              child: ListView(
                padding: EdgeInsets.zero,
                children: <Widget>[
                  DrawerHeader(
                    decoration: BoxDecoration(
                      color: Colors.blue,
                    ),
                    child: Text('Menu', style: TextStyle(fontSize: 24)),
                  ),
                  ListTile(
                    title: Text('Item 1', style: TextStyle(fontSize: 18)),
                    onTap: () {},
                  ),
                  ListTile(
                    title: Text('Item 2', style: TextStyle(fontSize: 18)),
                    onTap: () {},
                  ),
                ],
              ),
            );
          } else {
            return null;
          }
        },
      ),
            ),
            ListTile(
              title: Text('Item 1', style: TextStyle(fontSize: 18.sp)),
              onTap: () {},
            ),
            ListTile(
              title: Text('Item 2', style: TextStyle(fontSize: 18.sp)),
              onTap: () {},
            ),
          ],
        ),
      ),
      body: Padding(
        padding: EdgeInsets.all(16.w),
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            Text('Welcome to our responsive app!', style: TextStyle(fontSize: 24.sp)),
            SizedBox(height: 20.h),
            Container(
              width: 0.8.sw,
              height: 150.h,
              color: Colors.blue,
              child: Center(
                child: Text('This container adapts to screen size!', style: TextStyle(fontSize: 18.sp)),
              ),
            ),
          ],
        ),
      ),
    );
  }
}
Enter fullscreen mode Exit fullscreen mode

In the above example, ScreenUtilInit is used to initialize the package, and sp, sw, sh, and w are used to adapt the layout to different screen sizes, ensuring that fonts and widget dimensions are scaled accordingly.

2. responsive_builder

responsive_builder is another package that simplifies the process of building responsive UIs by providing utilities to differentiate layouts based on the screen type (e.g., mobile, tablet, desktop). It makes your code cleaner and easier to maintain by providing clear conditions for different device types.

The responsive_builder package is great for developers who want to create device-specific UIs without writing a lot of conditional logic. It provides an easy way to specify different widgets for mobile, tablet, and desktop layouts, allowing for a more tailored user experience.

Example: Responsive Layout Using responsive_builder with Side Menu

import 'package:flutter/material.dart';
import 'package:responsive_builder/responsive_builder.dart';

class ResponsiveHomePage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Responsive Home with ResponsiveBuilder'),
      ),
      drawer: ResponsiveBuilder(
        builder: (context, sizingInformation) {
          if (sizingInformation.deviceScreenType == DeviceScreenType.tablet ||
              sizingInformation.deviceScreenType == DeviceScreenType.desktop) {
            return Drawer(
              child: ListView(
                padding: EdgeInsets.zero,
                children: <Widget>[
                  DrawerHeader(
                    decoration: BoxDecoration(
                      color: Colors.blue,
                    ),
                    child: Text('Menu', style: TextStyle(fontSize: 24)),
                  ),
                  ListTile(
                    title: Text('Item 1', style: TextStyle(fontSize: 18)),
                    onTap: () {},
                  ),
                  ListTile(
                    title: Text('Item 2', style: TextStyle(fontSize: 18)),
                    onTap: () {},
                  ),
                ],
              ),
            );
          } else {
            return null;
          }
        },
      ),
            ),
            ListTile(
              title: Text('Item 1', style: TextStyle(fontSize: 18)),
              onTap: () {},
            ),
            ListTile(
              title: Text('Item 2', style: TextStyle(fontSize: 18)),
              onTap: () {},
            ),
          ],
        ),
      ),
      body: ScreenTypeLayout(
        mobile: MobileHomePage(),
        tablet: TabletHomePage(),
        desktop: DesktopHomePage(),
      ),
    );
  }
}
class MobileHomePage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Center(child: Text('Mobile Layout'));
  }
}
class TabletHomePage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Center(child: Text('Tablet Layout'));
  }
}
class DesktopHomePage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Center(child: Text('Desktop Layout'));
  }
}
Enter fullscreen mode Exit fullscreen mode

The responsive_builder package makes it easy to define different layouts for mobile, tablet, and desktop devices by using ScreenTypeLayout. This approach keeps your code organized and helps you create a consistent experience for different screen sizes.

3. responsive_framework

responsive_framework helps you make your Flutter app responsive by wrapping your app with a ResponsiveWrapper widget. It provides breakpoints and handles layout changes automatically, making it a powerful choice for building adaptive UIs.

responsive_framework is particularly useful for developers who want more control over how their app behaves at various breakpoints. With features like scaling and resizing, this package helps you deliver a polished experience across all devices.

Example: Using responsive_framework for Adaptive Layouts with Side Menu

import 'package:flutter/material.dart';
import 'package:responsive_framework/responsive_framework.dart';

void main() {
  runApp(MyApp());
}
class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      builder: (context, widget) => ResponsiveWrapper.builder(
        widget,
        defaultScale: true,
        breakpoints: [
          ResponsiveBreakpoint.resize(480, name: MOBILE),
          ResponsiveBreakpoint.autoScale(800, name: TABLET),
          ResponsiveBreakpoint.autoScale(1000, name: DESKTOP),
        ],
      ),
      home: ResponsiveHomePage(),
    );
  }
}
class ResponsiveHomePage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Responsive Home with Responsive Framework'),
      ),
      drawer: ResponsiveBuilder(
        builder: (context, sizingInformation) {
          if (sizingInformation.deviceScreenType == DeviceScreenType.tablet ||
              sizingInformation.deviceScreenType == DeviceScreenType.desktop) {
            return Drawer(
              child: ListView(
                padding: EdgeInsets.zero,
                children: <Widget>[
                  DrawerHeader(
                    decoration: BoxDecoration(
                      color: Colors.blue,
                    ),
                    child: Text('Menu', style: TextStyle(fontSize: 24)),
                  ),
                  ListTile(
                    title: Text('Item 1', style: TextStyle(fontSize: 18)),
                    onTap: () {},
                  ),
                  ListTile(
                    title: Text('Item 2', style: TextStyle(fontSize: 18)),
                    onTap: () {},
                  ),
                ],
              ),
            );
          } else {
            return null;
          }
        },
      ),
            ),
            ListTile(
              title: Text('Item 1', style: TextStyle(fontSize: 18)),
              onTap: () {},
            ),
            ListTile(
              title: Text('Item 2', style: TextStyle(fontSize: 18)),
              onTap: () {},
            ),
          ],
        ),
      ),
      body: Center(
        child: Text('This layout adapts based on the breakpoints defined.'),
      ),
    );
  }
}
Enter fullscreen mode Exit fullscreen mode

With responsive_framework, you can easily define breakpoints to control how your UI adapts to different screen sizes, ensuring your app looks great on all devices.

4. sizer

sizer is another powerful package that makes it easy to create responsive UIs in Flutter. It allows you to define widget dimensions and fonts based on the percentage of the screen height and width.

The sizer package provides a simple and intuitive way to handle responsive design. By using percentage-based dimensions, it allows you to create layouts that look good on any screen size without the need for complex calculations or conditions.

Example: Using sizer for Responsive Layout with Side Menu

import 'package:flutter/material.dart';
import 'package:sizer/sizer.dart';

void main() {
  runApp(MyApp());
}
class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Sizer(
      builder: (context, orientation, deviceType) {
        return MaterialApp(
          home: ResponsiveHomePage(),
        );
      },
    );
  }
}
class ResponsiveHomePage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Responsive Home with Sizer', style: TextStyle(fontSize: 12.sp)),
      ),
      drawer: ResponsiveBuilder(
        builder: (context, sizingInformation) {
          if (sizingInformation.deviceScreenType == DeviceScreenType.tablet ||
              sizingInformation.deviceScreenType == DeviceScreenType.desktop) {
            return Drawer(
              child: ListView(
                padding: EdgeInsets.zero,
                children: <Widget>[
                  DrawerHeader(
                    decoration: BoxDecoration(
                      color: Colors.blue,
                    ),
                    child: Text('Menu', style: TextStyle(fontSize: 18.sp)),
                  ),
                  ListTile(
                    title: Text('Item 1', style: TextStyle(fontSize: 15.sp)),
                    onTap: () {},
                  ),
                  ListTile(
                    title: Text('Item 2', style: TextStyle(fontSize: 15.sp)),
                    onTap: () {},
                  ),
                ],
              ),
            );
          } else {
            return null;
          }
        },
      ),
            ),
            ListTile(
              title: Text('Item 1', style: TextStyle(fontSize: 15.sp)),
              onTap: () {},
            ),
            ListTile(
              title: Text('Item 2', style: TextStyle(fontSize: 15.sp)),
              onTap: () {},
            ),
          ],
        ),
      ),
      body: Padding(
        padding: EdgeInsets.all(5.w),
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            Text('Welcome to our responsive app!', style: TextStyle(fontSize: 15.sp)),
            SizedBox(height: 5.h),
            Container(
              width: 80.w,
              height: 20.h,
              color: Colors.blue,
              child: Center(
                child: Text('This container adapts to screen size!', style: TextStyle(fontSize: 10.sp)),
              ),
            ),
          ],
        ),
      ),
    );
  }
}
Enter fullscreen mode Exit fullscreen mode

The sizer package uses percentage-based dimensions, making it easy to create layouts that adapt to different screen sizes while maintaining consistent proportions.

Conclusion

Building responsive UIs in Flutter doesn’t have to be complicated. By using packages like flutter_screenutil, responsive_builder, responsive_framework, and sizer, you can create apps that look great across phones, tablets, and desktops. Each package offers different features that help you adapt your UI to various screen sizes, so choose the one that best fits your needs and start building responsive apps today!

To ensure your app provides the best user experience, consider the specific needs of your audience and the devices they use. Whether you’re building for mobile-first users or creating a web application that also needs to work on tablets and desktops, the packages discussed here will provide the tools you need to succeed. Take advantage of their features, experiment with different layouts, and see how they work in real-world scenarios.

https://pub.dev/packages/responsive_builder

Top comments (0)