首页 > 解决方案 > 将输入字段和按钮放置在屏幕中央

问题描述

我想要一个简单的输入字段,下面有按钮,我试图把它放在屏幕的中央。

这是我的代码:

Widget build(BuildContext context) {
    return new Container(
      decoration: new BoxDecoration(color: Colors.white10),
      child: new Center(
        child: Column(
          children: <Widget>[
            TextField(
            decoration: InputDecoration(border: OutlineInputBorder())),
            IconButton(
             icon: Icon(Icons.volume_up),
             tooltip: 'Increase volume by 10%',
             onPressed: () { setState(() { _volume *= 1.1; }); },)

          ],
        )
      ),
    );
  }

但是,即使我使用Center()小部件,它也会进入屏幕顶部。

标签: flutter

解决方案


你想要的只是垂直居中,使用mainAxisAlignmentcrossAxisAlignment

    Widget build(BuildContext context) {
    return new Container(
      decoration: new BoxDecoration(color: Colors.white10),
      child: new Center(
        child: Column(
           mainAxisAlignment: MainAxisAlignment.center,
           crossAxisAlignment: CrossAxisAlignment.center,
           children: <Widget>[
            TextField(
            decoration: InputDecoration(border: OutlineInputBorder())),
            IconButton(
             icon: Icon(Icons.volume_up),
             tooltip: 'Increase volume by 10%',
             onPressed: () { setState(() { _volume *= 1.1; }); },)

          ],
        )
      ),
    );
  }

推荐阅读