Space between Column's children in Flutter

后端 未结 14 795
刺人心
刺人心 2021-02-02 04:33

I have a Column widget with two TextField widgets as children and I want to have some space between both of them.

I already tried mainAxi

相关标签:
14条回答
  • 2021-02-02 05:11

    There are many answers here but I will put here the most important one which everyone should use.

    1. Column

     Column(
              children: <Widget>[
                Text('Widget A'), //Can be any widget
                SizedBox(height: 20,), //height is space betweeen your top and bottom widget
                Text('Widget B'), //Can be any widget
              ],
            ),
    

    2. Wrap

         Wrap(
              direction: Axis.vertical, // We have to declare Axis.vertical, otherwise by default widget are drawn in horizontal order
                spacing: 20, // Add spacing one time which is same for all other widgets in the children list
                children: <Widget>[
                  Text('Widget A'), // Can be any widget
                  Text('Widget B'), // Can be any widget
                ]
            )
    
    0 讨论(0)
  • 2021-02-02 05:13

    You may have to use SizedBox() widget between your column's children. Hope that'll be usefull

    0 讨论(0)
  • 2021-02-02 05:14

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

    1. Use Container and give some height:

      Column(
        children: <Widget>[
          Widget1(),
          Container(height: 10), // set height
          Widget2(),
        ],
      )
      
    2. Use Spacer

      Column(
        children: <Widget>[
          Widget1(),
          Spacer(), // use Spacer
          Widget2(),
        ],
      )
      
    3. Use Expanded

      Column(
        children: <Widget>[
          Widget1(),
          Expanded(child: SizedBox()), // use Expanded
          Widget2(),
        ],
      )
      
    4. Use mainAxisAlignment

      Column(
        mainAxisAlignment: MainAxisAlignment.spaceAround, // mainAxisAlignment
        children: <Widget>[
          Widget1(),
          Widget2(),
        ],
      )
      
    5. Use Wrap

      Wrap(
        direction: Axis.vertical, // make sure to set this
        spacing: 20, // set your spacing
        children: <Widget>[
          Widget1(),
          Widget2(),
        ],
      )
      
    0 讨论(0)
  • 2021-02-02 05:15

    The sized box will not help in the case, the phone is in landscape mode.

    body: Column(
          mainAxisAlignment: MainAxisAlignment.spaceBetween,
          crossAxisAlignment: CrossAxisAlignment.stretch,
          children: <Widget>[
            Expanded(
               child: Container(
                margin: EdgeInsets.all(15.0),
                decoration: BoxDecoration(
                  color: Color(0xFF1D1E33),
                  borderRadius: BorderRadius.circular(10.0),
                ),
              ),
            ),
            Expanded(
               child: Container(
                margin: EdgeInsets.all(15.0),
                decoration: BoxDecoration(
                  color: Color(0xFF1D1E33),
                  borderRadius: BorderRadius.circular(10.0),
                ),
              ),
            ),
            Expanded(
               child: Container(
                margin: EdgeInsets.all(15.0),
                decoration: BoxDecoration(
                  color: Color(0xFF1D1E33),
                  borderRadius: BorderRadius.circular(10.0),
                ),
              ),
            ),
          ],
         )
    
    0 讨论(0)
  • 2021-02-02 05:16

    The same way SizedBox is used above for the purpose of code readability, you can use the Padding widget in the same manner and not have to make it a parent widget to any of the Column's children

    Column(
      children: <Widget>[
        FirstWidget(),
        Padding(padding: EdgeInsets.only(top: 40.0)),
        SecondWidget(),
      ]
    )
    
    0 讨论(0)
  • 2021-02-02 05:19

    Columns Has no height by default, You can Wrap your Column to the Container and add the specific height to your Container. Then You can use something like below:

    Container(
       width: double.infinity,//Your desire Width
       height: height,//Your desire Height
       child: Column(
          mainAxisAlignment: MainAxisAlignment.spaceBetween,
          children: <Widget>[
             Text('One'),
             Text('Two')
          ],
       ),
    ),
    
    0 讨论(0)
提交回复
热议问题