Flutter Another exception was thrown: line 662 pos 12: 'child.parent == this': is not true.

弹出软键盘抛出异常布局内容溢出

错误日志

Another exception was thrown: 'package:flutter/src/rendering/viewport.dart': Failed assertion: line 662 pos 12: 'child.parent == this': is not true.

在这里插入图片描述

场景(已知)

类型1

我们在使用自定义dialog的时候,布局中有使用TextField TextFormField 当他们获取到了焦点,弹出软盘时,会抛出该异常。

类型2

我们在使用自定义dialog的时候,布局中有使用 多包含类型布局 如 ListView GridView 他们嵌套了TextField TextFormField 当他们获取到了焦点,弹出软盘时,会抛出该异常。

解决方案

包裹一层ScrollView 推荐使用简单的 SingleChildScrollView

伪代码

new SingleChildScrollView(
              child: TextField(
//          keyboardType: TextInputType.multiline,
//        controller: accountController,
              textAlign: TextAlign.center,
//              autofocus: true, //自动获取焦点
//              maxLength: 7,
              style: new TextStyle(fontSize: 14.0, color: Colors.black),
              maxLines: 1,
              decoration: InputDecoration(
                  contentPadding: EdgeInsets.all(2.0),
                  border: OutlineInputBorder(),
                  hintText: betItems[index].rate,
                  suffix: new ImageIcon(
                      new AssetImage("images/ic_edit_note.png"),
                      color: null,
                      size: 10.0)),
              onChanged: (String value) {
                betItems[index].rate = value;
              },
            ),
            ))
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章