Skip to content

Commit

Permalink
fix: ⚡ Optimize GetPosition class
Browse files Browse the repository at this point in the history
  • Loading branch information
ujas-m-simformsolutions authored and Sahil-Simform committed Mar 6, 2024
1 parent ff7aa9b commit 1edfcf9
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 61 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
## [2.0.4] (Un-Released)
- Feature [#387](https://github.com/SimformSolutionsPvtLtd/flutter_showcaseview/issues/387) - Provided barrier click disable functionality for a particular showcase.
- Fixed [#383](https://github.com/SimformSolutionsPvtLtd/flutter_showcaseview/issues/383) - Targeted widget focusing issue when we applying size constraint on root widget(MaterialApp).
- Improved internal `findRenderObject` calls.

## [2.0.3]
- Feature [#148](https://github.com/SimformSolutionsPvtLtd/flutter_showcaseview/issues/148) - Add feasibility to add `textDirection` of `title` and `description`.
Expand Down
122 changes: 61 additions & 61 deletions lib/src/get_position.dart
Original file line number Diff line number Diff line change
Expand Up @@ -20,105 +20,105 @@
* SOFTWARE.
*/

import 'dart:math';

import 'package:flutter/material.dart';

class GetPosition {
final GlobalKey? key;
final EdgeInsets padding;
final double? screenWidth;
final double? screenHeight;
final RenderObject? rootRenderObject;

GetPosition({
this.key,
required this.key,
required this.screenWidth,
required this.screenHeight,
this.padding = EdgeInsets.zero,
this.screenWidth,
this.screenHeight,
this.rootRenderObject,
});
}) {
getRenderBox();
}

Rect getRect() {
final box = key!.currentContext!.findRenderObject() as RenderBox;
final GlobalKey key;
final EdgeInsets padding;
final double screenWidth;
final double screenHeight;
final RenderObject? rootRenderObject;

var boxOffset = box.localToGlobal(
const Offset(0.0, 0.0),
late final RenderBox? _box;
late final Offset? _boxOffset;

void getRenderBox() {
var renderBox = key.currentContext?.findRenderObject() as RenderBox?;

if (renderBox == null) return;

_box = renderBox;
_boxOffset = _box?.localToGlobal(
Offset.zero,
ancestor: rootRenderObject,
);
if (boxOffset.dx.isNaN || boxOffset.dy.isNaN) {
return const Rect.fromLTRB(0, 0, 0, 0);
}
final topLeft = box.size.topLeft(boxOffset);
final bottomRight = box.size.bottomRight(boxOffset);
}

bool _checkBoxOrOffsetIsNull({bool checkDy = false, bool checkDx = false}) {
return _box == null ||
_boxOffset == null ||
(checkDx && (_boxOffset?.dx.isNaN ?? true)) ||
(checkDy && (_boxOffset?.dy.isNaN ?? true));
}

Rect getRect() {
if (_checkBoxOrOffsetIsNull(checkDy: true, checkDx: true)) {
return Rect.zero;
}
final topLeft = _box!.size.topLeft(_boxOffset!);
final bottomRight = _box!.size.bottomRight(_boxOffset!);
final leftDx = topLeft.dx - padding.left;
final leftDy = topLeft.dy - padding.top;
final rect = Rect.fromLTRB(
topLeft.dx - padding.left < 0 ? 0 : topLeft.dx - padding.left,
topLeft.dy - padding.top < 0 ? 0 : topLeft.dy - padding.top,
bottomRight.dx + padding.right > screenWidth!
? screenWidth!
: bottomRight.dx + padding.right,
bottomRight.dy + padding.bottom > screenHeight!
? screenHeight!
: bottomRight.dy + padding.bottom,
leftDx.clamp(0, leftDx),
leftDy.clamp(0, leftDy),
min(bottomRight.dx + padding.right, screenWidth),
min(bottomRight.dy + padding.bottom, screenHeight),
);
return rect;
}

///Get the bottom position of the widget
double getBottom() {
final box = key!.currentContext!.findRenderObject() as RenderBox;
final boxOffset = box.localToGlobal(
const Offset(0.0, 0.0),
ancestor: rootRenderObject,
);
if (boxOffset.dy.isNaN) return padding.bottom;
final bottomRight = box.size.bottomRight(boxOffset);
if (_checkBoxOrOffsetIsNull(checkDy: true)) {
return padding.bottom;
}
final bottomRight = _box!.size.bottomRight(_boxOffset!);
return bottomRight.dy + padding.bottom;
}

///Get the top position of the widget
double getTop() {
final box = key!.currentContext!.findRenderObject() as RenderBox;
final boxOffset = box.localToGlobal(
const Offset(0.0, 0.0),
ancestor: rootRenderObject,
);
if (boxOffset.dy.isNaN) return 0 - padding.top;
final topLeft = box.size.topLeft(boxOffset);
if (_checkBoxOrOffsetIsNull(checkDy: true)) {
return -padding.top;
}
final topLeft = _box!.size.topLeft(_boxOffset!);
return topLeft.dy - padding.top;
}

///Get the left position of the widget
double getLeft() {
final box = key!.currentContext!.findRenderObject() as RenderBox;
final boxOffset = box.localToGlobal(
const Offset(0.0, 0.0),
ancestor: rootRenderObject,
);
if (boxOffset.dx.isNaN) return 0 - padding.left;
final topLeft = box.size.topLeft(boxOffset);
if (_checkBoxOrOffsetIsNull(checkDx: true)) {
return -padding.left;
}
final topLeft = _box!.size.topLeft(_boxOffset!);
return topLeft.dx - padding.left;
}

///Get the right position of the widget
double getRight() {
final box = key!.currentContext!.findRenderObject() as RenderBox;
final boxOffset = box.localToGlobal(
const Offset(0.0, 0.0),
ancestor: rootRenderObject,
);
if (boxOffset.dx.isNaN) return padding.right;
final bottomRight = box.size.bottomRight(
box.localToGlobal(
const Offset(0.0, 0.0),
ancestor: rootRenderObject,
),
);
if (_checkBoxOrOffsetIsNull(checkDx: true)) {
return padding.right;
}
final bottomRight = _box!.size.bottomRight(_boxOffset!);
return bottomRight.dx + padding.right;
}

double getHeight() => getBottom() - getTop();

double getWidth() => getRight() - getLeft();

double getCenter() => (getLeft() + getRight()) / 2;
double getCenter() => (getLeft() + getRight()) * 0.5;
}

0 comments on commit 1edfcf9

Please sign in to comment.