opacity_repaint_test.dart 2.21 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12
// 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';

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

    expect(RenderTestObject.paintCount, 0);

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

    expect(RenderTestObject.paintCount, 1);

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

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

  testWidgets('RenderOpacity allows opacity layer to be dropped at 0 opacity', (WidgetTester tester) async {
    RenderTestObject.paintCount = 0;

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

    expect(RenderTestObject.paintCount, 1);

    await tester.pumpWidget(
65
      const ColoredBox(
66
        color: Colors.red,
67
        child: Opacity(
68 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
          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);
  }
}