23
23
import 'package:flutter/material.dart' ;
24
24
25
25
class GetPosition {
26
- final GlobalKey ? key;
26
+ final GlobalKey key;
27
27
final EdgeInsets padding;
28
28
final double ? screenWidth;
29
29
final double ? screenHeight;
30
30
31
31
GetPosition ({
32
- this .key,
32
+ required this .key,
33
33
this .padding = EdgeInsets .zero,
34
34
this .screenWidth,
35
35
this .screenHeight,
36
36
});
37
37
38
38
Rect getRect () {
39
- final box = key! .currentContext! .findRenderObject () as RenderBox ;
39
+ late final box = key.currentContext? .findRenderObject () as RenderBox ;
40
40
41
41
var boxOffset = box.localToGlobal (const Offset (0.0 , 0.0 ));
42
42
if (boxOffset.dx.isNaN || boxOffset.dy.isNaN) {
@@ -48,9 +48,7 @@ class GetPosition {
48
48
final rect = Rect .fromLTRB (
49
49
topLeft.dx - padding.left < 0 ? 0 : topLeft.dx - padding.left,
50
50
topLeft.dy - padding.top < 0 ? 0 : topLeft.dy - padding.top,
51
- bottomRight.dx + padding.right > screenWidth!
52
- ? screenWidth!
53
- : bottomRight.dx + padding.right,
51
+ bottomRight.dx + padding.right > screenWidth! ? screenWidth! : bottomRight.dx + padding.right,
54
52
bottomRight.dy + padding.bottom > screenHeight!
55
53
? screenHeight!
56
54
: bottomRight.dy + padding.bottom,
@@ -60,7 +58,7 @@ class GetPosition {
60
58
61
59
///Get the bottom position of the widget
62
60
double getBottom () {
63
- final box = key! .currentContext! .findRenderObject () as RenderBox ;
61
+ final box = key.currentContext? .findRenderObject () as RenderBox ;
64
62
final boxOffset = box.localToGlobal (const Offset (0.0 , 0.0 ));
65
63
if (boxOffset.dy.isNaN) return padding.bottom;
66
64
final bottomRight = box.size.bottomRight (boxOffset);
@@ -69,7 +67,7 @@ class GetPosition {
69
67
70
68
///Get the top position of the widget
71
69
double getTop () {
72
- final box = key! .currentContext! .findRenderObject () as RenderBox ;
70
+ final box = key.currentContext! .findRenderObject () as RenderBox ;
73
71
final boxOffset = box.localToGlobal (const Offset (0.0 , 0.0 ));
74
72
if (boxOffset.dy.isNaN) return 0 - padding.top;
75
73
final topLeft = box.size.topLeft (boxOffset);
@@ -90,8 +88,7 @@ class GetPosition {
90
88
final box = key! .currentContext! .findRenderObject () as RenderBox ;
91
89
final boxOffset = box.localToGlobal (const Offset (0.0 , 0.0 ));
92
90
if (boxOffset.dx.isNaN) return padding.right;
93
- final bottomRight =
94
- box.size.bottomRight (box.localToGlobal (const Offset (0.0 , 0.0 )));
91
+ final bottomRight = box.size.bottomRight (box.localToGlobal (const Offset (0.0 , 0.0 )));
95
92
return bottomRight.dx + padding.right;
96
93
}
97
94
0 commit comments