• Stars
    star
    666
  • Rank 66,059 (Top 2 %)
  • Language
    Dart
  • License
    MIT License
  • Created over 4 years ago
  • Updated 11 months ago

Reviews

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

Repository Details

Flutter | Create responsive layouts for mobile, web and desktop

Layout

pub package

Following the Material Design Guidelines, Layout encourage consistency across platforms, environments, and screen sizes by using uniform elements and spacing.

Install

Follow the instructions to install it here

Getting started

This package aims to provide the tools to implement a responsive layout in an easy and consistent way.

If you want to learn more in detail about layout in Material Design I recommend you the official website.

Let's get started!

Everything starts with the Layout widget. Usually added at the top of your widget tree, but you can place it wherever you need it. It uses its widget constraints to calculate its breakpoint, columns, gutters, and margins.

  @override
  Widget build(BuildContext context) {
    return Layout(
      child: MaterialApp(
      ....
      ),
    );
  }

Breakpoints

A breakpoint is the range of predetermined screen sizes that have specific layout requirements. At a given breakpoint range, the layout adjusts to suit the screen size and orientation.

Each breakpoint range determines the number of columns, and recommended margins and gutters, for each display size.

By default the breakpoints are defined as:

  • xs: 0 – 599
  • sm: 600 – 1023
  • md: 1024 – 1439
  • lg: 1440 – 1919
  • xl: 1920 +
 @override
 Widget build(BuildContext context) {
   if(context.breakpoint > LayoutBreakpoint.md)
     return TabletView();
   else
     return MobileView();
 }

LayoutValues

A layout value is relative to the width of the screen. This way you can define responsive variable, reuse them and apply them when needed.

final double padding = context.layout.value(xs: 0.0, sm: 12.0, md: 24.0, lg: 32.0, xl: 48.0);

The most important layout values are the ones relative to the breakpoint. This are the most common an useful as you can define a value for a different breakpoint sizes. If a breakpoint is not provided, its value will correspond to the first previous/smaller breakpoint.

final double padding = context.layout.value(
     xs: 0.0,  // sm value will be like xs 0.0
     md: 24.0, // lg value will be like md 24.0
     xl: 48.0
);

Layout values can be reused in different parts of the app with even different Layout widgets. For that they need to be created as

final displaySidebar = LayoutValue(xs: false, md: true);

final horizontalMargin = LayoutValue.builder((layout) {
    double margin = layout.width >= 500 ? 24.0 : 16.0;
    margin += 8.0 * layout.visualDensity.horizontal;
    return EdgeInsets.symmetric(horizontal: margin);
});

Then it can be used in any widget that as some Layout up in the tree as:

return Column(
  children: [
    Padding(
      padding: horizontalMargin.resolve(context),
      child:child,
    ),
    if(displaySidebar.resolve(context))
      SideBar(),
    ),
  ],
);

You can also create values relative to the layout width like.

final displaySidebar = LayoutValue.builder((layout) => layout.width > 600);

Margins

Margins are the space between content and the left and right edges of the screen.

  @override
  Widget build(BuildContext context) {
    return Margin(
      child: Text('This text'),
    );
  }

Margin widths are defined as fixed values at each breakpoint range. To better adapt to the screen, the margin width can change at different breakpoints. Wider margins are more appropriate for larger screens, as they create more whitespace around the perimeter of content.

By default the margin values are the ones from the Material Design Guidelines. 16dp for screens with a width less than 720dp and 24 for bigger screens. You can override this values in any moment by providing the margin param.

Fluid Margins

Some times you want to have a fixed width that stays the same across screen sizes.

  @override
  Widget build(BuildContext context) {
    return FluidMargin(
      child: Text('This text'),
    );
  }

Fluid margins are dinamically updated to keep a fixed size of its inner child. This fixed sizes are by default the ones from the Material Design Guidelines but can also easily customizable.

AdaptiveBuilder

A widget tha allows easily to build responsive layouts

  @override
  Widget build(BuildContext context) {
    return AdaptiveBuilder(
      xs: (context) => LayoutWithBottomNavigationBar(),
      lg: (context) => LayoutWithTrailingNavigationBar(),
    );
  }

or for more complex cases

  @override
  Widget build(BuildContext context) {
    return  AdaptiveBuilder.builder(
      builder: (context, layout, child) {
        if (layout.breakpoint < LayoutBreakpoint.lg) {
          return LayoutWithBottomNavigationBar(child: child);
        } else {
          return LayoutWithTrailingNavigationBar(child: child);
        }
      },
      child: child,
    );
  }

Contributing

If you want to take the time to make this project better, you can open an new issue, of a pull request.

More Repositories

1

modal_bottom_sheet

Flutter | Create advanced modal bottom sheets. Material, Cupertino or your own style
Dart
1,645
star
2

zflutter

Flat, round, designer-friendly pseudo-3D engine for Flutter
Dart
262
star
3

flutter_preview

Flutter | Because a widget-driven development requires a widget-driven preview.
Dart
255
star
4

flutter_lava_clock

Flutter Clock Challenge | Lava Clock
Dart
84
star
5

snap_scroll_physics

Flutter | Snap physics for your scrollview
Dart
50
star
6

arb_editor

Online Editor | Edit your .arb files and translate your app the easiest way
Dart
42
star
7

flutter_showcase

Flutter | A simple, fast and easy way to share you flutter project or mockups with the world
Dart
41
star
8

selection_controls_example

Dart
16
star
9

flutter-cupertino-modal

iOS 13 Modal for Flutter. Moved to https://github.com/jamesblasco/modal_bottom_sheet
Dart
11
star
10

slivers

Dart
9
star
11

fluttercon_stripe

A JS demo for FlutterCon
Dart
7
star
12

purchases

Extension for the package purchases_flutter
Dart
6
star
13

morse_coding_swift_playground

Swift playground accepted for WWDC'19 Scholarship
Swift
6
star
14

intl_translation_format

User-extensible translation file formats for dart
Dart
6
star
15

address_field

Flutter | An address field that expands and allows autofill and autocompletion
Dart
5
star
16

PopArtCamera

TrueDepth camera example project
Swift
3
star
17

jaime_bdbchallenge

Dart
3
star
18

modal_bottom_sheet_example

Dart
3
star
19

fluid_layout

Use layout instead
Dart
3
star
20

staggered_grid_view_examples

Dart
2
star
21

dart_arb

Package | A arb parser for dart
Dart
2
star
22

intl_translation_format_experiments

Experiments related to intl_tranlsation_format
Dart
2
star
23

layout_example

An example for the layout package
Dart
2
star
24

menus

Dart
1
star
25

flutter-web-scroll

Dart
1
star