|
| 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:io'; |
| 6 | + |
| 7 | +import 'package:flutter/material.dart'; |
| 8 | +import 'package:flutter/scheduler.dart' show timeDilation; |
| 9 | + |
| 10 | +void main() { |
| 11 | + runApp( |
| 12 | + const PlatformViewApp() |
| 13 | + ); |
| 14 | +} |
| 15 | + |
| 16 | +class PlatformViewApp extends StatefulWidget { |
| 17 | + const PlatformViewApp({ |
| 18 | + super.key, |
| 19 | + }); |
| 20 | + |
| 21 | + @override |
| 22 | + PlatformViewAppState createState() => PlatformViewAppState(); |
| 23 | +} |
| 24 | + |
| 25 | +class PlatformViewAppState extends State<PlatformViewApp> { |
| 26 | + @override |
| 27 | + Widget build(BuildContext context) { |
| 28 | + return MaterialApp( |
| 29 | + theme: ThemeData.light(), |
| 30 | + title: 'Advanced Layout', |
| 31 | + home: const PlatformViewLayout(), |
| 32 | + ); |
| 33 | + } |
| 34 | + |
| 35 | + void toggleAnimationSpeed() { |
| 36 | + setState(() { |
| 37 | + timeDilation = (timeDilation != 1.0) ? 1.0 : 5.0; |
| 38 | + }); |
| 39 | + } |
| 40 | +} |
| 41 | + |
| 42 | +class PlatformViewLayout extends StatelessWidget { |
| 43 | + const PlatformViewLayout({ super.key }); |
| 44 | + |
| 45 | + @override |
| 46 | + Widget build(BuildContext context) { |
| 47 | + return Scaffold( |
| 48 | + appBar: AppBar(title: const Text('Platform View Scrolling Layout')), |
| 49 | + body: ListView.builder( |
| 50 | + key: const Key('platform-views-scroll'), // This key is used by the driver test. |
| 51 | + itemCount: 200, |
| 52 | + itemBuilder: (BuildContext context, int index) { |
| 53 | + return Padding( |
| 54 | + padding: const EdgeInsets.all(5.0), |
| 55 | + child: Material( |
| 56 | + elevation: (index % 5 + 1).toDouble(), |
| 57 | + color: Colors.white, |
| 58 | + child: index.isEven |
| 59 | + ? CustomPaint(painter: ExpensivePainter(), size: const Size(400, 200)) |
| 60 | + : const DummyPlatformView() |
| 61 | + ), |
| 62 | + ); |
| 63 | + }, |
| 64 | + ), |
| 65 | + ); |
| 66 | + } |
| 67 | +} |
| 68 | + |
| 69 | +class DummyPlatformView extends StatelessWidget { |
| 70 | + const DummyPlatformView({super.key}); |
| 71 | + |
| 72 | + @override |
| 73 | + Widget build(BuildContext context) { |
| 74 | + const String viewType = 'benchmarks/platform_views_layout/DummyPlatformView'; |
| 75 | + late Widget nativeView; |
| 76 | + if (Platform.isIOS) { |
| 77 | + nativeView = const UiKitView( |
| 78 | + viewType: viewType, |
| 79 | + ); |
| 80 | + } else if (Platform.isAndroid) { |
| 81 | + nativeView = const AndroidView( |
| 82 | + viewType: viewType, |
| 83 | + ); |
| 84 | + } else { |
| 85 | + assert(false, 'Invalid platform'); |
| 86 | + } |
| 87 | + return Container( |
| 88 | + color: Colors.purple, |
| 89 | + height: 200.0, |
| 90 | + child: nativeView, |
| 91 | + ); |
| 92 | + } |
| 93 | +} |
| 94 | + |
| 95 | +class ExpensivePainter extends CustomPainter { |
| 96 | + @override |
| 97 | + void paint(Canvas canvas, Size size) { |
| 98 | + final double boxWidth = size.width / 50; |
| 99 | + final double boxHeight = size.height / 50; |
| 100 | + for (int i = 0; i < 50; i++) { |
| 101 | + for (int j = 0; j < 50; j++) { |
| 102 | + final Rect rect = Rect.fromLTWH(i * boxWidth, j * boxHeight, boxWidth, boxHeight); |
| 103 | + canvas.drawRect(rect, Paint() |
| 104 | + ..style = PaintingStyle.fill |
| 105 | + ..color = Colors.red |
| 106 | + ); |
| 107 | + } |
| 108 | + } |
| 109 | + } |
| 110 | + |
| 111 | + @override |
| 112 | + bool shouldRepaint(covariant CustomPainter oldDelegate) { |
| 113 | + return false; |
| 114 | + } |
| 115 | +} |
0 commit comments