Skip to content

Instantly share code, notes, and snippets.

@Craftplacer
Created October 27, 2023 04:48
Show Gist options
  • Save Craftplacer/420ca4f3f08a38c23edf4b9ecd0d0f4a to your computer and use it in GitHub Desktop.
Save Craftplacer/420ca4f3f08a38c23edf4b9ecd0d0f4a to your computer and use it in GitHub Desktop.
import 'package:flutter/rendering.dart';
import 'package:flutter/widgets.dart';
class SliverCrossAxisAlign extends SingleChildRenderObjectWidget {
final double alignment;
const SliverCrossAxisAlign({
super.key,
required this.alignment,
required Widget sliver,
}) : super(child: sliver);
@override
RenderSliverCrossAxisAlign createRenderObject(BuildContext context) {
return RenderSliverCrossAxisAlign(alignment);
}
@override
void updateRenderObject(
BuildContext context,
RenderSliverCrossAxisAlign renderObject,
) {
renderObject.alignment = alignment;
}
@override
void debugFillProperties(DiagnosticPropertiesBuilder properties) {
super.debugFillProperties(properties);
properties.add(DoubleProperty("alignment", alignment));
}
}
class RenderSliverCrossAxisAlign extends RenderSliver
with RenderObjectWithChildMixin<RenderSliver> {
double _alignment;
RenderSliverCrossAxisAlign(this._alignment);
double get alignment => _alignment;
set alignment(double value) {
if (_alignment == value) return;
_alignment = value;
markNeedsLayout();
}
@override
void setupParentData(RenderObject child) {
if (child.parentData is! SliverPhysicalParentData) {
child.parentData = SliverPhysicalParentData();
}
}
@override
void performLayout() {
final child = this.child;
if (child == null) {
geometry = SliverGeometry.zero;
return;
}
child.layout(constraints, parentUsesSize: true);
final childParentData = child.parentData! as SliverPhysicalParentData;
final childGeometry = child.geometry!;
final childExtent = childGeometry.crossAxisExtent;
if (childExtent != null) {
final crossAxisExtent = constraints.crossAxisExtent;
final crossAxisOffset = (crossAxisExtent - childExtent) * _alignment;
childParentData.paintOffset = Offset(crossAxisOffset, 0);
}
geometry = childGeometry;
}
@override
void paint(PaintingContext context, Offset offset) {
final child = this.child;
if (child == null) return;
final childParentData = child.parentData! as SliverPhysicalParentData;
context.paintChild(child, offset + childParentData.paintOffset);
}
@override
void debugFillProperties(DiagnosticPropertiesBuilder properties) {
super.debugFillProperties(properties);
properties.add(DoubleProperty("alignment", alignment));
}
@override
List<DiagnosticsNode> debugDescribeChildren() {
return <DiagnosticsNode>[
if (child != null) child!.toDiagnosticsNode(name: "child"),
];
}
@override
bool hitTestChildren(SliverHitTestResult result,
{required double mainAxisPosition, required double crossAxisPosition}) {
final child = this.child;
if (child == null) return false;
final childParentData = child.parentData! as SliverPhysicalParentData;
return child.hitTest(
result,
mainAxisPosition: mainAxisPosition,
crossAxisPosition: crossAxisPosition - childParentData.paintOffset.dx,
);
}
@override
void applyPaintTransform(covariant RenderObject child, Matrix4 transform) {
assert(child == this.child);
final SliverPhysicalParentData childParentData =
child.parentData! as SliverPhysicalParentData;
childParentData.applyPaintTransform(transform);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment