1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
// 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';
import '../rendering/mock_canvas.dart';
import 'semantics_tester.dart';
void main() {
testWidgets('Opacity', (WidgetTester tester) async {
final SemanticsTester semantics = SemanticsTester(tester);
// Opacity 1.0: Semantics and painting
await tester.pumpWidget(
const Opacity(
opacity: 1.0,
child: Text('a', textDirection: TextDirection.rtl),
),
);
expect(semantics, hasSemantics(
TestSemantics.root(
children: <TestSemantics>[
TestSemantics.rootChild(
id: 1,
rect: const Rect.fromLTRB(0.0, 0.0, 800.0, 600.0),
label: 'a',
textDirection: TextDirection.rtl,
),
],
),
));
expect(find.byType(Opacity), paints..paragraph());
// Opacity 0.0: Nothing
await tester.pumpWidget(
const Opacity(
opacity: 0.0,
child: Text('a', textDirection: TextDirection.rtl),
),
);
expect(semantics, hasSemantics(
TestSemantics.root(),
));
expect(find.byType(Opacity), paintsNothing);
// Opacity 0.0 with semantics: Just semantics
await tester.pumpWidget(
const Opacity(
opacity: 0.0,
alwaysIncludeSemantics: true,
child: Text('a', textDirection: TextDirection.rtl),
),
);
expect(semantics, hasSemantics(
TestSemantics.root(
children: <TestSemantics>[
TestSemantics.rootChild(
id: 1,
rect: const Rect.fromLTRB(0.0, 0.0, 800.0, 600.0),
label: 'a',
textDirection: TextDirection.rtl,
),
],
),
));
expect(find.byType(Opacity), paintsNothing);
// Opacity 0.0 without semantics: Nothing
await tester.pumpWidget(
const Opacity(
opacity: 0.0,
alwaysIncludeSemantics: false,
child: Text('a', textDirection: TextDirection.rtl),
),
);
expect(semantics, hasSemantics(
TestSemantics.root(),
));
expect(find.byType(Opacity), paintsNothing);
// Opacity 0.1: Semantics and painting
await tester.pumpWidget(
const Opacity(
opacity: 0.1,
child: Text('a', textDirection: TextDirection.rtl),
),
);
expect(semantics, hasSemantics(
TestSemantics.root(
children: <TestSemantics>[
TestSemantics.rootChild(
id: 1,
rect: const Rect.fromLTRB(0.0, 0.0, 800.0, 600.0),
label: 'a',
textDirection: TextDirection.rtl,
),
],
),
));
expect(find.byType(Opacity), paints..paragraph());
// Opacity 0.1 without semantics: Still has semantics and painting
await tester.pumpWidget(
const Opacity(
opacity: 0.1,
alwaysIncludeSemantics: false,
child: Text('a', textDirection: TextDirection.rtl),
),
);
expect(semantics, hasSemantics(
TestSemantics.root(
children: <TestSemantics>[
TestSemantics.rootChild(
id: 1,
rect: const Rect.fromLTRB(0.0, 0.0, 800.0, 600.0),
label: 'a',
textDirection: TextDirection.rtl,
),
],
),
));
expect(find.byType(Opacity), paints..paragraph());
// Opacity 0.1 with semantics: Semantics and painting
await tester.pumpWidget(
const Opacity(
opacity: 0.1,
alwaysIncludeSemantics: true,
child: Text('a', textDirection: TextDirection.rtl),
),
);
expect(semantics, hasSemantics(
TestSemantics.root(
children: <TestSemantics>[
TestSemantics.rootChild(
id: 1,
rect: const Rect.fromLTRB(0.0, 0.0, 800.0, 600.0),
label: 'a',
textDirection: TextDirection.rtl,
),
],
),
));
expect(find.byType(Opacity), paints..paragraph());
semantics.dispose();
});
testWidgets('offset is correctly handled in Opacity', (WidgetTester tester) async {
await tester.pumpWidget(
MaterialApp(
home: Scaffold(
body: SingleChildScrollView(
child: RepaintBoundary(
child: Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: List<Widget>.generate(10, (int index) {
return Opacity(
opacity: 0.5,
child: Padding(
padding: const EdgeInsets.all(5.0),
child: Container(
color: Colors.blue,
height: 50,
),
),
);
}),
),
),
),
),
),
);
await expectLater(
find.byType(RepaintBoundary).first,
matchesGoldenFile('opacity_test.offset.png'),
);
});
testWidgets('empty opacity does not crash', (WidgetTester tester) async {
await tester.pumpWidget(
RepaintBoundary(child: Opacity(opacity: 0.5, child: Container())),
);
final Element element = find.byType(RepaintBoundary).first.evaluate().single;
// The following line will send the layer to engine and cause crash if an
// empty opacity layer is sent.
final OffsetLayer offsetLayer = element.renderObject!.debugLayer! as OffsetLayer;
await offsetLayer.toImage(const Rect.fromLTRB(0.0, 0.0, 1.0, 1.0));
}, skip: isBrowser); // https://github.com/flutter/flutter/issues/42767
}