Skip to content

Commit

Permalink
Scale, RedirectPointer
Browse files Browse the repository at this point in the history
  • Loading branch information
pingbird committed Oct 5, 2023
1 parent 988e3dc commit c961ee7
Show file tree
Hide file tree
Showing 7 changed files with 382 additions and 150 deletions.
4 changes: 4 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"dart.flutterSdkPath": "C:\\Users\\ping\\.puro\\envs\\beta\\flutter",
"dart.sdkPath": "C:\\Users\\ping\\.puro\\envs\\beta\\flutter\\bin\\cache\\dart-sdk"
}
1 change: 1 addition & 0 deletions boxy/lib/redirect_pointer.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export 'package:boxy/redirect_pointer.dart';
1 change: 1 addition & 0 deletions boxy/lib/scale.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export 'src/scale.dart';
78 changes: 78 additions & 0 deletions boxy/lib/src/redirect_pointer.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
import 'package:flutter/cupertino.dart';
import 'package:flutter/rendering.dart';

class RedirectPointer extends SingleChildRenderObjectWidget {
const RedirectPointer({
super.key,
super.child,
this.above = const [],
this.below = const [],
});

final List<GlobalKey> below;
final List<GlobalKey> above;

@override
RenderRedirectPointer createRenderObject(BuildContext context) {
return RenderRedirectPointer(below: below, above: above);
}

@override
void updateRenderObject(
BuildContext context,
RenderRedirectPointer renderObject,
) {
renderObject
..below = below
..above = above;
}
}

class RenderRedirectPointer extends RenderProxyBox {
RenderRedirectPointer({
RenderBox? child,
this.below = const [],
this.above = const [],
}) : super(child);

List<GlobalKey> below;
List<GlobalKey> above;

@override
bool hitTestChildren(BoxHitTestResult result, {required Offset position}) {
for (final key in above.reversed) {
final child = key.currentContext?.findRenderObject() as RenderBox?;
if (child != null) {
final hit = result.addWithPaintTransform(
transform: child.getTransformTo(this),
position: position,
hitTest: (result, position) {
return child.hitTest(result, position: position);
},
);
if (hit) {
return true;
}
}
}
if (super.hitTestChildren(result, position: position)) {
return true;
}
for (final key in below.reversed) {
final child = key.currentContext?.findRenderObject() as RenderBox?;
if (child != null) {
final hit = result.addWithPaintTransform(
transform: child.getTransformTo(this),
position: position,
hitTest: (result, position) {
return child.hitTest(result, position: position);
},
);
if (hit) {
return true;
}
}
}
return false;
}
}
42 changes: 42 additions & 0 deletions boxy/lib/src/scale.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import 'package:flutter/material.dart';

import '../boxy.dart';

class Scale extends StatelessWidget {
const Scale({
super.key,
required this.child,
required this.scale,
});

final Widget child;
final double scale;

@override
Widget build(BuildContext context) {
return CustomBoxy(
delegate: _ScaleBoxy(scale),
children: [child],
);
}
}

class _ScaleBoxy extends BoxyDelegate {
_ScaleBoxy(this.scale);

final double scale;

@override
bool shouldRelayout(_ScaleBoxy oldDelegate) => scale != oldDelegate.scale;

@override
Size layout() {
final child = children.single;
if (scale == 0 || !scale.isFinite) {
child.layout(BoxConstraints.tight(Size.zero));
return Size.zero;
}
child.setTransform(Matrix4.identity()..scale(scale));
return child.layout(constraints / scale) * scale;
}
}
Loading

0 comments on commit c961ee7

Please sign in to comment.