Skip to content

Instantly share code, notes, and snippets.

@henry2man
Last active March 31, 2022 11:12
Show Gist options
  • Save henry2man/28595050f09633e16641d5f30f1a32ae to your computer and use it in GitHub Desktop.
Save henry2man/28595050f09633e16641d5f30f1a32ae to your computer and use it in GitHub Desktop.
Flutter doesn't provides out-of-the-box spacing attributes for Column and Row widgets (see https://github.com/flutter/flutter/issues/55378). This utility file helps to add horizontal and vertical padding on Columns and Rows
import 'package:flutter/widgets.dart';
List<Widget> addHorizontalSpacing(double spacing, List<Widget> children) {
assert(spacing >= 0.0);
if (children.length <= 1) {
return children;
}
return <Widget>[
children[0],
...children.sublist(1).map(
(item) =>
Padding(padding: EdgeInsets.only(left: spacing), child: item),
)
];
}
List<Widget> addVerticalSpacing(double spacing, List<Widget> children) {
assert(spacing >= 0.0);
if (children.length <= 1) {
return children;
}
return <Widget>[
children[0],
...children.sublist(1).map(
(item) =>
Padding(padding: EdgeInsets.only(top: spacing), child: item),
)
];
}
import 'columns_rows_spacing.dart';
Widget build(BuildContext context) {
return Column(
children: [
Text("Column with spacing"),
Column(
children: addVerticalSpacing(10,
[
Text("Text1"),
Text("Text2"),
Text("Text3"),
]),
),
Text("Row with spacing"),
Row(
children: addHorizontalSpacing(10,
[
Text("Text1"),
Text("Text2"),
Text("Text3"),
]),
),
]
);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment