text
stringlengths
1
474
MyApp(
items: List<ListItem>.generate(
1000,
(i) => i % 6 == 0
? HeadingItem('Heading $i')
: MessageItem('Sender $i', 'Message body $i'),
),
),
);
}
class MyApp extends StatelessWidget {
final List<ListItem> items;
const MyApp({super.key, required this.items});
@override
Widget build(BuildContext context) {
const title = 'Mixed List';
return MaterialApp(
title: title,
home: Scaffold(
appBar: AppBar(
title: const Text(title),
),
body: ListView.builder(
// Let the ListView know how many items it needs to build.
itemCount: items.length,
// Provide a builder function. This is where the magic happens.
// Convert each item into a widget based on the type of item it is.
itemBuilder: (context, index) {
final item = items[index];
return ListTile(
title: item.buildTitle(context),
subtitle: item.buildSubtitle(context),
);
},
),
),
);
}
}
/// The base class for the different types of items the list can contain.
abstract class ListItem {
/// The title line to show in a list item.
Widget buildTitle(BuildContext context);
/// The subtitle line, if any, to show in a list item.
Widget buildSubtitle(BuildContext context);
}
/// A ListItem that contains data to display a heading.
class HeadingItem implements ListItem {
final String heading;
HeadingItem(this.heading);
@override
Widget buildTitle(BuildContext context) {
return Text(
heading,
style: Theme.of(context).textTheme.headlineSmall,
);
}
@override
Widget buildSubtitle(BuildContext context) => const SizedBox.shrink();
}
/// A ListItem that contains data to display a message.
class MessageItem implements ListItem {
final String sender;
final String body;
MessageItem(this.sender, this.body);
@override
Widget buildTitle(BuildContext context) => Text(sender);
@override
Widget buildSubtitle(BuildContext context) => Text(body);
}<code_end>
<topic_end>
<topic_start>List with spaced items
Perhaps you want to create a list where all list items
are spaced evenly, so that the items take up the visible space.
For example, the four items in the following image are spaced evenly,
with “Item 0” at the top, and “Item 3” at the bottom.At the same time, you might want to allow users
to scroll through the list when the list of items won’t fit,
maybe because a device is too small, a user resized a window,
or the number of items exceeds the screen size.Typically, you use Spacer to tune the spacing between widgets,
or Expanded to expand a widget to fill the available space.
However, these solutions are not possible inside scrollable widgets,
because they need a finite height constraint.This recipe demonstrates how to use LayoutBuilder and ConstrainedBox
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