refactor: enhance chat message handling and scrolling behavior

- Added `sendMessageWithContainer` function to facilitate message sending with a ProviderContainer.
- Updated `_ChatPageState` to improve scroll behavior, ensuring smoother auto-scrolling when near the bottom of the chat.
- Refactored scroll logic to simplify conditions for showing and hiding the scroll-to-bottom button.
- Adjusted the `OptimizedList` widget to correctly handle item indexing based on the reverse property, enhancing list performance and usability.
This commit is contained in:
cogwheel0
2025-09-30 21:17:11 +05:30
parent 7debb7a055
commit 46bd057089
4 changed files with 43 additions and 36 deletions

View File

@@ -1447,6 +1447,15 @@ Future<void> sendMessageFromService(
await _sendMessageInternal(ref, message, attachments, toolIds);
}
Future<void> sendMessageWithContainer(
ProviderContainer container,
String message,
List<String>? attachments, [
List<String>? toolIds,
]) async {
await _sendMessageInternal(container, message, attachments, toolIds);
}
// Internal send message implementation
Future<void> _sendMessageInternal(
dynamic ref,

View File

@@ -372,10 +372,9 @@ class _ChatPageState extends ConsumerState<ChatPage> {
// Scroll to bottom after enqueuing (only if user was near bottom)
WidgetsBinding.instance.addPostFrameCallback((_) {
if (_scrollController.hasClients) {
final maxScroll = _scrollController.position.maxScrollExtent;
final currentScroll = _scrollController.position.pixels;
// Only auto-scroll if user was already near the bottom (within 300px)
if (maxScroll - currentScroll < 300) {
if (currentScroll <= 300) {
_scrollToBottom();
}
}
@@ -543,26 +542,18 @@ class _ChatPageState extends ConsumerState<ChatPage> {
_scrollDebounceTimer = Timer(const Duration(milliseconds: 80), () {
if (!mounted || _isDeactivated || !_scrollController.hasClients) return;
final maxScroll = _scrollController.position.maxScrollExtent;
final currentScroll = _scrollController.position.pixels;
final maxScroll = _scrollController.position.maxScrollExtent;
// Hysteresis thresholds to avoid flicker
const double showThreshold =
300.0; // show when farther than this from bottom
const double hideThreshold =
150.0; // hide when within this distance of bottom
const double showThreshold = 300.0;
const double hideThreshold = 150.0;
final bool farFromBottom = currentScroll < (maxScroll - showThreshold);
final bool nearBottom = currentScroll >= (maxScroll - hideThreshold);
final bool farFromBottom = currentScroll > showThreshold;
final bool nearBottom = currentScroll <= hideThreshold;
bool showButton;
if (_showScrollToBottom) {
// Currently shown: keep it until we are near the bottom
showButton = !nearBottom && maxScroll > showThreshold;
} else {
// Currently hidden: only show when far from bottom
showButton = farFromBottom && maxScroll > showThreshold;
}
final bool showButton = _showScrollToBottom
? !nearBottom && maxScroll > showThreshold
: farFromBottom && maxScroll > showThreshold;
if (showButton != _showScrollToBottom && mounted && !_isDeactivated) {
setState(() {
@@ -575,17 +566,15 @@ class _ChatPageState extends ConsumerState<ChatPage> {
void _scrollToBottom({bool smooth = true}) {
if (!_scrollController.hasClients) return;
final maxScroll = _scrollController.position.maxScrollExtent;
if (maxScroll <= 0) return;
final target = 0.0;
if (smooth) {
_scrollController.animateTo(
maxScroll,
target,
duration: const Duration(milliseconds: 200),
curve: Curves.easeOutCubic,
);
} else {
_scrollController.jumpTo(maxScroll);
_scrollController.jumpTo(target);
}
}
@@ -742,6 +731,7 @@ class _ChatPageState extends ConsumerState<ChatPage> {
scrollController: _scrollController,
physics: const AlwaysScrollableScrollPhysics(),
items: messages,
reverse: true,
padding: const EdgeInsets.fromLTRB(
Spacing.lg,
Spacing.md,
@@ -1029,9 +1019,8 @@ class _ChatPageState extends ConsumerState<ChatPage> {
WidgetsBinding.instance.addPostFrameCallback((_) {
if (!mounted) return;
if (_scrollController.hasClients) {
final maxScroll = _scrollController.position.maxScrollExtent;
final currentScroll = _scrollController.position.pixels;
if (maxScroll - currentScroll < 300) {
if (currentScroll <= 300) {
_scrollToBottom(smooth: true);
}
}

View File

@@ -18,7 +18,7 @@ import 'enhanced_attachment.dart';
import 'package:conduit/shared/widgets/chat_action_button.dart';
import '../../../shared/widgets/model_avatar.dart';
import 'package:url_launcher/url_launcher_string.dart';
import '../providers/chat_providers.dart' show sendMessage;
import '../providers/chat_providers.dart' show sendMessageWithContainer;
import '../../../core/utils/debug_logger.dart';
import 'sources/openwebui_sources.dart';
import '../providers/assistant_response_builder_provider.dart';
@@ -70,7 +70,8 @@ class _AssistantMessageWidgetState extends ConsumerState<AssistantMessageWidget>
return;
}
try {
await sendMessage(ref, trimmed, null);
final container = ProviderScope.containerOf(context, listen: false);
await sendMessageWithContainer(container, trimmed, null);
} catch (err, stack) {
DebugLogger.log(
'Failed to send follow-up: $err',