我正在尝试显示底部填充等于键盘高度的 showModalBottomSheet,因为在我的 showModalBottomSheet 中有两个 TextField,一个使用字母数字键盘,第二个使用数字键盘。我讨厌 showModalBottomSheet 相应地更改填充(因为数字键盘较低)。所以,我想出了一个想法,就是让底部填充等于显示的最高键盘。
我成功编写了一个工作代码,但动画很慢,因为是实时计算的。这是调用 showModalBottomSheet 的函数
void _startAddNewTransaction(BuildContext ctx) {
double keyboardheight = 0;
double calculateKeyboardHeight(double querypassed) {
if (querypassed > keyboardheight) {
keyboardheight = querypassed;
return keyboardheight;
} else {
return keyboardheight;
}
}
showModalBottomSheet(
isScrollControlled: true,
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.only(
topLeft: Radius.circular(29.5), topRight: Radius.circular(29.5)),
),
context: ctx,
builder: (_) {
return SingleChildScrollView(
child: Container(
padding: EdgeInsets.only(
bottom: calculateKeyboardHeight(
MediaQuery.of(context).viewInsets.bottom),
),
child: GestureDetector(
child: NewTransaction(_addNewTransaction),
onTap: () {},
behavior: HitTestBehavior.opaque),
),
);
},
);
}
现在,由于初始键盘高度为零,即使我autofocus: true
在第一个 Textfield 上设置,showModalBottomSheet 也会实时调整自身,但这会导致动画变慢。您对如何解决这个问题有任何想法吗?
这里有一个正在发生的事情的例子。