简体   繁体   中英

What's the most efficient way to add spacing with a specific width to Widgets in a Row()?

I use Expanded() to tell the Widgets in my Row to use the available space. I want to have 4px space between elements in my Row. I don't want to have the same space towards elements outside of the Row. Given that Flutter doesn't support negative Padding I therefore can't add Padding in a way that gives the first item no left Padding and the last item no right Padding.

Is there pattern to easily define spacing with a specific width? I want a solution that works with an arbitrary number of Widgets. For 4 Widgets I want something that produces the equivalent of:

Row(children: [
    Padding(
        padding: const EdgeInsets.fromLTRB(0, 0, 8, 0),
        child: Expanded(Text("Alice"))),
    Padding(
        padding: const EdgeInsets.fromLTRB(8, 0, 8, 0),
        child: Expanded(Text("Alice"))),
    Padding(
        padding: const EdgeInsets.fromLTRB(8, 0, 8, 0),
        child: Expanded(Text("Alice"))),
    Padding(
        padding: const EdgeInsets.fromLTRB(8, 0, 0, 0),
        child: Expanded(Text("Alice")))
]);

cant understand what you want to achieve exactly but try Use SizedBox widget very easy and simple check it here

https://api.flutter.dev/flutter/widgets/SizedBox-class.html

example

  SizedBox(
  width: 200.0,
  height: 300.0,
)

I usually use this:

Row(
  crossAxisAlignment: CrossAxisAlignment.center,
  mainAxisAlignment: MainAxisAlignment.spaceAround,
  children: [
    Widget(),
    Spacer(),
    Widget(),
    Widget(),
    ],
  ),

Spacer, a widget that takes up space proportional to it's flex value. Flutter Row Class

mainAxisAlignment property: How the children should be placed along the main axis. Flutter MainAxisAlignment enum

If you want to determine the size, you can use SizedBox(width: double) instead of Spacer () .

SizedBox: A box with a specified size. Flutter SizedBox class

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM