Skip to content

Commit 69666c7

Browse files
committed
Add FadeInImageTransition to control the placeholder-to-image animation
Fixes flutter#97068 Adds a FadeInImageTransition enum and a transition parameter to FadeInImage: - 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 fully opaque until the image has loaded, so there is no blank gap between the two images. fadeOutDuration and fadeOutCurve are nullable and have no effect in fadeInOver mode; each constructor asserts they are null there. Also fixes a pre-existing crash where a zero fadeOutDuration built a zero-weight TweenSequence item, and a pre-existing status-listener leak in the fade animation. Adds a runnable dartpad example comparing both modes and tests covering the new behavior.
1 parent 7099814 commit 69666c7

4 files changed

Lines changed: 625 additions & 49 deletions

File tree

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
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/material.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 const MaterialApp(home: FadeInImageTransitionExample());
20+
}
21+
}
22+
23+
class FadeInImageTransitionExample extends StatefulWidget {
24+
const FadeInImageTransitionExample({super.key});
25+
26+
@override
27+
State<FadeInImageTransitionExample> createState() =>
28+
_FadeInImageTransitionExampleState();
29+
}
30+
31+
class _FadeInImageTransitionExampleState
32+
extends State<FadeInImageTransitionExample> {
33+
// A solid, opaque placeholder so the difference between the two transitions
34+
// is visible: in sequential mode it fades out before the image appears, while
35+
// in fadeInOver mode it stays put until the image has faded in on top.
36+
static final Uint8List _placeholderBytes = base64Decode(
37+
'iVBORw0KGgoAAAANSUhEUgAAAAgAAAAICAYAAADED76LAAAAEklEQVR42mNIqO3+jw8zjAwFAA9JmcEHBMcCAAAAAElFTkSuQmCC',
38+
);
39+
40+
FadeInImageTransition _transition = FadeInImageTransition.sequential;
41+
int _reloadCount = 0;
42+
43+
void _selectTransition(FadeInImageTransition transition) {
44+
setState(() {
45+
_transition = transition;
46+
// Change the image's cache key so the fade-in animation plays again each
47+
// time a transition is selected.
48+
_reloadCount += 1;
49+
});
50+
}
51+
52+
@override
53+
Widget build(BuildContext context) {
54+
return Scaffold(
55+
appBar: AppBar(title: const Text('FadeInImage.transition')),
56+
body: Center(
57+
child: Column(
58+
mainAxisAlignment: MainAxisAlignment.center,
59+
children: <Widget>[
60+
SegmentedButton<FadeInImageTransition>(
61+
segments: const <ButtonSegment<FadeInImageTransition>>[
62+
ButtonSegment<FadeInImageTransition>(
63+
value: FadeInImageTransition.sequential,
64+
label: Text('sequential'),
65+
),
66+
ButtonSegment<FadeInImageTransition>(
67+
value: FadeInImageTransition.fadeInOver,
68+
label: Text('fadeInOver'),
69+
),
70+
],
71+
selected: <FadeInImageTransition>{_transition},
72+
onSelectionChanged: (Set<FadeInImageTransition> selection) {
73+
_selectTransition(selection.first);
74+
},
75+
),
76+
const SizedBox(height: 24),
77+
SizedBox(
78+
width: 250,
79+
height: 250,
80+
child: FadeInImage(
81+
// Rebuild the widget from scratch on each selection so the fade
82+
// animation replays and both transitions can be compared.
83+
key: ValueKey<int>(_reloadCount),
84+
placeholder: MemoryImage(_placeholderBytes),
85+
image: NetworkImage(
86+
'https://flutter.github.io/assets-for-api-docs/assets/widgets/puffin.jpg?reload=$_reloadCount',
87+
),
88+
fit: BoxFit.cover,
89+
transition: _transition,
90+
),
91+
),
92+
],
93+
),
94+
),
95+
);
96+
}
97+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
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/material.dart';
8+
import 'package:flutter_api_samples/widgets/fade_in_image/fade_in_image.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('selecting a transition updates the FadeInImage', (
20+
WidgetTester tester,
21+
) async {
22+
await tester.pumpWidget(const example.FadeInImageTransitionExampleApp());
23+
24+
FadeInImage fadeInImage() =>
25+
tester.widget<FadeInImage>(find.byType(FadeInImage));
26+
27+
// Defaults to the sequential transition.
28+
expect(fadeInImage().transition, FadeInImageTransition.sequential);
29+
30+
// Selecting fadeInOver updates the transition without throwing.
31+
await tester.tap(find.text('fadeInOver'));
32+
await tester.pumpAndSettle();
33+
expect(fadeInImage().transition, FadeInImageTransition.fadeInOver);
34+
35+
// Selecting sequential again switches back.
36+
await tester.tap(find.text('sequential'));
37+
await tester.pumpAndSettle();
38+
expect(fadeInImage().transition, FadeInImageTransition.sequential);
39+
40+
expect(tester.takeException(), isNull);
41+
});
42+
}

0 commit comments

Comments
 (0)