Skip to content

Commit a357014

Browse files
authored
[cupertino_ui] Fix platform view sheet transitions (flutter#12565)
From flutter#182935 Fixes flutter#169495 The scale transition in showCupertinoSheet for the underlying route defaulted to FilterQuality.medium, which pushes an implicit `ImageFilterLayer`. Platform views cannot be correctly sampled or transformed by rasterization-based filters. Consequently, they fail to reflect the scale transformation during the transition animation when an `ImageFilterLayer` is present, leading to a broken visual effect where the background scales but the platform view remains at its original size. **Solution:** Added `hasPlatformView` parameter. `false` by default which matches the original behavior. When set to `true`, we use standard `TransformLayer` by setting filterQuality to `null`. ## Pre-Review Checklist
1 parent 31c7afb commit a357014

3 files changed

Lines changed: 173 additions & 9 deletions

File tree

‎packages/cupertino_ui/lib/src/sheet.dart‎

Lines changed: 77 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,11 @@ typedef _GetSheetDragged = bool Function();
143143
/// When `showDragHandle` is set to `true`, then a drag handle will be placed at
144144
/// the top of the sheet. This flag will default to false.
145145
///
146+
/// The `hasPlatformViews` parameter should be set to `true` when the sheet or
147+
/// the route underneath it contains a platform view that is composited by the
148+
/// host platform. See [CupertinoSheetTransition.hasPlatformViews] for
149+
/// platform-specific details.
150+
///
146151
/// iOS sheet widgets are generally designed to be tightly coupled to the context
147152
/// of the widget that opened the sheet. As such, it is not recommended to push
148153
/// a non-sheet route that covers the sheet without first popping the sheet. If
@@ -194,6 +199,7 @@ Future<T?> showCupertinoSheet<T>({
194199
RouteSettings? settings,
195200
double? topGap,
196201
bool showDragHandle = false,
202+
bool hasPlatformViews = false,
197203
}) {
198204
assert(topGap == null || (topGap >= 0.0 && topGap <= 0.9), 'topGap must be between 0.0 and 0.9');
199205
assert(pageBuilder != null || builder != null || scrollableBuilder != null);
@@ -211,6 +217,7 @@ Future<T?> showCupertinoSheet<T>({
211217
settings: settings,
212218
enableDrag: enableDrag,
213219
topGap: topGap,
220+
hasPlatformViews: hasPlatformViews,
214221
);
215222

216223
return Navigator.of(context, rootNavigator: true).push<T>(route);
@@ -255,6 +262,7 @@ Future<T?> showCupertinoSheet<T>({
255262
settings: settings,
256263
enableDrag: enableDrag,
257264
topGap: topGap,
265+
hasPlatformViews: hasPlatformViews,
258266
);
259267
return Navigator.of(context, rootNavigator: true).push<T>(route);
260268
}
@@ -273,6 +281,7 @@ class CupertinoSheetTransition extends StatefulWidget {
273281
required this.secondaryRouteAnimation,
274282
required this.child,
275283
required this.linearTransition,
284+
this.hasPlatformViews = false,
276285
this.topGap = _kTopGapRatio,
277286
});
278287

@@ -304,21 +313,61 @@ class CupertinoSheetTransition extends StatefulWidget {
304313
/// {@endtemplate}
305314
final double topGap;
306315

316+
/// {@template cupertino_ui.CupertinoSheetTransition.hasPlatformViews}
317+
/// Whether the sheet or the route underneath it contains a platform view
318+
/// that is composited by the host platform.
319+
///
320+
/// On iOS and macOS, set this to `true` when the sheet or the route underneath
321+
/// it contains a platform view. On Android, this flag is generally not needed
322+
/// unless the platform view uses a native-view composition mode such as hybrid
323+
/// composition.
324+
///
325+
/// When set to `false` (the default), the transition uses image-filtered
326+
/// scaling for high-quality rendering. This can cause a native-composited
327+
/// platform view to appear detached from the rest of the route. Set this to
328+
/// `true` to keep it synchronized with the rest of the transition.
329+
/// {@endtemplate}
330+
final bool hasPlatformViews;
331+
332+
static Widget _delegateTransitionWithPlatformViews(
333+
BuildContext context,
334+
Animation<double> animation,
335+
Animation<double> secondaryAnimation,
336+
bool allowSnapshotting,
337+
Widget? child,
338+
) => delegateTransition(
339+
context,
340+
animation,
341+
secondaryAnimation,
342+
allowSnapshotting,
343+
child,
344+
hasPlatformViews: true,
345+
);
346+
307347
/// The primary delegated transition. Will slide a non [CupertinoSheetRoute] page down.
308348
///
309349
/// Provided to the previous route to coordinate transitions between routes.
310350
///
311351
/// If a [CupertinoSheetRoute] already exists in the stack, then it will
312352
/// slide the previous sheet upwards instead.
353+
///
354+
/// Set [hasPlatformViews] to `true` when the sheet or the route underneath it
355+
/// contains a platform view that is composited by the host platform. See
356+
/// [CupertinoSheetTransition.hasPlatformViews] for platform-specific details.
313357
static Widget delegateTransition(
314358
BuildContext context,
315359
Animation<double> animation,
316360
Animation<double> secondaryAnimation,
317361
bool allowSnapshotting,
318-
Widget? child,
319-
) {
362+
Widget? child, {
363+
bool hasPlatformViews = false,
364+
}) {
320365
if (CupertinoSheetRoute.hasParentSheet(context)) {
321-
return _delegatedCoverSheetSecondaryTransition(secondaryAnimation, child);
366+
return _delegatedCoverSheetSecondaryTransition(
367+
secondaryAnimation,
368+
child,
369+
hasPlatformViews: hasPlatformViews,
370+
);
322371
}
323372
final bool linear = Navigator.of(context).userGestureInProgress;
324373

@@ -377,7 +426,12 @@ class CupertinoSheetTransition extends StatefulWidget {
377426
position: slideAnimation,
378427
child: ScaleTransition(
379428
scale: scaleAnimation,
380-
filterQuality: FilterQuality.medium,
429+
// During animation, FilterQuality.medium uses an ImageFilterLayer, which is
430+
// usually worthwhile for animated transforms and avoids the observed subpixel
431+
// drift. A null filter quality always uses a TransformLayer to keep platform
432+
// views synchronized with the host. When not animating, filterQuality is ignored
433+
// and both options have the same effect.
434+
filterQuality: hasPlatformViews ? null : FilterQuality.medium,
381435
alignment: Alignment.topCenter,
382436
child: AnimatedBuilder(
383437
animation: radiusAnimation,
@@ -399,8 +453,9 @@ class CupertinoSheetTransition extends StatefulWidget {
399453

400454
static Widget _delegatedCoverSheetSecondaryTransition(
401455
Animation<double> secondaryAnimation,
402-
Widget? child,
403-
) {
456+
Widget? child, {
457+
bool hasPlatformViews = false,
458+
}) {
404459
const Curve curve = Curves.linearToEaseOut;
405460
const Curve reverseCurve = Curves.easeInToLinear;
406461
final curvedAnimation = CurvedAnimation(
@@ -418,7 +473,7 @@ class CupertinoSheetTransition extends StatefulWidget {
418473
transformHitTests: false,
419474
child: ScaleTransition(
420475
scale: scaleAnimation,
421-
filterQuality: FilterQuality.medium,
476+
filterQuality: hasPlatformViews ? null : FilterQuality.medium,
422477
alignment: Alignment.topCenter,
423478
child: ClipRSuperellipse(
424479
borderRadius: const BorderRadius.vertical(top: Radius.circular(12)),
@@ -537,7 +592,7 @@ class _CupertinoSheetTransitionState extends State<CupertinoSheetTransition>
537592
transformHitTests: false,
538593
child: ScaleTransition(
539594
scale: _secondaryScaleAnimation,
540-
filterQuality: FilterQuality.medium,
595+
filterQuality: widget.hasPlatformViews ? null : FilterQuality.medium,
541596
alignment: Alignment.topCenter,
542597
child: child,
543598
),
@@ -667,6 +722,7 @@ class CupertinoSheetRoute<T> extends PageRoute<T> with _CupertinoSheetRouteTrans
667722
this.scrollableBuilder,
668723
this.enableDrag = true,
669724
this.showDragHandle = false,
725+
this.hasPlatformViews = false,
670726
double? topGap,
671727
}) : assert(
672728
topGap == null || (topGap >= 0.0 && topGap <= 0.9),
@@ -712,6 +768,10 @@ class CupertinoSheetRoute<T> extends PageRoute<T> with _CupertinoSheetRouteTrans
712768
@override
713769
final bool enableDrag;
714770

771+
/// {@macro cupertino_ui.CupertinoSheetTransition.hasPlatformViews}
772+
@override
773+
final bool hasPlatformViews;
774+
715775
// The gap between the top of the screen and the top of the sheet.
716776
final double? _topGap;
717777

@@ -850,7 +910,9 @@ mixin _CupertinoSheetRouteTransitionMixin<T> on PageRoute<T> {
850910
if (_hasCustomTopGap) {
851911
return null;
852912
}
853-
return CupertinoSheetTransition.delegateTransition;
913+
return hasPlatformViews
914+
? CupertinoSheetTransition._delegateTransitionWithPlatformViews
915+
: CupertinoSheetTransition.delegateTransition;
854916
}
855917

856918
/// Determines whether the content can be dragged.
@@ -864,6 +926,9 @@ mixin _CupertinoSheetRouteTransitionMixin<T> on PageRoute<T> {
864926
/// {@macro cupertino_ui.CupertinoSheetTransition.topGap}
865927
double get topGap;
866928

929+
/// {@macro cupertino_ui.CupertinoSheetTransition.hasPlatformViews}
930+
bool get hasPlatformViews;
931+
867932
/// Whether a custom top gap has been set.
868933
bool get _hasCustomTopGap;
869934

@@ -898,13 +963,15 @@ mixin _CupertinoSheetRouteTransitionMixin<T> on PageRoute<T> {
898963
Widget child,
899964
bool enableDrag,
900965
double topGap,
966+
bool hasPlatformViews,
901967
) {
902968
final bool linearTransition = route.popGestureInProgress;
903969
return CupertinoSheetTransition(
904970
primaryRouteAnimation: animation,
905971
secondaryRouteAnimation: secondaryAnimation,
906972
linearTransition: linearTransition,
907973
topGap: topGap,
974+
hasPlatformViews: hasPlatformViews,
908975
child: _CupertinoDragGestureDetector<T>(
909976
enabledCallback: () => enableDrag,
910977
onStartPopGesture: () => _startPopGesture<T>(route, topGap),
@@ -941,6 +1008,7 @@ mixin _CupertinoSheetRouteTransitionMixin<T> on PageRoute<T> {
9411008
child,
9421009
enableDrag,
9431010
topGap,
1011+
hasPlatformViews,
9441012
);
9451013
}
9461014
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
changelog: |
2+
- Add `hasPlatformViews` to Cupertino sheet transitions to keep platform views synchronized with the underlying route.
3+
version: minor

‎packages/cupertino_ui/test/sheet_test.dart‎

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import 'dart:async' show unawaited;
66

77
import 'package:cupertino_ui/cupertino_ui.dart';
8+
import 'package:flutter/rendering.dart';
89
import 'package:flutter/services.dart';
910
import 'package:flutter_localizations/flutter_localizations.dart' show GlobalWidgetsLocalizations;
1011
import 'package:flutter_test/flutter_test.dart';
@@ -2298,6 +2299,98 @@ void main() {
22982299
await tester.pumpAndSettle();
22992300
});
23002301
});
2302+
2303+
group('hasPlatformViews parameter tests', () {
2304+
final GlobalKey scaffoldKey = GlobalKey();
2305+
2306+
bool checkHasImageFilterLayer() {
2307+
// The transition widget is the ancestor of the scaffold.
2308+
final BuildContext context = scaffoldKey.currentContext!;
2309+
var found = false;
2310+
context.visitAncestorElements((element) {
2311+
final RenderObject? renderObject = element.findRenderObject();
2312+
if (renderObject?.debugLayer is ImageFilterLayer) {
2313+
found = true;
2314+
return false;
2315+
}
2316+
return true;
2317+
});
2318+
return found;
2319+
}
2320+
2321+
Widget dragGestureApp(bool hasPlatformViews) {
2322+
return CupertinoApp(
2323+
home: CupertinoPageScaffold(
2324+
key: scaffoldKey,
2325+
child: Center(
2326+
child: Column(
2327+
children: <Widget>[
2328+
const Text('Page 1'),
2329+
CupertinoButton(
2330+
onPressed: () {
2331+
showCupertinoSheet<void>(
2332+
context: scaffoldKey.currentContext!,
2333+
hasPlatformViews: hasPlatformViews,
2334+
scrollableBuilder: (BuildContext context, ScrollController controller) {
2335+
return const CupertinoPageScaffold(child: Center(child: Text('Page 2')));
2336+
},
2337+
);
2338+
},
2339+
child: const Text('Push Page 2'),
2340+
),
2341+
],
2342+
),
2343+
),
2344+
),
2345+
);
2346+
}
2347+
2348+
Future<void> pumpSheet(WidgetTester tester, bool hasPlatformViews) async {
2349+
await tester.pumpWidget(dragGestureApp(hasPlatformViews));
2350+
await tester.tap(find.text('Push Page 2'));
2351+
await tester.pumpAndSettle();
2352+
expect(find.text('Page 2'), findsOneWidget);
2353+
}
2354+
2355+
testWidgets('hasPlatformViews=false should add ImageFilterLayer during transition', (
2356+
WidgetTester tester,
2357+
) async {
2358+
await pumpSheet(tester, false);
2359+
2360+
// ImageFilterLayer should not be found after the transition is completed
2361+
// because ScaleTransition will set the Transform.filterQuality to null.
2362+
expect(checkHasImageFilterLayer(), false);
2363+
2364+
final TestGesture gesture = await tester.startGesture(const Offset(100, 300));
2365+
await gesture.moveBy(const Offset(0, 100));
2366+
await tester.pump();
2367+
2368+
// ImageFilterLayer should be found when the sheet is dragged.
2369+
expect(checkHasImageFilterLayer(), true);
2370+
2371+
await gesture.up();
2372+
await tester.pumpAndSettle();
2373+
});
2374+
2375+
testWidgets('hasPlatformViews=true should not add ImageFilterLayer during transition', (
2376+
WidgetTester tester,
2377+
) async {
2378+
await pumpSheet(tester, true);
2379+
2380+
expect(checkHasImageFilterLayer(), false);
2381+
2382+
final TestGesture gesture = await tester.startGesture(const Offset(100, 300));
2383+
await gesture.moveBy(const Offset(0, 100));
2384+
await tester.pump();
2385+
2386+
// ImageFilterLayer should not be found even when the sheet is dragged.
2387+
expect(checkHasImageFilterLayer(), false);
2388+
2389+
await gesture.up();
2390+
await tester.pumpAndSettle();
2391+
});
2392+
});
2393+
23012394
testWidgets('didUpdateWidget in sheet transition does not try and use multiple tickers', (
23022395
WidgetTester tester,
23032396
) async {

0 commit comments

Comments
 (0)