Skip to content

Commit b6db61f

Browse files
browsquare-tombstudio
authored
perf(mobile): stop rebuilding the native nav bar on every scrolled frame (#9458)
On iOS, `FrostedAppBar` built its native `IosNavigationBar` inside the `ValueListenableBuilder` for the page's scroll offset, so every scrolled frame rebuilt the bar. Each rebuild `jsonEncode`s the whole native payload to see whether it changed, and that payload carries the header's avatars: base64 image data, and the data URLs of people whose profile picture is one (100 KB or more each). The bar already follows the offset itself, through its own listener that sends `scroll` to UIKit, so none of that work was needed. The bar is now built once per `FrostedAppBar` build, outside the offset builder. Scrolling still rebuilds the positioned frame around it (its height follows the large title), but passes the same bar widget, so Flutter skips it. ### Validation New test: scrolling a `FrostedScaffold` page leaves the same `IosNavigationBar` instance in the tree. It fails without this change. Full mobile test suite, `flutter analyze` and the file-size check pass. On the iPhone 12 mini (profile build, Builderlab), opening a busy channel whose header shows member avatars, and scrolling history with 10 swipes (about 30 seconds): | | `IosNavigationBar.build` (CPU samples) | BUILD total | LAYOUT total | UI frame time total | | --- | --- | --- | --- | --- | | Before | 1,581 ms | 3,304 ms | 3,514 ms | 7,013 ms | | After | 0 ms (no samples) | 591 ms | 748 ms | 4,718 ms | The remaining long frames (up to 130 ms) are older history loading in, and are the same before and after. The automated run covered a cold start, search, opening the channel from a search result, and scrolling its history. ### Regressions None expected. The bar's inputs don't depend on the offset; only the frame's height and animation duration do, and those still update per frame. Signed-off-by: Tom Brow <[email protected]> Co-authored-by: Tom Brow <[email protected]> Co-authored-by: studio <dc2fc481343672df31a689840c09299972e704dac592e0da6f18486646a7ee3e@buzz.block.builderlab.xyz>
1 parent 93bbd45 commit b6db61f

2 files changed

Lines changed: 73 additions & 40 deletions

File tree

‎mobile/lib/shared/widgets/frosted_app_bar.dart‎

Lines changed: 38 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import 'package:duo_kit/duo_kit.dart';
2+
23
import 'duo_toolbar.dart';
4+
35
import 'dart:math' show max;
46

57
import 'package:flutter/material.dart';
@@ -312,6 +314,41 @@ class FrostedAppBar extends StatelessWidget {
312314
);
313315
}
314316
final offset = IosNavigationScrollScope.maybeOf(context);
317+
// The bar reads the scroll offset itself, so scrolling rebuilds only
318+
// the frame around it, not its native payload.
319+
final nativeBar = IosNavigationBar(
320+
centerTitleInPane: strip != null && !nativeLargeTitle,
321+
titleInsets: nativeTitleInsets == EdgeInsets.zero
322+
? railInsets
323+
: nativeTitleInsets,
324+
alignCollapsedTitleToLeading: strip != null && nativeLargeTitle,
325+
horizontalControls:
326+
strip != null &&
327+
((leadingAction?.keepInHeaderOnDuo ?? false) ||
328+
trailingActions.any((action) => action.keepInHeaderOnDuo)),
329+
onReadyChanged: onNativeReadyChanged,
330+
title: nativeTitle ?? (title is Text ? (title as Text).data ?? '' : ''),
331+
subtitle: nativeSubtitle,
332+
ephemeralLabel: nativeEphemeralLabel,
333+
titleAvatar: nativeTitleAvatar,
334+
titleAvatars: nativeTitleAvatars,
335+
titleMaxWidth: nativeTitleMaxWidth,
336+
titlePresenceColor: nativeTitlePresenceColor,
337+
titleRefreshing: nativeTitleRefreshing,
338+
onTitlePressed: onNativeTitlePressed,
339+
largeTitle: nativeLargeTitle,
340+
alwaysFrosted: alwaysFrosted || strip != null,
341+
foregroundColor: iconColor,
342+
leading: strip == null || (leadingAction?.keepInHeaderOnDuo ?? false)
343+
? leadingAction
344+
: null,
345+
onBack: strip == null ? onBack : null,
346+
actions: strip == null
347+
? trailingActions
348+
: trailingActions
349+
.where((action) => action.keepInHeaderOnDuo)
350+
.toList(),
351+
);
315352
Widget buildNative(double scrollOffset) {
316353
// Scrolling tracks the finger directly; only the search focus change
317354
// uses the coordinated layout transition.
@@ -345,46 +382,7 @@ class FrostedAppBar extends StatelessWidget {
345382
left: 0,
346383
right: 0,
347384
height: barHeight,
348-
child: IosNavigationBar(
349-
centerTitleInPane: strip != null && !nativeLargeTitle,
350-
titleInsets: nativeTitleInsets == EdgeInsets.zero
351-
? railInsets
352-
: nativeTitleInsets,
353-
alignCollapsedTitleToLeading:
354-
strip != null && nativeLargeTitle,
355-
horizontalControls:
356-
strip != null &&
357-
((leadingAction?.keepInHeaderOnDuo ?? false) ||
358-
trailingActions.any(
359-
(action) => action.keepInHeaderOnDuo,
360-
)),
361-
onReadyChanged: onNativeReadyChanged,
362-
title:
363-
nativeTitle ??
364-
(title is Text ? (title as Text).data ?? '' : ''),
365-
subtitle: nativeSubtitle,
366-
ephemeralLabel: nativeEphemeralLabel,
367-
titleAvatar: nativeTitleAvatar,
368-
titleAvatars: nativeTitleAvatars,
369-
titleMaxWidth: nativeTitleMaxWidth,
370-
titlePresenceColor: nativeTitlePresenceColor,
371-
titleRefreshing: nativeTitleRefreshing,
372-
onTitlePressed: onNativeTitlePressed,
373-
largeTitle: nativeLargeTitle,
374-
alwaysFrosted: alwaysFrosted || strip != null,
375-
foregroundColor: iconColor,
376-
leading:
377-
strip == null ||
378-
(leadingAction?.keepInHeaderOnDuo ?? false)
379-
? leadingAction
380-
: null,
381-
onBack: strip == null ? onBack : null,
382-
actions: strip == null
383-
? trailingActions
384-
: trailingActions
385-
.where((action) => action.keepInHeaderOnDuo)
386-
.toList(),
387-
),
385+
child: nativeBar,
388386
),
389387
if (bottom != null)
390388
AnimatedPositioned(

‎mobile/test/shared/widgets/ios_navigation_bar_test.dart‎

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -379,6 +379,41 @@ void main() {
379379
});
380380
}
381381

382+
testWidgets('scrolling does not rebuild the native bar', (tester) async {
383+
debugDefaultTargetPlatformOverride = TargetPlatform.iOS;
384+
addTearDown(() => debugDefaultTargetPlatformOverride = null);
385+
final controller = ScrollController();
386+
addTearDown(controller.dispose);
387+
await tester.pumpWidget(
388+
_testApp(
389+
home: FrostedScaffold(
390+
appBar: const FrostedAppBar(title: Text('Conversation')),
391+
body: ListView(
392+
controller: controller,
393+
children: [
394+
for (var i = 0; i < 40; i++)
395+
SizedBox(height: 60, child: Text('Message $i')),
396+
],
397+
),
398+
),
399+
),
400+
);
401+
await tester.pumpAndSettle();
402+
final before = tester.widget<IosNavigationBar>(
403+
find.byType(IosNavigationBar),
404+
);
405+
controller.jumpTo(300);
406+
await tester.pumpAndSettle();
407+
// The same widget instance is not rebuilt, so its native payload is not
408+
// encoded again for every scrolled frame.
409+
expect(
410+
tester.widget<IosNavigationBar>(find.byType(IosNavigationBar)),
411+
same(before),
412+
);
413+
await tester.pumpWidget(const SizedBox());
414+
debugDefaultTargetPlatformOverride = null;
415+
});
416+
382417
testWidgets('native material follows live theme surface changes', (
383418
tester,
384419
) async {

0 commit comments

Comments
 (0)