Perf round 4: lazy chat sessions list
Chat sections are slivers: a lazy, keyed SliverList under the same heading, with SliverAnimatedExtent reproducing the clipped AnimatedSize for open, close and arriving rows. The first Chat tab visit no longer builds every row (150 chats: worst build frame 116 ms to 19 ms).
This commit is contained in:
@@ -2,6 +2,7 @@ import 'package:flutter/material.dart';
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
import 'package:pipelines_app/core/api_gateway.dart';
|
||||
import 'package:pipelines_app/ui/adaptive/segmented_bar.dart';
|
||||
import 'package:pipelines_app/ui/layout/section_heading.dart';
|
||||
|
||||
import 'chat_test_harness.dart';
|
||||
import 'fake_chat_gateway.dart';
|
||||
@@ -30,12 +31,15 @@ void main() {
|
||||
}
|
||||
|
||||
final bar = tester.getRect(find.byType(SegmentedBar));
|
||||
final list = tester.getRect(find.byType(ListView).first);
|
||||
final list = tester.getRect(find.byType(CustomScrollView).first);
|
||||
// The list reaches under the bar ...
|
||||
expect(list.top, lessThan(bar.bottom));
|
||||
// ... yet at rest its first row is clear of it.
|
||||
final first = tester.getRect(
|
||||
find.byKey(const ValueKey('chat-section-Chats')),
|
||||
find.descendant(
|
||||
of: find.byKey(const ValueKey('chat-section-Chats')),
|
||||
matching: find.byType(SectionHeading),
|
||||
),
|
||||
);
|
||||
expect(first.top, greaterThanOrEqualTo(bar.bottom));
|
||||
expect(tester.takeException(), isNull);
|
||||
|
||||
@@ -111,8 +111,11 @@ void main() {
|
||||
'rowRebuilds=${rebuilds.of(ChatSessionRow)} '
|
||||
'totalRebuilds=${rebuilds.total}',
|
||||
);
|
||||
// One row per change, not all 200 (10,000 row rebuilds before).
|
||||
expect(rebuilds.of(ChatSessionRow), 50);
|
||||
// Only the rows on screen are built (the list is lazy) ...
|
||||
expect(built, lessThan(40));
|
||||
// ... and at most the changed row rebuilds per change, not all 200
|
||||
// (10,000 row rebuilds before); a change to an offscreen row builds none.
|
||||
expect(rebuilds.of(ChatSessionRow), inInclusiveRange(1, 50));
|
||||
});
|
||||
|
||||
testWidgets('gallery: open a visit with 500 artifacts', (tester) async {
|
||||
|
||||
@@ -3,6 +3,26 @@ import 'package:flutter/material.dart';
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
import 'package:pipelines_app/ui/layout/collapsible_list_section.dart';
|
||||
|
||||
Widget _app(bool cupertino, Widget body) => cupertino
|
||||
? CupertinoApp(home: CupertinoPageScaffold(child: body))
|
||||
: MaterialApp(home: Scaffold(body: body));
|
||||
|
||||
Widget _list(ValueNotifier<int> rows) => ValueListenableBuilder<int>(
|
||||
valueListenable: rows,
|
||||
builder: (context, count, _) => CustomScrollView(
|
||||
slivers: [
|
||||
SliverCollapsibleListSection(
|
||||
title: 'Group',
|
||||
count: count,
|
||||
itemCount: count,
|
||||
itemKey: (i) => ValueKey(i),
|
||||
itemBuilder: (_, i) => SizedBox(height: 40, child: Text('Row $i')),
|
||||
),
|
||||
const SliverToBoxAdapter(child: Text('After')),
|
||||
],
|
||||
),
|
||||
);
|
||||
|
||||
void main() {
|
||||
for (final cupertino in [false, true]) {
|
||||
testWidgets(
|
||||
@@ -15,32 +35,64 @@ void main() {
|
||||
);
|
||||
final rows = ValueNotifier<int>(1);
|
||||
addTearDown(rows.dispose);
|
||||
final body = ValueListenableBuilder<int>(
|
||||
valueListenable: rows,
|
||||
builder: (context, count, _) => SingleChildScrollView(
|
||||
child: CollapsibleListSection(
|
||||
title: 'Group',
|
||||
count: count,
|
||||
children: [for (var i = 0; i < count; i++) Text('Row $i')],
|
||||
),
|
||||
),
|
||||
);
|
||||
await tester.pumpWidget(
|
||||
cupertino
|
||||
? CupertinoApp(home: CupertinoPageScaffold(child: body))
|
||||
: MaterialApp(home: Scaffold(body: body)),
|
||||
);
|
||||
await tester.pumpWidget(_app(cupertino, _list(rows)));
|
||||
await tester.pumpAndSettle();
|
||||
rows.value = 3;
|
||||
await tester.pumpAndSettle();
|
||||
await tester.pump();
|
||||
expect(find.text('Row 2'), findsOneWidget);
|
||||
expect(find.byType(AnimatedSize), findsNothing);
|
||||
expect(tester.hasRunningAnimations, isFalse);
|
||||
expect(tester.takeException(), isNull);
|
||||
await tester.tap(find.text('Group'));
|
||||
await tester.pumpAndSettle();
|
||||
await tester.pump();
|
||||
expect(find.text('Row 0'), findsNothing);
|
||||
expect(tester.takeException(), isNull);
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
testWidgets('arriving rows, opening and closing animate the height', (
|
||||
tester,
|
||||
) async {
|
||||
final rows = ValueNotifier<int>(2);
|
||||
addTearDown(rows.dispose);
|
||||
await tester.pumpWidget(_app(false, _list(rows)));
|
||||
double after() => tester.getTopLeft(find.text('After')).dy;
|
||||
final top = after() - 80;
|
||||
|
||||
// Rows arriving: what follows moves down over the animation.
|
||||
rows.value = 4;
|
||||
await tester.pump();
|
||||
await tester.pump(const Duration(milliseconds: 60));
|
||||
expect(after(), inExclusiveRange(top + 80, top + 160));
|
||||
await tester.pumpAndSettle();
|
||||
expect(after(), top + 160);
|
||||
|
||||
// Closing: the rows go at once and the space shrinks away.
|
||||
await tester.tap(find.text('Group'));
|
||||
await tester.pump();
|
||||
await tester.pump(const Duration(milliseconds: 60));
|
||||
expect(find.text('Row 0'), findsNothing);
|
||||
expect(after(), inExclusiveRange(top, top + 160));
|
||||
await tester.pumpAndSettle();
|
||||
expect(after(), top);
|
||||
|
||||
// Opening: the rows are drawn at once and revealed from the top.
|
||||
await tester.tap(find.text('Group'));
|
||||
await tester.pump();
|
||||
await tester.pump(const Duration(milliseconds: 60));
|
||||
expect(find.text('Row 0'), findsOneWidget);
|
||||
expect(after(), inExclusiveRange(top, top + 160));
|
||||
await tester.pumpAndSettle();
|
||||
expect(after(), top + 160);
|
||||
});
|
||||
|
||||
testWidgets('only the visible rows of a long section are built', (
|
||||
tester,
|
||||
) async {
|
||||
final rows = ValueNotifier<int>(500);
|
||||
addTearDown(rows.dispose);
|
||||
await tester.pumpWidget(_app(false, _list(rows)));
|
||||
expect(find.text('Row 0'), findsOneWidget);
|
||||
expect(find.text('Row 100'), findsNothing);
|
||||
});
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user