text
stringlengths
1
372
of 3 images, each is 100 pixels high. the height of the render box
(in this case, the entire screen) is more than 300 pixels, so
setting the main axis alignment to spaceEvenly divides the free vertical
space evenly between, above, and below each image.
app source: row_column
<topic_end>
<topic_start>
sizing widgets
when a layout is too large to fit a device, a yellow
and black striped pattern appears along the affected edge.
here is an example of a row that is too wide:
widgets can be sized to fit within a row or column by using the
expanded widget. to fix the previous example where the
row of images is too wide for its render box,
wrap each image with an expanded widget.
app source: sizing
perhaps you want a widget to occupy twice as much space as its
siblings. for this, use the expanded widget flex property,
an integer that determines the flex factor for a widget.
the default flex factor is 1. the following code sets
the flex factor of the middle image to 2:
app source: sizing
<topic_end>
<topic_start>
packing widgets
by default, a row or column occupies as much space along its main axis
as possible, but if you want to pack the children closely together,
set its mainAxisSize to MainAxisSize.min. the following example
uses this property to pack the star icons together.
app source: pavlova
<topic_end>
<topic_start>
nesting rows and columns
the layout framework allows you to nest rows and columns
inside of rows and columns as deeply as you need.
let’s look at the code for the outlined
section of the following layout:
the outlined section is implemented as two rows. the ratings row contains
five stars and the number of reviews. the icons row contains three
columns of icons and text.
the widget tree for the ratings row:
the ratings variable creates a row containing a smaller row
of 5 star icons, and text:
<code_start>
final stars = row(
mainAxisSize: MainAxisSize.min,
children: [
Icon(Icons.star, color: colors.green[500]),
Icon(Icons.star, color: colors.green[500]),
Icon(Icons.star, color: colors.green[500]),
const Icon(Icons.star, color: colors.black),
const Icon(Icons.star, color: colors.black),
],
);
final ratings = container(
padding: const EdgeInsets.all(20),
child: row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: [
stars,
const text(
'170 reviews',
style: TextStyle(
color: colors.black,
fontWeight: FontWeight.w800,
fontFamily: 'roboto',
letterSpacing: 0.5,
fontSize: 20,
),
),
],
),
);
<code_end>
lightbulb tip
to minimize the visual confusion that can result from
heavily nested layout code, implement pieces of the UI
in variables and functions.
the icons row, below the ratings row, contains 3 columns;
each column contains an icon and two lines of text,
as you can see in its widget tree:
the iconList variable defines the icons row:
<code_start>
const descTextStyle = TextStyle(
color: colors.black,
fontWeight: FontWeight.w800,
fontFamily: 'roboto',
letterSpacing: 0.5,
fontSize: 18,
height: 2,
);
// DefaultTextStyle.merge() allows you to create a default text
// style that is inherited by its child and all subsequent children.
final iconList = DefaultTextStyle.merge(
style: descTextStyle,
child: container(
padding: const EdgeInsets.all(20),
child: row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: [