|
| 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:convert'; |
| 6 | + |
| 7 | +import 'package:android_driver_extensions/extension.dart'; |
| 8 | +import 'package:flutter/foundation.dart'; |
| 9 | +import 'package:flutter/gestures.dart'; |
| 10 | +import 'package:flutter/material.dart'; |
| 11 | +import 'package:flutter/rendering.dart'; |
| 12 | +import 'package:flutter/services.dart'; |
| 13 | +import 'package:flutter_driver/driver_extension.dart'; |
| 14 | + |
| 15 | +import '../src/allow_list_devices.dart'; |
| 16 | + |
| 17 | +/// The size of the platform view, in *physical* pixels. |
| 18 | +/// |
| 19 | +/// Deliberately not a whole number of pixels. Android view geometry is |
| 20 | +/// integral, so the engine has to snap this to the pixel grid; if it truncates |
| 21 | +/// instead of rounding, the view ends up 200x200 and a line of |
| 22 | +/// [_underlayColor] is left uncovered along the right and bottom edges. |
| 23 | +/// |
| 24 | +/// This is the same defect that a platform view covering the whole screen hits |
| 25 | +/// on a device whose device pixel ratio does not evenly divide its resolution |
| 26 | +/// (e.g. 1080 / 2.625 narrowed to a float, multiplied back out, is |
| 27 | +/// 1079.9999656677246), just provoked deterministically on any device. |
| 28 | +/// |
| 29 | +/// See https://github.com/flutter/flutter/issues/189834. |
| 30 | +const double _viewPhysicalSize = 200.75; |
| 31 | + |
| 32 | +/// The size of the clip applied to the platform view, in *physical* pixels. |
| 33 | +/// |
| 34 | +/// Also deliberately not a whole number of pixels; the clip bounds are handed |
| 35 | +/// to `FlutterMutatorsStack.pushClipRect` and have the same rounding hazard. |
| 36 | +const double _clipPhysicalSize = 150.75; |
| 37 | + |
| 38 | +const Color _backgroundColor = Colors.red; |
| 39 | +const Color _underlayColor = Colors.green; |
| 40 | + |
| 41 | +void main() async { |
| 42 | + ensureAndroidDevice(); |
| 43 | + enableFlutterDriverExtension( |
| 44 | + handler: (String? command) async { |
| 45 | + return json.encode(<String, Object?>{ |
| 46 | + 'supported': await HybridAndroidViewController.checkIfSupported(), |
| 47 | + }); |
| 48 | + }, |
| 49 | + commands: <CommandExtension>[nativeDriverCommands], |
| 50 | + ); |
| 51 | + |
| 52 | + // Run on full screen. |
| 53 | + await SystemChrome.setEnabledSystemUIMode(SystemUiMode.immersive); |
| 54 | + runApp(const FractionalSizeApp()); |
| 55 | +} |
| 56 | + |
| 57 | +class FractionalSizeApp extends StatelessWidget { |
| 58 | + const FractionalSizeApp({super.key}); |
| 59 | + |
| 60 | + @override |
| 61 | + Widget build(BuildContext context) { |
| 62 | + return const MaterialApp(debugShowCheckedModeBanner: false, home: FractionalSizeHomePage()); |
| 63 | + } |
| 64 | +} |
| 65 | + |
| 66 | +class FractionalSizeHomePage extends StatefulWidget { |
| 67 | + const FractionalSizeHomePage({super.key}); |
| 68 | + |
| 69 | + @override |
| 70 | + State<FractionalSizeHomePage> createState() => _FractionalSizeHomePageState(); |
| 71 | +} |
| 72 | + |
| 73 | +class _FractionalSizeHomePageState extends State<FractionalSizeHomePage> { |
| 74 | + bool _isClipped = false; |
| 75 | + |
| 76 | + void _toggleClip() { |
| 77 | + setState(() { |
| 78 | + _isClipped = !_isClipped; |
| 79 | + }); |
| 80 | + } |
| 81 | + |
| 82 | + @override |
| 83 | + Widget build(BuildContext context) { |
| 84 | + final double devicePixelRatio = MediaQuery.devicePixelRatioOf(context); |
| 85 | + final double viewSize = _viewPhysicalSize / devicePixelRatio; |
| 86 | + final double underlayPhysicalSize = (_isClipped ? _clipPhysicalSize : _viewPhysicalSize) |
| 87 | + .roundToDouble(); |
| 88 | + final double underlaySize = underlayPhysicalSize / devicePixelRatio; |
| 89 | + |
| 90 | + Widget platformView = const _HybridCompositionAndroidPlatformView( |
| 91 | + viewType: 'box_platform_view', |
| 92 | + ); |
| 93 | + if (_isClipped) { |
| 94 | + platformView = ClipRect( |
| 95 | + clipper: _FractionalClipper(dimension: _clipPhysicalSize / devicePixelRatio), |
| 96 | + child: platformView, |
| 97 | + ); |
| 98 | + } |
| 99 | + |
| 100 | + // The platform view is pinned to the top left so that only its right and |
| 101 | + // bottom edges land on a fractional pixel, which is where the artifact |
| 102 | + // shows up. |
| 103 | + return Scaffold( |
| 104 | + backgroundColor: _backgroundColor, |
| 105 | + body: Stack( |
| 106 | + children: <Widget>[ |
| 107 | + Positioned( |
| 108 | + left: 0, |
| 109 | + top: 0, |
| 110 | + width: underlaySize, |
| 111 | + height: underlaySize, |
| 112 | + child: const ColoredBox(color: _underlayColor), |
| 113 | + ), |
| 114 | + Positioned(left: 0, top: 0, width: viewSize, height: viewSize, child: platformView), |
| 115 | + Positioned( |
| 116 | + left: 0, |
| 117 | + right: 0, |
| 118 | + bottom: 0, |
| 119 | + child: Center( |
| 120 | + child: ElevatedButton( |
| 121 | + key: const ValueKey<String>('toggle_clip_button'), |
| 122 | + onPressed: _toggleClip, |
| 123 | + child: Text(_isClipped ? 'Disable ClipRect' : 'Enable ClipRect'), |
| 124 | + ), |
| 125 | + ), |
| 126 | + ), |
| 127 | + ], |
| 128 | + ), |
| 129 | + ); |
| 130 | + } |
| 131 | +} |
| 132 | + |
| 133 | +/// Clips to a square of [dimension] anchored at the top left of the widget. |
| 134 | +final class _FractionalClipper extends CustomClipper<Rect> { |
| 135 | + const _FractionalClipper({required this.dimension}); |
| 136 | + |
| 137 | + final double dimension; |
| 138 | + |
| 139 | + @override |
| 140 | + Rect getClip(Size size) => Rect.fromLTWH(0, 0, dimension, dimension); |
| 141 | + |
| 142 | + @override |
| 143 | + bool shouldReclip(covariant _FractionalClipper oldClipper) { |
| 144 | + return oldClipper.dimension != dimension; |
| 145 | + } |
| 146 | +} |
| 147 | + |
| 148 | +final class _HybridCompositionAndroidPlatformView extends StatelessWidget { |
| 149 | + const _HybridCompositionAndroidPlatformView({required this.viewType}); |
| 150 | + |
| 151 | + final String viewType; |
| 152 | + |
| 153 | + @override |
| 154 | + Widget build(BuildContext context) { |
| 155 | + return PlatformViewLink( |
| 156 | + viewType: viewType, |
| 157 | + surfaceFactory: (BuildContext context, PlatformViewController controller) { |
| 158 | + return AndroidViewSurface( |
| 159 | + controller: controller as AndroidViewController, |
| 160 | + gestureRecognizers: const <Factory<OneSequenceGestureRecognizer>>{}, |
| 161 | + hitTestBehavior: PlatformViewHitTestBehavior.transparent, |
| 162 | + ); |
| 163 | + }, |
| 164 | + onCreatePlatformView: (PlatformViewCreationParams params) { |
| 165 | + return PlatformViewsService.initHybridAndroidView( |
| 166 | + id: params.id, |
| 167 | + viewType: viewType, |
| 168 | + layoutDirection: TextDirection.ltr, |
| 169 | + creationParamsCodec: const StandardMessageCodec(), |
| 170 | + ) |
| 171 | + ..addOnPlatformViewCreatedListener(params.onPlatformViewCreated) |
| 172 | + ..create(); |
| 173 | + }, |
| 174 | + ); |
| 175 | + } |
| 176 | +} |
0 commit comments