utils/design_system/lib/src/fit_grid.dart
Krzysztof kuhy Rudnicki db1ce8c378 design_system 0.4.0: import material_ui
Flutter 3.50 deprecates package:flutter/material.dart for the
material_ui package; its ThemeData is a different type, so apps on
3.50 need the theme builders to return material_ui's.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-12 09:35:00 +01:00

200 lines
6.0 KiB
Dart

import 'dart:math' as math;
import 'package:design_system/src/tokens.dart';
import 'package:material_ui/material_ui.dart';
/// A grid of square cells that always fits its box, so it never scrolls
/// vertically.
///
/// Rows per page come from the height the parent gives it; the overflow moves
/// to further pages in a horizontal [PageView], marked by dots once there is
/// more than one (a "n / N" counter past 12 pages). [itemBuilder] gets the
/// global item index and must fill the cell it is given: a square of up to
/// column width plus [labelHeight], the rows stretching to use up the
/// leftover height.
class FitGrid extends StatefulWidget {
/// Creates the grid.
const FitGrid({
required this.itemCount,
required this.itemBuilder,
this.columns = 3,
this.labelHeight = 0,
this.minSquare = 1,
this.spacing = AppSpacing.sm,
this.padding = const EdgeInsets.symmetric(horizontal: AppSpacing.md),
super.key,
});
/// Number of items across all pages.
final int itemCount;
/// Builds item `index`, counted over all pages.
final IndexedWidgetBuilder itemBuilder;
/// Cells per row.
final int columns;
/// Height reserved under each square, for a caption.
final double labelHeight;
/// Least square side, as a fraction of the column width. Below 1 the
/// builder may be given a cell too short for a full-width square, and must
/// shrink its square to the cell height minus [labelHeight].
final double minSquare;
/// Gap between cells, both ways.
final double spacing;
/// Space around the cells.
final EdgeInsets padding;
@override
State<FitGrid> createState() => _FitGridState();
}
const double _dotSize = AppSpacing.sm;
const double _dotsHeight = AppSpacing.md + AppSpacing.xs;
// Above this many pages a row of dots outgrows a phone width, so the
// indicator becomes a "n / N" counter instead.
const int _maxDots = 12;
class _FitGridState extends State<FitGrid> {
final _controller = PageController();
var _page = 0;
@override
void dispose() {
_controller.dispose();
super.dispose();
}
int _rowsFor(double height, double cellHeight) => math.max(
1,
((height + widget.spacing) / (cellHeight + widget.spacing)).floor(),
);
int _pagesFor(int perPage) =>
math.max(1, (widget.itemCount / perPage).ceil());
@override
Widget build(BuildContext context) => LayoutBuilder(
builder: (context, box) {
final w = widget;
final inner = box.maxWidth - w.padding.horizontal;
final cellW = (inner - w.spacing * (w.columns - 1)) / w.columns;
final height = box.maxHeight - w.padding.vertical;
final minCellH = cellW * w.minSquare + w.labelHeight;
var rows = _rowsFor(height, minCellH);
final pages = _pagesFor(rows * w.columns);
if (pages > 1) {
rows = _rowsFor(height - _dotsHeight, minCellH);
}
final perPage = rows * w.columns;
final count = _pagesFor(perPage);
final pageHeight = height - (count > 1 ? _dotsHeight : 0);
final cellH = math.max(
minCellH,
(pageHeight + w.spacing) / rows - w.spacing,
);
return Column(
children: [
Expanded(
child: PageView.builder(
controller: _controller,
itemCount: count,
onPageChanged: (p) => setState(() => _page = p),
itemBuilder: (context, p) => Padding(
padding: w.padding,
child: FittedBox(
fit: BoxFit.scaleDown,
alignment: Alignment.topCenter,
child: _pageOf(
context,
p * perPage,
rows,
Size(cellW, cellH),
),
),
),
),
),
if (count > 1)
_Dots(count: count, current: math.min(_page, count - 1)),
],
);
},
);
Widget _pageOf(BuildContext context, int first, int rows, Size cell) {
final w = widget;
return Column(
mainAxisSize: MainAxisSize.min,
children: [
for (var r = 0; r < rows; r++) ...[
if (r > 0) SizedBox(height: w.spacing),
Row(
children: [
for (var c = 0; c < w.columns; c++) ...[
if (c > 0) SizedBox(width: w.spacing),
SizedBox.fromSize(
size: cell,
child: first + r * w.columns + c < w.itemCount
? w.itemBuilder(context, first + r * w.columns + c)
: null,
),
],
],
),
],
],
);
}
}
class _Dots extends StatelessWidget {
const _Dots({required this.count, required this.current});
final int count;
final int current;
@override
Widget build(BuildContext context) {
final scheme = Theme.of(context).colorScheme;
return ExcludeSemantics(
child: SizedBox(
height: _dotsHeight,
child: count > _maxDots
? Center(
child: Text(
'${current + 1} / $count',
key: const ValueKey<String>('fit-grid-page-counter'),
style: Theme.of(context).textTheme.labelSmall,
textScaler: TextScaler.noScaling,
),
)
: Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
for (var i = 0; i < count; i++)
Container(
key: ValueKey<String>('fit-grid-dot-$i'),
width: _dotSize,
height: _dotSize,
margin: const EdgeInsets.symmetric(
horizontal: AppSpacing.xs,
),
decoration: BoxDecoration(
shape: BoxShape.circle,
color: i == current
? scheme.primary
: scheme.outlineVariant,
),
),
],
),
),
);
}
}