implemented keyboard-aware back navigation and refined message sending logic to prevent phantom drafts and handle mid-send navigation
This commit is contained in:
@@ -189,9 +189,7 @@ class _ChatViewState extends State<ChatView> with RouteAware {
|
|||||||
setState(() {
|
setState(() {
|
||||||
_activeMatchIndex = (_activeMatchIndex + 1) % _matches.length;
|
_activeMatchIndex = (_activeMatchIndex + 1) % _matches.length;
|
||||||
});
|
});
|
||||||
WidgetsBinding.instance.addPostFrameCallback(
|
WidgetsBinding.instance.addPostFrameCallback((_) => _scrollToActiveMatch());
|
||||||
(_) => _scrollToActiveMatch(),
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void _goToNextMatch() {
|
void _goToNextMatch() {
|
||||||
@@ -200,9 +198,7 @@ class _ChatViewState extends State<ChatView> with RouteAware {
|
|||||||
_activeMatchIndex =
|
_activeMatchIndex =
|
||||||
(_activeMatchIndex - 1 + _matches.length) % _matches.length;
|
(_activeMatchIndex - 1 + _matches.length) % _matches.length;
|
||||||
});
|
});
|
||||||
WidgetsBinding.instance.addPostFrameCallback(
|
WidgetsBinding.instance.addPostFrameCallback((_) => _scrollToActiveMatch());
|
||||||
(_) => _scrollToActiveMatch(),
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void _scrollToActiveMatch() {
|
void _scrollToActiveMatch() {
|
||||||
@@ -230,8 +226,9 @@ class _ChatViewState extends State<ChatView> with RouteAware {
|
|||||||
final activeId = _matches.isNotEmpty
|
final activeId = _matches.isNotEmpty
|
||||||
? _matches[_activeMatchIndex].messageId
|
? _matches[_activeMatchIndex].messageId
|
||||||
: null;
|
: null;
|
||||||
final highlightQuery =
|
final highlightQuery = _searchActive && _searchQuery.trim().isNotEmpty
|
||||||
_searchActive && _searchQuery.trim().isNotEmpty ? _searchQuery : null;
|
? _searchQuery
|
||||||
|
: null;
|
||||||
|
|
||||||
final messages = <Widget>[];
|
final messages = <Widget>[];
|
||||||
final chronologicalMatchIndex = <int, int>{};
|
final chronologicalMatchIndex = <int, int>{};
|
||||||
@@ -320,7 +317,19 @@ class _ChatViewState extends State<ChatView> with RouteAware {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@override
|
@override
|
||||||
Widget build(BuildContext context) => Scaffold(
|
Widget build(BuildContext context) {
|
||||||
|
// Swallow the first back gesture while the keyboard is visible so it
|
||||||
|
// dismisses the IME instead of popping the chat — matches platform UX
|
||||||
|
// expectations (e.g. WhatsApp/Telegram) and prevents accidental exits
|
||||||
|
// mid-typing.
|
||||||
|
final keyboardOpen = MediaQuery.viewInsetsOf(context).bottom > 0;
|
||||||
|
return PopScope(
|
||||||
|
canPop: !keyboardOpen,
|
||||||
|
onPopInvokedWithResult: (didPop, _) {
|
||||||
|
if (didPop) return;
|
||||||
|
FocusManager.instance.primaryFocus?.unfocus();
|
||||||
|
},
|
||||||
|
child: Scaffold(
|
||||||
backgroundColor: const Color(0xffefeae2),
|
backgroundColor: const Color(0xffefeae2),
|
||||||
appBar: _searchActive
|
appBar: _searchActive
|
||||||
? ChatSearchAppBar(
|
? ChatSearchAppBar(
|
||||||
@@ -377,8 +386,9 @@ class _ChatViewState extends State<ChatView> with RouteAware {
|
|||||||
state.currentToken == widget.room.token,
|
state.currentToken == widget.room.token,
|
||||||
enablePullToRefresh: false,
|
enablePullToRefresh: false,
|
||||||
child: (state, _) {
|
child: (state, _) {
|
||||||
final items =
|
final items = _buildMessages(
|
||||||
_buildMessages(state.chatResponse!).reversed.toList();
|
state.chatResponse!,
|
||||||
|
).reversed.toList();
|
||||||
return ScrollablePositionedList.builder(
|
return ScrollablePositionedList.builder(
|
||||||
reverse: true,
|
reverse: true,
|
||||||
itemScrollController: _itemScrollController,
|
itemScrollController: _itemScrollController,
|
||||||
@@ -402,5 +412,7 @@ class _ChatViewState extends State<ChatView> with RouteAware {
|
|||||||
],
|
],
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
|
),
|
||||||
);
|
);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -110,17 +110,27 @@ class _ChatTextfieldState extends State<ChatTextfield> {
|
|||||||
if (_textBoxController.text.isEmpty) return;
|
if (_textBoxController.text.isEmpty) return;
|
||||||
final text = _textBoxController.text;
|
final text = _textBoxController.text;
|
||||||
final replyTo = chatBloc.state.data?.referenceMessageId?.toString();
|
final replyTo = chatBloc.state.data?.referenceMessageId?.toString();
|
||||||
|
final ownToken = widget.sendToToken;
|
||||||
setState(() => _sendError = null);
|
setState(() => _sendError = null);
|
||||||
await SendMessage(
|
await SendMessage(
|
||||||
widget.sendToToken,
|
ownToken,
|
||||||
SendMessageParams(text, replyTo: replyTo),
|
SendMessageParams(text, replyTo: replyTo),
|
||||||
).run();
|
).run();
|
||||||
if (!mounted) return;
|
// Reached only on success — SendMessage.run() throws on failure and
|
||||||
chatBloc.refresh();
|
// skips this block, leaving the persisted draft as a recovery aid.
|
||||||
_textBoxController.text = '';
|
// Drafts live on the global SettingsCubit keyed by token, so clear
|
||||||
|
// them even when the user navigated away mid-send — otherwise the
|
||||||
|
// sent message lingers as a phantom draft on the chat list.
|
||||||
_setDraft('');
|
_setDraft('');
|
||||||
chatBloc.setReferenceMessageId(null);
|
|
||||||
_setDraftReply(null);
|
_setDraftReply(null);
|
||||||
|
// The global ChatBloc may already point at a different chat (user
|
||||||
|
// switched mid-send); only touch it while it still references us.
|
||||||
|
if (chatBloc.state.data?.currentToken == ownToken) {
|
||||||
|
chatBloc.setReferenceMessageId(null);
|
||||||
|
chatBloc.refresh();
|
||||||
|
}
|
||||||
|
if (!mounted) return;
|
||||||
|
_textBoxController.text = '';
|
||||||
}
|
}
|
||||||
|
|
||||||
@override
|
@override
|
||||||
|
|||||||
Reference in New Issue
Block a user