diff options
Diffstat (limited to 'lib/widgets/common/grid/sections')
16 files changed, 1583 insertions, 0 deletions
diff --git a/lib/widgets/common/grid/sections/fixed/list_layout.dart b/lib/widgets/common/grid/sections/fixed/list_layout.dart new file mode 100644 index 0000000..162fcc8 --- /dev/null +++ b/lib/widgets/common/grid/sections/fixed/list_layout.dart @@ -0,0 +1,62 @@ +import 'dart:math'; + +import 'package:aves/model/source/section_keys.dart'; +import 'package:aves/widgets/common/grid/sections/list_layout.dart'; +import 'package:collection/collection.dart'; +import 'package:flutter/material.dart'; +import 'package:flutter/rendering.dart'; + +class FixedExtentSectionedListLayout<T> extends SectionedListLayout<T> { + final int columnCount; + final double tileWidth, tileHeight; + + const FixedExtentSectionedListLayout({ + required super.sections, + required super.showHeaders, + required this.columnCount, + required this.tileWidth, + required this.tileHeight, + required super.spacing, + required super.horizontalPadding, + required super.sectionLayouts, + }); + + @override + Rect? getTileRect(T item) { + final MapEntry<SectionKey?, List<T>>? section = sections.entries.firstWhereOrNull((kv) => kv.value.contains(item)); + if (section == null) return null; + + final sectionKey = section.key; + final sectionLayout = sectionLayouts.firstWhereOrNull((sl) => sl.sectionKey == sectionKey); + if (sectionLayout == null) return null; + + final sectionItemIndex = section.value.indexOf(item); + final column = sectionItemIndex % columnCount; + final row = (sectionItemIndex / columnCount).floor(); + final listIndex = sectionLayout.firstIndex + 1 + row; + + final left = horizontalPadding + tileWidth * column + spacing * (column - 1); + final top = sectionLayout.indexToLayoutOffset(listIndex); + return Rect.fromLTWH(left, top, tileWidth, tileHeight); + } + + @override + T? getItemAt(Offset position) { + var dy = position.dy; + final sectionLayout = getSectionAt(dy); + if (sectionLayout == null) return null; + + final section = sections[sectionLayout.sectionKey]; + if (section == null) return null; + + dy -= sectionLayout.minOffset + sectionLayout.headerExtent; + if (dy < 0) return null; + + final row = dy ~/ (tileHeight + spacing); + final column = max(0, position.dx - horizontalPadding) ~/ (tileWidth + spacing); + final index = row * columnCount + column; + if (index >= section.length) return null; + + return section[index]; + } +} diff --git a/lib/widgets/common/grid/sections/fixed/row.dart b/lib/widgets/common/grid/sections/fixed/row.dart new file mode 100644 index 0000000..f0f9e1c --- /dev/null +++ b/lib/widgets/common/grid/sections/fixed/row.dart @@ -0,0 +1,162 @@ +import 'package:flutter/material.dart'; +import 'package:flutter/rendering.dart'; + +class FixedExtentGridRow extends MultiChildRenderObjectWidget { + final double width, height, spacing; + final TextDirection textDirection; + + const FixedExtentGridRow({ + super.key, + required this.width, + required this.height, + required this.spacing, + required this.textDirection, + required super.children, + }); + + @override + RenderObject createRenderObject(BuildContext context) { + return RenderFixedExtentGridRow( + width: width, + height: height, + spacing: spacing, + textDirection: textDirection, + ); + } + + @override + void updateRenderObject(BuildContext context, RenderFixedExtentGridRow renderObject) { + renderObject.width = width; + renderObject.height = height; + renderObject.spacing = spacing; + renderObject.textDirection = textDirection; + } + + @override + void debugFillProperties(DiagnosticPropertiesBuilder properties) { + super.debugFillProperties(properties); + properties.add(DoubleProperty('width', width)); + properties.add(DoubleProperty('height', height)); + properties.add(DoubleProperty('spacing', spacing)); + properties.add(EnumProperty<TextDirection>('textDirection', textDirection)); + } +} + +class _GridRowParentData extends ContainerBoxParentData<RenderBox> {} + +class RenderFixedExtentGridRow extends RenderBox with ContainerRenderObjectMixin<RenderBox, _GridRowParentData>, RenderBoxContainerDefaultsMixin<RenderBox, _GridRowParentData> { + RenderFixedExtentGridRow({ + List<RenderBox>? children, + required double width, + required double height, + required double spacing, + required TextDirection textDirection, + }) : _width = width, + _height = height, + _spacing = spacing, + _textDirection = textDirection { + addAll(children); + } + + double get width => _width; + double _width; + + set width(double value) { + if (_width == value) return; + _width = value; + markNeedsLayout(); + } + + double get height => _height; + double _height; + + set height(double value) { + if (_height == value) return; + _height = value; + markNeedsLayout(); + } + + double get spacing => _spacing; + double _spacing; + + set spacing(double value) { + if (_spacing == value) return; + _spacing = value; + markNeedsLayout(); + } + + TextDirection get textDirection => _textDirection; + TextDirection _textDirection; + + set textDirection(TextDirection value) { + if (_textDirection == value) return; + _textDirection = value; + markNeedsLayout(); + } + + @override + void setupParentData(RenderBox child) { + if (child.parentData is! _GridRowParentData) { + child.parentData = _GridRowParentData(); + } + } + + double get intrinsicWidth => width * childCount + spacing * (childCount - 1); + + @override + double computeMinIntrinsicWidth(double height) => intrinsicWidth; + + @override + double computeMaxIntrinsicWidth(double height) => intrinsicWidth; + + @override + double computeMinIntrinsicHeight(double width) => height; + + @override + double computeMaxIntrinsicHeight(double width) => height; + + @override + void performLayout() { + var child = firstChild; + if (child == null) { + size = constraints.smallest; + return; + } + size = Size(constraints.maxWidth, height); + final childConstraints = BoxConstraints.tight(Size(width, height)); + final flipMainAxis = textDirection == TextDirection.rtl; + var offset = Offset(flipMainAxis ? size.width - width : 0, 0); + final dx = (flipMainAxis ? -1 : 1) * (width + spacing); + while (child != null) { + child.layout(childConstraints, parentUsesSize: false); + final childParentData = child.parentData! as _GridRowParentData; + childParentData.offset = offset; + offset += Offset(dx, 0); + child = childParentData.nextSibling; + } + } + + @override + double? computeDistanceToActualBaseline(TextBaseline baseline) { + return defaultComputeDistanceToHighestActualBaseline(baseline); + } + + @override + bool hitTestChildren(BoxHitTestResult result, {required Offset position}) { + return defaultHitTestChildren(result, position: position); + } + + @override + void paint(PaintingContext context, Offset offset) { + defaultPaint(context, offset); + } + + @override + void debugFillProperties(DiagnosticPropertiesBuilder properties) { + super.debugFillProperties(properties); + properties.add(DoubleProperty('width', width)); + properties.add(DoubleProperty('height', height)); + properties.add(DoubleProperty('spacing', spacing)); + properties.add(EnumProperty<TextDirection>('textDirection', textDirection)); + } +} diff --git a/lib/widgets/common/grid/sections/fixed/scale_grid.dart b/lib/widgets/common/grid/sections/fixed/scale_grid.dart new file mode 100644 index 0000000..c31b6ea --- /dev/null +++ b/lib/widgets/common/grid/sections/fixed/scale_grid.dart @@ -0,0 +1,111 @@ +import 'dart:ui' as ui; + +import 'package:aves_model/aves_model.dart'; +import 'package:flutter/material.dart'; + +class FixedExtentGridPainter extends CustomPainter { + final TileLayout tileLayout; + final Offset tileCenter; + final Size tileSize; + final double spacing, horizontalPadding, borderWidth; + final Radius borderRadius; + final Color color; + final TextDirection textDirection; + + const FixedExtentGridPainter({ + required this.tileLayout, + required this.tileCenter, + required this.tileSize, + required this.spacing, + required this.horizontalPadding, + required this.borderWidth, + required this.borderRadius, + required this.color, + required this.textDirection, + }); + + @override + void paint(Canvas canvas, Size size) { + late final Offset chipCenter; + late final Size chipSize; + late final int deltaColumn; + late final Shader strokeShader; + switch (tileLayout) { + case TileLayout.mosaic: + return; + case TileLayout.grid: + chipCenter = tileCenter; + chipSize = tileSize; + deltaColumn = 2; + strokeShader = ui.Gradient.radial( + tileCenter, + chipSize.shortestSide * 2, + [ + color, + Colors.transparent, + ], + [ + .8, + 1, + ], + ); + case TileLayout.list: + chipSize = Size.square(tileSize.shortestSide); + final chipCenterToEdge = chipSize.width / 2; + chipCenter = Offset(textDirection == TextDirection.rtl ? size.width - (chipCenterToEdge + horizontalPadding) : chipCenterToEdge + horizontalPadding, tileCenter.dy); + deltaColumn = 0; + strokeShader = ui.Gradient.linear( + tileCenter - Offset(0, chipSize.shortestSide * 3), + tileCenter + Offset(0, chipSize.shortestSide * 3), + [ + Colors.transparent, + color, + color, + Colors.transparent, + ], + [ + 0, + .2, + .8, + 1, + ], + ); + } + final strokePaint = Paint() + ..style = PaintingStyle.stroke + ..strokeWidth = borderWidth + ..shader = strokeShader; + final fillPaint = Paint() + ..style = PaintingStyle.fill + ..color = color.withValues(alpha: .25); + + final chipWidth = chipSize.width; + final chipHeight = chipSize.height; + + final deltaX = tileSize.width + spacing; + final deltaY = tileSize.height + spacing; + for (var i = -deltaColumn; i <= deltaColumn; i++) { + final dx = deltaX * i; + for (var j = -2; j <= 2; j++) { + if (i == 0 && j == 0) continue; + final dy = deltaY * j; + final rect = RRect.fromRectAndRadius( + Rect.fromCenter( + center: chipCenter + Offset(dx, dy), + width: chipWidth - borderWidth, + height: chipHeight - borderWidth, + ), + borderRadius, + ); + + if ((i.abs() == 1 && j == 0) || (j.abs() == 1 && i == 0)) { + canvas.drawRRect(rect, fillPaint); + } + canvas.drawRRect(rect, strokePaint); + } + } + } + + @override + bool shouldRepaint(covariant CustomPainter oldDelegate) => true; +} diff --git a/lib/widgets/common/grid/sections/fixed/scale_overlay.dart b/lib/widgets/common/grid/sections/fixed/scale_overlay.dart new file mode 100644 index 0000000..56be3a9 --- /dev/null +++ b/lib/widgets/common/grid/sections/fixed/scale_overlay.dart @@ -0,0 +1,136 @@ +import 'package:aves/theme/durations.dart'; +import 'package:aves/widgets/common/extensions/build_context.dart'; +import 'package:aves/widgets/common/extensions/theme.dart'; +import 'package:aves/widgets/common/providers/media_query_data_provider.dart'; +import 'package:aves_model/aves_model.dart'; +import 'package:aves_utils/aves_utils.dart'; +import 'package:flutter/material.dart'; + +class FixedExtentScaleOverlay extends StatelessWidget { + final TileLayout tileLayout; + final Offset tileCenter; + final double xMin, xMax; + final ValueNotifier<Size> scaledSizeNotifier; + final Widget Function(Offset center, Size tileSize, Widget child) gridBuilder; + final Widget Function(Size scaledTileSize) builder; + + FixedExtentScaleOverlay({ + super.key, + required this.tileLayout, + required this.tileCenter, + required Rect contentRect, + required this.scaledSizeNotifier, + required this.gridBuilder, + required this.builder, + }) : xMin = contentRect.left, + xMax = contentRect.right; + + @override + Widget build(BuildContext context) { + return MediaQueryDataProvider( + child: IgnorePointer( + child: _OverlayBackground( + gradientCenter: tileLayout == TileLayout.grid ? tileCenter : Offset(context.isRtl ? xMax : xMin, tileCenter.dy), + child: ValueListenableBuilder<Size>( + valueListenable: scaledSizeNotifier, + builder: (context, scaledSize, child) { + final width = scaledSize.width; + final height = scaledSize.height; + // keep scaled thumbnail within the screen + var dx = .0; + if (tileCenter.dx - width / 2 < xMin) { + dx = xMin - (tileCenter.dx - width / 2); + } else if (tileCenter.dx + width / 2 > xMax) { + dx = xMax - (tileCenter.dx + width / 2); + } + final clampedCenter = tileCenter.translate(dx, 0); + + var child = builder(scaledSize); + child = Stack( + children: [ + Positioned( + left: clampedCenter.dx - width / 2, + top: clampedCenter.dy - height / 2, + child: DefaultTextStyle( + style: const TextStyle(), + child: child, + ), + ), + ], + ); + child = gridBuilder(clampedCenter, scaledSize, child); + return child; + }, + ), + ), + ), + ); + } +} + +class _OverlayBackground extends StatefulWidget { + final Offset gradientCenter; + final Widget child; + + const _OverlayBackground({ + required this.gradientCenter, + required this.child, + }); + + @override + State<_OverlayBackground> createState() => _OverlayBackgroundState(); +} + +class _OverlayBackgroundState extends State<_OverlayBackground> { + bool _initialized = false; + + @override + void initState() { + super.initState(); + WidgetsBinding.instance.addPostFrameCallback((_) => setState(() => _initialized = true)); + } + + @override + Widget build(BuildContext context) { + return AnimatedContainer( + decoration: _buildBackgroundDecoration(context), + duration: ADurations.scalingGridBackgroundAnimation, + child: widget.child, + ); + } + + BoxDecoration _buildBackgroundDecoration(BuildContext context) { + final isDark = Theme.of(context).isDark; + final gradientCenter = widget.gradientCenter; + return _initialized + ? BoxDecoration( + gradient: RadialGradient( + center: FractionalOffset.fromOffsetAndSize(gradientCenter, MediaQuery.sizeOf(context)), + radius: 1, + colors: isDark + ? const [ + Colors.black, + Colors.black54, + ] + : const [ + Colors.white, + Colors.white38, + ], + ), + ) + : BoxDecoration( + // provide dummy gradient to lerp to the other one during animation + gradient: RadialGradient( + colors: isDark + ? const [ + ColorUtils.transparentBlack, + ColorUtils.transparentBlack, + ] + : const [ + ColorUtils.transparentWhite, + ColorUtils.transparentWhite, + ], + ), + ); + } +} diff --git a/lib/widgets/common/grid/sections/fixed/section_layout.dart b/lib/widgets/common/grid/sections/fixed/section_layout.dart new file mode 100644 index 0000000..7cdfbc9 --- /dev/null +++ b/lib/widgets/common/grid/sections/fixed/section_layout.dart @@ -0,0 +1,41 @@ +import 'package:aves/widgets/common/grid/sections/section_layout.dart'; + +class FixedExtentSectionLayout extends SectionLayout { + final double tileHeight, mainAxisStride; + + @override + List<Object?> get props => [sectionKey, firstIndex, lastIndex, minOffset, maxOffset, headerExtent, tileHeight, spacing]; + + const FixedExtentSectionLayout({ + required super.sectionKey, + required super.firstIndex, + required super.lastIndex, + required super.minOffset, + required super.maxOffset, + required super.headerExtent, + required this.tileHeight, + required super.spacing, + required super.builder, + }) : mainAxisStride = tileHeight + spacing; + + @override + double indexToLayoutOffset(int index) { + index -= bodyFirstIndex; + if (index < 0) return minOffset; + return bodyMinOffset + index * mainAxisStride; + } + + @override + int getMinChildIndexForScrollOffset(double scrollOffset) { + scrollOffset -= bodyMinOffset; + if (mainAxisStride == 0 || !scrollOffset.isFinite || scrollOffset < 0) return firstIndex; + return bodyFirstIndex + scrollOffset ~/ mainAxisStride; + } + + @override + int getMaxChildIndexForScrollOffset(double scrollOffset) { + scrollOffset -= bodyMinOffset; + if (mainAxisStride == 0 || !scrollOffset.isFinite || scrollOffset < 0) return firstIndex; + return bodyFirstIndex + (scrollOffset / mainAxisStride).ceil() - 1; + } +} diff --git a/lib/widgets/common/grid/sections/fixed/section_layout_builder.dart b/lib/widgets/common/grid/sections/fixed/section_layout_builder.dart new file mode 100644 index 0000000..e450f23 --- /dev/null +++ b/lib/widgets/common/grid/sections/fixed/section_layout_builder.dart @@ -0,0 +1,109 @@ +import 'package:aves/model/source/section_keys.dart'; +import 'package:aves/widgets/common/grid/sections/fixed/list_layout.dart'; +import 'package:aves/widgets/common/grid/sections/fixed/row.dart'; +import 'package:aves/widgets/common/grid/sections/fixed/section_layout.dart'; +import 'package:aves/widgets/common/grid/sections/list_layout.dart'; +import 'package:aves/widgets/common/grid/sections/section_layout.dart'; +import 'package:aves/widgets/common/grid/sections/section_layout_builder.dart'; +import 'package:flutter/material.dart'; + +class FixedExtentSectionLayoutBuilder<T> extends SectionLayoutBuilder<T> { + int _currentIndex = 0; + double _currentOffset = 0; + final List<Size> _itemSizes; + + FixedExtentSectionLayoutBuilder({ + required super.sections, + required super.showHeaders, + required super.getHeaderExtent, + required super.buildHeader, + required super.scrollableWidth, + required super.tileLayout, + required super.columnCount, + required super.spacing, + required super.horizontalPadding, + required super.tileWidth, + required super.tileHeight, + required super.tileBuilder, + required super.tileAnimationDelay, + }) : _itemSizes = List.generate(columnCount, (index) => Size(tileWidth, tileHeight)); + + @override + SectionedListLayout<T> updateLayouts(BuildContext context) { + final sectionLayouts = sections.keys + .map((sectionKey) => buildSectionLayout( + headerExtent: showHeaders ? getHeaderExtent(context, sectionKey) : 0.0, + sectionKey: sectionKey, + section: sections[sectionKey]!, + animate: animate, + )) + .toList(); + + return FixedExtentSectionedListLayout<T>( + sections: sections, + showHeaders: showHeaders, + columnCount: columnCount, + tileWidth: tileWidth, + tileHeight: tileHeight, + spacing: spacing, + horizontalPadding: horizontalPadding, + sectionLayouts: sectionLayouts, + ); + } + + @override + SectionLayout buildSectionLayout({ + required double headerExtent, + required SectionKey sectionKey, + required List<T> section, + required bool animate, + }) { + final sectionItemCount = section.length; + final rowCount = (sectionItemCount / columnCount).ceil(); + final sectionChildCount = 1 + rowCount; + + final sectionFirstIndex = _currentIndex; + _currentIndex += sectionChildCount; + final sectionLastIndex = _currentIndex - 1; + + final sectionMinOffset = _currentOffset; + _currentOffset += headerExtent + tileHeight * rowCount + spacing * (rowCount - 1); + final sectionMaxOffset = _currentOffset; + + return FixedExtentSectionLayout( + sectionKey: sectionKey, + firstIndex: sectionFirstIndex, + lastIndex: sectionLastIndex, + minOffset: sectionMinOffset, + maxOffset: sectionMaxOffset, + headerExtent: headerExtent, + tileHeight: tileHeight, + spacing: spacing, + builder: (context, listIndex) { + final textDirection = Directionality.of(context); + final sectionChildIndex = listIndex - sectionFirstIndex; + return buildSectionWidget( + context: context, + section: section, + sectionGridIndex: listIndex * columnCount, + sectionChildIndex: sectionChildIndex, + itemIndexRange: () => ( + (sectionChildIndex - 1) * columnCount, + sectionChildIndex * columnCount, + ), + sectionKey: sectionKey, + headerExtent: headerExtent, + itemSizes: _itemSizes, + animate: animate, + buildGridRow: (children) => FixedExtentGridRow( + width: tileWidth, + height: tileHeight, + spacing: spacing, + textDirection: textDirection, + children: children, + ), + ); + }, + ); + } +} diff --git a/lib/widgets/common/grid/sections/list_layout.dart b/lib/widgets/common/grid/sections/list_layout.dart new file mode 100644 index 0000000..06e9f05 --- /dev/null +++ b/lib/widgets/common/grid/sections/list_layout.dart @@ -0,0 +1,28 @@ +import 'package:aves/model/source/section_keys.dart'; +import 'package:aves/widgets/common/grid/sections/section_layout.dart'; +import 'package:collection/collection.dart'; +import 'package:flutter/material.dart'; +import 'package:flutter/rendering.dart'; + +abstract class SectionedListLayout<T> { + final Map<SectionKey, List<T>> sections; + final bool showHeaders; + final double spacing, horizontalPadding; + final List<SectionLayout> sectionLayouts; + + const SectionedListLayout({ + required this.sections, + required this.showHeaders, + required this.spacing, + required this.horizontalPadding, + required this.sectionLayouts, + }); + + // returns tile rectangle in layout space, i.e. x=0 is start + Rect? getTileRect(T item); + + SectionLayout? getSectionAt(double offsetY) => sectionLayouts.firstWhereOrNull((sl) => offsetY < sl.maxOffset); + + // `position` in layout space, i.e. x=0 is start + T? getItemAt(Offset position); +} diff --git a/lib/widgets/common/grid/sections/mosaic/list_layout.dart b/lib/widgets/common/grid/sections/mosaic/list_layout.dart new file mode 100644 index 0000000..4590bf9 --- /dev/null +++ b/lib/widgets/common/grid/sections/mosaic/list_layout.dart @@ -0,0 +1,78 @@ +import 'package:aves/model/source/section_keys.dart'; +import 'package:aves/widgets/common/grid/sections/list_layout.dart'; +import 'package:aves/widgets/common/grid/sections/mosaic/section_layout.dart'; +import 'package:aves/widgets/common/grid/sections/section_layout.dart'; +import 'package:collection/collection.dart'; +import 'package:flutter/material.dart'; +import 'package:flutter/rendering.dart'; + +class MosaicSectionedListLayout<T> extends SectionedListLayout<T> { + const MosaicSectionedListLayout({ + required super.sections, + required super.showHeaders, + required super.spacing, + required super.horizontalPadding, + required super.sectionLayouts, + }); + + List<MosaicRowLayout> _rowsFor(SectionLayout sectionLayout) => (sectionLayout as MosaicSectionLayout).rows; + + @override + Rect? getTileRect(T item) { + final MapEntry<SectionKey?, List<T>>? section = sections.entries.firstWhereOrNull((kv) => kv.value.contains(item)); + if (section == null) return null; + + final sectionKey = section.key; + final sectionLayout = sectionLayouts.firstWhereOrNull((sl) => sl.sectionKey == sectionKey); + if (sectionLayout == null) return null; + + final sectionItemIndex = section.value.indexOf(item); + final row = _rowsFor(sectionLayout).firstWhereOrNull((row) => sectionItemIndex <= row.lastIndex); + if (row == null) return null; + + final rowItemIndex = sectionItemIndex - row.firstIndex; + final tileWidth = row.itemWidths[rowItemIndex]; + final tileHeight = row.height - spacing; + + var left = horizontalPadding; + row.itemWidths.forEachIndexedWhile((i, width) { + if (i == rowItemIndex) return true; + + left += width + spacing; + return false; + }); + final listIndex = sectionLayout.firstIndex + 1 + _rowsFor(sectionLayout).indexOf(row); + + final top = sectionLayout.indexToLayoutOffset(listIndex); + return Rect.fromLTWH(left, top, tileWidth, tileHeight); + } + + @override + T? getItemAt(Offset position) { + var dy = position.dy; + final sectionLayout = getSectionAt(dy); + if (sectionLayout == null) return null; + + final section = sections[sectionLayout.sectionKey]; + if (section == null) return null; + + dy -= sectionLayout.minOffset + sectionLayout.headerExtent; + if (dy < 0) return null; + + final row = _rowsFor(sectionLayout).firstWhereOrNull((v) => dy < v.maxOffset); + if (row == null) return null; + + var dx = position.dx - horizontalPadding; + var index = -1; + row.itemWidths.forEachIndexedWhile((i, width) { + dx -= width + spacing; + if (dx > 0) return true; + + index = row.firstIndex + i; + return false; + }); + + if (index < 0 || index >= section.length) return null; + return section[index]; + } +} diff --git a/lib/widgets/common/grid/sections/mosaic/row.dart b/lib/widgets/common/grid/sections/mosaic/row.dart new file mode 100644 index 0000000..1198405 --- /dev/null +++ b/lib/widgets/common/grid/sections/mosaic/row.dart @@ -0,0 +1,159 @@ +import 'package:aves/widgets/common/grid/sections/mosaic/section_layout.dart'; +import 'package:collection/collection.dart'; +import 'package:flutter/material.dart'; +import 'package:flutter/rendering.dart'; + +class MosaicGridRow extends MultiChildRenderObjectWidget { + final MosaicRowLayout rowLayout; + final double spacing; + final TextDirection textDirection; + + const MosaicGridRow({ + super.key, + required this.rowLayout, + required this.spacing, + required this.textDirection, + required super.children, + }); + + @override + RenderObject createRenderObject(BuildContext context) { + return RenderMosaicGridRow( + rowLayout: rowLayout, + spacing: spacing, + textDirection: textDirection, + ); + } + + @override + void updateRenderObject(BuildContext context, RenderMosaicGridRow renderObject) { + renderObject.rowLayout = rowLayout; + renderObject.spacing = spacing; + renderObject.textDirection = textDirection; + } + + @override + void debugFillProperties(DiagnosticPropertiesBuilder properties) { + super.debugFillProperties(properties); + properties.add(DiagnosticsProperty<MosaicRowLayout>('rowLayout', rowLayout)); + properties.add(DoubleProperty('spacing', spacing)); + properties.add(EnumProperty<TextDirection>('textDirection', textDirection)); + } +} + +class _GridRowParentData extends ContainerBoxParentData<RenderBox> {} + +class RenderMosaicGridRow extends RenderBox with ContainerRenderObjectMixin<RenderBox, _GridRowParentData>, RenderBoxContainerDefaultsMixin<RenderBox, _GridRowParentData> { + RenderMosaicGridRow({ + List<RenderBox>? children, + required MosaicRowLayout rowLayout, + required double spacing, + required TextDirection textDirection, + }) : _rowLayout = rowLayout, + _spacing = spacing, + _textDirection = textDirection { + addAll(children); + } + + MosaicRowLayout get rowLayout => _rowLayout; + MosaicRowLayout _rowLayout; + + set rowLayout(MosaicRowLayout value) { + if (_rowLayout == value) return; + _rowLayout = value; + markNeedsLayout(); + } + + double get spacing => _spacing; + double _spacing; + + set spacing(double value) { + if (_spacing == value) return; + _spacing = value; + markNeedsLayout(); + } + + TextDirection get textDirection => _textDirection; + TextDirection _textDirection; + + set textDirection(TextDirection value) { + if (_textDirection == value) return; + _textDirection = value; + markNeedsLayout(); + } + + @override + void setupParentData(RenderBox child) { + if (child.parentData is! _GridRowParentData) { + child.parentData = _GridRowParentData(); + } + } + + double get intrinsicWidth => rowLayout.itemWidths.sum + spacing * (childCount - 1); + + @override + double computeMinIntrinsicWidth(double height) => intrinsicWidth; + + @override + double computeMaxIntrinsicWidth(double height) => intrinsicWidth; + + @override + double computeMinIntrinsicHeight(double width) => rowLayout.height; + + @override + double computeMaxIntrinsicHeight(double width) => rowLayout.height; + + @override + void performLayout() { + var child = firstChild; + if (child == null) { + size = constraints.smallest; + return; + } + final thumbnailHeight = rowLayout.height - spacing; + size = Size(constraints.maxWidth, rowLayout.height); + final flipMainAxis = textDirection == TextDirection.rtl; + var i = 0; + double offsetX = flipMainAxis ? size.width : 0; + while (child != null) { + final thumbnailWidth = rowLayout.itemWidths[i]; + final childConstraints = BoxConstraints.tight(Size(thumbnailWidth, thumbnailHeight)); + child.layout(childConstraints, parentUsesSize: false); + final childParentData = child.parentData! as _GridRowParentData; + if (flipMainAxis) { + offsetX -= thumbnailWidth; + } + childParentData.offset = Offset(offsetX, 0); + if (flipMainAxis) { + offsetX -= spacing; + } else { + offsetX += thumbnailWidth + spacing; + } + child = childParentData.nextSibling; + i++; + } + } + + @override + double? computeDistanceToActualBaseline(TextBaseline baseline) { + return defaultComputeDistanceToHighestActualBaseline(baseline); + } + + @override + bool hitTestChildren(BoxHitTestResult result, {required Offset position}) { + return defaultHitTestChildren(result, position: position); + } + + @override + void paint(PaintingContext context, Offset offset) { + defaultPaint(context, offset); + } + + @override + void debugFillProperties(DiagnosticPropertiesBuilder properties) { + super.debugFillProperties(properties); + properties.add(DiagnosticsProperty<MosaicRowLayout>('rowLayout', rowLayout)); + properties.add(DoubleProperty('spacing', spacing)); + properties.add(EnumProperty<TextDirection>('textDirection', textDirection)); + } +} diff --git a/lib/widgets/common/grid/sections/mosaic/scale_grid.dart b/lib/widgets/common/grid/sections/mosaic/scale_grid.dart new file mode 100644 index 0000000..820d5c7 --- /dev/null +++ b/lib/widgets/common/grid/sections/mosaic/scale_grid.dart @@ -0,0 +1,74 @@ +import 'package:aves/theme/durations.dart'; +import 'package:aves/widgets/common/grid/sections/mosaic/scale_overlay.dart'; +import 'package:aves/widgets/common/grid/sections/mosaic/section_layout_builder.dart'; +import 'package:flutter/material.dart'; + +class MosaicGrid extends StatelessWidget { + final Rect contentRect; + final Size tileSize; + final double spacing; + final MosaicItemBuilder builder; + + static const _itemRatios = <double>[ + 3 / 4, + 16 / 9, + 9 / 16, + 3 / 4, + 4 / 3, + 4 / 3, + 3 / 4, + 4 / 3, + 4 / 3, + 4 / 3, + ]; + + const MosaicGrid({ + super.key, + required this.contentRect, + required this.tileSize, + required this.spacing, + required this.builder, + }); + + @override + Widget build(BuildContext context) { + final children = <Widget>[]; + + final targetExtent = tileSize.width; + final rows = MosaicSectionLayoutBuilder.computeMosaicRows( + section: List.generate(5, (i) => _itemRatios).expand((v) => v).toList(), + availableWidthFor: (itemCount) => contentRect.width - (itemCount - 1) * spacing, + heightMax: targetExtent * MosaicSectionLayoutBuilder.heightMaxFactor, + targetExtent: targetExtent, + spacing: spacing, + bottom: tileSize.height - tileSize.width, + coverRatioResolver: (item) => item, + ); + + var i = 0; + var dy = contentRect.top; + rows.forEach((row) { + var dx = contentRect.left; + final itemHeight = row.height - spacing; + row.itemWidths.forEach((itemWidth) { + children.add( + AnimatedPositioned( + left: dx, + top: dy, + width: itemWidth, + height: itemHeight, + duration: ADurations.scalingGridPositionAnimation, + child: builder(i, targetExtent), + ), + ); + dx += itemWidth + spacing; + i++; + }); + dy += row.height; + }); + + return Stack( + children: children, + ); + } +} diff --git a/lib/widgets/common/grid/sections/mosaic/scale_overlay.dart b/lib/widgets/common/grid/sections/mosaic/scale_overlay.dart new file mode 100644 index 0000000..1e44634 --- /dev/null +++ b/lib/widgets/common/grid/sections/mosaic/scale_overlay.dart @@ -0,0 +1,117 @@ +import 'package:aves/theme/durations.dart'; +import 'package:aves/widgets/common/extensions/theme.dart'; +import 'package:aves/widgets/common/grid/sections/mosaic/scale_grid.dart'; +import 'package:aves/widgets/common/providers/media_query_data_provider.dart'; +import 'package:aves_utils/aves_utils.dart'; +import 'package:flutter/material.dart'; + +typedef MosaicItemBuilder = Widget Function(int index, double targetExtent); + +class MosaicScaleOverlay extends StatelessWidget { + final Rect contentRect; + final double spacing, extentMax; + final ValueNotifier<Size> scaledSizeNotifier; + final MosaicItemBuilder itemBuilder; + + const MosaicScaleOverlay({ + super.key, + required this.contentRect, + required this.spacing, + required this.extentMax, + required this.scaledSizeNotifier, + required this.itemBuilder, + }); + + @override + Widget build(BuildContext context) { + return MediaQueryDataProvider( + child: IgnorePointer( + child: _OverlayBackground( + child: ValueListenableBuilder<Size>( + valueListenable: scaledSizeNotifier, + builder: (context, scaledSize, child) { + final theme = Theme.of(context); + final colorScheme = theme.colorScheme; + Widget _buildBar(double width, Color color) => ClipRRect( + borderRadius: const BorderRadius.all(Radius.circular(4)), + child: Container( + color: color, + width: width, + height: 4, + ), + ); + return SafeArea( + left: false, + right: false, + bottom: false, + child: Column( + children: [ + Padding( + padding: const EdgeInsets.symmetric(vertical: 4), + child: Stack( + alignment: Alignment.center, + children: [ + _buildBar(extentMax, colorScheme.onSurface.withValues(alpha: .2)), + _buildBar(scaledSize.width, colorScheme.primary), + ], + ), + ), + Expanded( + child: MosaicGrid( + contentRect: contentRect, + tileSize: scaledSize, + spacing: spacing, + builder: itemBuilder, + ), + ), + ], + ), + ); + }, + ), + ), + ), + ); + } +} + +class _OverlayBackground extends StatefulWidget { + final Widget child; + + const _OverlayBackground({ + required this.child, + }); + + @override + State<_OverlayBackground> createState() => _OverlayBackgroundState(); +} + +class _OverlayBackgroundState extends State<_OverlayBackground> { + bool _initialized = false; + + @override + void initState() { + super.initState(); + WidgetsBinding.instance.addPostFrameCallback((_) => setState(() => _initialized = true)); + } + + @override + Widget build(BuildContext context) { + return AnimatedContainer( + decoration: _buildBackgroundDecoration(context), + duration: ADurations.scalingGridBackgroundAnimation, + child: widget.child, + ); + } + + BoxDecoration _buildBackgroundDecoration(BuildContext context) { + final isDark = Theme.of(context).isDark; + return _initialized + ? BoxDecoration( + color: isDark ? Colors.black87 : const Color(0xDDFFFFFF), + ) + : BoxDecoration( + color: isDark ? ColorUtils.transparentBlack : ColorUtils.transparentWhite, + ); + } +} diff --git a/lib/widgets/common/grid/sections/mosaic/section_layout.dart b/lib/widgets/common/grid/sections/mosaic/section_layout.dart new file mode 100644 index 0000000..0cba43a --- /dev/null +++ b/lib/widgets/common/grid/sections/mosaic/section_layout.dart @@ -0,0 +1,61 @@ +import 'package:aves/widgets/common/grid/sections/section_layout.dart'; +import 'package:collection/collection.dart'; +import 'package:equatable/equatable.dart'; + +class MosaicSectionLayout extends SectionLayout { + final List<MosaicRowLayout> rows; + + @override + List<Object?> get props => [sectionKey, firstIndex, lastIndex, minOffset, maxOffset, headerExtent, rows, spacing]; + + const MosaicSectionLayout({ + required super.sectionKey, + required super.firstIndex, + required super.lastIndex, + required super.minOffset, + required super.maxOffset, + required super.headerExtent, + required this.rows, + required super.spacing, + required super.builder, + }); + + @override + double indexToLayoutOffset(int index) { + index -= bodyFirstIndex; + if (index < 0) return minOffset; + return bodyMinOffset + (index < rows.length ? rows[index].minOffset : rows.lastOrNull?.maxOffset ?? 0); + } + + @override + int getMinChildIndexForScrollOffset(double scrollOffset) { + scrollOffset -= bodyMinOffset; + if (scrollOffset < 0) return firstIndex; + return bodyFirstIndex + rows.indexWhere((v) => scrollOffset < v.maxOffset); + } + + @override + int getMaxChildIndexForScrollOffset(double scrollOffset) { + scrollOffset -= bodyMinOffset; + if (scrollOffset < 0) return firstIndex; + final rowIndex = rows.indexWhere((v) => scrollOffset < v.maxOffset); + return bodyFirstIndex + (rowIndex == -1 ? rows.length - 1 : rowIndex); + } +} + +class MosaicRowLayout extends Equatable { + final int firstIndex, lastIndex; + final double minOffset, maxOffset, height; + final List<double> itemWidths; + + @override + List<Object?> get props => [firstIndex, lastIndex, minOffset, maxOffset, height, itemWidths]; + + const MosaicRowLayout({ + required this.firstIndex, + required this.lastIndex, + required this.minOffset, + required this.height, + required this.itemWidths, + }) : maxOffset = minOffset + height; +} diff --git a/lib/widgets/common/grid/sections/mosaic/section_layout_builder.dart b/lib/widgets/common/grid/sections/mosaic/section_layout_builder.dart new file mode 100644 index 0000000..26701c5 --- /dev/null +++ b/lib/widgets/common/grid/sections/mosaic/section_layout_builder.dart @@ -0,0 +1,198 @@ +import 'dart:math'; + +import 'package:aves/model/source/section_keys.dart'; +import 'package:aves/widgets/common/grid/sections/list_layout.dart'; +import 'package:aves/widgets/common/grid/sections/mosaic/list_layout.dart'; +import 'package:aves/widgets/common/grid/sections/mosaic/row.dart'; +import 'package:aves/widgets/common/grid/sections/mosaic/section_layout.dart'; +import 'package:aves/widgets/common/grid/sections/provider.dart'; +import 'package:aves/widgets/common/grid/sections/section_layout.dart'; +import 'package:aves/widgets/common/grid/sections/section_layout_builder.dart'; +import 'package:collection/collection.dart'; +import 'package:flutter/foundation.dart'; +import 'package:flutter/material.dart'; + +class MosaicSectionLayoutBuilder<T> extends SectionLayoutBuilder<T> { + int _currentIndex = 0; + double _currentOffset = 0; + late double Function(int itemCount) rowAvailableWidth; + late double rowHeightMax; + final CoverRatioResolver<T> coverRatioResolver; + + static const double heightMaxFactor = 2.4; + static const double minThumbnailAspectRatio = 9 / 32; + static const double maxThumbnailAspectRatio = 32 / 9; + + MosaicSectionLayoutBuilder({ + required super.sections, + required super.showHeaders, + required super.getHeaderExtent, + required super.buildHeader, + required super.scrollableWidth, + required super.tileLayout, + required super.columnCount, + required super.spacing, + required super.horizontalPadding, + required super.tileWidth, + required super.tileHeight, + required super.tileBuilder, + required Duration tileAnimationDelay, + required this.coverRatioResolver, + }) : super(tileAnimationDelay: Duration(milliseconds: (tileAnimationDelay.inMilliseconds / columnCount).ceil())) { + final rowWidth = scrollableWidth - horizontalPadding * 2; + rowAvailableWidth = (itemCount) => rowWidth - (itemCount - 1) * spacing; + rowHeightMax = tileWidth * heightMaxFactor; + } + + @override + SectionedListLayout<T> updateLayouts(BuildContext context) { + final sectionLayouts = sections.keys + .map((sectionKey) => buildSectionLayout( + headerExtent: showHeaders ? getHeaderExtent(context, sectionKey) : 0.0, + sectionKey: sectionKey, + section: sections[sectionKey]!, + animate: animate, + )) + .toList(); + + return MosaicSectionedListLayout<T>( + sections: sections, + showHeaders: showHeaders, + spacing: spacing, + horizontalPadding: horizontalPadding, + sectionLayouts: sectionLayouts, + ); + } + + @override + SectionLayout buildSectionLayout({ + required double headerExtent, + required SectionKey sectionKey, + required List<T> section, + required bool animate, + }) { + final rows = computeMosaicRows( + section: section, + availableWidthFor: rowAvailableWidth, + heightMax: rowHeightMax, + targetExtent: tileWidth, + spacing: spacing, + bottom: bottom, + coverRatioResolver: (item) => coverRatioResolver(item).clamp(minThumbnailAspectRatio, maxThumbnailAspectRatio), + ); + final rowCount = rows.length; + final sectionChildCount = 1 + rowCount; + + final sectionFirstIndex = _currentIndex; + _currentIndex += sectionChildCount; + final sectionLastIndex = _currentIndex - 1; + + final sectionMinOffset = _currentOffset; + _currentOffset += headerExtent + rows.map((v) => v.height).sum - spacing; + final sectionMaxOffset = _currentOffset; + + return MosaicSectionLayout( + sectionKey: sectionKey, + firstIndex: sectionFirstIndex, + lastIndex: sectionLastIndex, + minOffset: sectionMinOffset, + maxOffset: sectionMaxOffset, + headerExtent: headerExtent, + rows: rows, + spacing: spacing, + builder: (context, listIndex) { + final textDirection = Directionality.of(context); + final sectionChildIndex = listIndex - sectionFirstIndex; + final isHeader = sectionChildIndex == 0; + final row = isHeader ? rows.first : rows[sectionChildIndex - 1]; + final sectionGridIndex = isHeader ? sectionFirstIndex * columnCount : (sectionChildIndex + 1) * columnCount + row.firstIndex; + return buildSectionWidget( + context: context, + section: section, + sectionGridIndex: sectionGridIndex, + sectionChildIndex: sectionChildIndex, + itemIndexRange: () => isHeader ? const (0, 0) : (row.firstIndex, row.lastIndex + 1), + sectionKey: sectionKey, + headerExtent: headerExtent, + itemSizes: row.itemWidths.map((v) => Size(v, row.height)).toList(), + animate: animate, + buildGridRow: (children) { + return isHeader + ? const SizedBox() + : MosaicGridRow( + rowLayout: row, + spacing: spacing, + textDirection: textDirection, + children: children, + ); + }, + ); + }, + ); + } + + static List<MosaicRowLayout> computeMosaicRows<T>({ + required List<T> section, + required double Function(int itemCount) availableWidthFor, + required double heightMax, + required double targetExtent, + required double spacing, + required double bottom, + required CoverRatioResolver<T> coverRatioResolver, + }) { + final rows = <MosaicRowLayout>[]; + final items = <T>[]; + double ratioSum = 0, ratioMin = double.infinity; + int firstIndex = 0; + double minOffset = 0; + + void addRow(int i, {required bool complete}) { + if (items.isEmpty) return; + + final availableWidth = availableWidthFor(items.length); + var height = availableWidth / ratioSum + spacing; + if (height > heightMax + precisionErrorTolerance) { + if (!complete) { + ratioSum = availableWidth / (heightMax - spacing); + addRow(i, complete: complete); + } + return; + } + + height += bottom; + rows.add(MosaicRowLayout( + firstIndex: firstIndex, + lastIndex: i - 1, + minOffset: minOffset, + height: height, + itemWidths: items.map((item) => availableWidth * coverRatioResolver(item) / ratioSum).toList(), + )); + firstIndex = i; + minOffset += height; + ratioMin = double.infinity; + ratioSum = 0; + items.clear(); + } + + section.forEachIndexed((i, item) { + final ratio = coverRatioResolver(item); + final nextAvailableWidth = availableWidthFor(items.length + 1); + final nextRatioSum = ratio + ratioSum; + final nextItemMinWidth = nextAvailableWidth * min(ratio, ratioMin) / nextRatioSum; + final nextHeight = nextAvailableWidth / nextRatioSum + spacing; + if (nextItemMinWidth < targetExtent || nextHeight < targetExtent) { + // add row when appending the next item would make other items too small + addRow(i, complete: true); + } + items.add(item); + ratioMin = min(ratio, ratioMin); + ratioSum += ratio; + }); + if (items.isNotEmpty) { + // add last row, possibly incomplete + addRow(section.length, complete: false); + } + + return rows; + } +} diff --git a/lib/widgets/common/grid/sections/provider.dart b/lib/widgets/common/grid/sections/provider.dart new file mode 100644 index 0000000..d8b2eb9 --- /dev/null +++ b/lib/widgets/common/grid/sections/provider.dart @@ -0,0 +1,105 @@ +import 'package:aves/model/source/section_keys.dart'; +import 'package:aves/widgets/common/grid/sections/fixed/section_layout_builder.dart'; +import 'package:aves/widgets/common/grid/sections/list_layout.dart'; +import 'package:aves/widgets/common/grid/sections/mosaic/section_layout_builder.dart'; +import 'package:aves/widgets/common/grid/sections/section_layout_builder.dart'; +import 'package:aves_model/aves_model.dart'; +import 'package:flutter/material.dart'; +import 'package:flutter/rendering.dart'; +import 'package:provider/provider.dart'; + +typedef CoverRatioResolver<T> = double Function(T item); + +abstract class SectionedListLayoutProvider<T> extends StatelessWidget { + final double scrollableWidth; + final TileLayout tileLayout; + final int columnCount; + final double spacing, horizontalPadding, tileWidth, tileHeight; + final TileBuilder<T> tileBuilder; + final Duration tileAnimationDelay; + final CoverRatioResolver<T> coverRatioResolver; + final Widget child; + + const SectionedListLayoutProvider({ + super.key, + required this.scrollableWidth, + required this.tileLayout, + required int columnCount, + required this.spacing, + required this.horizontalPadding, + required double tileWidth, + required this.tileHeight, + required this.tileBuilder, + required this.tileAnimationDelay, + required this.coverRatioResolver, + required this.child, + }) : assert(scrollableWidth != 0), + columnCount = tileLayout == TileLayout.list ? 1 : columnCount, + tileWidth = tileLayout == TileLayout.list ? scrollableWidth - (horizontalPadding * 2) : tileWidth; + + @override + Widget build(BuildContext context) { + return ProxyProvider0<SectionedListLayout<T>>( + update: (context, _) { + switch (tileLayout) { + case TileLayout.mosaic: + return MosaicSectionLayoutBuilder<T>( + sections: sections, + showHeaders: showHeaders, + getHeaderExtent: getHeaderExtent, + buildHeader: buildHeader, + scrollableWidth: scrollableWidth, + tileLayout: tileLayout, + columnCount: columnCount, + spacing: spacing, + horizontalPadding: horizontalPadding, + tileWidth: tileWidth, + tileHeight: tileHeight, + tileBuilder: tileBuilder, + tileAnimationDelay: tileAnimationDelay, + coverRatioResolver: coverRatioResolver, + ).updateLayouts(context); + case TileLayout.grid: + case TileLayout.list: + return FixedExtentSectionLayoutBuilder<T>( + sections: sections, + showHeaders: showHeaders, + buildHeader: buildHeader, + getHeaderExtent: getHeaderExtent, + scrollableWidth: scrollableWidth, + tileLayout: tileLayout, + columnCount: columnCount, + spacing: spacing, + horizontalPadding: horizontalPadding, + tileWidth: tileWidth, + tileHeight: tileHeight, + tileBuilder: tileBuilder, + tileAnimationDelay: tileAnimationDelay, + ).updateLayouts(context); + } + }, + child: child, + ); + } + + bool get showHeaders; + + Map<SectionKey, List<T>> get sections; + + double getHeaderExtent(BuildContext context, SectionKey sectionKey); + + Widget buildHeader(BuildContext context, SectionKey sectionKey, double headerExtent); + + @override + void debugFillProperties(DiagnosticPropertiesBuilder properties) { + super.debugFillProperties(properties); + properties.add(DoubleProperty('scrollableWidth', scrollableWidth)); + properties.add(EnumProperty<TileLayout>('tileLayout', tileLayout)); + properties.add(IntProperty('columnCount', columnCount)); + properties.add(DoubleProperty('spacing', spacing)); + properties.add(DoubleProperty('horizontalPadding', horizontalPadding)); + properties.add(DoubleProperty('tileWidth', tileWidth)); + properties.add(DoubleProperty('tileHeight', tileHeight)); + properties.add(DiagnosticsProperty<bool>('showHeaders', showHeaders)); + } +} diff --git a/lib/widgets/common/grid/sections/section_layout.dart b/lib/widgets/common/grid/sections/section_layout.dart new file mode 100644 index 0000000..ccc7b99 --- /dev/null +++ b/lib/widgets/common/grid/sections/section_layout.dart @@ -0,0 +1,37 @@ +import 'package:aves/model/source/section_keys.dart'; +import 'package:equatable/equatable.dart'; +import 'package:flutter/material.dart'; + +@immutable +abstract class SectionLayout extends Equatable { + final SectionKey sectionKey; + final int firstIndex, lastIndex, bodyFirstIndex; + final double minOffset, maxOffset, bodyMinOffset; + final double headerExtent, spacing; + final IndexedWidgetBuilder builder; + + @override + List<Object?> get props => [sectionKey, firstIndex, lastIndex, minOffset, maxOffset, headerExtent, spacing]; + + const SectionLayout({ + required this.sectionKey, + required this.firstIndex, + required this.lastIndex, + required this.minOffset, + required this.maxOffset, + required this.headerExtent, + required this.spacing, + required this.builder, + }) : bodyFirstIndex = firstIndex + 1, + bodyMinOffset = minOffset + headerExtent; + + bool hasChild(int index) => firstIndex <= index && index <= lastIndex; + + bool hasChildAtOffset(double scrollOffset) => minOffset <= scrollOffset && scrollOffset <= maxOffset; + + double indexToLayoutOffset(int index); + + int getMinChildIndexForScrollOffset(double scrollOffset); + + int getMaxChildIndexForScrollOffset(double scrollOffset); +} diff --git a/lib/widgets/common/grid/sections/section_layout_builder.dart b/lib/widgets/common/grid/sections/section_layout_builder.dart new file mode 100644 index 0000000..2be7e97 --- /dev/null +++ b/lib/widgets/common/grid/sections/section_layout_builder.dart @@ -0,0 +1,105 @@ +import 'package:aves/model/source/section_keys.dart'; +import 'package:aves/theme/durations.dart'; +import 'package:aves/widgets/common/grid/sections/list_layout.dart'; +import 'package:aves/widgets/common/grid/sections/section_layout.dart'; +import 'package:aves_model/aves_model.dart'; +import 'package:flutter/material.dart'; +import 'package:flutter_staggered_animations/flutter_staggered_animations.dart'; +import 'package:provider/provider.dart'; + +typedef TileBuilder<T> = Widget Function(T item, Size tileSize); + +abstract class SectionLayoutBuilder<T> { + final Map<SectionKey, List<T>> sections; + final bool showHeaders; + final double Function(BuildContext context, SectionKey sectionKey) getHeaderExtent; + final Widget Function(BuildContext context, SectionKey sectionKey, double headerExtent) buildHeader; + final double scrollableWidth; + final TileLayout tileLayout; + final int columnCount; + final double spacing, horizontalPadding, tileWidth, tileHeight, bottom; + final TileBuilder<T> tileBuilder; + final Duration tileAnimationDelay; + final bool animate; + + const SectionLayoutBuilder({ + required this.sections, + required this.showHeaders, + required this.getHeaderExtent, + required this.buildHeader, + required this.scrollableWidth, + required this.tileLayout, + required this.columnCount, + required this.spacing, + required this.horizontalPadding, + required this.tileWidth, + required this.tileHeight, + required this.tileBuilder, + required this.tileAnimationDelay, + }) : animate = tileAnimationDelay > Duration.zero, + bottom = tileHeight - tileWidth; + + SectionedListLayout<T> updateLayouts(BuildContext context); + + SectionLayout buildSectionLayout({ + required double headerExtent, + required SectionKey sectionKey, + required List<T> section, + required bool animate, + }); + + Widget buildSectionWidget({ + required BuildContext context, + required List<T> section, + required int sectionGridIndex, + required int sectionChildIndex, + required (int, int) Function() itemIndexRange, + required SectionKey sectionKey, + required double headerExtent, + required List<Size> itemSizes, + required bool animate, + required Widget Function(List<Widget> children) buildGridRow, + }) { + if (sectionChildIndex == 0) { + final header = headerExtent > 0 ? buildHeader(context, sectionKey, headerExtent) : const SizedBox(); + return animate ? _buildAnimation(context, sectionGridIndex, header) : header; + } + + final sectionItemCount = section.length; + final itemMinMax = itemIndexRange(); + final minItemIndex = itemMinMax.$1.clamp(0, sectionItemCount); + final maxItemIndex = itemMinMax.$2.clamp(0, sectionItemCount); + final childrenCount = maxItemIndex - minItemIndex; + final children = <Widget>[]; + for (var i = 0; i < childrenCount; i++) { + final item = RepaintBoundary( + child: tileBuilder(section[minItemIndex + i], itemSizes[i]), + ); + if (animate) { + children.add(_buildAnimation(context, sectionGridIndex + i, item)); + } else { + children.add(item); + } + } + return Padding( + padding: EdgeInsets.symmetric(horizontal: horizontalPadding), + child: buildGridRow(children), + ); + } + + Widget _buildAnimation(BuildContext context, int index, Widget child) { + final durations = context.watch<DurationsData>(); + return AnimationConfiguration.staggeredGrid( + position: index, + columnCount: tileLayout == TileLayout.mosaic ? 1 : columnCount, + duration: durations.staggeredAnimation, + delay: tileAnimationDelay, + child: SlideAnimation( + verticalOffset: 50.0, + child: FadeInAnimation( + child: child, + ), + ), + ); + } +} |
