当前位置:网站首页>flutter解决键盘和输入框不适配问题
flutter解决键盘和输入框不适配问题
2022-08-02 07:18:00 【氤氲息】
//是否显示键盘
bool isShowKeyboard = false;
double keyboardSize = Global.screenHeight * 0.4;
@override
void initState() {
super.initState();
//输入框焦点监测
_focusNode.addListener(() {
if (_focusNode.hasFocus) {
//下面这句是当UI被遮挡的时候,遮挡高度大于0时就意味着键盘弹起来了
if (MediaQuery.of(context).viewInsets.bottom > 0) {
//设置为true
isShowKeyboard = true;
}
} else {
isShowKeyboard = false;
}
if (mounted) {
setState(() {
});
}
});
} //软键盘高度
@override
void didChangeMetrics() {
super.didChangeMetrics();
WidgetsBinding.instance!.addPostFrameCallback((_) {
// 以后是安卓零碎并且在焦点聚焦的状况下
if (Platform.isAndroid && _focusNode.hasFocus) {
if (MediaQuery.of(context).viewInsets.bottom > 0) {
isShowKeyboard = true;
} else {
isShowKeyboard = false;
}
if (mounted) {
setState(() {
});
}
}
});
}
//如果isShowKeyboard是true就用下边距为keyboardSize(Global.screenHeight * 0.4),如果是false就是30了
布局中就可以设置bottom: isShowKeyboard ? keyboardSize : 30
边栏推荐
猜你喜欢
随机推荐
通过建立新的SaaS业务来推动增长的六种方法
WebForm DropDownList分别绑定年月
Enterprise training and reproduction guidebook - training and reasoning of the OpenPose model based on Huawei ModelArts platform, realizing the recognition of two behaviors of climbing and climbing ov
(2022 Niu Ke Duo School 5) B-Watches (two points)
OC-NSString
MySQL error 1055 solution: [Err] 1055 - Expression #1 of ORDER BY clause is not in GROUP BY clause and contains
Metasploit(MSF)基础超级详细版
初探形式化方法基本原理
敏捷、DevOps和嵌入式系统测试
查找最大的n个文件
About the SQL concat () function problem, how to splice
OC-NSDictionary
HCIP 第六天
redis-高级篇
Aided by training and learning by battle | The new version of the Offensive and Defense World Platform is officially launched!
LeetCode 2312. 卖木头块
研发创新编码器霍尔板,引领企业高质量发展
Link with Game Glitch(spfa判负环)
PanGu-Coder:函数级的代码生成模型
Splunk Field Caculated 计算字段