text stringlengths 1 372 |
|---|
title: item.buildTitle(context), |
subtitle: item.buildSubtitle(context), |
); |
}, |
) |
<code_end> |
<topic_end> |
<topic_start> |
interactive example |
<code_start> |
import 'package:flutter/material.dart'; |
void main() { |
runApp( |
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 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.