在Row Flutter中设置Elements之间的空间

mar*_*drb 14 row dart flutter

码:

new Container(
          alignment: FractionalOffset.center,
          child: new Row(
            mainAxisAlignment: MainAxisAlignment.spaceEvenly,
            children: <Widget>[
              new FlatButton(
                child: new Text('Don\'t have an account?', style: new TextStyle(color: Color(0xFF2E3233))),
              ),
              new FlatButton(
                child: new Text('Register.', style: new TextStyle(color: Color(0xFF84A2AF), fontWeight: FontWeight.bold),),
                onPressed: moveToRegister,
              )
            ],
          ),
        ),  
Run Code Online (Sandbox Code Playgroud)

这是结果:https:
//www.dropbox.com/s/sxklyutyvbbura8/Screenshot_20181104-150928.png?dl = 0

如何确定两个FlatButton元素并排,屏幕宽度中心之间没有空间?

She*_*ard 46

MainAxisAlignment

开始 - 将孩子放在尽可能靠近主轴起点的位置.

在此输入图像描述

end - 将子项尽可能靠近主轴的末端.

在此输入图像描述

center - 将孩子放在尽可能靠近主轴中间的位置.

在此输入图像描述

spaceBetween - 在子节点之间均匀放置可用空间.

在此输入图像描述

spaceAround - 将自由空间均匀放置在子节点之间,以及第一个和最后一个子节点之前和之后的一半空间.

在此输入图像描述

spaceEvenly - 将自由空间均匀放置在孩子之间以及第一个和最后一个孩子之前和之后.

在此输入图像描述

例:

  child: Row(
        mainAxisAlignment: MainAxisAlignment.spaceBetween,
        children: <Widget>[
          Text('Row1'),
          Text('Row2')
        ],
      )
Run Code Online (Sandbox Code Playgroud)


anm*_*ail 23

删除空间-:

new Row(
            mainAxisAlignment: MainAxisAlignment.center,
            children: <Widget>[
              GestureDetector(
                child: new Text('Don\'t have an account?',
                    style: new TextStyle(color: Color(0xFF2E3233))),
                onTap: () {},
              ),
              GestureDetector(
                onTap: (){},
                  child: new Text(
                'Register.',
                style: new TextStyle(
                    color: Color(0xFF84A2AF), fontWeight: FontWeight.bold),
              ))
            ],
          ),
Run Code Online (Sandbox Code Playgroud)

或者

GestureDetector(
            onTap: (){},
            child: new Row(
              mainAxisAlignment: MainAxisAlignment.center,
              children: <Widget>[
                new Text('Don\'t have an account?',
                    style: new TextStyle(color: Color(0xFF2E3233))),
                new Text(
                  'Register.',
                  style: new TextStyle(
                  color: Color(0xFF84A2AF), fontWeight: FontWeight.bold),
                )
              ],
            ),
          ),
Run Code Online (Sandbox Code Playgroud)

  • 他们是并排的,但之间的空间太大了。 (2认同)
  • 使用“GestureDetector”的一个缺点是,您必须自己管理连锁反应(按钮反馈),如果没有这个,您最终会得到一个奇怪的用户体验 (2认同)

Cop*_*oad 9

这样做的方法很多,这里列出了一些方法:

  1. 使用SizedBox,如果你想设置一些特定空间

    Row(
      children: <Widget>[
        Text("1"),
        SizedBox(width: 50), // give it width
        Text("2"),
      ],
    )
    
    Run Code Online (Sandbox Code Playgroud)

    在此处输入图片说明


  1. 使用Spacer,如果你想既要尽可能远离可能的。

    Row(
      children: <Widget>[
        Text("1"),
        Spacer(), // use Spacer
        Text("2"),
      ],
    )
    
    Run Code Online (Sandbox Code Playgroud)

    在此处输入图片说明


  1. 使用mainAxisAlignment根据自己的需要:

    Row(
      mainAxisAlignment: MainAxisAlignment.spaceEvenly, // use whichever suits your need
      children: <Widget>[
        Text("1"),
        Text("2"),
      ],
    )
    
    Run Code Online (Sandbox Code Playgroud)

    在此处输入图片说明


  1. 使用Wrap代替,Row并给一些spacing

    Wrap(
      spacing: 100, // set spacing here
      children: <Widget>[
        Text("1"),
        Text("2"),
      ],
    )
    
    Run Code Online (Sandbox Code Playgroud)

    在此处输入图片说明


  1. 使用Wrap代替,Row并使其对齐

    Wrap(
      alignment: WrapAlignment.spaceAround, // set your alignment
      children: <Widget>[
        Text("1"),
        Text("2"),
      ],
    )
    
    Run Code Online (Sandbox Code Playgroud)

    在此处输入图片说明

  • 没有改变。两个`FlatButton`之间的空间是任意的,就像我的Question中的图像一样。 (3认同)
  • 精彩的答案 (2认同)

Ala*_*ley 7

You can use Spacers if all you want is a little bit of spacing between items in a row. The example below centers 2 Text widgets within a row with some spacing between them.

    widget = Row (
    children: <Widget>[
      Spacer(flex: 20),
      Text(
        "Item #1",
      ),
      Spacer(),  // Defaults to flex: 1
      Text(
        "Item #2",
      ),
      Spacer(flex: 20),
    ]
  );
Run Code Online (Sandbox Code Playgroud)


Iva*_*iuk 6

为了获得精确的间距,我使用Padding. 有两个图像的示例:

Row(
    mainAxisAlignment: MainAxisAlignment.spaceAround,
    children: <Widget>[
      Expanded(
        child: Padding(
          padding: const EdgeInsets.all(16.0),
          child: Image.asset('images/user1.png'),
        ),
      ),
      Expanded(
        child: Padding(
          padding: const EdgeInsets.all(16.0),
          child: Image.asset('images/user2.png'),
        ),
      )
    ],
  ),
Run Code Online (Sandbox Code Playgroud)


Guv*_*nch 6

Row(
 children: <Widget>[
  Flexible(
   child: TextFormField()),
  Container(width: 20, height: 20),
  Flexible(
   child: TextFormField())
 ])
Run Code Online (Sandbox Code Playgroud)

这对我有用,行内有 3 个小部件:灵活、容器、灵活

  • 既然你要走这条路,为什么不直接使用 SizedBox(width: 10).... (2认同)

Oma*_*ail 5

只需在元素之间添加“Container(width: 5, color: Colors.transparent),”

new Container(
      alignment: FractionalOffset.center,
      child: new Row(
        mainAxisAlignment: MainAxisAlignment.spaceEvenly,
        children: <Widget>[
          new FlatButton(
            child: new Text('Don\'t have an account?', style: new TextStyle(color: Color(0xFF2E3233))),
          ),
            Container(width: 5, color: Colors.transparent),
          new FlatButton(
            child: new Text('Register.', style: new TextStyle(color: Color(0xFF84A2AF), fontWeight: FontWeight.bold),),
            onPressed: moveToRegister,
          )
        ],
      ),
    ),  
Run Code Online (Sandbox Code Playgroud)