Skip to content

Commit 66af50f

Browse files
fix: ⚡ Optimize GetPosition class (#370)
1 parent ff7aa9b commit 66af50f

File tree

2 files changed

+62
-61
lines changed

2 files changed

+62
-61
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
## [2.0.4] (Un-Released)
22
- Feature [#387](https://github.com/SimformSolutionsPvtLtd/flutter_showcaseview/issues/387) - Provided barrier click disable functionality for a particular showcase.
33
- Fixed [#383](https://github.com/SimformSolutionsPvtLtd/flutter_showcaseview/issues/383) - Targeted widget focusing issue when we applying size constraint on root widget(MaterialApp).
4+
- Improved internal `findRenderObject` calls.
45

56
## [2.0.3]
67
- Feature [#148](https://github.com/SimformSolutionsPvtLtd/flutter_showcaseview/issues/148) - Add feasibility to add `textDirection` of `title` and `description`.

lib/src/get_position.dart

Lines changed: 61 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -20,105 +20,105 @@
2020
* SOFTWARE.
2121
*/
2222

23+
import 'dart:math';
24+
2325
import 'package:flutter/material.dart';
2426

2527
class GetPosition {
26-
final GlobalKey? key;
27-
final EdgeInsets padding;
28-
final double? screenWidth;
29-
final double? screenHeight;
30-
final RenderObject? rootRenderObject;
31-
3228
GetPosition({
33-
this.key,
29+
required this.key,
30+
required this.screenWidth,
31+
required this.screenHeight,
3432
this.padding = EdgeInsets.zero,
35-
this.screenWidth,
36-
this.screenHeight,
3733
this.rootRenderObject,
38-
});
34+
}) {
35+
getRenderBox();
36+
}
3937

40-
Rect getRect() {
41-
final box = key!.currentContext!.findRenderObject() as RenderBox;
38+
final GlobalKey key;
39+
final EdgeInsets padding;
40+
final double screenWidth;
41+
final double screenHeight;
42+
final RenderObject? rootRenderObject;
4243

43-
var boxOffset = box.localToGlobal(
44-
const Offset(0.0, 0.0),
44+
late final RenderBox? _box;
45+
late final Offset? _boxOffset;
46+
47+
void getRenderBox() {
48+
var renderBox = key.currentContext?.findRenderObject() as RenderBox?;
49+
50+
if (renderBox == null) return;
51+
52+
_box = renderBox;
53+
_boxOffset = _box?.localToGlobal(
54+
Offset.zero,
4555
ancestor: rootRenderObject,
4656
);
47-
if (boxOffset.dx.isNaN || boxOffset.dy.isNaN) {
48-
return const Rect.fromLTRB(0, 0, 0, 0);
49-
}
50-
final topLeft = box.size.topLeft(boxOffset);
51-
final bottomRight = box.size.bottomRight(boxOffset);
57+
}
58+
59+
bool _checkBoxOrOffsetIsNull({bool checkDy = false, bool checkDx = false}) {
60+
return _box == null ||
61+
_boxOffset == null ||
62+
(checkDx && (_boxOffset?.dx.isNaN ?? true)) ||
63+
(checkDy && (_boxOffset?.dy.isNaN ?? true));
64+
}
5265

66+
Rect getRect() {
67+
if (_checkBoxOrOffsetIsNull(checkDy: true, checkDx: true)) {
68+
return Rect.zero;
69+
}
70+
final topLeft = _box!.size.topLeft(_boxOffset!);
71+
final bottomRight = _box!.size.bottomRight(_boxOffset!);
72+
final leftDx = topLeft.dx - padding.left;
73+
final leftDy = topLeft.dy - padding.top;
5374
final rect = Rect.fromLTRB(
54-
topLeft.dx - padding.left < 0 ? 0 : topLeft.dx - padding.left,
55-
topLeft.dy - padding.top < 0 ? 0 : topLeft.dy - padding.top,
56-
bottomRight.dx + padding.right > screenWidth!
57-
? screenWidth!
58-
: bottomRight.dx + padding.right,
59-
bottomRight.dy + padding.bottom > screenHeight!
60-
? screenHeight!
61-
: bottomRight.dy + padding.bottom,
75+
leftDx.clamp(0, leftDx),
76+
leftDy.clamp(0, leftDy),
77+
min(bottomRight.dx + padding.right, screenWidth),
78+
min(bottomRight.dy + padding.bottom, screenHeight),
6279
);
6380
return rect;
6481
}
6582

6683
///Get the bottom position of the widget
6784
double getBottom() {
68-
final box = key!.currentContext!.findRenderObject() as RenderBox;
69-
final boxOffset = box.localToGlobal(
70-
const Offset(0.0, 0.0),
71-
ancestor: rootRenderObject,
72-
);
73-
if (boxOffset.dy.isNaN) return padding.bottom;
74-
final bottomRight = box.size.bottomRight(boxOffset);
85+
if (_checkBoxOrOffsetIsNull(checkDy: true)) {
86+
return padding.bottom;
87+
}
88+
final bottomRight = _box!.size.bottomRight(_boxOffset!);
7589
return bottomRight.dy + padding.bottom;
7690
}
7791

7892
///Get the top position of the widget
7993
double getTop() {
80-
final box = key!.currentContext!.findRenderObject() as RenderBox;
81-
final boxOffset = box.localToGlobal(
82-
const Offset(0.0, 0.0),
83-
ancestor: rootRenderObject,
84-
);
85-
if (boxOffset.dy.isNaN) return 0 - padding.top;
86-
final topLeft = box.size.topLeft(boxOffset);
94+
if (_checkBoxOrOffsetIsNull(checkDy: true)) {
95+
return -padding.top;
96+
}
97+
final topLeft = _box!.size.topLeft(_boxOffset!);
8798
return topLeft.dy - padding.top;
8899
}
89100

90101
///Get the left position of the widget
91102
double getLeft() {
92-
final box = key!.currentContext!.findRenderObject() as RenderBox;
93-
final boxOffset = box.localToGlobal(
94-
const Offset(0.0, 0.0),
95-
ancestor: rootRenderObject,
96-
);
97-
if (boxOffset.dx.isNaN) return 0 - padding.left;
98-
final topLeft = box.size.topLeft(boxOffset);
103+
if (_checkBoxOrOffsetIsNull(checkDx: true)) {
104+
return -padding.left;
105+
}
106+
final topLeft = _box!.size.topLeft(_boxOffset!);
99107
return topLeft.dx - padding.left;
100108
}
101109

102110
///Get the right position of the widget
103111
double getRight() {
104-
final box = key!.currentContext!.findRenderObject() as RenderBox;
105-
final boxOffset = box.localToGlobal(
106-
const Offset(0.0, 0.0),
107-
ancestor: rootRenderObject,
108-
);
109-
if (boxOffset.dx.isNaN) return padding.right;
110-
final bottomRight = box.size.bottomRight(
111-
box.localToGlobal(
112-
const Offset(0.0, 0.0),
113-
ancestor: rootRenderObject,
114-
),
115-
);
112+
if (_checkBoxOrOffsetIsNull(checkDx: true)) {
113+
return padding.right;
114+
}
115+
final bottomRight = _box!.size.bottomRight(_boxOffset!);
116116
return bottomRight.dx + padding.right;
117117
}
118118

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

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

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

0 commit comments

Comments
 (0)