Skip to content

Commit 3e62302

Browse files
authored
Update horizontal list recipe (#12599)
- Add more items to the list so the list is scrollable on wider displays. - Creates one container for each of Flutter's supported primary colors rather than hard coding the list. - Highlight the primary `scrollDirection: Axis.horizontal,` line rather than relying on an inline comment. - Add a custom scroll behavior to the list so it's draggable on desktops as well. - Rely on a new inline comment about this rather than the note now that the referenced change has occurred a long time ago. Fixes #12596
1 parent bfb7cb5 commit 3e62302

File tree

2 files changed

+41
-46
lines changed

2 files changed

+41
-46
lines changed

examples/cookbook/lists/horizontal_list/lib/main.dart

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import 'dart:ui';
2+
13
import 'package:flutter/material.dart';
24

35
void main() => runApp(const MyApp());
@@ -7,7 +9,7 @@ class MyApp extends StatelessWidget {
79

810
@override
911
Widget build(BuildContext context) {
10-
const title = 'Horizontal List';
12+
const title = 'Horizontal list';
1113

1214
return MaterialApp(
1315
title: title,
@@ -16,19 +18,22 @@ class MyApp extends StatelessWidget {
1618
body: Container(
1719
margin: const EdgeInsets.symmetric(vertical: 20),
1820
height: 200,
19-
// #docregion ListView
20-
child: ListView(
21-
// This next line does the trick.
22-
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),
29-
],
21+
child: ScrollConfiguration(
22+
// Add a custom scroll behavior that
23+
// allows all devices to drag the list.
24+
behavior: const MaterialScrollBehavior().copyWith(
25+
dragDevices: {...PointerDeviceKind.values},
26+
),
27+
// #docregion list-view
28+
child: ListView(
29+
scrollDirection: Axis.horizontal,
30+
children: [
31+
for (final color in Colors.primaries)
32+
Container(width: 160, color: color),
33+
],
34+
),
35+
// #enddocregion list-view
3036
),
31-
// #enddocregion ListView
3237
),
3338
),
3439
);

src/content/cookbook/lists/horizontal-list.md

Lines changed: 23 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -15,35 +15,25 @@ The [`ListView`][] widget supports horizontal lists.
1515
Use 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
2020
ListView(
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+
4737
import 'package:flutter/material.dart';
4838
4939
void 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

Comments
 (0)