fix(mobile): keep agent poster up until the iOS video draws (#9688)

On iOS, an animated agent avatar (a preset character with a transparent
video) blinks out for one to three frames right after navigation
settles. Opening a Solo chat blanks the header avatar; going back to
Messages blanks the bestie bubble.

The cause is the hand-off from the still poster to the video. While a
route animates, `PresetAgentAvatar` shows the poster; once it settles it
replaced the poster with a native `AVPlayerLayer`, which draws nothing
until its first video frame arrives. The avatar picker and the
create-agent page already covered this by passing a preloaded poster;
the DM and Solo header and the bestie bubble pass none, so they went
straight to the bare player.

Now every iOS caller gets that hand-off, using its own poster when it
passes none: the poster stays up, below the player, until playback
advances in that showing, then fades out over 160 ms. The poster sits
below the native layer rather than over it, because a Flutter overlay
above a platform view also costs a blank frame when the view first
appears. Each showing starts covered again and waits for playback to
advance from where the player actually is, so coming back to a chat from
a page above it doesn't blink either. The poster is keyed by its URL, so
when the avatar changes to another character (for example a synced
bestie change) it never shows the previous character's poster. Android
is unchanged; it already waits for a presented frame.

### Validation

Measured on an iPhone 17 Pro simulator, recording at 60 fps and checking
the avatar region frame by frame, opening a Solo chat with an animated
agent and going back:

| | Header blank on open | Bubble blank on back |
| --- | --- | --- |
| main, 3 runs | 1 frame every run | 2 to 3 frames every run |
| this PR, 3 runs | none | none |

Before (left) and after (right), slowed 4x: the Solo header on open,
then the bestie bubble on back.

<video
src="https://github.com/user-attachments/assets/4ddf16c0-bb8d-4d77-b53e-84f83ff45e98"
width="480"></video>

Added test: [iOS keeps the poster over a new player until playback
advances](2b172d1b68/mobile/test/shared/widgets/preset_agent_avatar_test.dart),
covering a first showing, a return to the route, and a change of
character while the video is showing.

### Regressions

- No extra work: the poster image was already loaded for the transition,
and the video plays exactly as before.
- During the 160 ms fade, poster and video overlap. They show the same
character, so the fade reads as a single image, as it already did in the
avatar picker.
- If the video never starts playing, the poster just stays up, which is
what callers showed before the video was ready anyway.





buzz-review-completed

---------

Signed-off-by: Tom Brow <tomb@block.xyz>
Co-authored-by: studio <dc2fc481343672df31a689840c09299972e704dac592e0da6f18486646a7ee3e@buzz.block.builderlab.xyz>
This commit is contained in:
Tom Brow
2026-11-18 19:57:44 -08:00
committed by GitHub
parent 008d72a133
commit 39ffc3dcc5
2 changed files with 176 additions and 47 deletions

View File

@@ -75,24 +75,44 @@ class PresetAgentAvatar extends HookWidget {
[videoUrl, isAndroid],
);
final ready = useState<VideoPlayerController?>(null);
final presented = useState<VideoPlayerController?>(null);
useListenable(
isIos && posterPlaceholder != null && presented.value != controller
? controller
: null,
);
final videoShown =
showNativeVideo && controller != null && ready.value == controller;
// A fresh AVPlayerLayer draws nothing until its first frame arrives, so
// keep the poster under it until playback advances in this showing. The
// baseline is read from the player, and the controller's last polled
// position is kept beside it: while paused that value lags the player,
// so it differs from the baseline until a poll in this showing.
final shownFrom = useState<(Duration, Duration)?>(null);
useEffect(() {
if (isIos &&
posterPlaceholder != null &&
controller != null &&
ready.value == controller &&
controller.value.position > Duration.zero) {
presented.value = controller;
shownFrom.value = null;
if (!videoShown) return null;
var active = true;
unawaited(
controller.position
.then((position) {
if (active) {
shownFrom.value = (
position ?? Duration.zero,
controller.value.position,
);
}
})
.catchError((Object _) {}),
);
return () => active = false;
}, [videoShown, controller]);
final presented = useState(false);
useListenable(isIos && videoShown && !presented.value ? controller : null);
useEffect(() {
if (!videoShown) {
presented.value = false;
} else if (shownFrom.value case (final fresh, final polled)
when controller.value.position != fresh &&
controller.value.position != polled) {
presented.value = true;
}
return null;
}, [controller, controller?.value.position]);
final hasPlaybackFrame =
controller != null && presented.value == controller;
}, [videoShown, controller, shownFrom.value, controller?.value.position]);
final lifecycle = useAppLifecycleState();
final playing =
showNativeVideo &&
@@ -135,6 +155,18 @@ class PresetAgentAvatar extends HookWidget {
return null;
}, [controller, ready.value, playing]);
// Keyed by URL so a new character never shows the previous one's poster
// while its own loads.
final posterImage = KeyedSubtree(
key: ValueKey(poster),
child:
posterPlaceholder ??
AvatarImageContent(
imageUrl: poster,
preferThumbnail: true,
fallback: fallback,
),
);
return SizedBox.square(
dimension: size,
child: AvatarClip(
@@ -146,41 +178,39 @@ class PresetAgentAvatar extends HookWidget {
key: ValueKey(videoUrl),
url: videoUrl,
playing: playing,
fallback:
posterPlaceholder ??
AvatarImageContent(
imageUrl: poster,
preferThumbnail: true,
fallback: fallback,
),
fallback: posterImage,
)
: showNativeVideo &&
controller != null &&
ready.value == controller
? posterPlaceholder == null || !isIos
: !isIos
? videoShown
? VideoPlayer(controller)
: Stack(
fit: StackFit.expand,
children: [
Opacity(
opacity: hasPlaybackFrame ? 1 : 0,
child: VideoPlayer(controller),
),
IgnorePointer(
child: AnimatedOpacity(
opacity: hasPlaybackFrame ? 0 : 1,
duration: const Duration(milliseconds: 160),
child: posterPlaceholder!,
),
),
],
)
: posterPlaceholder ??
AvatarImageContent(
imageUrl: poster,
preferThumbnail: true,
fallback: fallback,
: posterImage
: Stack(
fit: StackFit.expand,
children: [
// The poster stays below the native layer, in Flutter's
// base layer: an overlay above it costs a blank frame.
// It stays mounted under the video so the next showing
// covers at once instead of reloading it.
IgnorePointer(
key: const ValueKey('preset-poster'),
child: AnimatedOpacity(
opacity: videoShown && presented.value ? 0 : 1,
// Cover a hidden player at once; fade only onto
// video.
duration: videoShown && presented.value
? const Duration(milliseconds: 160)
: Duration.zero,
child: posterImage,
),
),
if (videoShown)
Opacity(
key: const ValueKey('preset-video'),
opacity: presented.value ? 1 : 0,
child: VideoPlayer(controller),
),
],
),
),
),
);

View File

@@ -191,6 +191,105 @@ void main() {
debugDefaultTargetPlatformOverride = null;
},
);
testWidgets('iOS keeps the poster over a new player until playback advances', (
tester,
) async {
debugDefaultTargetPlatformOverride = TargetPlatform.iOS;
addTearDown(() => debugDefaultTargetPlatformOverride = null);
final original = VideoPlayerPlatform.instance;
final player = _Player();
VideoPlayerPlatform.instance = player;
addTearDown(() => VideoPlayerPlatform.instance = original);
double posterOpacity() =>
tester.widget<AnimatedOpacity>(find.byType(AnimatedOpacity)).opacity;
double videoOpacity() => tester
.widget<Opacity>(
find.ancestor(
of: find.byKey(const Key('video')),
matching: find.byType(Opacity),
),
)
.opacity;
await tester.pumpWidget(
const ProviderScope(
child: MaterialApp(
home: PresetAgentAvatar(
imageUrl:
'https://dwwgwmfqqjotj.cloudfront.net/avatars/20260925T154625166Z/poster/gloopies/gloopies-13.png',
size: 64,
fallback: Text('A'),
),
),
),
);
await tester.pumpAndSettle();
expect(find.byKey(const Key('video')), findsOneWidget);
expect(videoOpacity(), 0);
expect(posterOpacity(), 1);
player.position = const Duration(milliseconds: 100);
await tester.pump(const Duration(milliseconds: 600));
await tester.pumpAndSettle();
expect(videoOpacity(), 1);
expect(posterOpacity(), 0);
// Returning to the route mounts a new player, which starts covered.
final navigator = tester.state<NavigatorState>(find.byType(Navigator));
navigator.push<void>(
PageRouteBuilder<void>(
opaque: false,
pageBuilder: (_, _, _) => const SizedBox.shrink(),
),
);
await tester.pump();
expect(find.byKey(const Key('video')), findsNothing);
expect(posterOpacity(), 1);
// The player is past the last polled position when it comes back; the
// poster waits for playback to advance from where it actually is.
player.position = const Duration(milliseconds: 700);
await tester.pumpAndSettle();
navigator.pop();
// Step real frames: the baseline lands before the first poll does.
for (var frame = 0; frame < 60; frame++) {
await tester.pump(const Duration(milliseconds: 16));
if (find.byKey(const Key('video')).evaluate().isNotEmpty) {
expect(videoOpacity(), 0);
expect(posterOpacity(), 1);
}
}
expect(find.byKey(const Key('video')), findsOneWidget);
player.position = const Duration(milliseconds: 800);
await tester.pump(const Duration(milliseconds: 600));
await tester.pumpAndSettle();
expect(videoOpacity(), 1);
expect(posterOpacity(), 0);
// A new character covers with its own poster, never the previous one
// kept by gapless playback.
final previous = tester.state(find.byType(AvatarImageContent));
await tester.pumpWidget(
const ProviderScope(
child: MaterialApp(
home: PresetAgentAvatar(
imageUrl:
'https://dwwgwmfqqjotj.cloudfront.net/avatars/20260925T154625166Z/poster/gloopies/gloopies-14.png',
size: 64,
fallback: Text('A'),
),
),
),
);
await tester.pump();
final posters = tester
.widgetList<AvatarImageContent>(find.byType(AvatarImageContent))
.map((poster) => poster.imageUrl);
expect(posters, [endsWith('gloopies-14.png')]);
expect(
tester.state(find.byType(AvatarImageContent)),
isNot(same(previous)),
);
await tester.pumpWidget(const SizedBox.shrink());
debugDefaultTargetPlatformOverride = null;
});
testWidgets('Android uses transparent renderer and pauses offscreen', (
tester,
) async {