Skip to content

Commit 644a7c8

Browse files
anazr9victorsanni
andauthored
Add FadeInImageTransition.fadeInOver to fade the image in over the placeholder (#186246)
Adds a `FadeInImageTransition` enum and a `transition` parameter to `FadeInImage`, giving control over how the widget animates from the placeholder to the image: - `FadeInImageTransition.sequential` (default, unchanged behavior): the placeholder fades out, then the image fades in. - `FadeInImageTransition.fadeInOver`: the image fades in **on top of** the placeholder, which stays at full opacity until the image has fully loaded, so there is no blank gap between the two images. - In `fadeInOver` mode the placeholder is never faded out, so `fadeOutDuration` and `fadeOutCurve` have no effect; this is documented on the enum value, the `transition` property, and both fields. - Fixed a pre-existing status listener leak in `_AnimatedFadeOutFadeInState`: `didUpdateTweens()` registered a new status listener on every call, which forwards to the persistent `AnimationController`, so listeners accumulated. https://github.com/user-attachments/assets/c54650e7-e081-4c63-8cfa-9a6c6273e097 Fixes #97068 ## Pre-launch Checklist - [x] I read the [Contributor Guide] and followed the process outlined there for submitting PRs. - [x] I read the [AI contribution guidelines] and understand my responsibilities, or I am not using AI tools. - [x] I read the [Tree Hygiene] wiki page, which explains my responsibilities. - [x] I read and followed the [Flutter Style Guide], including [Features we expect every widget to implement]. - [x] I signed the [CLA]. - [x] I listed at least one issue that this PR fixes in the description above. - [x] I updated/added relevant documentation (doc comments with `///`). - [x] I added new tests to check the change I am making, or this PR is [test-exempt]. - [x] I followed the [breaking change policy] and added [Data Driven Fixes] where supported. - [x] All existing and new tests are passing. --------- Co-authored-by: Victor Sanni <[email protected]>
1 parent 60f4006 commit 644a7c8

4 files changed

Lines changed: 788 additions & 44 deletions

File tree

Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
// Copyright 2014 The Flutter Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style license that can be
3+
// found in the LICENSE file.
4+
5+
import 'dart:convert';
6+
import 'dart:typed_data';
7+
8+
import 'package:flutter/widgets.dart';
9+
10+
/// Flutter code sample for [FadeInImage.transition].
11+
12+
void main() => runApp(const FadeInImageTransitionExampleApp());
13+
14+
class FadeInImageTransitionExampleApp extends StatelessWidget {
15+
const FadeInImageTransitionExampleApp({super.key});
16+
17+
@override
18+
Widget build(BuildContext context) {
19+
return WidgetsApp(
20+
color: const Color(0xFFFFFFFF),
21+
builder: (BuildContext context, Widget? child) {
22+
return const FadeInImageTransitionExample();
23+
},
24+
);
25+
}
26+
}
27+
28+
class FadeInImageTransitionExample extends StatefulWidget {
29+
const FadeInImageTransitionExample({super.key});
30+
31+
@override
32+
State<FadeInImageTransitionExample> createState() =>
33+
_FadeInImageTransitionExampleState();
34+
}
35+
36+
class _FadeInImageTransitionExampleState
37+
extends State<FadeInImageTransitionExample> {
38+
// A solid, opaque placeholder makes the difference between the two
39+
// transitions visible: with FadeInImageTransition.sequential it fades out
40+
// before the image appears, while with FadeInImageTransition.fadeInOver it
41+
// stays put until the image has faded in on top of it.
42+
static final Uint8List _placeholderBytes = base64Decode(
43+
'iVBORw0KGgoAAAANSUhEUgAAAAgAAAAICAYAAADED76LAAAAEklEQVR42mNIqO3+jw8zjAwFAA9JmcEHBMcCAAAAAElFTkSuQmCC',
44+
);
45+
46+
FadeInImageTransition _transition = FadeInImageTransition.sequential;
47+
48+
@override
49+
Widget build(BuildContext context) {
50+
return ColoredBox(
51+
color: const Color(0xFFFFFFFF),
52+
child: Center(
53+
child: Column(
54+
mainAxisAlignment: MainAxisAlignment.center,
55+
children: <Widget>[
56+
Row(
57+
mainAxisAlignment: MainAxisAlignment.center,
58+
children: <Widget>[
59+
for (final FadeInImageTransition transition
60+
in FadeInImageTransition.values)
61+
_TransitionButton(
62+
transition: transition,
63+
selected: transition == _transition,
64+
onPressed: () {
65+
setState(() {
66+
_transition = transition;
67+
});
68+
},
69+
),
70+
],
71+
),
72+
const SizedBox(height: 24),
73+
SizedBox(
74+
width: 250,
75+
height: 250,
76+
child: FadeInImage(
77+
placeholder: MemoryImage(_placeholderBytes),
78+
image: const NetworkImage(
79+
'https://flutter.github.io/assets-for-api-docs/assets/widgets/puffin.jpg',
80+
),
81+
fit: BoxFit.cover,
82+
transition: _transition,
83+
),
84+
),
85+
],
86+
),
87+
),
88+
);
89+
}
90+
}
91+
92+
class _TransitionButton extends StatelessWidget {
93+
const _TransitionButton({
94+
required this.transition,
95+
required this.selected,
96+
required this.onPressed,
97+
});
98+
99+
final FadeInImageTransition transition;
100+
final bool selected;
101+
final VoidCallback onPressed;
102+
103+
@override
104+
Widget build(BuildContext context) {
105+
return GestureDetector(
106+
onTap: onPressed,
107+
child: Container(
108+
margin: const EdgeInsets.symmetric(horizontal: 4),
109+
padding: const EdgeInsets.symmetric(horizontal: 12, vertical: 8),
110+
decoration: BoxDecoration(
111+
border: Border.all(color: const Color(0xFF0175C2)),
112+
color: selected ? const Color(0xFF0175C2) : const Color(0xFFFFFFFF),
113+
borderRadius: BorderRadius.circular(4),
114+
),
115+
child: Text(
116+
transition.name,
117+
style: TextStyle(
118+
color: selected ? const Color(0xFFFFFFFF) : const Color(0xFF0175C2),
119+
fontSize: 14,
120+
),
121+
),
122+
),
123+
);
124+
}
125+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
// Copyright 2014 The Flutter Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style license that can be
3+
// found in the LICENSE file.
4+
5+
import 'dart:io';
6+
7+
import 'package:flutter/widgets.dart';
8+
import 'package:flutter_api_samples/widgets/fade_in_image/fade_in_image.transition.0.dart'
9+
as example;
10+
import 'package:flutter_test/flutter_test.dart';
11+
12+
void main() {
13+
// The app being tested loads images via HTTP which the test
14+
// framework defeats by default.
15+
setUpAll(() {
16+
HttpOverrides.global = null;
17+
});
18+
19+
testWidgets('switches the transition in place', (WidgetTester tester) async {
20+
await tester.pumpWidget(const example.FadeInImageTransitionExampleApp());
21+
await tester.pumpAndSettle();
22+
23+
FadeInImage fadeInImage() =>
24+
tester.widget<FadeInImage>(find.byType(FadeInImage));
25+
final State state = tester.state(find.byType(FadeInImage));
26+
27+
expect(fadeInImage().transition, FadeInImageTransition.sequential);
28+
29+
await tester.tap(find.text('fadeInOver'));
30+
await tester.pumpAndSettle();
31+
expect(fadeInImage().transition, FadeInImageTransition.fadeInOver);
32+
// The transition is swapped in place rather than the widget being rebuilt
33+
// from scratch, so the same state is kept throughout.
34+
expect(tester.state(find.byType(FadeInImage)), same(state));
35+
36+
await tester.tap(find.text('sequential'));
37+
await tester.pumpAndSettle();
38+
expect(fadeInImage().transition, FadeInImageTransition.sequential);
39+
expect(tester.state(find.byType(FadeInImage)), same(state));
40+
41+
expect(tester.takeException(), isNull);
42+
});
43+
}

0 commit comments

Comments
 (0)