mirror of
https://github.com/flutter/flutter.git
synced 2026-02-20 02:29:02 +08:00
Update to ListView Sample Code in API Docs (#29072)
* Updated ListView Sample code with more examples for different constructors and also to match asset diagrams. * Fixed MIA semicolons. * Code cleanup. * Added context for ListView.builder example. * Analyzer does not like const and static usages. * Replaced the const declarations with final. The analyzer does not like the use of const here, at all. * Fixed parameterized declarations.
This commit is contained in:
parent
816ae4b193
commit
c4ffbb5eb9
@ -604,19 +604,78 @@ abstract class BoxScrollView extends ScrollView {
|
||||
/// padding. To avoid this behavior, override with a zero [padding] property.
|
||||
///
|
||||
/// {@tool sample}
|
||||
///
|
||||
/// An infinite list of children:
|
||||
/// This example uses the default constructor for [ListView] which takes an
|
||||
/// explicit [List<Widget>] of children. This [ListView]'s children are made up
|
||||
/// of [Container]s with [Text].
|
||||
///
|
||||
/// ```dart
|
||||
/// ListView.builder(
|
||||
/// padding: EdgeInsets.all(8.0),
|
||||
/// itemExtent: 20.0,
|
||||
/// itemBuilder: (BuildContext context, int index) {
|
||||
/// return Text('entry $index');
|
||||
/// },
|
||||
/// ListView(
|
||||
/// padding: const EdgeInsets.all(8.0),
|
||||
/// children: <Widget>[
|
||||
/// Container(
|
||||
/// height: 50,
|
||||
/// color: Colors.amber[600],
|
||||
/// child: const Center(child: Text('Entry A')),
|
||||
/// ),
|
||||
/// Container(
|
||||
/// height: 50,
|
||||
/// color: Colors.amber[500],
|
||||
/// child: const Center(child: Text('Entry B')),
|
||||
/// ),
|
||||
/// Container(
|
||||
/// height: 50,
|
||||
/// color: Colors.amber[100],
|
||||
/// child: const Center(child: Text('Entry C')),
|
||||
/// ),
|
||||
/// ],
|
||||
/// )
|
||||
/// ```
|
||||
/// {@end-tool}
|
||||
/// {@tool sample}
|
||||
/// This example mirrors the previous one, creating the same list using the
|
||||
/// [ListView.builder] constructor. Using the [IndexedWidgetBuilder], children
|
||||
/// are built lazily and can be infinite in number.
|
||||
///
|
||||
/// ```dart
|
||||
/// final List<String> entries = <String>['A', 'B', 'C'];
|
||||
/// final List<int> colorCodes = <int>[600, 500, 100];
|
||||
///
|
||||
/// ListView.builder(
|
||||
/// padding: const EdgeInsets.all(8.0),
|
||||
/// itemCount: entries.length,
|
||||
/// itemBuilder: (BuildContext context, int index) {
|
||||
/// return Container(
|
||||
/// height: 50,
|
||||
/// color: Colors.amber[colorCodes[index]],
|
||||
/// child: Center(child: Text('Entry ${entries[index]}')),
|
||||
/// );
|
||||
/// }
|
||||
/// );
|
||||
/// ```
|
||||
/// {@end-tool}
|
||||
/// {@tool sample}
|
||||
/// This example continues to build from our the previous ones, creating a
|
||||
/// similar list using [ListView.separated]. Here, a [Divider] is used as a
|
||||
/// separator.
|
||||
///
|
||||
/// ```dart
|
||||
/// final List<String> entries = <String>['A', 'B', 'C'];
|
||||
/// final List<int> colorCodes = <int>[600, 500, 100];
|
||||
///
|
||||
/// ListView.separated(
|
||||
/// padding: const EdgeInsets.all(8.0),
|
||||
/// itemCount: entries.length,
|
||||
/// itemBuilder: (BuildContext context, int index) {
|
||||
/// return Container(
|
||||
/// height: 50,
|
||||
/// color: Colors.amber[colorCodes[index]],
|
||||
/// child: Center(child: Text('Entry ${entries[index]}')),
|
||||
/// );
|
||||
/// },
|
||||
/// separatorBuilder: (BuildContext context, int index) => const Divider(),
|
||||
/// );
|
||||
/// ```
|
||||
/// {@end-tool}
|
||||
///
|
||||
/// ## Child elements' lifecycle
|
||||
///
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user