@@ -15,35 +15,25 @@ The [`ListView`][] widget supports horizontal lists.
1515Use the standard ` ListView ` constructor, passing in a horizontal
1616` scrollDirection ` , which overrides the default vertical direction.
1717
18- <? code-excerpt "lib/main.dart (ListView )" replace="/^child\: //g"?>
19- ``` dart
18+ <? code-excerpt "lib/main.dart (list-view )" replace="/^child\: //g"?>
19+ ``` dart highlightLines=2
2020ListView(
21- // This next line does the trick.
2221 scrollDirection: Axis.horizontal,
23- children: <Widget>[
24- Container(width: 160, color: Colors.red),
25- Container(width: 160, color: Colors.blue),
26- Container(width: 160, color: Colors.green),
27- Container(width: 160, color: Colors.yellow),
28- Container(width: 160, color: Colors.orange),
22+ children: [
23+ for (final color in Colors.primaries)
24+ Container(width: 160, color: color),
2925 ],
3026),
3127```
3228
33- ## Interactive example
34-
35- :::note Desktop and web note
36- This example works in the browser and on the desktop.
37- However, as this list scrolls on the horizontal axis
38- (left to right or right to left),
39- hold <kbd >Shift</kbd > while using the mouse scroll wheel to scroll the list.
29+ [ `ListView` ] : {{site.api}}/flutter/widgets/ListView-class.html
4030
41- To learn more, read the [ breaking change] [ ] page on the
42- default drag for scrolling devices.
43- :::
31+ ## Interactive example
4432
4533<? code-excerpt "lib/main.dart"?>
4634``` dartpad title="Flutter horizontal list hands-on example in DartPad" run="true"
35+ import 'dart:ui';
36+
4737import 'package:flutter/material.dart';
4838
4939void main() => runApp(const MyApp());
@@ -53,7 +43,7 @@ class MyApp extends StatelessWidget {
5343
5444 @override
5545 Widget build(BuildContext context) {
56- const title = 'Horizontal List ';
46+ const title = 'Horizontal list ';
5747
5848 return MaterialApp(
5949 title: title,
@@ -62,16 +52,19 @@ class MyApp extends StatelessWidget {
6252 body: Container(
6353 margin: const EdgeInsets.symmetric(vertical: 20),
6454 height: 200,
65- child: ListView(
66- // This next line does the trick.
67- scrollDirection: Axis.horizontal,
68- children: <Widget>[
69- Container(width: 160, color: Colors.red),
70- Container(width: 160, color: Colors.blue),
71- Container(width: 160, color: Colors.green),
72- Container(width: 160, color: Colors.yellow),
73- Container(width: 160, color: Colors.orange),
74- ],
55+ child: ScrollConfiguration(
56+ // Add a custom scroll behavior that
57+ // allows all devices to drag the list.
58+ behavior: const MaterialScrollBehavior().copyWith(
59+ dragDevices: {...PointerDeviceKind.values},
60+ ),
61+ child: ListView(
62+ scrollDirection: Axis.horizontal,
63+ children: [
64+ for (final color in Colors.primaries)
65+ Container(width: 160, color: color),
66+ ],
67+ ),
7568 ),
7669 ),
7770 ),
@@ -83,6 +76,3 @@ class MyApp extends StatelessWidget {
8376<noscript >
8477 <img src =" /assets/images/docs/cookbook/horizontal-list.webp " alt =" Horizontal List Demo " class =" site-mobile-screenshot " />
8578</noscript >
86-
87- [ breaking change ] : /release/breaking-changes/default-scroll-behavior-drag
88- [ `ListView` ] : {{site.api}}/flutter/widgets/ListView-class.html
0 commit comments