text
stringlengths 1
474
|
|---|
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.<topic_end>
|
<topic_start>
|
Interactive example
|
This example shows a list of items that are spaced evenly within a column.
|
The list can be scrolled up and down when the items don’t fit the screen.
|
The number of items is defined by the variable items,
|
change this value to see what happens when the items won’t fit the screen.
|
<code_start>import 'package:flutter/material.dart';
|
void main() => runApp(const SpacedItemsList());
|
class SpacedItemsList extends StatelessWidget {
|
const SpacedItemsList({super.key});
|
@override
|
Widget build(BuildContext context) {
|
const items = 4;
|
return MaterialApp(
|
title: 'Flutter Demo',
|
debugShowCheckedModeBanner: false,
|
theme: ThemeData(
|
colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple),
|
cardTheme: CardTheme(color: Colors.blue.shade50),
|
useMaterial3: true,
|
),
|
home: Scaffold(
|
body: LayoutBuilder(builder: (context, constraints) {
|
return SingleChildScrollView(
|
child: ConstrainedBox(
|
constraints: BoxConstraints(minHeight: constraints.maxHeight),
|
child: Column(
|
mainAxisAlignment: MainAxisAlignment.spaceBetween,
|
crossAxisAlignment: CrossAxisAlignment.stretch,
|
children: List.generate(
|
items, (index) => ItemWidget(text: 'Item $index')),
|
),
|
),
|
);
|
}),
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.