text
stringlengths
1
372
in this example, 1 density unit equals 6 pixels,
but this is totally up to your views to decide.
the fact that it is unit-less makes it quite versatile,
and it should work in most contexts.
it’s worth noting that the material components generally
use a value of around 4 logical pixels for each
visual density unit. for more information about the
supported components, see VisualDensity API.
for more information about density principles in general,
see the material design guide.
<topic_end>
<topic_start>
contextual layout
if you need more than density changes and can’t find a
widget that does what you need, you can take a more
procedural approach to adjust parameters, calculate sizes,
swap widgets, or completely restructure your UI to suit
a particular form factor.
<topic_end>
<topic_start>
screen-based breakpoints
the simplest form of procedural layouts uses
screen-based breakpoints. in flutter,
this can be done with the MediaQuery API.
there are no hard and fast rules for the sizes to use
here, but these are general values:
<code_start>
class FormFactor {
static double desktop = 900;
static double tablet = 600;
static double handset = 300;
}
<code_end>
using breakpoints, you can set up a simple system
to determine the device type:
<code_start>
ScreenType getFormFactor(BuildContext context) {
// use .shortestside to detect device type regardless of orientation
double deviceWidth = MediaQuery.of(context).size.shortestSide;
if (devicewidth > FormFactor.desktop) return ScreenType.desktop;
if (devicewidth > FormFactor.tablet) return ScreenType.tablet;
if (devicewidth > FormFactor.handset) return ScreenType.handset;
return ScreenType.watch;
}
<code_end>
as an alternative, you could abstract it more
and define it in terms of small to large:
<code_start>
enum ScreenSize { small, normal, large, extraLarge }
ScreenSize getSize(BuildContext context) {
double deviceWidth = MediaQuery.of(context).size.shortestSide;
if (devicewidth > 900) return ScreenSize.extraLarge;
if (devicewidth > 600) return ScreenSize.large;
if (devicewidth > 300) return ScreenSize.normal;
return ScreenSize.small;
}
<code_end>
screen-based breakpoints are best used for making
top-level decisions in your app. changing things like
visual density, paddings, or font-sizes are best when
defined on a global basis.
you can also use screen-based breakpoints to reflow your
top-level widget trees. for example, you could switch
from a vertical to a horizontal layout when the user isn’t on a handset:
<code_start>
bool isHandset = MediaQuery.of(context).size.width < 600;
return flex(
direction: isHandset ? axis.vertical : axis.horizontal,
children: const [text('foo'), Text('Bar'), Text('Baz')],
);
<code_end>
in another widget,
you might swap some of the children completely:
<code_start>
widget foo = row(
children: [
...ishandset ? _getHandsetChildren() : _getNormalChildren(),
],
);
<code_end>
<topic_end>
<topic_start>
use LayoutBuilder for extra flexibility
even though checking total screen size is great for
full-screen pages or making global layout decisions,
it’s often not ideal for nested subviews.
often, subviews have their own internal breakpoints
and care only about the space that they have available to render.
the simplest way to handle this in flutter is using the
LayoutBuilder class. LayoutBuilder allows a
widget to respond to incoming local size constraints,
which can make the widget more versatile than if it
depended on a global value.
the previous example could be rewritten using LayoutBuilder:
<code_start>
widget foo = LayoutBuilder(builder: (context, constraints) {
bool useVerticalLayout = constraints.maxWidth < 400;
return flex(
direction: useVerticalLayout ? axis.vertical : axis.horizontal,
children: const [