text
stringlengths 1
474
|
|---|
<topic_start>Building adaptive apps
|
<topic_end>
|
<topic_start>
|
Overview
|
Flutter provides new opportunities to build apps that can
|
run on mobile, desktop, and the web from a single codebase.
|
However, with these opportunities, come new challenges.
|
You want your app to feel familiar to users,
|
adapting to each platform by maximizing usability and
|
ensuring a comfortable and seamless experience.
|
That is, you need to build apps that are not just
|
multiplatform, but are fully platform adaptive.There are many considerations for developing platform-adaptive
|
apps, but they fall into three major categories:This page covers all three categories in detail
|
using code snippets to illustrate the concepts.
|
If you’d like to see how these concepts come together,
|
check out the Flokk and Folio examples that
|
were built using the concepts described here.Original demo code for adaptive app development techniques from flutter-adaptive-demo.<topic_end>
|
<topic_start>
|
Building adaptive layouts
|
One of the first things you must consider when writing
|
your app for multiple platforms is how to adapt
|
it to the various sizes and shapes of the screens that
|
it will run on.<topic_end>
|
<topic_start>
|
Layout widgets
|
If you’ve been building apps or websites,
|
you’re probably familiar with creating responsive interfaces.
|
Luckily for Flutter developers,
|
there are a large set of widgets to make this easier.Some of Flutter’s most useful layout widgets include:Single childAlign—Aligns a child within itself.
|
It takes a double value between -1 and 1,
|
for both the vertical and horizontal alignment.AspectRatio—Attempts to size the
|
child to a specific aspect ratio.ConstrainedBox—Imposes size constraints on its child,
|
offering control over the minimum or maximum size.CustomSingleChildLayout—Uses a delegate function
|
to position a single child. The delegate can determine
|
the layout constraints and positioning for the child.Expanded and Flexible—Allows a child of a
|
Row or Column to shrink or grow to fill any available space.FractionallySizedBox—Sizes its child to a fraction
|
of the available space.LayoutBuilder—Builds a widget that can reflow
|
itself based on its parents size.SingleChildScrollView—Adds scrolling to a single child.
|
Often used with a Row or Column.MultichildColumn, Row, and Flex—Lays out children
|
in a single horizontal or vertical run.
|
Both Column and Row extend the Flex widget.CustomMultiChildLayout—Uses a delegate function to
|
position multiple children during the layout phase.Flow—Similar to CustomMultiChildLayout,
|
but more efficient because it’s performed during the
|
paint phase rather than the layout phase.ListView, GridView, and
|
CustomScrollView—Provides scrollable
|
lists of children.Stack—Layers and positions multiple children
|
relative to the edges of the Stack.
|
Functions similarly to position-fixed in CSS.Table—Uses a classic table layout algorithm for
|
its children, combining multiple rows and columns.Wrap—Displays its children in multiple horizontal
|
or vertical runs.To see more available widgets and example code, see
|
Layout widgets.<topic_end>
|
<topic_start>
|
Visual density
|
Different input devices offer various levels of precision,
|
which necessitate differently sized hit areas.
|
Flutter’s VisualDensity class makes it easy to adjust the
|
density of your views across the entire application,
|
for example, by making a button larger
|
(and therefore easier to tap) on a touch device.When you change the VisualDensity for your MaterialApp,
|
MaterialComponents that support it animate their densities
|
to match. By default, both horizontal and vertical densities
|
are set to 0.0, but you can set the densities to any negative
|
or positive value that you want. By switching between different
|
densities, you can easily adjust your UI:To set a custom visual density, inject the density into
|
your MaterialApp theme:
|
<code_start>double densityAmt = touchMode ? 0.0 : -1.0;
|
VisualDensity density =
|
VisualDensity(horizontal: densityAmt, vertical: densityAmt);
|
return MaterialApp(
|
theme: ThemeData(visualDensity: density),
|
home: MainAppScaffold(),
|
debugShowCheckedModeBanner: false,
|
);<code_end>
|
To use VisualDensity inside your own views,
|
you can look it up:
|
<code_start>VisualDensity density = Theme.of(context).visualDensity;<code_end>
|
Not only does the container react automatically to changes
|
in density, it also animates when it changes.
|
This ties together your custom components,
|
along with the built-in components,
|
for a smooth transition effect across the app.As shown, VisualDensity is unit-less,
|
so it can mean different things to different views.
|
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
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.