flutter responsive layouts
responsive layouts
this plugin provides a easy and productive way to work with responsive layouts for flutter applications in mobile, desktop and web, allowing your layout to adapt and wrap widgets ( container, rows, columns and richtext ) referent to the size of his parent element.
licensing
responsive layouts
this project follows the gnu general public license v3, wich means you can change the entire project the way as you want to, but you need to share your improvements back to the community.
to share your improvements, please first do a download of this project, change what you need to and finally do a pull request. and don´t let to share your ideas and needs on “issues” page, even before to start your changes.
important notes:
- this plugin was based on bootstrap web project, but do not implement all its features (not yet).
- the column responsivity is based on his closest parent widget size, not over the screen size, such as bootstrap.
- all the widgets contains margin, padding, width, height (maximun and minimum edges), such as
div
html element. - fell free to improve and change this project.
how to use
responsive layouts
add the dependency bellow into your pubspec.yaml
file.
dependencies:
flutter_responsive: ^0.0.4 #please, ensure to use the most updated version
add the reference into your .dart
files
import 'package:flutter_responsive/flutter_responsive.dart';
use the widgets responsivecontainer
, responsiverow
, responsivecol
and responsivetext
as the way you want to.
screen sizes
responsive layouts
this plugin was based on bootstrap web project and split the screen in 12 columns, considering 7 screen sizes:
- us – ultra small screens – 0px to 309px
- xs – extra small screens – 310px to 575px
- sm – small screens – 576px to 767px
- md – medium screens – 768px to 991px
- lg – large small screens – 992px to 1199px
- xl – extra large screens – 1200px to 1999px
- ul – ultra large screens – 2000px and beyond
personalizing limits (optional)
responsive layouts
all the limits could be personalized as you need, changing the limit hashmap into responsivescreen class.
/* map<string, double> */
responsivescreen.limits = {
'us': 0.00,
// smart watches
'xs': 310.00,
// small phones (5c)
'sm': 576.00,
// medium phones
'md': 768.00,
// large phones (iphone x)
'lg': 992.00,
// tablets
'xl': 1200.00,
// laptops
'ul': 2000.00,
// desktops and tvs 4k
};
grid usage
responsive layouts
this plugin have 3 major grid elements:
- responsivecontainer: container to all page elements, such as rows and columns, centralizing the content and beeing limited to a maximum width size
responsivecontainer(
// determines the container's limit size
widthlimit: responsivescreen.limits['lg'],
margin: edgeinsets.symmetric(horizontal: 10),
children: <widget>[
responsiverow(),
responsiverow(),
responsiverow(),
]
)
- responsiverow: container wich contains many columns or even other widget.
obs: its not necessary to wrap inner widgets into a responsivecol object.
responsiverow(
margin: edgeinsets.only(top: 20, bottom: 5),
children: <widget>[
responsivecol(),
responsivecol(),
responsivecol(),
text('it´s fine to use another widget directly inside a responsiverow', style: responsivetypography.q)
]
),
- responsivecol:
responsivecol(
margin: edgeinsets.all(10),
padding: edgeinsets.all(10),
backgroundcolor: colors.blue,
gridsizes: {
'xs' : 4,
'sm' : 3,
'lg' : 2,
'xl' : 1,
},
children: [
text('lorem ipsum dolor sit amet, consectetur adipiscing elit', style: responsivetypography.h2)
]
)
full example
import 'package:flutter/material.dart';
import 'package:flutter_responsive/flutter_responsive.dart';
class homepage extends statefulwidget {
@override
_homepage createstate() => _homepage();
}
class _homepage extends state<homepage> {
@override
void initstate() {
super.initstate();
}
@override
widget build(buildcontext context) {
/*create 12 columns*/
list<widget> responsivegridexample =
/*repeat 12 times*/
list<int>.generate(12, (index) => index).map((colindex) =>
responsivecol(
padding: edgeinsets.all(10),
backgroundcolor: colors.blue,
gridsizes: {
'xs' : 4,
'sm' : 3,
'lg' : 2,
'xl' : 1,
},
children: [
text('lorem ipsum dolor sit amet, consectetur adipiscing elit')
]
)
).tolist();
mediaquerydata mediaquery = mediaquery.of(context);
return scaffold(
appbar: appbar(
title: const text('home', overflow: textoverflow.ellipsis),
),
body: container(
color: color(0xffcccccc),
child: listview(
children: <widget>[
responsivecontainer(
margin: edgeinsets.symmetric(vertical: 10),
padding: edgeinsets.symmetric(horizontal: 10),
backgroundcolor: colors.white,
widthlimit: mediaquery.size.width * 0.95,
children: <widget>[
responsiverow(
margin: edgeinsets.symmetric(vertical: 10),
children: <widget>[
responsivecol(
padding: edgeinsets.all(10),
margin: edgeinsets.only(bottom: 20),
backgroundcolor: colors.bluegrey,
children: [
text('flutter responsive layout', style: responsivetypography.h4.merge(textstyle(color: colors.white)))
]
),
]
),
responsiverow(
margin: edgeinsets.symmetric(vertical: 10),
children: <widget>[
// by default, the column occupies the entire row, always
responsivecol(
children: [
responsivetext(
shrinktofit: true,
padding: edgeinsets.only(bottom: 20),
stylesheet: {
'h3': responsivestylesheet(
textstyle: textstyle(color: theme.of(context).primarycolor),
displaystyle: displaystyle.block
),
'h6': responsivestylesheet(
textstyle: textstyle(color: theme.of(context).primarycolor),
displaystyle: displaystyle.block
)
},
margin: edgeinsets.symmetric(horizontal: 10, vertical: 20),
text:
'<div>'
'<h3>responsive layouts</h3><h6>for <i>flutter</i></h6>'
'<br><br>'
'<p>this <b>richtext</b> was easily produced and personalized using pure html</p>'
'<p>bellow there is an example of 12 columns, wich changes the amount of each line depending of his father´s widget size.</p>'
'</div>',
)
]
)
]..addall(
responsivegridexample
)
)
],
)
],
),
)
);
}
}
final result:
how to run the plugin example
this project is a starting point for a flutter
plug-in package,
a specialized package that includes platform-specific implementation code for
android and/or ios.
for help getting started with flutter, view our
online documentation, which offers tutorials,
samples, guidance on mobile development, and a full api reference.
to run the full example app, wich contains performance and case tests, do the steps bellow:
- download this project into your local machine using github desktop or any other git program of your preference.
- download android studio and the last flutter sdk into your local machine. configure they to works properly such as this article here
- run
flutter pub get
to update all the dependencies - debug the file
example/lib/main.dart
or any of the unity case tests located ontest
folder on emulator or real device. - to run properly the performance tests, please run the app using
flutter run --release
Comments are closed.