@@ -4,116 +4,55 @@ import '../popover.dart';
4
4
import 'popover_context.dart' ;
5
5
import 'popover_position_widget.dart' ;
6
6
import 'utils/build_context_extension.dart' ;
7
- import 'utils/utils.dart' ;
8
7
9
8
class PopoverItem extends StatefulWidget {
10
9
final Widget child;
11
10
final Color ? backgroundColor;
12
11
final PopoverDirection ? direction;
13
12
final double ? radius;
14
13
final List <BoxShadow >? boxShadow;
15
- final Animation <double >? animation;
14
+ final Animation <double > animation;
16
15
final double ? arrowWidth;
17
- final double ? arrowHeight;
16
+ final double arrowHeight;
18
17
final BoxConstraints ? constraints;
19
18
final BuildContext context;
20
- final double ? arrowDxOffset;
21
- final double ? arrowDyOffset;
22
- final double ? contentDyOffset;
23
- final bool Function ()? isParentAlive;
19
+ final double arrowDxOffset;
20
+ final double arrowDyOffset;
21
+ final double contentDyOffset;
24
22
final PopoverTransition transition;
25
23
26
24
const PopoverItem ({
27
25
required this .child,
28
26
required this .context,
29
27
required this .transition,
28
+ required this .animation,
29
+ required this .arrowHeight,
30
30
this .backgroundColor,
31
31
this .direction,
32
32
this .radius,
33
33
this .boxShadow,
34
- this .animation,
35
34
this .arrowWidth,
36
- this .arrowHeight,
37
35
this .constraints,
38
- this .arrowDxOffset,
39
- this .arrowDyOffset,
40
- this .contentDyOffset,
41
- this .isParentAlive,
42
- Key ? key,
43
- }) : super (key: key);
36
+ this .arrowDxOffset = 0 ,
37
+ this .arrowDyOffset = 0 ,
38
+ this .contentDyOffset = 0 ,
39
+ super .key,
40
+ });
44
41
45
42
@override
46
43
_PopoverItemState createState () => _PopoverItemState ();
47
44
}
48
45
49
46
class _PopoverItemState extends State <PopoverItem > {
50
- late BoxConstraints constraints;
51
- late Offset offset;
52
- late Rect bounds;
53
- late Rect attachRect;
54
-
55
- @override
56
- Widget build (BuildContext context) {
57
- return LayoutBuilder (
58
- builder: (_, __) {
59
- _configure ();
60
- return Stack (
61
- children: [
62
- PopoverPositionWidget (
63
- attachRect: attachRect,
64
- scale: widget.animation,
65
- constraints: constraints,
66
- direction: widget.direction,
67
- arrowHeight: widget.arrowHeight,
68
- child: PopoverContext (
69
- attachRect: attachRect,
70
- animation: widget.animation,
71
- radius: widget.radius,
72
- backgroundColor: widget.backgroundColor,
73
- boxShadow: widget.boxShadow,
74
- direction: widget.direction,
75
- arrowWidth: widget.arrowWidth,
76
- arrowHeight: widget.arrowHeight,
77
- transition: widget.transition,
78
- child: Material (
79
- type: MaterialType .transparency,
80
- child: widget.child,
81
- ),
82
- ),
83
- )
84
- ],
85
- );
86
- },
87
- );
88
- }
89
-
90
- void _configure () {
91
- final bool isParentAlive;
92
-
93
- if (widget.isParentAlive != null ) {
94
- isParentAlive = widget.isParentAlive !();
95
- } else {
96
- isParentAlive = true ;
97
- }
98
-
99
- if (! isParentAlive) {
100
- return ;
101
- }
102
-
103
- final box = widget.context.findRenderObject () as RenderBox ;
104
- if (mounted && box.owner != null ) {
105
- _configureConstraints ();
106
- _configureRect ();
107
- }
108
- }
47
+ late Rect _attachRect;
48
+ BoxConstraints ? _constraints;
109
49
110
50
void _configureConstraints () {
111
- BoxConstraints _constraints;
51
+ final size = MediaQuery .of (context).size;
52
+ var constraints = BoxConstraints .loose (size);
53
+
112
54
if (widget.constraints != null ) {
113
- _constraints = BoxConstraints (
114
- maxHeight: Utils ().screenHeight / 2 ,
115
- maxWidth: Utils ().screenHeight / 2 ,
116
- ).copyWith (
55
+ constraints = constraints.copyWith (
117
56
minWidth: widget.constraints! .minWidth.isFinite
118
57
? widget.constraints! .minWidth
119
58
: null ,
@@ -127,34 +66,81 @@ class _PopoverItemState extends State<PopoverItem> {
127
66
? widget.constraints! .maxHeight
128
67
: null ,
129
68
);
130
- } else {
131
- _constraints = BoxConstraints (
132
- maxHeight: Utils ().screenHeight / 2 ,
133
- maxWidth: Utils ().screenHeight / 2 ,
134
- );
135
69
}
70
+
136
71
if (widget.direction == PopoverDirection .top ||
137
72
widget.direction == PopoverDirection .bottom) {
138
- constraints = _constraints.copyWith (
139
- maxHeight: _constraints.maxHeight + widget.arrowHeight! ,
140
- maxWidth: _constraints.maxWidth,
141
- );
73
+ final maxHeight = constraints.maxHeight + widget.arrowHeight;
74
+ constraints = constraints.copyWith (maxHeight: maxHeight);
142
75
} else {
143
- constraints = _constraints .copyWith (
144
- maxHeight: _constraints .maxHeight + widget.arrowHeight! ,
145
- maxWidth: _constraints .maxWidth + widget.arrowWidth! ,
76
+ constraints = constraints .copyWith (
77
+ maxHeight: constraints .maxHeight + widget.arrowHeight,
78
+ maxWidth: constraints .maxWidth + widget.arrowWidth! ,
146
79
);
147
80
}
81
+
82
+ _constraints = constraints;
148
83
}
149
84
150
85
void _configureRect () {
151
- offset = BuildContextExtension .getWidgetLocalToGlobal (widget.context);
152
- bounds = BuildContextExtension .getWidgetBounds (widget.context);
153
- attachRect = Rect .fromLTWH (
154
- offset.dx + (widget.arrowDxOffset ?? 0.0 ),
155
- offset.dy + (widget.arrowDyOffset ?? 0.0 ),
156
- bounds.width,
157
- bounds.height + (widget.contentDyOffset ?? 0.0 ),
86
+ final offset = BuildContextExtension .getWidgetLocalToGlobal (widget.context);
87
+ final bounds = BuildContextExtension .getWidgetBounds (widget.context);
88
+
89
+ if (offset != null && bounds != null ) {
90
+ _attachRect = Rect .fromLTWH (
91
+ offset.dx + (widget.arrowDxOffset),
92
+ offset.dy + (widget.arrowDyOffset),
93
+ bounds.width,
94
+ bounds.height + (widget.contentDyOffset),
95
+ );
96
+ }
97
+ }
98
+
99
+ @override
100
+ void initState () {
101
+ _configureRect ();
102
+ super .initState ();
103
+ }
104
+
105
+ @override
106
+ void didChangeDependencies () {
107
+ _configureConstraints ();
108
+ WidgetsBinding .instance.addPostFrameCallback (
109
+ (_) => setState (_configureRect),
110
+ );
111
+
112
+ super .didChangeDependencies ();
113
+ }
114
+
115
+ @override
116
+ Widget build (BuildContext context) {
117
+ return Stack (
118
+ children: [
119
+ PopoverPositionWidget (
120
+ attachRect: _attachRect,
121
+ constraints: _constraints,
122
+ direction: widget.direction,
123
+ arrowHeight: widget.arrowHeight,
124
+ child: AnimatedBuilder (
125
+ animation: widget.animation,
126
+ builder: (context, child) {
127
+ return PopoverContext (
128
+ attachRect: _attachRect,
129
+ animation: widget.animation,
130
+ radius: widget.radius,
131
+ backgroundColor: widget.backgroundColor,
132
+ boxShadow: widget.boxShadow,
133
+ direction: widget.direction,
134
+ arrowWidth: widget.arrowWidth,
135
+ arrowHeight: widget.arrowHeight,
136
+ transition: widget.transition,
137
+ child: child,
138
+ );
139
+ },
140
+ child: Material (child: widget.child),
141
+ ),
142
+ )
143
+ ],
158
144
);
159
145
}
160
146
}
0 commit comments