简体   繁体   English

在 Flutter 应用程序中隐藏/关闭 ListView 滚动上的键盘

[英]Hide/Dismiss keyboard on ListView scroll in Flutter app

I have chat window in Flutter app.我在 Flutter 应用中有聊天窗口。 Messages are presented as widgets inside ListView widget and I also have widget for message input attached to bottom of the window.消息在ListView小部件内显示为小部件,我还有附加到窗口底部的消息输入小部件。

I want to我想要

  1. hide keyboard when I scroll the ListView滚动ListView时隐藏键盘
  2. scroll to last message when new is added from InputWidgetInputWidget添加新消息时滚动到最后一条消息

code:代码:

class _MessagesPageState extends State<MessagesPage> {
  final ScrollController listScrollController = ScrollController();

  @override
  Widget build(BuildContext context) {
    return Scaffold(
    ....
    body: Stack(
        children: [
          ListView.builder(
              controller: listScrollController
              ....
          ),
          InputWidget()]
    );
}

class InputWidget extends StatelessWidget {

  final TextEditingController _textEditingController = TextEditingController();

....
Row (
  children: [
    TextField(
     controller: _textEditingController
    ), 
    IconButton(icon: ...., onPressed: (){})
  ]
 )}

For hiding keyboard on listview scrolling, you simply add keyboardDismissBehavior to it.要在列表视图滚动时隐藏键盘,您只需向其添加keyboardDismissBehavior Example例子

ListView(
 keyboardDismissBehavior: ScrollViewKeyboardDismissBehavior.onDrag,
 children: [],
)

As to point 1 of you question :至于你的第 1 点问题:

You can create a listener function for listScrollController containing a call to an anonymous FocusNode (idea taken from this highly voted answer ), and when any scroll event occurs, focus will be taken from your TextField and keyboard will be dismissed:您可以为listScrollController创建一个侦听器函数, listScrollController包含对匿名FocusNode的调用(想法取自此高度投票的答案),并且当发生任何滚动事件时,焦点将从您的TextField获取,并且键盘将被解除:

 class _MessagesPageState extends State<MessagesPage> {

 final ScrollController listScrollController = ScrollController();

 @override
 void initState() {
   listScrollController.addListener(_scrollListener);
   super.initState();
 }

 _scrollListener() {
   FocusScope.of(context).requestFocus(FocusNode());
 }

 @override
 Widget build(BuildContext context) {
   return Scaffold(
   ....
   body: Stack(
    children: [
      ListView.builder(
          controller: listScrollController
          ....
      ),
      InputWidget(controller: listScrollController)]
   );
}

Point 2 :第 2 点:

You'll notice that I modified your InputWidget to take a ScrollController as a parameter, so you can pass your ListView controller to it.您会注意到我修改了InputWidget以将ScrollController作为参数,因此您可以将ListView控制器传递给它。 and when the IconButton is pressed, listScrollController will jump to the end as you desire.而当IconButton被按下时, listScrollController会根据您的需要跳转到最后。

 class InputWidget extends StatelessWidget {

 InputWidget({Key key,this.controller}) : super(key: key);

 final ScrollController controller ;

 final TextEditingController _textEditingController = TextEditingController();

 ....
 Row (
  children: [
    TextField(
      controller: _textEditingController
    ), 
    IconButton(icon: ...., onPressed: (){
      controller.jumpTo(controller.position.maxScrollExtent);
    })
  ]
 )}

it's easy..Follow these step..很简单..按照这些步骤..

  1. Change class to StatefullWidget将类更改为StatefullWidget
  2. create final ScrollController listScrollController = ScrollController();创建final ScrollController listScrollController = ScrollController();
  3. ListView should be like this: ListView 应该是这样的:

     ListView.builder( controller: listScrollController, reverse: true,
  4. If you use firebase change order by like this:如果您像这样使用 firebase 更改顺序:

    .orderBy('timestamp', descending: true)

  5. Add this code in your Send button在您的发送按钮中添加此代码

listScrollController.animateTo(0.0,duration: Duration(milliseconds: 300), curve: Curves.easeOut);

Automatically scroll up your textField, Add your textField and ListView inside Stack自动向上滚动你的 textField,在Stack添加你的textFieldListView

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM