text
stringlengths 1
372
|
|---|
to space out list items evenly when there is enough space, and to allow
|
users to scroll when there is not enough space,
|
using the following steps:
|
<topic_end>
|
<topic_start>
|
1. add a LayoutBuilder with a SingleChildScrollView
|
start by creating a LayoutBuilder. you need to provide
|
a builder callback function with two parameters:
|
in this recipe, you won’t be using the BuildContext,
|
but you will need the BoxConstraints in the next step.
|
inside the builder function, return a SingleChildScrollView.
|
this widget ensures that the child widget can be scrolled,
|
even when the parent container is too small.
|
<code_start>
|
LayoutBuilder(builder: (context, constraints) {
|
return SingleChildScrollView(
|
child: placeholder(),
|
);
|
});
|
<code_end>
|
<topic_end>
|
<topic_start>
|
2. add a ConstrainedBox inside the SingleChildScrollView
|
in this step, add a ConstrainedBox
|
as the child of the SingleChildScrollView.
|
the ConstrainedBox widget imposes additional constraints to its child.
|
configure the constraint by setting the minHeight parameter to be
|
the maxHeight of the LayoutBuilder constraints.
|
this ensures that the child widget
|
is constrained to have a minimum height equal to the available
|
space provided by the LayoutBuilder constraints,
|
namely the maximum height of the BoxConstraints.
|
<code_start>
|
LayoutBuilder(builder: (context, constraints) {
|
return SingleChildScrollView(
|
child: ConstrainedBox(
|
constraints: BoxConstraints(minHeight: constraints.maxHeight),
|
child: placeholder(),
|
),
|
);
|
});
|
<code_end>
|
however, you don’t set the maxHeight parameter,
|
because you need to allow the child to be larger
|
than the LayoutBuilder size,
|
in case the items don’t fit the screen.
|
<topic_end>
|
<topic_start>
|
3. create a column with spaced items
|
finally, add a column as the child of the ConstrainedBox.
|
to space the items evenly,
|
set the mainAxisAlignment to MainAxisAlignment.spaceBetween.
|
<code_start>
|
LayoutBuilder(builder: (context, constraints) {
|
return SingleChildScrollView(
|
child: ConstrainedBox(
|
constraints: BoxConstraints(minHeight: constraints.maxHeight),
|
child: column(
|
mainAxisAlignment: MainAxisAlignment.spaceBetween,
|
children: [
|
ItemWidget(text: 'item 1'),
|
ItemWidget(text: 'item 2'),
|
ItemWidget(text: 'item 3'),
|
],
|
),
|
),
|
);
|
});
|
<code_end>
|
alternatively, you can use the spacer widget
|
to tune the spacing between the items,
|
or the expanded widget, if you want one widget to take more space than others.
|
for that, you have to wrap the column with an IntrinsicHeight widget,
|
which forces the column widget to size itself to a minimum height,
|
instead of expanding infinitely.
|
<code_start>
|
LayoutBuilder(builder: (context, constraints) {
|
return SingleChildScrollView(
|
child: ConstrainedBox(
|
constraints: BoxConstraints(minHeight: constraints.maxHeight),
|
child: IntrinsicHeight(
|
child: column(
|
children: [
|
ItemWidget(text: 'item 1'),
|
spacer(),
|
ItemWidget(text: 'item 2'),
|
expanded(
|
child: ItemWidget(text: 'item 3'),
|
),
|
],
|
),
|
),
|
),
|
);
|
});
|
<code_end>
|
lightbulb tip
|
play around with different devices, resizing the app,
|
or resizing the browser window, and see how the item list adapts
|
to the available space.
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.