yubioath-flutter/lib/widgets/flex_box.dart

110 lines
2.9 KiB
Dart
Raw Normal View History

2024-06-13 10:29:30 +03:00
import 'package:flutter/material.dart';
enum FlexLayout { grid, list }
class FlexBox<T> extends StatelessWidget {
final List<T> items;
final Widget Function(T value) itemBuilder;
2024-06-17 12:16:14 +03:00
final int Function(double width)? getItemsPerRow;
2024-06-13 10:29:30 +03:00
final FlexLayout layout;
2024-06-14 14:04:55 +03:00
final double? runSpacing;
2024-06-13 10:29:30 +03:00
const FlexBox({
super.key,
required this.items,
required this.itemBuilder,
2024-06-17 12:16:14 +03:00
this.getItemsPerRow,
2024-06-13 10:29:30 +03:00
this.layout = FlexLayout.list,
2024-06-14 14:04:55 +03:00
this.runSpacing,
2024-06-13 10:29:30 +03:00
});
2024-06-17 12:16:14 +03:00
int _getItemsPerRow(double width) {
2024-06-13 10:29:30 +03:00
int itemsPerRow = 1;
if (layout == FlexLayout.grid) {
if (width <= 420) {
// single column
itemsPerRow = 1;
} else if (width <= 620) {
// 2 column
itemsPerRow = 2;
} else if (width < 860) {
// 3 column
itemsPerRow = 3;
} else if (width < 1200) {
// 4 column
itemsPerRow = 4;
2024-06-14 11:23:18 +03:00
} else if (width < 1500) {
2024-06-13 10:29:30 +03:00
// 5 column
itemsPerRow = 5;
2024-06-14 11:23:18 +03:00
} else if (width < 1800) {
2024-06-17 12:16:14 +03:00
// 6 column
2024-06-13 10:29:30 +03:00
itemsPerRow = 6;
2024-06-14 11:23:18 +03:00
} else if (width < 2000) {
2024-06-17 12:16:14 +03:00
// 7 column
2024-06-14 11:23:18 +03:00
itemsPerRow = 7;
} else {
2024-06-17 12:16:14 +03:00
// 8 column
2024-06-14 11:23:18 +03:00
itemsPerRow = 8;
2024-06-13 10:29:30 +03:00
}
}
return itemsPerRow;
}
List<List<T>> getChunks(int itemsPerChunk) {
List<List<T>> chunks = [];
final numChunks = (items.length / itemsPerChunk).ceil();
for (int i = 0; i < numChunks; i++) {
final index = i * itemsPerChunk;
int endIndex = index + itemsPerChunk;
if (endIndex > items.length) {
endIndex = items.length;
}
chunks.add(items.sublist(index, endIndex));
}
return chunks;
}
@override
Widget build(BuildContext context) {
return LayoutBuilder(
builder: (context, constraints) {
final width = constraints.maxWidth;
2024-06-17 12:16:14 +03:00
final itemsPerRow = layout == FlexLayout.grid
? getItemsPerRow?.call(width) ?? _getItemsPerRow(width)
: 1;
2024-06-13 10:29:30 +03:00
final chunks = getChunks(itemsPerRow);
return Column(
children: [
for (final c in chunks) ...[
2024-06-14 14:04:55 +03:00
if (chunks.indexOf(c) > 0 &&
layout == FlexLayout.grid &&
runSpacing != null)
SizedBox(height: runSpacing),
2024-06-13 10:29:30 +03:00
Row(
children: [
for (final entry in c) ...[
Flexible(
child: itemBuilder(entry),
),
if (itemsPerRow != 1 && c.indexOf(entry) != c.length - 1)
const SizedBox(width: 8),
],
if (c.length < itemsPerRow) ...[
// Prevents resizing when an items is removed
SizedBox(width: 8 * (itemsPerRow - c.length).toDouble()),
Spacer(
flex: itemsPerRow - c.length,
)
]
],
),
]
],
);
},
);
}
}