text
stringlengths
1
474
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 [
Text('Hello'),
Text('World'),
],
);
});<code_end>
This widget can now be composed within a side panel,
dialog, or even a full-screen view,
and adapt its layout to whatever space is provided.<topic_end>
<topic_start>Device segmentation
There are times when you want to make layout decisions
based on the actual platform you’re running on,
regardless of size. For example, when building a
custom title bar, you might need to check the operating
system type and tweak the layout of your title bar,
so it doesn’t get covered by the native window buttons.To determine which combination of platforms you’re on,
you can use the Platform API along with the kIsWeb value:
<code_start>bool get isMobileDevice => !kIsWeb && (Platform.isIOS || Platform.isAndroid);
bool get isDesktopDevice =>
!kIsWeb && (Platform.isMacOS || Platform.isWindows || Platform.isLinux);
bool get isMobileDeviceOrWeb => kIsWeb || isMobileDevice;
bool get isDesktopDeviceOrWeb => kIsWeb || isDesktopDevice;<code_end>
The Platform API can’t be accessed from web builds without
throwing an exception, because the dart.io package isn’t
supported on the web target. As a result, the above code checks
for web first, and because of short-circuiting,
Dart never calls Platform on web targets.Use Platform/kIsWeb when the logic absolutely must
run for a given platform. For example,
talking to a plugin that only works on iOS,
or displaying a widget that only conforms to
Play Store policy and not the App Store’s.<topic_end>
<topic_start>
Single source of truth for styling
You’ll probably find it easier to maintain your views
if you create a single source of truth for styling values
like padding, spacing, corner shape, font sizes, and so on.
This can be done easily with some helper classes:
<code_start>class Insets {