Last active
March 31, 2022 11:12
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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), | |
) | |
]; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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