bench_card_infinite_scroll.dart 1.92 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77
// 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 {
  BenchCardInfiniteScroll() : super(name: benchmarkName);

  static const String benchmarkName = 'bench_card_infinite_scroll';

  @override
  Widget createWidget() => const MaterialApp(
        title: 'Infinite Card Scroll Benchmark',
        home: _InfiniteScrollCards(),
      );
}

class _InfiniteScrollCards extends StatefulWidget {
  const _InfiniteScrollCards({Key key}) : super(key: key);

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

class _InfiniteScrollCardsState extends State<_InfiniteScrollCards> {
  ScrollController scrollController;

  double offset;
  static const double distance = 1000;
  static const Duration stepDuration = Duration(seconds: 1);

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

    scrollController = ScrollController();
    offset = 0;

    // Without the timer the animation doesn't begin.
    Timer.run(() async {
      while (true) {
        await scrollController.animateTo(
          offset + distance,
          curve: Curves.linear,
          duration: stepDuration,
        );
        offset += distance;
      }
    });
  }

  @override
  Widget build(BuildContext context) {
    return ListView.builder(
      controller: scrollController,
      itemBuilder: (BuildContext context, int index) {
        return SizedBox(
          height: 100.0,
          child: Card(
            elevation: 16.0,
            child: Text(
              lipsum[index % lipsum.length],
              textAlign: TextAlign.center,
            ),
          ),
        );
      },
    );
  }
}