Разделение навигации и реактивности
This commit is contained in:
@@ -35,44 +35,48 @@ class _ChatDetailPageState extends State<ChatDetailPage> {
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return BlocBuilder<ChatBloc, ChatState>(
|
||||
builder: (context, state) {
|
||||
final currentChat = state.maybeWhen(
|
||||
chatSelected: (chat, _, __) => chat,
|
||||
orElse: () => widget.chat,
|
||||
);
|
||||
|
||||
return Scaffold(
|
||||
appBar: AppBar(
|
||||
centerTitle: false,
|
||||
title: Column(
|
||||
return Scaffold(
|
||||
appBar: AppBar(
|
||||
centerTitle: false,
|
||||
title: BlocBuilder<ChatBloc, ChatState>(
|
||||
builder: (context, state) {
|
||||
final currentChat = state.maybeWhen(
|
||||
chatSelected: (chat, _, __) => chat,
|
||||
messagesLoaded: (chat, _, __) => chat,
|
||||
orElse: () => widget.chat,
|
||||
);
|
||||
return Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
Text(currentChat.type == 'favorites' ? AppLocalizations.of(context)!.favorites : currentChat.title),
|
||||
if (currentChat.type != 'favorites')
|
||||
_buildPresenceStatus(currentChat, state),
|
||||
],
|
||||
),
|
||||
actions: [
|
||||
IconButton(icon: const Icon(Icons.more_vert), onPressed: () {}),
|
||||
],
|
||||
),
|
||||
body: Column(
|
||||
children: [
|
||||
Expanded(
|
||||
child: state.maybeWhen(
|
||||
);
|
||||
},
|
||||
),
|
||||
actions: [
|
||||
IconButton(icon: const Icon(Icons.more_vert), onPressed: () {}),
|
||||
],
|
||||
),
|
||||
body: Column(
|
||||
children: [
|
||||
Expanded(
|
||||
child: BlocBuilder<ChatBloc, ChatState>(
|
||||
builder: (context, state) {
|
||||
return state.maybeWhen(
|
||||
chatSelected: (_, messages, __) => _buildMessageList(messages),
|
||||
messagesLoaded: (messages, _) => _buildMessageList(messages),
|
||||
messagesLoaded: (_, messages, __) => _buildMessageList(messages),
|
||||
loading: (_) => const Center(child: CircularProgressIndicator()),
|
||||
error: (msg, _) => Center(child: Text('Ошибка: $msg')),
|
||||
orElse: () => const Center(child: Text('Начните общение')),
|
||||
),
|
||||
),
|
||||
_buildMessageInput(currentChat),
|
||||
],
|
||||
);
|
||||
},
|
||||
),
|
||||
),
|
||||
);
|
||||
},
|
||||
_buildMessageInput(widget.chat),
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
@@ -29,6 +29,8 @@ class _ChatsPageState extends State<ChatsPage> {
|
||||
context.read<ChatBloc>().add(ChatEvent.chatsLoaded(currentUserId: userId));
|
||||
}
|
||||
|
||||
bool _isNavigating = false;
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final l10n = AppLocalizations.of(context)!;
|
||||
@@ -48,6 +50,21 @@ class _ChatsPageState extends State<ChatsPage> {
|
||||
listener: (context, state) {
|
||||
state.whenOrNull(
|
||||
chatSelected: (chat, _, __) {
|
||||
if (_isNavigating) return;
|
||||
|
||||
// Extra safety: check if we are already on chat_detail
|
||||
bool isAlreadyOnDetail = false;
|
||||
Navigator.popUntil(context, (route) {
|
||||
if (route.settings.name == 'chat_detail') {
|
||||
isAlreadyOnDetail = true;
|
||||
}
|
||||
return true;
|
||||
});
|
||||
|
||||
if (isAlreadyOnDetail) return;
|
||||
|
||||
_isNavigating = true;
|
||||
|
||||
final authState = context.read<AuthBloc>().state;
|
||||
final userId = authState.maybeWhen(
|
||||
authenticated: (id) => id,
|
||||
@@ -56,12 +73,14 @@ class _ChatsPageState extends State<ChatsPage> {
|
||||
|
||||
Navigator.of(context).push(
|
||||
MaterialPageRoute(
|
||||
settings: const RouteSettings(name: 'chat_detail'),
|
||||
builder: (_) => ChatDetailPage(
|
||||
chat: chat,
|
||||
currentUserId: userId,
|
||||
),
|
||||
),
|
||||
).then((_) {
|
||||
_isNavigating = false;
|
||||
// Reload chats when returning from detail page
|
||||
if (context.mounted) {
|
||||
context.read<ChatBloc>().add(ChatEvent.chatsLoaded(currentUserId: userId));
|
||||
|
||||
Reference in New Issue
Block a user