【发布时间】:2020-07-05 17:31:15
【问题描述】:
我有一个相当简单的颤振应用程序。它具有聊天功能。 但是,我的聊天功能有问题。 它由一个 Scaffold 小部件和其中的 SingleChildScrollView 组成 - 它有一个消息列表(容器)和输入区域(容器)。附上代码。
问题是:如果我点击输入框,键盘会打开并推送消息列表。 如果您已经在消息列表的底部,则推送消息列表是可以接受的。 但是,如果用户向上滚动并看到一些旧消息,我不希望消息列表小部件被向上推。 另外,如果我只有少量消息,我不希望消息列表被向上推送(因为这只会在键盘打开时使消息消失,然后我需要去滚动到已推送的消息[用户在滚动之前只有 0 条可见消息])。
我尝试了不同的方法 - 比如
resizeToAvoidBottomInset: false
但似乎没有什么对我有用,这似乎应该是一个简单的行为(例如,whatsapp 的行为就像所需的行为)。 我担心的唯一选择是听键盘打开事件,但我希望有一个更优雅的解决方案。
这是我的代码:
@override
Widget build(BuildContext context) {
height = MediaQuery.of(context).size.height;
width = MediaQuery.of(context).size.width;
return Scaffold(
body: SingleChildScrollView(
child: Column(
children: <Widget>[
SizedBox(height: height * 0.1),
buildMessageList(), // container
buildInputArea(context), // container
],
),
),
);
Widget buildInputArea(BuildContext context) {
return Container(
height: height * 0.1,
width: width,
child: Row(
children: <Widget>[
buildChatInput(),
buildSendButton(context),
],
),
);
}
Widget buildMessageList() {
return Container(
height: height * 0.8,
width: width,
child: ListView.builder(
controller: scrollController,
itemCount: messages.length,
itemBuilder: (BuildContext context, int index) {
return buildSingleMessage(index);
},
),
);
}
【问题讨论】:
-
问题解决了吗?
标签: flutter