简体   繁体   English

出现键盘时重建整个小部件树

[英]Rebuild whole widget tree when a keyboard appears

How to resolve re-render the whole widget tree when keyboard appears in flutter?当键盘出现在 flutter 中时,如何解决重新渲染整个小部件树的问题? In my app, whenever keyboard appears and disappears, the whole widget tree will be re build?在我的应用程序中,每当键盘出现和消失时,整个小部件树都会重新构建? What is the solution to avoid this?避免这种情况的解决方案是什么?

There are multiple ways to handle this, here are some solutions I've used in the past.有多种方法可以解决这个问题,这里有一些我过去使用过的解决方案。

Scaffold's resizeToAvoidBottomInset parameter Scaffold 的resizeToAvoidBottomInset参数

Set this to false and your screen will not resize, the downside of this is that the keyboard will appear on top of your UI将此设置为false并且您的屏幕不会调整大小,这样做的缺点是键盘将出现在您的 UI 顶部

Example:例子:

class SampleScreen extends StatelessWidget {
  const SampleScreen({Key key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      resizeToAvoidBottomInset: false,
      body: Align(
        alignment: Alignment.bottomCenter,
        child: SafeArea(
          minimum: const EdgeInsets.all(20),
          child: TextField(
            decoration: new InputDecoration(
              enabledBorder: OutlineInputBorder(
                borderSide: BorderSide(color: Colors.black, width: 1.0),
              ),
              hintText: 'Mobile Number',
            ),
          ),
        ),
      ),
    );
  }
}

在此处输入图像描述

Wrap your Scaffold's body in a ScrollView将 Scaffold 的主体包裹在 ScrollView 中

By placing your whole body inside a ScrollView you may now have a lengthy form, only caveat here is that you will not be able to use Spacer and Expanded widgets通过将你的整个身体放在一个 ScrollView 中,你现在可能有一个冗长的表单,这里唯一需要注意的是你将无法使用SpacerExpanded小部件

Example例子

class SampleScreen extends StatelessWidget {
  const SampleScreen({Key key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: SafeArea(
        minimum: const EdgeInsets.all(20),
        child: SingleChildScrollView(
          child: Column(children: [
            Container(
              padding: EdgeInsets.all(20),
              margin: EdgeInsets.all(10),
              color: Colors.red,
              height: MediaQuery.of(context).size.height - 165,
            ),
            TextField(
              decoration: new InputDecoration(
                enabledBorder: OutlineInputBorder(
                  borderSide: BorderSide(color: Colors.black, width: 1.0),
                ),
                hintText: 'Mobile Number',
              ),
            ),
          ]),
        ),
      ),
    );
  }
}

在此处输入图像描述

Save your current State保存您当前的 State

Use a stateful widget and save your app's current data somewhere.使用有状态小部件并将应用的当前数据保存在某处。

暂无
暂无

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

相关问题 当键盘出现时,我的整个小部件树会重建吗? - Will my whole widget tree rebuild when a keyboard appears? Flutter state 出现键盘时重建 - Flutter state rebuild when keyboard appears 为什么我的小部件在我使用键盘时重建 - Why does my widget rebuild when I use keyboard flutter - 避免整个小部件重建 - flutter - Avoiding whole widget rebuild setState 是否为 flutter 中的屏幕重建整个小部件树以及它与其他 State 管理相比如何 - Does setState rebuild the whole widget tree for a screen in flutter and how does it compare with other State management 当 stream 在小部件树之外的 class 中获得新值时,如何在有状态 class 中重建小部件? - How to rebuild widget in stateful class when stream gets new value in a class outside of widget tree? 当它是 MaterialApp 的父级时,继承的 Widget 更改是否会重建整个应用程序? - Does Inherited Widget changes rebuild whole application when it's the parrent of MaterialApp? 如何重建特定的小部件属性而不是整个小部件? - How to rebuild a particular widget property instead of the whole widget? Flutter:检测在屏幕上不可见但在小部件树中的任何小部件的重建 - Flutter: Detect rebuild of any widget which is not visible on screen but is in the widget tree Flutter :- 如何在 Stack 视图上滚动屏幕而不缩小屏幕?(当键盘出现时滚动整个屏幕) - Flutter :- How to scroll the sceen without shrink the screen on the Stack view?(Scroll the the whole screen when the keyboard appears )
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM