Space between Column's children in Flutter

后端 未结 14 797
刺人心
刺人心 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:22

    you can use Wrap() widget instead Column() to add space between child widgets.And use spacing property to give equal spacing between children

    Wrap(
      spacing: 20, // to apply margin in the main axis of the wrap
      runSpacing: 20, // to apply margin in the cross axis of the wrap
      children: <Widget>[
         Text('child 1'),
         Text('child 2')
      ]
    )
    
    0 讨论(0)
  • 2021-02-02 05:23

    You can put a SizedBox with a specific height between the widgets, like so:

    Column(
      children: <Widget>[
        FirstWidget(),
        SizedBox(height: 100),
        SecondWidget(),
      ],
    ),
    

    Why to prefer this over wrapping the widgets in Padding? Readability! There is less visual boilerplate, less indention and the code follows the typical reading-order.

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

    Just use padding to wrap it like this:

    Column(
      children: <Widget>[
      Padding(
        padding: EdgeInsets.all(8.0),
        child: Text('Hello World!'),
      ),
      Padding(
        padding: EdgeInsets.all(8.0),
        child: Text('Hello World2!'),
      )
    ]);
    

    You can also use Container(padding...) or SizeBox(height: x.x). The last one is the most common but it will depents of how you want to manage the space of your widgets, I like to use padding if the space is part of the widget indeed and use sizebox for lists for example.

    0 讨论(0)
  • 2021-02-02 05:29
    Column(
      children: <Widget>[
        FirstWidget(),
        Spacer(),
        SecondWidget(),
      ]
    )
    

    Spacer creates a flexible space to insert into a [Flexible] widget. (Like a column)

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

    You can also use a helper function to add spacing after each child.

    List<Widget> childrenWithSpacing({
      @required List<Widget> children,
      double spacing = 8,
    }) {
      final space = Container(width: spacing, height: spacing);
      return children.expand((widget) => [widget, space]).toList();
    }
    

    So then, the returned list may be used as a children of a column

    Column(
      children: childrenWithSpacing(
        spacing: 14,
        children: [
          Text('This becomes a text with an adjacent spacing'),
          if (true == true) Text('Also, makes it easy to add conditional widgets'),
        ],
      ),
    );
    

    I'm not sure though if it's wrong or have a performance penalty to run the children through a helper function for the same goal?

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

    You can solve this problem in different way.

    If you use Row/Column then you have to use mainAxisAlignment: MainAxisAlignment.spaceEvenly

    If you use Wrap Widget you have to use runSpacing: 5, spacing: 10,

    In anywhere you can use SizeBox()

    0 讨论(0)
提交回复
热议问题