键盘高度: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风格组件国际化问题 请看之前的文章 告辞