With a column extension
, use like:
Column.withSpacing(
spacing: 20,
children: [
Text('One'),
Text('Two'),
],
)
Extension:
import 'package:flutter/material.dart';
extension SpacingExtension on Column {
static Column withSpacing({
Key? key,
required List<Widget> children,
int spacing = 10,
MainAxisAlignment mainAxisAlignment = MainAxisAlignment.start,
MainAxisSize mainAxisSize = MainAxisSize.max,
CrossAxisAlignment crossAxisAlignment = CrossAxisAlignment.center,
TextDirection? textDirection,
VerticalDirection verticalDirection = VerticalDirection.down,
TextBaseline? textBaseline,
}) {
var spacedChildren = children.expand((widget) => [widget, SizedBox(height: spacing.toDouble())]).toList();
spacedChildren.removeLast(); // Remove the extra SizedBox
return Column(
key: key,
mainAxisAlignment: mainAxisAlignment,
mainAxisSize: mainAxisSize,
crossAxisAlignment: crossAxisAlignment,
textDirection: textDirection,
verticalDirection: verticalDirection,
textBaseline: textBaseline,
children: spacedChildren,
);
}
}
Or as a widget:
Use:
SpacingColumn(
spacing: 20,
children: [
Text('One'),
Text('Two'),
],
)
Widget:
import 'package:flutter/material.dart';
class SpacingColumn extends StatelessWidget {
final List<Widget> children;
final int spacing;
final MainAxisAlignment mainAxisAlignment;
final MainAxisSize mainAxisSize;
final CrossAxisAlignment crossAxisAlignment;
final TextDirection? textDirection;
final VerticalDirection verticalDirection;
final TextBaseline? textBaseline;
SpacingColumn({
Key? key,
required this.children,
this.spacing = 10,
this.mainAxisAlignment = MainAxisAlignment.start,
this.mainAxisSize = MainAxisSize.max,
this.crossAxisAlignment = CrossAxisAlignment.center,
this.textDirection,
this.verticalDirection = VerticalDirection.down,
this.textBaseline,
}) : super(key: key);
@override
Widget build(BuildContext context) {
return Column(
mainAxisAlignment: mainAxisAlignment,
mainAxisSize: mainAxisSize,
crossAxisAlignment: crossAxisAlignment,
textDirection: textDirection,
verticalDirection: verticalDirection,
textBaseline: textBaseline,
children: children.expand((widget) => [widget, SizedBox(height: spacing.toDouble())]).toList()..removeLast(),
);
}
}
MainAxisAlignment.spaceAround
? – Yusukspaces2
: see this post – Give