Useful Widgets for Responsive Design To ensure your app looks good and is usable on all screen sizes, you can use a combination of responsive design techniques.
On pub.dev, there are a number of packages that can help you with this, but I have not used any of them because this app is not complex enough to warrant their use.
Instead I’ve been able to build a consistent, responsive UIs across diffent pages, with just a handful of custom widgets, which I’ve saved in the common_widgets
folder:
ResponsiveCenter
ResponsiveScrollableCard
ResponsiveTwoColumnLayout
Here’s what they look like.
ResponsiveCenter
/// Reusable widget for showing a child with a maximum content width constraint.
/// If available width is larger than the maximum width, the child will be
/// centered.
/// If available width is smaller than the maximum width, the child use all the
/// available width.
class ResponsiveCenter extends StatelessWidget {
const ResponsiveCenter ({
super .key,
this .maxContentWidth = Breakpoint .desktop,
this .padding = EdgeInsets .zero,
required this .child,
});
final double maxContentWidth;
final EdgeInsetsGeometry padding;
final Widget child;
@override
Widget build ( BuildContext context) {
// Use Center as it has *unconstrained* width (loose constraints)
return Center (
// together with SizedBox to specify the max width (tight constraints)
// See this thread for more info:
// https://twitter.com/biz84/status/1445400059894542337
child : SizedBox (
width : maxContentWidth,
child : Padding (
padding : padding,
child : child,
),
),
);
}
}
/// Scrollable widget that shows a responsive card with a given child widget.
/// Useful for displaying forms and other widgets that need to be scrollable.
class ResponsiveScrollableCard extends StatelessWidget {
const ResponsiveScrollableCard ({ super .key, required this .child});
final Widget child;
@override
Widget build ( BuildContext context) {
return SingleChildScrollView (
child : ResponsiveCenter (
maxContentWidth : Breakpoint .tablet,
child : Padding (
padding : const EdgeInsets . all ( Sizes .p16),
child : Card (
child : Padding (
padding : const EdgeInsets . all ( Sizes .p16),
child : child,
),
),
),
),
);
}
}
ResponsiveTwoColumnLayout
/// Responsive layout that shows two child widgets side by side if there is
/// enough space, or vertically stacked if there is not enough space.
class ResponsiveTwoColumnLayout extends StatelessWidget {
const ResponsiveTwoColumnLayout ({
super .key,
required this .startContent,
required this .endContent,
this .startFlex = 1 ,
this .endFlex = 1 ,
this .breakpoint = Breakpoint .tablet,
required this .spacing,
this .rowMainAxisAlignment = MainAxisAlignment .start,
this .rowCrossAxisAlignment = CrossAxisAlignment .start,
this .columnMainAxisAlignment = MainAxisAlignment .start,
this .columnCrossAxisAlignment = CrossAxisAlignment .stretch,
});
final Widget startContent;
final Widget endContent;
final int startFlex;
final int endFlex;
final double breakpoint;
final double spacing;
final MainAxisAlignment rowMainAxisAlignment;
final CrossAxisAlignment rowCrossAxisAlignment;
final MainAxisAlignment columnMainAxisAlignment;
final CrossAxisAlignment columnCrossAxisAlignment;
@override
Widget build ( BuildContext context) {
return LayoutBuilder (builder : (context, constraints) {
if (constraints.maxWidth >= breakpoint) {
return Row (
mainAxisAlignment : rowMainAxisAlignment,
crossAxisAlignment : rowCrossAxisAlignment,
children : [
Flexible (flex : startFlex, child : startContent),
SizedBox (width : spacing),
Flexible (flex : endFlex, child : endContent),
],
);
} else {
return Column (
mainAxisAlignment : columnMainAxisAlignment,
crossAxisAlignment : columnCrossAxisAlignment,
children : [
startContent,
SizedBox (height : spacing),
endContent,
],
);
}
});
}
}
Layout Breakpoints
All the widgets above reference some properties of the Breakpoint
class, which is defined like this:
/// Layout breakpoints used in the app.
class Breakpoint {
static const double desktop = 900 ;
static const double tablet = 600 ;
}
The main idea with layout breakpoints is that you return different layouts depending on the available width .
For example, the build
method of the ResponsiveTwoColumnLayout
widget arranges its children inside a Row
or a Column
depending on the maximum width returned by the LayoutBuilder
widget:
Widget build ( BuildContext context) {
return LayoutBuilder (builder : (context, constraints) {
if (constraints.maxWidth >= breakpoint) {
return Row (
mainAxisAlignment : rowMainAxisAlignment,
crossAxisAlignment : rowCrossAxisAlignment,
children : [
Flexible (flex : startFlex, child : startContent),
SizedBox (width : spacing),
Flexible (flex : endFlex, child : endContent),
],
);
} else {
return Column (
mainAxisAlignment : columnMainAxisAlignment,
crossAxisAlignment : columnCrossAxisAlignment,
children : [
startContent,
SizedBox (height : spacing),
endContent,
],
);
}
});
}
Resources about Building UIs
This course does not focus on building UIs with Flutter.
If you’re looking for resources about this, check this lesson from my “Get Started with Flutter” course: