Skip to content

Commit 29306b0

Browse files
author
Harry Terkelsen
authored
[Web] Add Material Card Infinite Scroll benchmark (flutter#51005)
* [Web] Add Material Card Infinite Scroll benchmark Adds a benchmark that makes an infinite list of Material cards and scrolls it. This benchmark exercises more heavyweight rendering like shadows and clipping and paths. * Fix analyzer warnings. Respond to comments
1 parent 52ee8a6 commit 29306b0

File tree

2 files changed

+79
-0
lines changed

2 files changed

+79
-0
lines changed
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
// Copyright 2014 The Flutter Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style license that can be
3+
// found in the LICENSE file.
4+
5+
import 'dart:async';
6+
7+
import 'package:flutter/material.dart';
8+
9+
import 'recorder.dart';
10+
import 'test_data.dart';
11+
12+
/// Creates an infinite list of Material cards and scrolls it.
13+
class BenchCardInfiniteScroll extends WidgetRecorder {
14+
BenchCardInfiniteScroll() : super(name: benchmarkName);
15+
16+
static const String benchmarkName = 'bench_card_infinite_scroll';
17+
18+
@override
19+
Widget createWidget() => const MaterialApp(
20+
title: 'Infinite Card Scroll Benchmark',
21+
home: _InfiniteScrollCards(),
22+
);
23+
}
24+
25+
class _InfiniteScrollCards extends StatefulWidget {
26+
const _InfiniteScrollCards({Key key}) : super(key: key);
27+
28+
@override
29+
State<_InfiniteScrollCards> createState() => _InfiniteScrollCardsState();
30+
}
31+
32+
class _InfiniteScrollCardsState extends State<_InfiniteScrollCards> {
33+
ScrollController scrollController;
34+
35+
double offset;
36+
static const double distance = 1000;
37+
static const Duration stepDuration = Duration(seconds: 1);
38+
39+
@override
40+
void initState() {
41+
super.initState();
42+
43+
scrollController = ScrollController();
44+
offset = 0;
45+
46+
// Without the timer the animation doesn't begin.
47+
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+
}
56+
});
57+
}
58+
59+
@override
60+
Widget build(BuildContext context) {
61+
return ListView.builder(
62+
controller: scrollController,
63+
itemBuilder: (BuildContext context, int index) {
64+
return SizedBox(
65+
height: 100.0,
66+
child: Card(
67+
elevation: 16.0,
68+
child: Text(
69+
lipsum[index % lipsum.length],
70+
textAlign: TextAlign.center,
71+
),
72+
),
73+
);
74+
},
75+
);
76+
}
77+
}

dev/benchmarks/macrobenchmarks/lib/web_benchmarks.dart

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import 'dart:html' as html;
88

99
import 'package:macrobenchmarks/src/web/bench_text_out_of_picture_bounds.dart';
1010

11+
import 'src/web/bench_card_infinite_scroll.dart';
1112
import 'src/web/bench_draw_rect.dart';
1213
import 'src/web/bench_simple_lazy_text_scroll.dart';
1314
import 'src/web/bench_text_out_of_picture_bounds.dart';
@@ -16,6 +17,7 @@ import 'src/web/recorder.dart';
1617
typedef RecorderFactory = Recorder Function();
1718

1819
final Map<String, RecorderFactory> benchmarks = <String, RecorderFactory>{
20+
BenchCardInfiniteScroll.benchmarkName: () => BenchCardInfiniteScroll(),
1921
BenchDrawRect.benchmarkName: () => BenchDrawRect(),
2022
BenchTextOutOfPictureBounds.benchmarkName: () => BenchTextOutOfPictureBounds(),
2123
BenchSimpleLazyTextScroll.benchmarkName: () => BenchSimpleLazyTextScroll(),

0 commit comments

Comments
 (0)