fluffychat/lib/pages/chat/chat_event_list.dart

136 lines
5.2 KiB
Dart
Raw Normal View History

2022-06-28 16:51:29 +02:00
import 'package:flutter/material.dart';
import 'package:matrix/matrix.dart';
import 'package:scroll_to_index/scroll_to_index.dart';
import 'package:fluffychat/config/themes.dart';
import 'package:fluffychat/pages/chat/chat.dart';
import 'package:fluffychat/pages/chat/events/message.dart';
import 'package:fluffychat/pages/chat/seen_by_row.dart';
import 'package:fluffychat/pages/chat/typing_indicators.dart';
import 'package:fluffychat/pages/user_bottom_sheet/user_bottom_sheet.dart';
2023-01-07 10:29:34 +01:00
import 'package:fluffychat/utils/adaptive_bottom_sheet.dart';
2022-12-30 17:54:01 +01:00
import 'package:fluffychat/utils/matrix_sdk_extensions/filtered_timeline_extension.dart';
2022-06-28 16:51:29 +02:00
import 'package:fluffychat/utils/platform_infos.dart';
class ChatEventList extends StatelessWidget {
final ChatController controller;
const ChatEventList({
Key? key,
required this.controller,
}) : super(key: key);
@override
Widget build(BuildContext context) {
final horizontalPadding = FluffyThemes.isColumnMode(context) ? 8.0 : 0.0;
// create a map of eventId --> index to greatly improve performance of
// ListView's findChildIndexCallback
final thisEventsKeyMap = <String, int>{};
for (var i = 0; i < controller.timeline!.events.length; i++) {
thisEventsKeyMap[controller.timeline!.events[i].eventId] = i;
2022-06-28 16:51:29 +02:00
}
2022-06-28 16:51:29 +02:00
return ListView.custom(
padding: EdgeInsets.only(
top: 16,
bottom: 4,
left: horizontalPadding,
right: horizontalPadding,
),
reverse: true,
controller: controller.scrollController,
keyboardDismissBehavior: PlatformInfos.isIOS
? ScrollViewKeyboardDismissBehavior.onDrag
: ScrollViewKeyboardDismissBehavior.manual,
childrenDelegate: SliverChildBuilderDelegate(
(BuildContext context, int i) {
// Footer to display typing indicator and read receipts:
if (i == 0) {
if (controller.timeline!.isRequestingFuture) {
return const Center(
child: CircularProgressIndicator.adaptive(strokeWidth: 2),
);
}
if (controller.timeline!.canRequestFuture) {
return Center(
child: IconButton(
onPressed: controller.requestFuture,
icon: const Icon(Icons.refresh_outlined),
),
);
}
2022-06-28 16:51:29 +02:00
return Column(
mainAxisSize: MainAxisSize.min,
children: [
SeenByRow(controller),
TypingIndicators(controller),
],
);
}
// Request history button or progress indicator:
if (i == controller.timeline!.events.length + 1) {
2022-06-28 16:51:29 +02:00
if (controller.timeline!.isRequestingHistory) {
return const Center(
child: CircularProgressIndicator.adaptive(strokeWidth: 2),
);
}
if (controller.timeline!.canRequestHistory) {
return Center(
child: IconButton(
2022-06-28 16:51:29 +02:00
onPressed: controller.requestHistory,
icon: const Icon(Icons.refresh_outlined),
2022-06-28 16:51:29 +02:00
),
);
}
return const SizedBox.shrink();
2022-06-28 16:51:29 +02:00
}
// The message at this index:
final event = controller.timeline!.events[i - 1];
2022-06-28 16:51:29 +02:00
return AutoScrollTag(
key: ValueKey(event.eventId),
2022-06-28 16:51:29 +02:00
index: i - 1,
controller: controller.scrollController,
child: event.isVisibleInGui
? Message(
event,
onSwipe: (direction) =>
controller.replyAction(replyTo: event),
onInfoTab: controller.showEventInfo,
2023-01-07 10:29:34 +01:00
onAvatarTab: (Event event) => showAdaptiveBottomSheet(
context: context,
builder: (c) => UserBottomSheet(
user: event.senderFromMemoryOrFallback,
outerContext: context,
onMention: () => controller.sendController.text +=
'${event.senderFromMemoryOrFallback.mention} ',
),
),
onSelect: controller.onSelectMessage,
scrollToEventId: (String eventId) =>
controller.scrollToEventId(eventId),
longPressSelect: controller.selectedEvents.isEmpty,
selected: controller.selectedEvents
.any((e) => e.eventId == event.eventId),
timeline: controller.timeline!,
2023-03-23 15:02:32 +01:00
displayReadMarker:
controller.readMarkerEventId == event.eventId &&
controller.timeline?.allowNewEvent == false,
nextEvent: i < controller.timeline!.events.length
? controller.timeline!.events[i]
: null,
)
: const SizedBox.shrink(),
2022-06-28 16:51:29 +02:00
);
},
childCount: controller.timeline!.events.length + 2,
2022-06-28 16:51:29 +02:00
findChildIndexCallback: (key) =>
controller.findChildIndexCallback(key, thisEventsKeyMap),
),
);
}
}