mirror of
https://github.com/krille-chan/fluffychat
synced 2024-09-10 11:45:10 +00:00
fix: BuildContext crash when joining room
This commit is contained in:
parent
d60ae0d8b8
commit
88f1fc1720
5 changed files with 139 additions and 125 deletions
|
@ -1,6 +1,7 @@
|
||||||
import 'package:flutter/material.dart';
|
import 'package:flutter/material.dart';
|
||||||
|
|
||||||
import 'package:flutter_gen/gen_l10n/l10n.dart';
|
import 'package:flutter_gen/gen_l10n/l10n.dart';
|
||||||
|
import 'package:go_router/go_router.dart';
|
||||||
import 'package:matrix/matrix.dart';
|
import 'package:matrix/matrix.dart';
|
||||||
|
|
||||||
import 'package:fluffychat/pages/archive/archive.dart';
|
import 'package:fluffychat/pages/archive/archive.dart';
|
||||||
|
@ -59,6 +60,8 @@ class ArchiveView extends StatelessWidget {
|
||||||
itemBuilder: (BuildContext context, int i) => ChatListItem(
|
itemBuilder: (BuildContext context, int i) => ChatListItem(
|
||||||
controller.archive[i],
|
controller.archive[i],
|
||||||
onForget: () => controller.forgetRoomAction(i),
|
onForget: () => controller.forgetRoomAction(i),
|
||||||
|
onTap: () => context
|
||||||
|
.go('/rooms/archive/${controller.archive[i].id}'),
|
||||||
),
|
),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
|
@ -11,6 +11,7 @@ import 'package:fluffychat/pages/chat_list/chat_list_item.dart';
|
||||||
import 'package:fluffychat/pages/chat_list/search_title.dart';
|
import 'package:fluffychat/pages/chat_list/search_title.dart';
|
||||||
import 'package:fluffychat/pages/chat_list/space_view.dart';
|
import 'package:fluffychat/pages/chat_list/space_view.dart';
|
||||||
import 'package:fluffychat/pages/chat_list/status_msg_list.dart';
|
import 'package:fluffychat/pages/chat_list/status_msg_list.dart';
|
||||||
|
import 'package:fluffychat/pages/chat_list/utils/on_chat_tap.dart';
|
||||||
import 'package:fluffychat/pages/user_bottom_sheet/user_bottom_sheet.dart';
|
import 'package:fluffychat/pages/user_bottom_sheet/user_bottom_sheet.dart';
|
||||||
import 'package:fluffychat/utils/adaptive_bottom_sheet.dart';
|
import 'package:fluffychat/utils/adaptive_bottom_sheet.dart';
|
||||||
import 'package:fluffychat/utils/matrix_sdk_extensions/matrix_locals.dart';
|
import 'package:fluffychat/utils/matrix_sdk_extensions/matrix_locals.dart';
|
||||||
|
@ -251,6 +252,7 @@ class ChatListViewBody extends StatelessWidget {
|
||||||
)) {
|
)) {
|
||||||
return const SizedBox.shrink();
|
return const SizedBox.shrink();
|
||||||
}
|
}
|
||||||
|
final activeChat = controller.activeChat == rooms[i].id;
|
||||||
return ChatListItem(
|
return ChatListItem(
|
||||||
rooms[i],
|
rooms[i],
|
||||||
key: Key('chat_list_item_${rooms[i].id}'),
|
key: Key('chat_list_item_${rooms[i].id}'),
|
||||||
|
@ -258,10 +260,10 @@ class ChatListViewBody extends StatelessWidget {
|
||||||
controller.selectedRoomIds.contains(rooms[i].id),
|
controller.selectedRoomIds.contains(rooms[i].id),
|
||||||
onTap: controller.selectMode == SelectMode.select
|
onTap: controller.selectMode == SelectMode.select
|
||||||
? () => controller.toggleSelection(rooms[i].id)
|
? () => controller.toggleSelection(rooms[i].id)
|
||||||
: null,
|
: () => onChatTap(rooms[i], context),
|
||||||
onLongPress: () =>
|
onLongPress: () =>
|
||||||
controller.toggleSelection(rooms[i].id),
|
controller.toggleSelection(rooms[i].id),
|
||||||
activeChat: controller.activeChat == rooms[i].id,
|
activeChat: activeChat,
|
||||||
);
|
);
|
||||||
},
|
},
|
||||||
childCount: rooms.length,
|
childCount: rooms.length,
|
||||||
|
|
|
@ -3,7 +3,6 @@ import 'package:flutter/material.dart';
|
||||||
import 'package:adaptive_dialog/adaptive_dialog.dart';
|
import 'package:adaptive_dialog/adaptive_dialog.dart';
|
||||||
import 'package:flutter_gen/gen_l10n/l10n.dart';
|
import 'package:flutter_gen/gen_l10n/l10n.dart';
|
||||||
import 'package:future_loading_dialog/future_loading_dialog.dart';
|
import 'package:future_loading_dialog/future_loading_dialog.dart';
|
||||||
import 'package:go_router/go_router.dart';
|
|
||||||
import 'package:matrix/matrix.dart';
|
import 'package:matrix/matrix.dart';
|
||||||
|
|
||||||
import 'package:fluffychat/config/app_config.dart';
|
import 'package:fluffychat/config/app_config.dart';
|
||||||
|
@ -14,7 +13,6 @@ import '../../config/themes.dart';
|
||||||
import '../../utils/date_time_extension.dart';
|
import '../../utils/date_time_extension.dart';
|
||||||
import '../../widgets/avatar.dart';
|
import '../../widgets/avatar.dart';
|
||||||
import '../../widgets/matrix.dart';
|
import '../../widgets/matrix.dart';
|
||||||
import '../chat/send_file_dialog.dart';
|
|
||||||
|
|
||||||
enum ArchivedRoomAction { delete, rejoin }
|
enum ArchivedRoomAction { delete, rejoin }
|
||||||
|
|
||||||
|
@ -22,132 +20,20 @@ class ChatListItem extends StatelessWidget {
|
||||||
final Room room;
|
final Room room;
|
||||||
final bool activeChat;
|
final bool activeChat;
|
||||||
final bool selected;
|
final bool selected;
|
||||||
final void Function()? onTap;
|
|
||||||
final void Function()? onLongPress;
|
final void Function()? onLongPress;
|
||||||
final void Function()? onForget;
|
final void Function()? onForget;
|
||||||
|
final void Function() onTap;
|
||||||
|
|
||||||
const ChatListItem(
|
const ChatListItem(
|
||||||
this.room, {
|
this.room, {
|
||||||
this.activeChat = false,
|
this.activeChat = false,
|
||||||
this.selected = false,
|
this.selected = false,
|
||||||
this.onTap,
|
required this.onTap,
|
||||||
this.onLongPress,
|
this.onLongPress,
|
||||||
this.onForget,
|
this.onForget,
|
||||||
super.key,
|
super.key,
|
||||||
});
|
});
|
||||||
|
|
||||||
void clickAction(BuildContext context) async {
|
|
||||||
if (onTap != null) return onTap!();
|
|
||||||
if (activeChat) return;
|
|
||||||
if (room.membership == Membership.invite) {
|
|
||||||
final inviterId =
|
|
||||||
room.getState(EventTypes.RoomMember, room.client.userID!)?.senderId;
|
|
||||||
final inviteAction = await showModalActionSheet<InviteActions>(
|
|
||||||
context: context,
|
|
||||||
message: room.isDirectChat
|
|
||||||
? L10n.of(context)!.invitePrivateChat
|
|
||||||
: L10n.of(context)!.inviteGroupChat,
|
|
||||||
title: room.getLocalizedDisplayname(MatrixLocals(L10n.of(context)!)),
|
|
||||||
actions: [
|
|
||||||
SheetAction(
|
|
||||||
key: InviteActions.accept,
|
|
||||||
label: L10n.of(context)!.accept,
|
|
||||||
icon: Icons.check_outlined,
|
|
||||||
isDefaultAction: true,
|
|
||||||
),
|
|
||||||
SheetAction(
|
|
||||||
key: InviteActions.decline,
|
|
||||||
label: L10n.of(context)!.decline,
|
|
||||||
icon: Icons.close_outlined,
|
|
||||||
isDestructiveAction: true,
|
|
||||||
),
|
|
||||||
SheetAction(
|
|
||||||
key: InviteActions.block,
|
|
||||||
label: L10n.of(context)!.block,
|
|
||||||
icon: Icons.block_outlined,
|
|
||||||
isDestructiveAction: true,
|
|
||||||
),
|
|
||||||
],
|
|
||||||
);
|
|
||||||
if (inviteAction == null) return;
|
|
||||||
if (inviteAction == InviteActions.block) {
|
|
||||||
context.go('/rooms/settings/security/ignorelist', extra: inviterId);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
if (inviteAction == InviteActions.decline) {
|
|
||||||
await showFutureLoadingDialog(
|
|
||||||
context: context,
|
|
||||||
future: room.leave,
|
|
||||||
);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
final joinResult = await showFutureLoadingDialog(
|
|
||||||
context: context,
|
|
||||||
future: () async {
|
|
||||||
final waitForRoom = room.client.waitForRoomInSync(
|
|
||||||
room.id,
|
|
||||||
join: true,
|
|
||||||
);
|
|
||||||
await room.join();
|
|
||||||
await waitForRoom;
|
|
||||||
},
|
|
||||||
);
|
|
||||||
if (joinResult.error != null) return;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (room.membership == Membership.ban) {
|
|
||||||
ScaffoldMessenger.of(context).showSnackBar(
|
|
||||||
SnackBar(
|
|
||||||
content: Text(L10n.of(context)!.youHaveBeenBannedFromThisChat),
|
|
||||||
),
|
|
||||||
);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (room.membership == Membership.leave) {
|
|
||||||
context.go('/rooms/archive/${room.id}');
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Share content into this room
|
|
||||||
final shareContent = Matrix.of(context).shareContent;
|
|
||||||
if (shareContent != null) {
|
|
||||||
final shareFile = shareContent.tryGet<MatrixFile>('file');
|
|
||||||
if (shareContent.tryGet<String>('msgtype') == 'chat.fluffy.shared_file' &&
|
|
||||||
shareFile != null) {
|
|
||||||
await showDialog(
|
|
||||||
context: context,
|
|
||||||
useRootNavigator: false,
|
|
||||||
builder: (c) => SendFileDialog(
|
|
||||||
files: [shareFile],
|
|
||||||
room: room,
|
|
||||||
),
|
|
||||||
);
|
|
||||||
Matrix.of(context).shareContent = null;
|
|
||||||
} else {
|
|
||||||
final consent = await showOkCancelAlertDialog(
|
|
||||||
context: context,
|
|
||||||
title: L10n.of(context)!.forward,
|
|
||||||
message: L10n.of(context)!.forwardMessageTo(
|
|
||||||
room.getLocalizedDisplayname(MatrixLocals(L10n.of(context)!)),
|
|
||||||
),
|
|
||||||
okLabel: L10n.of(context)!.forward,
|
|
||||||
cancelLabel: L10n.of(context)!.cancel,
|
|
||||||
);
|
|
||||||
if (consent == OkCancelResult.cancel) {
|
|
||||||
Matrix.of(context).shareContent = null;
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
if (consent == OkCancelResult.ok) {
|
|
||||||
room.sendEvent(shareContent);
|
|
||||||
Matrix.of(context).shareContent = null;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
context.go('/rooms/${room.id}');
|
|
||||||
}
|
|
||||||
|
|
||||||
Future<void> archiveAction(BuildContext context) async {
|
Future<void> archiveAction(BuildContext context) async {
|
||||||
{
|
{
|
||||||
if ([Membership.leave, Membership.ban].contains(room.membership)) {
|
if ([Membership.leave, Membership.ban].contains(room.membership)) {
|
||||||
|
@ -391,7 +277,7 @@ class ChatListItem extends StatelessWidget {
|
||||||
),
|
),
|
||||||
],
|
],
|
||||||
),
|
),
|
||||||
onTap: () => clickAction(context),
|
onTap: onTap,
|
||||||
trailing: onForget == null
|
trailing: onForget == null
|
||||||
? hovered || selected
|
? hovered || selected
|
||||||
? IconButton(
|
? IconButton(
|
||||||
|
@ -417,9 +303,3 @@ class ChatListItem extends StatelessWidget {
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
enum InviteActions {
|
|
||||||
accept,
|
|
||||||
decline,
|
|
||||||
block,
|
|
||||||
}
|
|
||||||
|
|
|
@ -11,6 +11,7 @@ import 'package:matrix/matrix.dart';
|
||||||
import 'package:fluffychat/pages/chat_list/chat_list.dart';
|
import 'package:fluffychat/pages/chat_list/chat_list.dart';
|
||||||
import 'package:fluffychat/pages/chat_list/chat_list_item.dart';
|
import 'package:fluffychat/pages/chat_list/chat_list_item.dart';
|
||||||
import 'package:fluffychat/pages/chat_list/search_title.dart';
|
import 'package:fluffychat/pages/chat_list/search_title.dart';
|
||||||
|
import 'package:fluffychat/pages/chat_list/utils/on_chat_tap.dart';
|
||||||
import 'package:fluffychat/utils/matrix_sdk_extensions/matrix_locals.dart';
|
import 'package:fluffychat/utils/matrix_sdk_extensions/matrix_locals.dart';
|
||||||
import 'package:fluffychat/widgets/avatar.dart';
|
import 'package:fluffychat/widgets/avatar.dart';
|
||||||
import '../../utils/localized_exception_extension.dart';
|
import '../../utils/localized_exception_extension.dart';
|
||||||
|
@ -429,6 +430,7 @@ class _SpaceViewState extends State<SpaceView> {
|
||||||
onLongPress: () =>
|
onLongPress: () =>
|
||||||
_onSpaceChildContextMenu(spaceChild, room),
|
_onSpaceChildContextMenu(spaceChild, room),
|
||||||
activeChat: widget.controller.activeChat == room.id,
|
activeChat: widget.controller.activeChat == room.id,
|
||||||
|
onTap: () => onChatTap(room, context),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
final isSpace = spaceChild.roomType == 'm.space';
|
final isSpace = spaceChild.roomType == 'm.space';
|
||||||
|
|
127
lib/pages/chat_list/utils/on_chat_tap.dart
Normal file
127
lib/pages/chat_list/utils/on_chat_tap.dart
Normal file
|
@ -0,0 +1,127 @@
|
||||||
|
import 'package:flutter/material.dart';
|
||||||
|
|
||||||
|
import 'package:adaptive_dialog/adaptive_dialog.dart';
|
||||||
|
import 'package:flutter_gen/gen_l10n/l10n.dart';
|
||||||
|
import 'package:future_loading_dialog/future_loading_dialog.dart';
|
||||||
|
import 'package:go_router/go_router.dart';
|
||||||
|
import 'package:matrix/matrix.dart';
|
||||||
|
|
||||||
|
import 'package:fluffychat/pages/chat/send_file_dialog.dart';
|
||||||
|
import 'package:fluffychat/utils/matrix_sdk_extensions/matrix_locals.dart';
|
||||||
|
import 'package:fluffychat/widgets/matrix.dart';
|
||||||
|
|
||||||
|
void onChatTap(Room room, BuildContext context) async {
|
||||||
|
if (room.membership == Membership.invite) {
|
||||||
|
final inviterId =
|
||||||
|
room.getState(EventTypes.RoomMember, room.client.userID!)?.senderId;
|
||||||
|
final inviteAction = await showModalActionSheet<InviteActions>(
|
||||||
|
context: context,
|
||||||
|
message: room.isDirectChat
|
||||||
|
? L10n.of(context)!.invitePrivateChat
|
||||||
|
: L10n.of(context)!.inviteGroupChat,
|
||||||
|
title: room.getLocalizedDisplayname(MatrixLocals(L10n.of(context)!)),
|
||||||
|
actions: [
|
||||||
|
SheetAction(
|
||||||
|
key: InviteActions.accept,
|
||||||
|
label: L10n.of(context)!.accept,
|
||||||
|
icon: Icons.check_outlined,
|
||||||
|
isDefaultAction: true,
|
||||||
|
),
|
||||||
|
SheetAction(
|
||||||
|
key: InviteActions.decline,
|
||||||
|
label: L10n.of(context)!.decline,
|
||||||
|
icon: Icons.close_outlined,
|
||||||
|
isDestructiveAction: true,
|
||||||
|
),
|
||||||
|
SheetAction(
|
||||||
|
key: InviteActions.block,
|
||||||
|
label: L10n.of(context)!.block,
|
||||||
|
icon: Icons.block_outlined,
|
||||||
|
isDestructiveAction: true,
|
||||||
|
),
|
||||||
|
],
|
||||||
|
);
|
||||||
|
if (inviteAction == null) return;
|
||||||
|
if (inviteAction == InviteActions.block) {
|
||||||
|
context.go('/rooms/settings/security/ignorelist', extra: inviterId);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (inviteAction == InviteActions.decline) {
|
||||||
|
await showFutureLoadingDialog(
|
||||||
|
context: context,
|
||||||
|
future: room.leave,
|
||||||
|
);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
final joinResult = await showFutureLoadingDialog(
|
||||||
|
context: context,
|
||||||
|
future: () async {
|
||||||
|
final waitForRoom = room.client.waitForRoomInSync(
|
||||||
|
room.id,
|
||||||
|
join: true,
|
||||||
|
);
|
||||||
|
await room.join();
|
||||||
|
await waitForRoom;
|
||||||
|
},
|
||||||
|
);
|
||||||
|
if (joinResult.error != null) return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (room.membership == Membership.ban) {
|
||||||
|
ScaffoldMessenger.of(context).showSnackBar(
|
||||||
|
SnackBar(
|
||||||
|
content: Text(L10n.of(context)!.youHaveBeenBannedFromThisChat),
|
||||||
|
),
|
||||||
|
);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (room.membership == Membership.leave) {
|
||||||
|
context.go('/rooms/archive/${room.id}');
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Share content into this room
|
||||||
|
final shareContent = Matrix.of(context).shareContent;
|
||||||
|
if (shareContent != null) {
|
||||||
|
final shareFile = shareContent.tryGet<MatrixFile>('file');
|
||||||
|
if (shareContent.tryGet<String>('msgtype') == 'chat.fluffy.shared_file' &&
|
||||||
|
shareFile != null) {
|
||||||
|
await showDialog(
|
||||||
|
context: context,
|
||||||
|
useRootNavigator: false,
|
||||||
|
builder: (c) => SendFileDialog(
|
||||||
|
files: [shareFile],
|
||||||
|
room: room,
|
||||||
|
),
|
||||||
|
);
|
||||||
|
Matrix.of(context).shareContent = null;
|
||||||
|
} else {
|
||||||
|
final consent = await showOkCancelAlertDialog(
|
||||||
|
context: context,
|
||||||
|
title: L10n.of(context)!.forward,
|
||||||
|
message: L10n.of(context)!.forwardMessageTo(
|
||||||
|
room.getLocalizedDisplayname(MatrixLocals(L10n.of(context)!)),
|
||||||
|
),
|
||||||
|
okLabel: L10n.of(context)!.forward,
|
||||||
|
cancelLabel: L10n.of(context)!.cancel,
|
||||||
|
);
|
||||||
|
if (consent == OkCancelResult.cancel) {
|
||||||
|
Matrix.of(context).shareContent = null;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (consent == OkCancelResult.ok) {
|
||||||
|
room.sendEvent(shareContent);
|
||||||
|
Matrix.of(context).shareContent = null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
context.go('/rooms/${room.id}');
|
||||||
|
}
|
||||||
|
|
||||||
|
enum InviteActions {
|
||||||
|
accept,
|
||||||
|
decline,
|
||||||
|
block,
|
||||||
|
}
|
Loading…
Reference in a new issue