bench_card_infinite_scroll.dart 2.36 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13
// Copyright 2014 The Flutter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

import 'dart:async';

import 'package:flutter/material.dart';

import 'recorder.dart';
import 'test_data.dart';

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

  BenchCardInfiniteScroll.backward()
    : initialOffset = 30000.0,
      finalOffset = 0.0,
      super(name: benchmarkNameBackward);
23 24

  static const String benchmarkName = 'bench_card_infinite_scroll';
25 26 27 28
  static const String benchmarkNameBackward = 'bench_card_infinite_scroll_backward';

  final double initialOffset;
  final double finalOffset;
29 30

  @override
31 32 33 34
  Widget createWidget() => MaterialApp(
    title: 'Infinite Card Scroll Benchmark',
    home: _InfiniteScrollCards(initialOffset, finalOffset),
  );
35 36 37
}

class _InfiniteScrollCards extends StatefulWidget {
38 39 40 41
  const _InfiniteScrollCards(this.initialOffset, this.finalOffset, {Key key}) : super(key: key);

  final double initialOffset;
  final double finalOffset;
42 43 44 45 46 47

  @override
  State<_InfiniteScrollCards> createState() => _InfiniteScrollCardsState();
}

class _InfiniteScrollCardsState extends State<_InfiniteScrollCards> {
48
  static const Duration stepDuration = Duration(seconds: 20);
49

50
  ScrollController scrollController;
51 52 53 54 55 56
  double offset;

  @override
  void initState() {
    super.initState();

57 58 59 60 61
    offset = widget.initialOffset;

    scrollController = ScrollController(
      initialScrollOffset: offset,
    );
62 63 64

    // Without the timer the animation doesn't begin.
    Timer.run(() async {
65 66 67 68 69
      await scrollController.animateTo(
        widget.finalOffset,
        curve: Curves.linear,
        duration: stepDuration,
      );
70 71 72 73 74 75 76
    });
  }

  @override
  Widget build(BuildContext context) {
    return ListView.builder(
      controller: scrollController,
77
      itemExtent: 100.0,
78 79 80 81 82 83
      itemBuilder: (BuildContext context, int index) {
        return SizedBox(
          height: 100.0,
          child: Card(
            elevation: 16.0,
            child: Text(
84
              '${lipsum[index % lipsum.length]} $index',
85 86 87 88 89 90 91 92
              textAlign: TextAlign.center,
            ),
          ),
        );
      },
    );
  }
}