将输入字段和按钮放在屏幕中央

时间:2019-02-21 22:41:19

标签: flutter

我想在其下方有一个简单的输入字段并带有按钮,我试图将其放在屏幕中央。

这是我的代码:

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()小部件,它也会到达屏幕顶部。

1 个答案:

答案 0 :(得分:1)

您需要的全部是使用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; }); },)

          ],
        )
      ),
    );
  }