Skip to content

SnapshotWidget renders incorrectly when enabled #114398

@fzyzcjy

Description

@fzyzcjy

Please see linked PR for reproduction

copied here:

In short, the SnapshotWidget is rendered differently when enabled vs disabled.

Reproduction:

import 'dart:ui' as ui;

import 'package:flutter/material.dart';
import 'package:flutter/rendering.dart';
import 'package:flutter_test/flutter_test.dart';

void main() {
  final binding = TestWidgetsFlutterBinding.ensureInitialized();
  testWidgets('SnapshotWidget should have same result when enabled', (tester) async {
    binding.window
      ..physicalSizeTestValue = const Size(10, 10)
      ..devicePixelRatioTestValue = 1;
    addTearDown(() => binding.window
      ..clearPhysicalSizeTestValue()
      ..clearDevicePixelRatioTestValue());

    final controller = SnapshotController(allowSnapshotting: false);
    await tester.pumpWidget(MaterialApp(
      debugShowCheckedModeBanner: false,
      home: Container(
        color: Colors.black,
        padding: const EdgeInsets.only(right: 0.6, bottom: 0.6),
        child: SnapshotWidget(
          controller: controller,
          child: Container(
            margin: const EdgeInsets.only(right: 0.4, bottom: 0.4),
            color: Colors.blue,
          ),
        ),
      ),
    ));

    final imageWhenDisabled = await _captureImage(tester.element(find.byType(MaterialApp)));

    controller.allowSnapshotting = true;
    await tester.pump();

    final imageWhenEnabled = await _captureImage(tester.element(find.byType(MaterialApp)));
    await expectLater(imageWhenEnabled, matchesReferenceImage(imageWhenDisabled));
  });
}

Future<ui.Image> _captureImage(Element element) {
  assert(element.renderObject != null);
  RenderObject renderObject = element.renderObject!;
  while (!renderObject.isRepaintBoundary) {
    renderObject = renderObject.parent! as RenderObject;
  }
  assert(!renderObject.debugNeedsPaint);
  final OffsetLayer layer = renderObject.debugLayer! as OffsetLayer;
  return layer.toImage(renderObject.paintBounds);
}

If you dump the images, will see:

imageWhenDisabled
image

imageWhenEnabled
image

Metadata

Metadata

Assignees

No one assigned

    Labels

    found in release: 3.5Found to occur in 3.5frameworkflutter/packages/flutter repository. See also f: labels.has reproducible stepsThe issue has been confirmed reproducible and is ready to work onr: fixedIssue is closed as already fixed in a newer version

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions