键盘弹出时如何避免整页重新加载? [英] How to avoid full page reloading when keyboard pops up?

查看:95
本文介绍了键盘弹出时如何避免整页重新加载?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

观看此视频以获取问题演示 https://youtu.be/GsdWcTEbUbg

Check out this video for problem demo https://youtu.be/GsdWcTEbUbg

由于存在大量代码,因此我将在此处尝试总结代码的结构. 简而言之:

As there is a large number of code, I will try to summarize the structure of the code here. In a nutshell:

Scaffold(
  appBar: AppBar(),
  body: StreamBuilder<dynamic>(
    stream: globals.chatRoomBloc.threadScreen,
    builder: (BuildContext context, AsyncSnapshot snapshot) {
      return Column(
        crossAxisAlignment:
            CrossAxisAlignment.stretch, // sticks to the keyboard
        children: <Widget>[
          Expanded(
            child: Scrollbar(
              child: ListView.builder(
                shrinkWrap: true,
                itemCount: list.length,
                itemBuilder: (BuildContext context, int index) {
                  return TileWidget();
                },
              ),
            ),
          ),
        ],
      );
    },
  ),
)

TileWidget是有状态的,当按下回复按钮时,将附加TextField并调整窗口小部件的大小.然后,当用户单击文本字段"时,将弹出键盘.

TileWidget is stateful, appends the TextField and resize the widget when the reply button is pressed. Then when the user clicks on the Textfield, the keyboard pops up.

现在我的问题是,键盘弹出时屏幕会重新加载.

Now my problem is that the screen is reloaded when the keyboard pops up.

我尝试了以下解决方案:

I tried the following solutions:

  1. 在Streambuilder中使用TextField :我正在设置仅流一次,即第一次加载页面时.添加新的聊天或条目后,将对流进行任何更改.就我而言,这不会发生.
  2. 将Flutter切换到Tab重新加载小部件并运行FutureBuilder 我不确定这是否是相同的问题,但是解决方案对我来说并没有任何改变.
  3. 问题#11895 -我也经历了此问题,但并没有帮助
  1. Using TextField inside a Streambuilder : I am setting up the stream only once, that is when the page is loaded the first time. Any changes to the stream is made when a new chat or entry is added. In my case, this does not happen.
  2. Flutter Switching to Tab Reloads Widgets and runs FutureBuilder I am not sure if this is the same problem, but the solution does not change anything for me.
  3. Issue #11895 - I went through this as well, but is not helping.

我认为屏幕正在尝试调整大小并重新绘制以适应键盘抽屉.但这是由于某种原因而无法执行此操作并重新加载所有内容.我想念什么吗?有办法解决吗?

I think the screen is trying to resize and redraw to accommodate the keyboard drawer. But it is for some reason failing to do that and loading everything over again. Am I missing something ? Is there a way around this ?

推荐答案

您提供的链接之一指出,只要应用程序的状态发生变化(即键盘弹出),您的buildmethod就会触发,因此将streambuilder移出它.您还可以进行其他更改.尝试以下操作,

As one of the links you provides points out your buildmethod fires whenever the state of the app changes i.e the keyboard pops up, so move the streambuilder outside of it. There are a few more changes you could do. Try the following,

在build方法之外创建一个变量.

create a variable outside of the build method.

var myStreamBuilder;
//....
//inside initialstate method
     myStreamBuilder = StreamBuilder<dynamic>(
            stream: globals.chatRoomBloc.threadScreen,
            builder: (BuildContext context, AsyncSnapshot snapshot) {
              return Column(
                crossAxisAlignment:
                    CrossAxisAlignment.stretch, // sticks to the keyboard
                children: <Widget>[
                  Expanded(
                    child: Scrollbar(
                      child: ListView.builder(
                        shrinkWrap: true,
                        itemCount: list.length,
                        itemBuilder: (BuildContext context, int index) {
                          return TileWidget();
                        },
                      ),
                    ),
                  ),
                ],
              );
            },
          ),

然后在您的构建方法中调用该变量.

Then in your build method call the variable.

Scaffold(
  appBar: AppBar(),
  resizeToAvoidBottomInset: false, //don't forget this!
  body: myStreamBuilder
)

确保您检查快照是否具有hasData.如果没有数据,则应返回某些内容,直到它包含数据为止,这样可以通知用户应用程序在做什么.

Make sure you check your snapshot that it hasData or not. If there is no data then something should be returned until it does have data, this way the user is informed what the app is doing.

此属性也可能对您有所帮助. resizeToAvoidBottomInset- https://api.flutter.dev/flutter/material/Scaffold/resizeToAvoidBottomInset.html

Also this property might also be helpful to you. resizeToAvoidBottomInset - https://api.flutter.dev/flutter/material/Scaffold/resizeToAvoidBottomInset.html

这篇关于键盘弹出时如何避免整页重新加载?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆