flutter 弹出框键盘遮挡 获取键盘高度

键盘高度:MediaQuery.of(context).viewInsets.bottom (此高度只能在键盘唤醒时在build中获取,弹出键盘会执行build)

看代码:
iimport ‘package:flutter/cupertino.dart’;
import ‘package:flutter/material.dart’;

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: ‘Flutter Demo’,
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: MyHomePage(title: ‘Flutter Demo Home Page’),
);
}
}

class MyHomePage extends StatefulWidget {
MyHomePage({Key key, this.title}) : super(key: key);

final String title;

@override
_MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State {
String hint = ‘请输入…’;

@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
backgroundColor: Colors.red,
centerTitle: true,
title: Text(‘appBar’),
),
body: Container(
color: Colors.green,
child: InkWell(
child: Center(
child: Text(
‘点击弹出框’,
style: TextStyle(color: Colors.white),
),
),
onTap: () {
showDialog(
barrierDismissible: true,
context: context,
builder: (BuildContext context) {
return Scaffold(
backgroundColor: Colors.transparent,
body: Container(
width: MediaQuery.of(context).size.width,
child: Stack(
children: [
Positioned(
child: CupertinoAlertDialog(
title: const Text(‘输入框’),
content:Container(
color: Colors.white,
child: CupertinoTextField(
placeholder: hint,
style: TextStyle(fontSize: 15, color: Colors.black),
decoration: BoxDecoration(
borderRadius: BorderRadius.all(Radius.circular(2)),
),
maxLines: 5,
maxLength: 30,
maxLengthEnforced: true,
),
),
),
bottom: MediaQuery.of(context).viewInsets.bottom > 0 ? 0 : 300,//300换成键盘高度值效果更好
left: 0,
right: 0,
)
],
),
),
);
});
},
),
));
}
}

/*
class _MyHomePageState extends State {

@override
void initState() {
super.initState();
}

String str = ‘’;

@override
Widget build(BuildContext context) {
MediaQuery.of(context);
print(‘build~~~~~’);
return Scaffold(
appBar: AppBar(
centerTitle: true,
title: Text(‘appA的flutter’),
),
body: Center(
child: TextField(),
)

);

}
}*/
至于 ios风格组件国际化问题 请看之前的文章 告辞

  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 14
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 14
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值