Flutter 当键盘显示时,所有的东西都向上推,我得到一个错误
我有以下代码:Flutter 当键盘显示时,所有的东西都向上推,我得到一个错误,flutter,flutter-layout,Flutter,Flutter Layout,我有以下代码: class _MyHomePageState extends State { @override Widget build(BuildContext context) { // This method is rerun every time setState is called, for instance as done // by the _incrementCounter method above.
class _MyHomePageState extends State {
@override
Widget build(BuildContext context) {
// This method is rerun every time setState is called, for instance as done
// by the _incrementCounter method above.
//
// The Flutter framework has been optimized to make rerunning build methods
// fast, so that you can just rebuild anything that needs updating rather
// than having to individually change instances of widgets.
return new Scaffold(
body: new Column(
children: [
new Container(
color: JBTheme.colorGreenBrand,
height: 130.0,
alignment: Alignment.bottomLeft,
child: new Center(
child: new Align(
alignment: Alignment.bottomCenter,
child: new Container(
color: JBTheme.colorOrange,
constraints: new BoxConstraints(maxWidth: 270.0),
child: new Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: [
new Column(
mainAxisSize: MainAxisSize.min,
mainAxisAlignment: MainAxisAlignment.center,
children: [
new Image.asset("flags/flag_dk.png"),
new Container(
margin: const EdgeInsets.only(top: 4.0, bottom: 4.0),
child: new Text("Danmark"),
),
new Text("DKK")
],
),
new Expanded(child: new Image.asset("images/arrow.png")),
new Column(
mainAxisSize: MainAxisSize.min,
mainAxisAlignment: MainAxisAlignment.center,
children: [
new Image.asset("flags/flag_us.png"),
new Container(
margin: const EdgeInsets.only(top: 4.0, bottom: 4.0),
child: new Text("USA"),
),
new Text("USD")
],
)
],
),
),
),
),
),
new Expanded(
child: new Container(
color: JBTheme.colorGreyMedium,
child: new Column(
children: [
new Expanded(child: new Container()),
new Padding(
padding: const EdgeInsets.only(bottom: 8.0),
child: new Card(
elevation: -8.0,
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(15.0),
),
child: new Container(
width: 200.0,
height: 30.0,
color: JBTheme.colorWhite,
child: new Stack(
children: [
new TextField(
textAlign: TextAlign.center
)
],
)
),
)
)
],
),
)),
new Container(
height: 260.0,
color: JBTheme.colorGreenMint,
)
],
));
}
}
看起来是这样的:
但当我点击文本字段,键盘打开时,我会看到:
我没有想到我的所有布局都会向上移动整个键盘高度,我希望它只向上移动足够的距离,以便聚焦的文本字段可见(并且不会给出错误)。我如何解决这个问题,为什么会发生这种情况
谢谢你瑟伦(Søren)这是一种向我们展示当用户使用键盘时,有多少像素的内容会被隐藏起来。尝试设置为false。。。从文档: 主体(和其他浮动小部件)是否应自行调整大小 以避免窗口的底部填充 这将避免调整大小,因此您至少可以避免显示dev警告。但请记住,用户不会看到某些内容,这也是开发人员的警告 2019年10月17日更新 resizeToAvoidBottomPadding现在不推荐使用 使用resizeToAvoidBottomInset指定当键盘出现时,主体是否应调整大小
Scaffold(
resizeToAvoidBottomInset: false,
这个问题有两种解决方案
Scaffold(
resizeToAvoidBottomPadding: false,
body: ...)
Scaffold主体
放在可滚动视图中(如SingleChildScrollView或ListView)
您可以找到类似的问题和答案此错误的完整描述和两种可能的解决方案,可在上的媒体文章中找到。最后一个对我来说很好。如果你只使用SingleChildScrollView内容,就会失去垂直对齐。您还可以将SingleChildScrollView包装在中心小部件中
child: Center(
child: SingleChildScrollView(
child: Column(
children: <Widget>...
child:Center(
子:SingleChildScrollView(
子:列(
孩子们:。。。
请不要只是发布一个链接作为答案。一旦链接可能被破坏,寻找相同答案的人将不会得到任何帮助。请更新您的答案,以涵盖解决方案的关键功能,并仅提供链接作为参考。如果在此处添加至少一点代码片段,我会对这个答案进行投票!
new Scaffold(
body: SingleChildScrollView(child: //your existing body
...)
child: Center(
child: SingleChildScrollView(
child: Column(
children: <Widget>...