Skip to content

Commit

Permalink
Added spacing to item width (#3)
Browse files Browse the repository at this point in the history
* Added spacing to item width

* bump package version

* updated changelogs
  • Loading branch information
M97Chahboun authored Jul 22, 2024
1 parent dd8393d commit ed0cac8
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 6 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,4 @@ migrate_working_dir/
**/doc/api/
.dart_tool/
build/
.vscode/branch-timer.json
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
## 0.0.1

* Adds FlexibleWrap widget

## 0.0.3
* Adds spacing to item width
7 changes: 5 additions & 2 deletions example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ class MyHomePage extends StatefulWidget {

class _MyHomePageState extends State<MyHomePage> {
final padding = 8.0;
final spacing = 10.0;
final itemWidth = 380.0;
@override
Widget build(BuildContext context) {
return Scaffold(
Expand All @@ -44,18 +46,19 @@ class _MyHomePageState extends State<MyHomePage> {
length: 35, // Number of children to display
runAlignment: WrapAlignment.start,
crossAxisAlignment: WrapCrossAlignment.end,
spacing: spacing,
builder: (int index, double itemExtraWidth) {
return Padding(
padding: EdgeInsets.all(padding),
child: Container(
height: 60,
color: Colors.blue,
width: 380.0 + itemExtraWidth,
width: itemWidth + itemExtraWidth,
child: Center(child: Text('Item $index')),
),
);
},
itemWidth: 380.0 +
itemWidth: itemWidth +
(padding *
2), // Width of each item + padding value, 2 => horizontal and vertical
direction: Axis.horizontal, // Direction to arrange the children
Expand Down
7 changes: 4 additions & 3 deletions lib/src/flexible_wrap_base.dart
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ class FlexibleWrap extends StatelessWidget {
final int length;

/// The builder function to generate children with the given index and extra width.
final Widget Function(int index, double itemExtraWIdth) builder;
final Widget Function(int index, double itemExtraWidth) builder;

/// The width of each item in the wrap.
final double itemWidth;
Expand Down Expand Up @@ -66,9 +66,10 @@ class FlexibleWrap extends StatelessWidget {
Widget build(BuildContext context) {
return LayoutBuilder(builder: (context, constraint) {
double extraWidth = 0.0;
final double widthWithSpacing = itemWidth + spacing;
if (constraint.maxWidth.isFinite) {
int items = (constraint.maxWidth / itemWidth).floor();
double remainder = constraint.maxWidth.remainder(itemWidth);
int items = (constraint.maxWidth / widthWithSpacing).floor();
double remainder = constraint.maxWidth.remainder(widthWithSpacing);
extraWidth = remainder / items;
}
return Wrap(
Expand Down
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: flexible_wrap
description: "FlexibleWrap is a Flutter widget designed to provide a flexible and customizable way to arrange widgets in a wrap layout"
version: 0.0.2
version: 0.0.3
homepage: https://github.com/bixat/flexible_wrap

environment:
Expand Down

0 comments on commit ed0cac8

Please sign in to comment.