Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set the space between Elements in Row Flutter

Code:

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,               )             ],           ),         ),   

And here is the result: https://dartpad.dev/?id=6bbfc6139bdf32aa7b47eebcdf9623ba

How to fix that the two FlatButton elements are side by side without space between the center of the screen width?

like image 805
marcelo.wdrb Avatar asked Nov 04 '18 14:11

marcelo.wdrb


People also ask

How do you give a space between Row elements in Flutter?

The Spacer widget in Flutter, allows you to add an empty space that you can adjust to match your design. By default, it takes all the available space and shifts the adjacent widget to the far side. Using the flex property of the Spacer widget, you can control the space between widgets.

How do you put a space between in Flutter?

It is not compulsory to provide a child widget to SizedBox. For instance, if you are having two Card widgets and you want to give space between them, then you can use SizedBox. You can add SizedBox between those cards and pass the required height and width values.

How do you put a space between two columns in Flutter?

You can use Padding widget in between those two widget or wrap those widgets with Padding widget. SizedBox widget can be use in between two widget to add space between two widget and it makes code more readable than padding widget.


2 Answers

There are many ways of doing it, I'm listing a few here:

  1. Use SizedBox if you want to set some specific space

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

    enter image description here


  1. Use Spacer if you want both to be as far apart as possible.

    Row(   children: <Widget>[     Text("1"),     Spacer(), // use Spacer     Text("2"),   ], ) 

    enter image description here


  1. Use mainAxisAlignment according to your needs:

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

    enter image description here


  1. Use Wrap instead of Row and give some spacing

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

    enter image description here


  1. Use Wrap instead of Row and give it alignment

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

    enter image description here

like image 186
CopsOnRoad Avatar answered Oct 12 '22 09:10

CopsOnRoad


MainAxisAlignment

start - Place the children as close to the start of the main axis as possible.

enter image description here

end - Place the children as close to the end of the main axis as possible.

enter image description here

center - Place the children as close to the middle of the main axis as possible.

enter image description here

spaceBetween - Place the free space evenly between the children.

enter image description here

spaceAround - Place the free space evenly between the children as well as half of that space before and after the first and last child.

enter image description here

spaceEvenly - Place the free space evenly between the children as well as before and after the first and last child.

enter image description here

Example:

  child: Row(         mainAxisAlignment: MainAxisAlignment.spaceBetween,         children: <Widget>[           Text('Row1'),           Text('Row2')         ],       ) 
like image 23
Shelly Pritchard Avatar answered Oct 12 '22 08:10

Shelly Pritchard