修:输出过长的回答时拉不下来
用户报的。定位到 ChatScreen 里这段:
LaunchedEffect(messages.size, last.content.length) {
listState.animateScrollToItem(messages.size - 1)
}
两个毛病叠在一起:
1. animateScrollToItem(index) 是把这一项的**顶部**对齐到视口顶部。
长回答比一屏高得多,所以它压根没"滚到底",只是回到那条回答的开头。
2. 流式输出时 content.length 每一帧都在变 → 这个 effect 每一帧重跑 →
用户刚往下滑一点就被拽回开头,等于根本滑不动。
所以现象是"短回答没事、长回答尤其还在输出的时候拉不下来"。
改法:
- 只有用户本来就贴着底部时才跟着滚(atBottom 用 derivedStateOf 算:
最后一项可见 且 它的底部已进入视口,留 24px 容差)。用户自己往上翻就完全不打扰。
- 跟的时候用 scrollToItem(index, Int.MAX_VALUE) 滚到**内容末尾**,不是项顶部;
也不用 animate —— 流式时每帧都要跟,动画互相打架看起来就是抖。
- 新增「回到底部」浮动按钮:长回答往上翻之后有个一键回去的路。
到底后自动消失。
顺带修一个编译坑:按钮一开始写在 Box 里,Kotlin 把 AnimatedVisibility 解析成
ColumnScope 那个重载(外层还有个 Column),报"不能在这个上下文调用"。
抽成没有 receiver 的独立 composable 就只解析到顶层那个了。
验收:
- 新增 scripts/repro-long-scroll.mjs:注入一条 3400 字的长回答,
截图 → 连滑三次 → 再截图,比较两张图有没有变。
- ★ 第一版复现脚本是假通过:App 启动时 messages 本来就是空的
(openConversation 只在用户点会话时才调),不点会话看到的是欢迎页,
那时候"滑不动"当然成立。补上"打开会话"这一步才测到真东西 ——
这个教训写进脚本注释了。
- 设备实测:能滚到真正的结尾、按钮正常出现和消失。
This commit is contained in:
@@ -13,6 +13,7 @@ import androidx.compose.animation.slideInVertically
|
||||
import androidx.compose.animation.slideOutVertically
|
||||
import androidx.compose.foundation.background
|
||||
import androidx.compose.foundation.border
|
||||
import androidx.compose.foundation.BorderStroke
|
||||
import androidx.compose.foundation.clickable
|
||||
import androidx.compose.foundation.horizontalScroll
|
||||
import androidx.compose.foundation.interaction.MutableInteractionSource
|
||||
@@ -30,6 +31,7 @@ import androidx.compose.foundation.text.selection.SelectionContainer
|
||||
import androidx.compose.material.icons.Icons
|
||||
import androidx.compose.material.icons.filled.*
|
||||
import androidx.compose.material3.Icon
|
||||
import androidx.compose.material3.Surface
|
||||
import androidx.compose.material3.Text
|
||||
import androidx.compose.runtime.*
|
||||
import androidx.compose.ui.Alignment
|
||||
@@ -50,6 +52,7 @@ import androidx.compose.ui.unit.dp
|
||||
import androidx.compose.ui.unit.sp
|
||||
import androidx.core.content.ContextCompat
|
||||
import com.doubao.lynxclone.data.ChatMessage
|
||||
import kotlinx.coroutines.launch
|
||||
import com.doubao.lynxclone.speech.SpeechController
|
||||
import kotlinx.coroutines.delay
|
||||
|
||||
@@ -60,6 +63,42 @@ import kotlinx.coroutines.delay
|
||||
* 大头像 + 灰色问候气泡、白底描边快捷胶囊、输入框是大圆角描边胶囊、发送键是蓝色圆钮。
|
||||
* 动效:气泡淡入上移、思考面板从底部升起、typing 三点跳动、按钮按下回弹。
|
||||
*/
|
||||
@Composable
|
||||
private fun ScrollToBottomChip(
|
||||
visible: Boolean,
|
||||
modifier: Modifier = Modifier,
|
||||
onClick: () -> Unit,
|
||||
) {
|
||||
AnimatedVisibility(
|
||||
visible = visible,
|
||||
enter = fadeIn() + slideInVertically { it / 2 },
|
||||
exit = fadeOut() + slideOutVertically { it / 2 },
|
||||
modifier = modifier,
|
||||
) {
|
||||
Surface(
|
||||
shape = CircleShape,
|
||||
color = doubaoCard(),
|
||||
border = BorderStroke(1.dp, doubaoBorder()),
|
||||
shadowElevation = 3.dp,
|
||||
onClick = onClick,
|
||||
) {
|
||||
Row(
|
||||
Modifier.padding(horizontal = 14.dp, vertical = 7.dp),
|
||||
verticalAlignment = Alignment.CenterVertically,
|
||||
) {
|
||||
Icon(
|
||||
Icons.Filled.KeyboardArrowDown,
|
||||
"回到底部",
|
||||
modifier = Modifier.size(18.dp),
|
||||
tint = Doubao.Blue,
|
||||
)
|
||||
Spacer(Modifier.width(3.dp))
|
||||
Text("回到底部", color = Doubao.Blue, fontSize = 13.sp)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Composable
|
||||
fun ChatScreen(vm: AppViewModel) {
|
||||
val agent = vm.activeAgent
|
||||
@@ -124,33 +163,68 @@ fun ChatScreen(vm: AppViewModel) {
|
||||
}
|
||||
}
|
||||
|
||||
// 新消息 / 流式增量都滚到底
|
||||
LaunchedEffect(vm.messages.size, vm.messages.lastOrNull()?.content?.length) {
|
||||
if (vm.messages.isNotEmpty()) listState.animateScrollToItem(vm.messages.size - 1)
|
||||
// 新消息 / 流式增量都滚到底,但**只在用户本来就贴着底部时**才跟。
|
||||
//
|
||||
// ★ 这里曾经有两个叠在一起的毛病,表现是"输出过长的回答时拉不下来":
|
||||
// 1. animateScrollToItem(index) 是把这一项的**顶部**对齐到视口顶部。
|
||||
// 长回答比一屏高得多,所以它压根没"滚到底",只是回到那条回答的开头。
|
||||
// 2. 流式输出时 content.length 每一帧都在变 → 这个 effect 每一帧重跑 →
|
||||
// 用户刚往下滑一点就被拽回开头,等于根本滑不动。
|
||||
// 现在:贴着底部才跟;跟的时候用 scrollToItem + 大 offset 滚到**内容末尾**;
|
||||
// 用户自己往上翻了就完全不打扰(要回底部有下面的按钮)。
|
||||
val atBottom by remember {
|
||||
derivedStateOf {
|
||||
val info = listState.layoutInfo
|
||||
val last = info.visibleItemsInfo.lastOrNull() ?: return@derivedStateOf true
|
||||
last.index >= vm.messages.size - 1 &&
|
||||
last.offset + last.size <= info.viewportEndOffset + 24
|
||||
}
|
||||
}
|
||||
val contentLen = vm.messages.lastOrNull()?.content?.length ?: 0
|
||||
LaunchedEffect(vm.messages.size, contentLen) {
|
||||
if (vm.messages.isEmpty() || !atBottom) return@LaunchedEffect
|
||||
// 不用 animate:流式时每帧都要跟,动画会互相打架,看起来就是抖
|
||||
listState.scrollToItem(vm.messages.size - 1, Int.MAX_VALUE)
|
||||
}
|
||||
|
||||
Column(Modifier.fillMaxSize().background(doubaoBg())) {
|
||||
ChatHeader(vm, agent?.name ?: "豆包", speech)
|
||||
|
||||
LazyColumn(
|
||||
state = listState,
|
||||
modifier = Modifier.weight(1f).fillMaxWidth(),
|
||||
contentPadding = PaddingValues(horizontal = 16.dp, vertical = 10.dp),
|
||||
verticalArrangement = Arrangement.spacedBy(12.dp),
|
||||
) {
|
||||
if (vm.messages.isEmpty()) {
|
||||
val tagline = agent?.greeting.orEmpty().trim()
|
||||
val looksLikeSentence = tagline.length >= 12 &&
|
||||
(tagline.endsWith("。") || tagline.endsWith("!") ||
|
||||
tagline.endsWith("?") || tagline.contains(","))
|
||||
val greeting =
|
||||
if (looksLikeSentence) tagline else "嗨,我是${agent?.name ?: "豆包"},有什么可以帮你的吗?"
|
||||
item { AppearOnce { HeroSection(greeting) } }
|
||||
}
|
||||
items(vm.messages.size) { index ->
|
||||
AppearOnce(offsetY = 10.dp) {
|
||||
MessageItem(vm, index, vm.messages[index], speech)
|
||||
Box(Modifier.weight(1f).fillMaxWidth()) {
|
||||
LazyColumn(
|
||||
state = listState,
|
||||
modifier = Modifier.fillMaxSize(),
|
||||
contentPadding = PaddingValues(horizontal = 16.dp, vertical = 10.dp),
|
||||
verticalArrangement = Arrangement.spacedBy(12.dp),
|
||||
) {
|
||||
if (vm.messages.isEmpty()) {
|
||||
val tagline = agent?.greeting.orEmpty().trim()
|
||||
val looksLikeSentence = tagline.length >= 12 &&
|
||||
(tagline.endsWith("。") || tagline.endsWith("!") ||
|
||||
tagline.endsWith("?") || tagline.contains(","))
|
||||
val greeting =
|
||||
if (looksLikeSentence) tagline else "嗨,我是${agent?.name ?: "豆包"},有什么可以帮你的吗?"
|
||||
item { AppearOnce { HeroSection(greeting) } }
|
||||
}
|
||||
items(vm.messages.size) { index ->
|
||||
AppearOnce(offsetY = 10.dp) {
|
||||
MessageItem(vm, index, vm.messages[index], speech)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 「回到底部」:长回答往上翻之后,得有个一键回去的路。
|
||||
// 只在用户确实翻上去了、且内容装不下一屏时才出现,平时不占地方。
|
||||
//
|
||||
// ★ 这里必须抽成独立 composable,不能直接在这儿写 AnimatedVisibility:
|
||||
// 我们现在在 Box 里,但外层还有个 Column,Kotlin 会解析到
|
||||
// ColumnScope.AnimatedVisibility 那个重载,报"不能在这个上下文调用"。
|
||||
// 放到一个没有 receiver 的函数里,就只会解析到顶层那个。
|
||||
ScrollToBottomChip(
|
||||
visible = !atBottom && vm.messages.isNotEmpty(),
|
||||
modifier = Modifier.align(Alignment.BottomCenter).padding(bottom = 12.dp),
|
||||
) {
|
||||
scope.launch { listState.animateScrollToItem(vm.messages.size - 1, Int.MAX_VALUE) }
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user