| 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 'package:flutter/material.dart'; |
| 6 | import 'package:flutter_driver/driver_extension.dart'; |
| 7 | |
| 8 | /// This sample application creates a hard to render frame, causing the |
| 9 | /// driver script to race the raster thread. If the driver script wins the |
| 10 | /// race, it will screenshot the previous frame. If the raster thread wins |
| 11 | /// it, it will screenshot the latest frame. |
| 12 | void main() { |
| 13 | enableFlutterDriverExtension(); |
| 14 | |
| 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 | class TogglerState extends State<Toggler> { |
| 26 | bool _visible = false; |
| 27 | |
| 28 | @override |
| 29 | Widget build(BuildContext context) { |
| 30 | return MaterialApp( |
| 31 | home: Scaffold( |
| 32 | appBar: AppBar(title: const Text('FlutterDriver test' )), |
| 33 | body: Material( |
| 34 | child: Column( |
| 35 | children: <Widget>[ |
| 36 | TextButton( |
| 37 | key: const ValueKey<String>('toggle' ), |
| 38 | child: const Text('Toggle visibility' ), |
| 39 | onPressed: () { |
| 40 | setState(() { |
| 41 | _visible = !_visible; |
| 42 | }); |
| 43 | }, |
| 44 | ), |
| 45 | Expanded(child: ListView(children: _buildRows(_visible ? 10 : 0))), |
| 46 | ], |
| 47 | ), |
| 48 | ), |
| 49 | ), |
| 50 | ); |
| 51 | } |
| 52 | } |
| 53 | |
| 54 | List<Widget> _buildRows(int count) { |
| 55 | return List<Widget>.generate(count, (int i) { |
| 56 | return Row(children: _buildCells(i / count)); |
| 57 | }); |
| 58 | } |
| 59 | |
| 60 | /// Builds cells that are known to take time to render causing a delay on the |
| 61 | /// raster thread. |
| 62 | List<Widget> _buildCells(double epsilon) { |
| 63 | return List<Widget>.generate(15, (int i) { |
| 64 | return Expanded( |
| 65 | child: Material( |
| 66 | // A magic color that the test will be looking for on the screenshot. |
| 67 | color: const Color(0xffff0102), |
| 68 | borderRadius: BorderRadius.all(Radius.circular(i.toDouble() + epsilon)), |
| 69 | elevation: 5.0, |
| 70 | child: const SizedBox(height: 10.0, width: 10.0), |
| 71 | ), |
| 72 | ); |
| 73 | }); |
| 74 | } |
| 75 | |