screenshot.dart 2.23 KB
Newer Older
Ian Hickson's avatar
Ian Hickson committed
1
// Copyright 2014 The Flutter Authors. All rights reserved.
2 3 4 5 6 7 8
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

import 'package:flutter/material.dart';
import 'package:flutter_driver/driver_extension.dart';

/// This sample application creates a hard to render frame, causing the
9 10
/// driver script to race the raster thread. If the driver script wins the
/// race, it will screenshot the previous frame. If the raster thread wins
11 12 13 14
/// it, it will screenshot the latest frame.
void main() {
  enableFlutterDriverExtension();

15
  runApp(const Toggler());
16 17 18
}

class Toggler extends StatefulWidget {
19
  const Toggler({super.key});
20

21
  @override
22
  State<Toggler> createState() => TogglerState();
23 24 25 26 27 28 29
}

class TogglerState extends State<Toggler> {
  bool _visible = false;

  @override
  Widget build(BuildContext context) {
30 31 32
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
33 34
          title: const Text('FlutterDriver test'),
        ),
35 36
        body: Material(
          child: Column(
37
            children: <Widget>[
38
              TextButton(
39 40 41 42 43 44 45 46
                key: const ValueKey<String>('toggle'),
                child: const Text('Toggle visibility'),
                onPressed: () {
                  setState(() {
                    _visible = !_visible;
                  });
                },
              ),
47 48
              Expanded(
                child: ListView(
49 50 51 52 53 54 55 56 57 58 59 60
                  children: _buildRows(_visible ? 10 : 0),
                ),
              ),
            ],
          ),
        ),
      ),
    );
  }
}

List<Widget> _buildRows(int count) {
61 62
  return List<Widget>.generate(count, (int i) {
    return Row(
63 64 65 66 67 68
      children: _buildCells(i / count),
    );
  });
}

/// Builds cells that are known to take time to render causing a delay on the
69
/// raster thread.
70
List<Widget> _buildCells(double epsilon) {
71 72 73
  return List<Widget>.generate(15, (int i) {
    return Expanded(
      child: Material(
74 75
        // A magic color that the test will be looking for on the screenshot.
        color: const Color(0xffff0102),
76
        borderRadius: BorderRadius.all(Radius.circular(i.toDouble() + epsilon)),
77 78 79 80 81 82
        elevation: 5.0,
        child: const SizedBox(height: 10.0, width: 10.0),
      ),
    );
  });
}