text stringlengths 1 372 |
|---|
<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')), |
), |
), |
); |
}), |
), |
); |
} |
} |
class ItemWidget extends StatelessWidget { |
const ItemWidget({ |
super.key, |
required this.text, |
}); |
final string text; |
@override |
widget build(BuildContext context) { |
return card( |
child: SizedBox( |
height: 100, |
child: center(child: text(text)), |
), |
); |
} |
} |
<code_end> |
<topic_end> |
<topic_start> |
work with long lists |
the standard ListView constructor works well |
for small lists. to work with lists that contain |
a large number of items, it’s best to use the |
ListView.builder constructor. |
in contrast to the default ListView constructor, which requires |
creating all items at once, the ListView.builder() constructor |
creates items as they’re scrolled onto the screen. |
<topic_end> |
<topic_start> |
1. create a data source |
first, you need a data source. for example, your data source |
might be a list of messages, search results, or products in a store. |
most of the time, this data comes from the internet or a database. |
for this example, generate a list of 10,000 strings using the |
list.generate constructor. |
<code_start> |
List<String>.generate(10000, (i) => 'item $i'), |
<code_end> |
<topic_end> |
<topic_start> |
2. convert the data source into widgets |
to display the list of strings, render each string as a widget |
using ListView.builder(). |
in this example, display each string on its own line. |
<code_start> |
ListView.builder( |
itemCount: items.length, |
prototypeItem: ListTile( |
title: text(items.first), |
), |
itemBuilder: (context, index) { |
return ListTile( |
title: text(items[index]), |
); |
}, |
) |
<code_end> |
<topic_end> |
<topic_start> |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.