• Stars
    star
    474
  • Rank 92,640 (Top 2 %)
  • Language
    Dart
  • License
    MIT License
  • Created about 5 years ago
  • Updated over 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

A set of widgets to make responsive UI building in flutter more readable

Responsive UI in Flutter Banner

Responsive Builder πŸ’»βž‘οΈπŸ–₯βž‘οΈπŸ“±βž‘οΈβŒšοΈ

The responsive builder package contains widgets that allows you to create a readable responsive UI. The package is inspired by the Responsive UI Flutter series created by FilledStacks.

It aims to provide you with widgets that make it easy to build different UI's along two different Axis. Orientation x ScreenType. This means you can have a separate layout for Mobile - Landscape, Mobile - Portrait, Tablet - Landscape and Tablet-Portrait.

If you follow along with the series you will have a complete understanding of how it's built and how to use it. Part 2 goes over how we build the example included in this project.

Responsive Layout Preview

Installation

Add responsive_builder as dependency to your pubspec file.

responsive_builder:

Usage

This package provides a widget called ResponsiveBuilder that provides you with a builder function that returns the current SizingInformation. The SizingInformation includes the DeviceScreenType, screenSize and localWidgetSize. This can be used for fine grained responsive control from a view level down to per widget responsive level.

Responsive Builder

The ResponsiveBuilder is used as any other builder widget.

// import the package
import 'package:responsive_builder/responsive_builder.dart';

// Use the widget
ResponsiveBuilder(
    builder: (context, sizingInformation) {
      // Check the sizing information here and return your UI
          if (sizingInformation.deviceScreenType == DeviceScreenType.desktop) {
          return Container(color:Colors.blue);
        }

        if (sizingInformation.deviceScreenType == DeviceScreenType.tablet) {
          return Container(color:Colors.red);
        }

        if (sizingInformation.deviceScreenType == DeviceScreenType.watch) {
          return Container(color:Colors.yellow);
        }

        return Container(color:Colors.purple);
      },
    },
  );
}

This will return different colour containers depending on which device it's being shown on. A simple way to test this is to either run your code on Flutter web and resize the window or add the device_preview package and view on different devices.

Orientation Layout Builder

This widget can be seen as a duplicate of the OrientationBuilder that comes with Flutter, but the point of this library is to help you produce a readable responsive UI code base. As mentioned in the follow along tutorial I didn't want responsive code riddled with conditionals around orientation, MediaQuery or Renderbox sizes. That's why I created this builder.

The usage is easy. Provide a builder function that returns a UI for each of the orientations.

// import the package
import 'package:responsive_builder/responsive_builder.dart';

// Return a widget function per orientation
OrientationLayoutBuilder(
  portrait: (context) => Container(color: Colors.green),
  landscape: (context) => Container(color: Colors.pink),
),

This will return a different coloured container when you swap orientations for your device. In a more readable manner than checking the orientation with a conditional.

Sometimes you want your app to stay in a certain orientation. use mode property in OrientationLayoutBuilder to enforce this.

OrientationLayoutBuilder(
  /// default mode is 'auto'
  mode: info.isMobile
    ? OrientationLayoutBuilderMode.portrait
    : OrientationLayoutBuilderMode.auto,
  ...
),

Screen Type Layout

This widget is similar to the Orientation Layout Builder in that it takes in Widgets that are named and displayed for different screen types.

// import the package
import 'package:responsive_builder/responsive_builder.dart';

// Construct and pass in a widget per screen type
ScreenTypeLayout(
  mobile: Container(color:Colors.blue)
  tablet: Container(color: Colors.yellow),
  desktop: Container(color: Colors.red),
  watch: Container(color: Colors.purple),
);

If you don't want to build all the widgets at once, you can use the widget builder. A widget for the right type of screen will be created only when needed.

// Construct and pass in a widget builder per screen type
ScreenTypeLayout.builder(
  mobile: (BuildContext context) => Container(color:Colors.blue),
  tablet: (BuildContext context) => Container(color:Colors.yellow),
  desktop: (BuildContext context) => Container(color:Colors.red),
  watch: (BuildContext context) => Container(color:Colors.purple),
);

Custom Screen Breakpoints

If you wish to define your own custom break points you can do so by supplying either the ScreenTypeLayout or ResponsiveBuilder widgets with a breakpoints argument.

// import the package
import 'package:responsive_builder/responsive_builder.dart';

//ScreenTypeLayout with custom breakpoints supplied
ScreenTypeLayout(
  breakpoints: ScreenBreakpoints(
    tablet: 600,
    desktop: 950,
    watch: 300
  ),
  mobile: Container(color:Colors.blue)
  tablet: Container(color: Colors.yellow),
  desktop: Container(color: Colors.red),
  watch: Container(color: Colors.purple),
);

To get a more in depth run through of this package I would highly recommend watching this tutorial where I show you how it was built and how to use it.

Global Screen Breakpoints

If you want to set the breakpoints for the responsive builders once you can call the line below before the app starts, or wherever you see fit.

void main() {
  ResponsiveSizingConfig.instance.setCustomBreakpoints(
    ScreenBreakpoints(desktop: 800, tablet: 550, watch: 200),
  );
  runApp(MyApp());
}

This will then reflect the screen types based on what you have set here. You can then still pass in custom break points per ScreenTypeLayout if you wish that will override these values for that specific ScreenTypeLayout builder.

Screen Type specific values

Sometimes you don't want to write an entire new UI just to change one value. Say for instance you want your padding on mobile to be 10, on the tablet 30 and desktop 60. Instead of re-writing UI you can use the getValueForScreenType function. This is a generic function that will return your value based on the screen type you're on. Take this example below.

Container(
  padding: EdgeInsets.all(10),
  child: Text('Best Responsive Package'),
)

What if you ONLY want to update the padding based on the device screen size. You could do.

var deviceType = getDeviceType(MediaQuery.of(context).size);
var paddingValue = 0;
switch(deviceType) {
  case DeviceScreenType.desktop:
    paddingValue = 60;
    break;
  case DeviceScreenType.tablet:
    paddingValue = 30;
    break;
  case DeviceScreenType.mobile:
    paddingValue = 10;
    break;
}
Container(
  padding: EdgeInsets.all(paddingValue),
  child: Text('Best Responsive Package'),
)

Ooooorrrr, you can use shorthand for that.

Container(
  padding: EdgeInsets.all(getValueForScreenType<double>(
                context: context,
                mobile: 10,
                tablet: 30,
                desktop: 60,
              )),
  child: Text('Best Responsive Package'),
)

It will return the value you give it for the DeviceScreen you're viewing the app on. For instance you want to hide a widget on mobile and not on tablet?

getValueForScreenType<bool>(
    context: context,
    mobile: false,
    tablet: true,
  ) ? MyWidget() : Container()

That will return true on tablet devices and false on mobile.

Responsive Sizing

In addition to providing specific layouts per device type there's also the requirement to size items based on the screen width or height. To use this functionality we added some responsive extensions. To use this wrap your Material or Cupertino App with the ResponsiveApp widget.

ResponsiveApp(
  builder: (context) => MaterialApp(
    ...
  )
)

This is required to use the following functionality.

Responsive Sizing

To use the responsive sizing all you need to do is the following.

import 'package:responsive_builder/responsive_builder.dart';

SizedBox(height: 30.screenHeight); // Or sh for shorthand
Text('respond to width', style: TextStyle(fontSize: 10.sw));

Use the number you want as the percentage and call the screenHeight or screenWidth extension. These also have shorthand extensions sh and sw.

Contribution

  1. Fork it!
  2. Create your feature branch: git checkout -b my-new-feature
  3. Commit your changes: git commit -am 'Add some feature'
  4. Push to the branch: git push origin my-new-feature
  5. Submit a pull request.

More Repositories

1

flutter-tutorials

The repo contains the source code for all the tutorials on the FilledStacks Youtube channel.
Dart
4,697
star
2

stacked

A Flutter application architecture created from real world scenarios
Dart
745
star
3

boxtout

An open source food delivery product and service that will be developed on the FilledStacks YouTube channel
Dart
406
star
4

smart_flare

This is a wrapper widget that provides intractable functionality to the underlying FlareActor
Dart
90
star
5

provider_architecture

A set of widgets to help with the implementation of the Provider architecture as shown by FilledStacks
Dart
86
star
6

stacked-example

A full example of a production ready architecture setup by FilledStacks for application development
Dart
83
star
7

flutter-architecture-skeletons

This repository contains starter skeletons for flutter applications built with certain architectures
Dart
73
star
8

firebase-backend

A package that helps with the management and expansion of a maintainable firebase backend
TypeScript
59
star
9

testsweets

A utility package for TestSweets that allows you to generate and upload your automation keys for the TestSweets tool
Dart
25
star
10

feedback-nova

The source code for learning practical unit testing using Dart
Dart
19
star
11

sqflite_migration

A more readable migration setup that can be used with the sqflite package
Dart
16
star
12

stacked-docs

The documentation website for stacked
TypeScript
16
star
13

academy

The official FilledStacks Academy Website
Dart
13
star
14

session_mate

Session Replay for Flutter
Dart
12
star
15

places_service

a wrapper for the places functionality to improve the usability of it
Dart
11
star
16

winnie-track

An open source live live device tracking package for applications
Dart
10
star
17

stacked-mobile-starter

A mobile app starter template that's generated from the stacked-cli as a starter project
Dart
10
star
18

dart_flutter_workshops

A set of workshops that cover Dart and Flutter concepts
Dart
9
star
19

website-written-tutorials

Contains all the written tutorials for the filledstacks.com website.
6
star
20

overflown-stacks

This repository will be dedicated to my series Overflown Stacks where either share my answers and process in solving questions on StackOverflow or I find re-occurring questions and provide solutions and detailed explanations.
Dart
4
star
21

responsive_reducers

A set of functions that helps with responsive sizing and spacing in general
Dart
4
star
22

startup_bounty

Bounty for some startup logic
C++
4
star
23

testsweets-example

An example application to use when following the get started guide for TestSweets
Dart
3
star
24

fs_architecture_v2_proposal

This repo represents the Proposed architecture for the revised version of the origial Mvvm inspired architecture
Dart
3
star
25

flutter-hanging-bug

This is a bug that I'm experiencing using Flutter.
Dart
2
star
26

DailyUI-Flutter

The repo contains the code for the daily challenge to implement a UI in flutter everyday based on the Daily UI challenge.
Dart
2
star
27

widget_tree_bounty

Bounty for widget tree scanning in Flutter
Dart
2
star
28

session_mate_cli

CLI to interact with SessionMate
Dart
1
star
29

session_mate_core

Shared types for Session Mate
Dart
1
star
30

device_finder

Device management for our driving technology
Dart
1
star
31

academy_backend

The backend for the new FilledStacks academy
Dart
1
star
32

FilledStacks

My github profile
1
star
33

acceptpermission_bounty

The source code for the permission accept bounty
Dart
1
star