aboutsummaryrefslogtreecommitdiff
path: root/lib/widgets/common/grid/sections/fixed
diff options
context:
space:
mode:
Diffstat (limited to 'lib/widgets/common/grid/sections/fixed')
-rw-r--r--lib/widgets/common/grid/sections/fixed/list_layout.dart62
-rw-r--r--lib/widgets/common/grid/sections/fixed/row.dart162
-rw-r--r--lib/widgets/common/grid/sections/fixed/scale_grid.dart111
-rw-r--r--lib/widgets/common/grid/sections/fixed/scale_overlay.dart136
-rw-r--r--lib/widgets/common/grid/sections/fixed/section_layout.dart41
-rw-r--r--lib/widgets/common/grid/sections/fixed/section_layout_builder.dart109
6 files changed, 621 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,
+ ),
+ );
+ },
+ );
+ }
+}