opacity_repaint_test.dart 2.32 KB
Newer Older
1 2 3 4 5 6 7
// Copyright 2014 The Flutter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

import 'package:flutter/material.dart';
import 'package:flutter/rendering.dart';
import 'package:flutter_test/flutter_test.dart';
8
import 'package:leak_tracker_flutter_testing/leak_tracker_flutter_testing.dart';
9 10

void main() {
11
  testWidgetsWithLeakTracking('RenderOpacity avoids repainting and does not drop layer at fully opaque', (WidgetTester tester) async {
12 13
    RenderTestObject.paintCount = 0;
    await tester.pumpWidget(
14
      const ColoredBox(
15
        color: Colors.red,
16
        child: Opacity(
17 18 19 20 21 22 23 24 25
          opacity: 0.0,
          child: TestWidget(),
        ),
      )
    );

    expect(RenderTestObject.paintCount, 0);

    await tester.pumpWidget(
26
      const ColoredBox(
27
        color: Colors.red,
28
        child: Opacity(
29 30 31 32 33 34 35 36 37
          opacity: 0.1,
          child: TestWidget(),
        ),
      )
    );

    expect(RenderTestObject.paintCount, 1);

    await tester.pumpWidget(
38
      const ColoredBox(
39
        color: Colors.red,
40
        child: Opacity(
41 42 43 44 45 46 47 48 49
          opacity: 1,
          child: TestWidget(),
        ),
      )
    );

    expect(RenderTestObject.paintCount, 1);
  });

50
  testWidgetsWithLeakTracking('RenderOpacity allows opacity layer to be dropped at 0 opacity', (WidgetTester tester) async {
51 52 53
    RenderTestObject.paintCount = 0;

    await tester.pumpWidget(
54
      const ColoredBox(
55
        color: Colors.red,
56
        child: Opacity(
57 58 59 60 61 62 63 64 65
          opacity: 0.5,
          child: TestWidget(),
        ),
      )
    );

    expect(RenderTestObject.paintCount, 1);

    await tester.pumpWidget(
66
      const ColoredBox(
67
        color: Colors.red,
68
        child: Opacity(
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97
          opacity: 0.0,
          child: TestWidget(),
        ),
      )
    );

    expect(RenderTestObject.paintCount, 1);
    expect(tester.layers, isNot(contains(isA<OpacityLayer>())));
  });
}

class TestWidget extends SingleChildRenderObjectWidget {
  const TestWidget({super.key, super.child});

  @override
  RenderObject createRenderObject(BuildContext context) {
    return RenderTestObject();
  }
}

class RenderTestObject extends RenderProxyBox {
  static int paintCount = 0;

  @override
  void paint(PaintingContext context, Offset offset) {
    paintCount += 1;
    super.paint(context, offset);
  }
}