improved performance and battery usage on older devices
This commit is contained in:
@@ -75,6 +75,17 @@ class _ChatListViewState extends State<_ChatListView> {
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final bloc = context.read<ChatListBloc>();
|
||||
// Selected here, not in the LoadableStateConsumer child: that closure runs
|
||||
// during the consumer's build, where this context may not subscribe.
|
||||
// Draft keys are part of the selection so the draft marker follows; the
|
||||
// draft text itself is saved without notifying.
|
||||
final (favoritesToTop, unreadToTop, _) = context.select(
|
||||
(SettingsCubit c) => (
|
||||
c.state.talkSettings.sortFavoritesToTop,
|
||||
c.state.talkSettings.sortUnreadToTop,
|
||||
c.state.talkSettings.drafts.keys.join('|'),
|
||||
),
|
||||
);
|
||||
return SplitView.material(
|
||||
placeholder: const SplitViewPlaceholder(),
|
||||
breakpoint: 1000,
|
||||
@@ -129,13 +140,9 @@ class _ChatListViewState extends State<_ChatListView> {
|
||||
final rooms = state.rooms;
|
||||
if (rooms == null) return const SizedBox.shrink();
|
||||
|
||||
final talkSettings = context
|
||||
.watch<SettingsCubit>()
|
||||
.val()
|
||||
.talkSettings;
|
||||
final sorted = rooms.sortBy(
|
||||
favoritesToTop: talkSettings.sortFavoritesToTop,
|
||||
unreadToTop: talkSettings.sortUnreadToTop,
|
||||
favoritesToTop: favoritesToTop,
|
||||
unreadToTop: unreadToTop,
|
||||
);
|
||||
|
||||
if (sorted.isEmpty) {
|
||||
@@ -145,23 +152,25 @@ class _ChatListViewState extends State<_ChatListView> {
|
||||
);
|
||||
}
|
||||
|
||||
return ListView(
|
||||
final drafts = _settings.val().talkSettings.drafts;
|
||||
final indexByToken = {
|
||||
for (var i = 0; i < sorted.length; i++) sorted[i].token: i,
|
||||
};
|
||||
return ListView.builder(
|
||||
padding: EdgeInsets.zero,
|
||||
children: sorted.map((room) {
|
||||
final hasDraft = _settings
|
||||
.val()
|
||||
.talkSettings
|
||||
.drafts
|
||||
.containsKey(room.token);
|
||||
// Stable key keeps element identity across re-sorts so the
|
||||
// inner UserAvatar reuses its cached bytes instead of
|
||||
// flashing on every list update.
|
||||
itemCount: sorted.length,
|
||||
// Keeps each tile's state (cached avatar bytes) with its room
|
||||
// when a re-sort moves it to another index.
|
||||
findChildIndexCallback: (key) =>
|
||||
indexByToken[(key as ValueKey<String>).value],
|
||||
itemBuilder: (context, index) {
|
||||
final room = sorted[index];
|
||||
return ChatTile(
|
||||
key: ValueKey(room.token),
|
||||
data: room,
|
||||
hasDraft: hasDraft,
|
||||
hasDraft: drafts.containsKey(room.token),
|
||||
);
|
||||
}).toList(),
|
||||
},
|
||||
);
|
||||
},
|
||||
),
|
||||
|
||||
Reference in New Issue
Block a user