Skip to content

Commit be5b5c8

Browse files
authored
add a backward variant of BenchCardInfiniteScroll (flutter#58140)
1 parent 9989c54 commit be5b5c8

File tree

2 files changed

+37
-21
lines changed

2 files changed

+37
-21
lines changed

dev/benchmarks/macrobenchmarks/lib/src/web/bench_card_infinite_scroll.dart

+35-20
Original file line numberDiff line numberDiff line change
@@ -11,62 +11,77 @@ import 'test_data.dart';
1111

1212
/// Creates an infinite list of Material cards and scrolls it.
1313
class BenchCardInfiniteScroll extends WidgetRecorder {
14-
BenchCardInfiniteScroll() : super(name: benchmarkName);
14+
BenchCardInfiniteScroll.forward()
15+
: initialOffset = 0.0,
16+
finalOffset = 30000.0,
17+
super(name: benchmarkName);
18+
19+
BenchCardInfiniteScroll.backward()
20+
: initialOffset = 30000.0,
21+
finalOffset = 0.0,
22+
super(name: benchmarkNameBackward);
1523

1624
static const String benchmarkName = 'bench_card_infinite_scroll';
25+
static const String benchmarkNameBackward = 'bench_card_infinite_scroll_backward';
26+
27+
final double initialOffset;
28+
final double finalOffset;
1729

1830
@override
19-
Widget createWidget() => const MaterialApp(
20-
title: 'Infinite Card Scroll Benchmark',
21-
home: _InfiniteScrollCards(),
22-
);
31+
Widget createWidget() => MaterialApp(
32+
title: 'Infinite Card Scroll Benchmark',
33+
home: _InfiniteScrollCards(initialOffset, finalOffset),
34+
);
2335
}
2436

2537
class _InfiniteScrollCards extends StatefulWidget {
26-
const _InfiniteScrollCards({Key key}) : super(key: key);
38+
const _InfiniteScrollCards(this.initialOffset, this.finalOffset, {Key key}) : super(key: key);
39+
40+
final double initialOffset;
41+
final double finalOffset;
2742

2843
@override
2944
State<_InfiniteScrollCards> createState() => _InfiniteScrollCardsState();
3045
}
3146

3247
class _InfiniteScrollCardsState extends State<_InfiniteScrollCards> {
33-
ScrollController scrollController;
48+
static const Duration stepDuration = Duration(seconds: 20);
3449

50+
ScrollController scrollController;
3551
double offset;
36-
static const double distance = 1000;
37-
static const Duration stepDuration = Duration(seconds: 1);
3852

3953
@override
4054
void initState() {
4155
super.initState();
4256

43-
scrollController = ScrollController();
44-
offset = 0;
57+
offset = widget.initialOffset;
58+
59+
scrollController = ScrollController(
60+
initialScrollOffset: offset,
61+
);
4562

4663
// Without the timer the animation doesn't begin.
4764
Timer.run(() async {
48-
while (true) {
49-
await scrollController.animateTo(
50-
offset + distance,
51-
curve: Curves.linear,
52-
duration: stepDuration,
53-
);
54-
offset += distance;
55-
}
65+
await scrollController.animateTo(
66+
widget.finalOffset,
67+
curve: Curves.linear,
68+
duration: stepDuration,
69+
);
5670
});
5771
}
5872

5973
@override
6074
Widget build(BuildContext context) {
6175
return ListView.builder(
6276
controller: scrollController,
77+
itemExtent: 100.0,
6378
itemBuilder: (BuildContext context, int index) {
6479
return SizedBox(
6580
height: 100.0,
6681
child: Card(
6782
elevation: 16.0,
6883
child: Text(
69-
lipsum[index % lipsum.length],
84+
'${lipsum[index % lipsum.length]} $index',
7085
textAlign: TextAlign.center,
7186
),
7287
),

dev/benchmarks/macrobenchmarks/lib/web_benchmarks.dart

+2-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,8 @@ const bool isCanvasKit = bool.fromEnvironment('FLUTTER_WEB_USE_SKIA', defaultVal
3030
/// When adding a new benchmark, add it to this map. Make sure that the name
3131
/// of your benchmark is unique.
3232
final Map<String, RecorderFactory> benchmarks = <String, RecorderFactory>{
33-
BenchCardInfiniteScroll.benchmarkName: () => BenchCardInfiniteScroll(),
33+
BenchCardInfiniteScroll.benchmarkName: () => BenchCardInfiniteScroll.forward(),
34+
BenchCardInfiniteScroll.benchmarkNameBackward: () => BenchCardInfiniteScroll.backward(),
3435
BenchClippedOutPictures.benchmarkName: () => BenchClippedOutPictures(),
3536
BenchDrawRect.benchmarkName: () => BenchDrawRect(),
3637
BenchPathRecording.benchmarkName: () => BenchPathRecording(),

0 commit comments

Comments
 (0)