-
Notifications
You must be signed in to change notification settings - Fork 4
/
grid_card.dart
148 lines (129 loc) · 3.9 KB
/
grid_card.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
import 'dart:math';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:niku/namespace.dart' as n;
import 'home_controller.dart';
class GridCard extends StatelessWidget {
final IconData icon;
final String title;
final Widget content;
GridCard({
super.key,
required this.icon,
required this.title,
required this.content,
});
HomeController get c => Get.find();
// style
final rounded = 10.0;
BoxShadow get shadow => BoxShadow(
color: Colors.black12.withOpacity(opacity * 0.12),
blurRadius: 10,
spreadRadius: 10,
);
// config
final headHeight = 34.0;
final cardsGap = 40.0;
final listPaddingTop = 30.0;
// data
final height = 0.0.obs;
final positionInList = 0.0.obs;
final boxKey = GlobalKey(debugLabel: 'gridCardBox');
double get globalY {
final renderObj = boxKey.currentContext?.findRenderObject();
if (renderObj == null) return 0.0;
return (renderObj as RenderBox?)?.localToGlobal(Offset.zero).dy ?? 0.0;
}
double get animationHeight => topOffset < 0.0
? max(min(height.value + topOffset, height.value), headHeight)
: height.value;
double get topOffset => positionInList.value - c.listOffset.value;
bool get inTheAnimation {
return height.value != 0.0
? topOffset < 0 && topOffset > -height.value + headHeight - cardsGap
: false;
}
updateHeightData() {
if (inTheAnimation) return;
height.value = (boxKey.currentContext?.findRenderObject() as RenderBox?)
?.size
.height ??
0.0;
}
updatePositionInListData() {
positionInList.value =
globalY - c.listViewTop.value + c.listOffset.value - listPaddingTop;
}
double? get shellHeight => inTheAnimation ? height.value : null;
double? get contentHeight => inTheAnimation ? animationHeight : null;
double get translateY => inTheAnimation ? -topOffset : 0.0;
double get opacity => topOffset < (-height.value + headHeight)
? min(
max((1.0 - ((-topOffset - (height.value - headHeight)) / cardsGap)),
0.0),
1.0)
: 1.0;
@override
Widget build(BuildContext context) {
WidgetsBinding.instance.addPostFrameCallback((timeStamp) {
updatePositionInListData();
ever(c.listViewTop, (value) {
updatePositionInListData();
});
updateHeightData();
ever(c.listOffset, (value) {
updateHeightData();
});
});
return SizedBox(
key: boxKey,
child: Obx(
() => n.Box(
n.Box(
n.Wrap([
n.Row([
n.Icon(icon)
..color = Colors.blue[50]!.withOpacity(0.7)
..size = 14,
n.Text(title)
..color = Colors.blue[50]!.withOpacity(0.7)
..fontSize = 14
..fontWeight = FontWeight.w600
..maxLines = 1
..overflow = TextOverflow.ellipsis,
])
..pb = 4
..gap = 4,
n.Box(content)
..rounded = 0
..wFull
..useChild(
(child) => ClipRect(child: child..translateY = -translateY),
),
])
..horizontal,
)
..p = 8
..useChild(
(child) => SizedBox(
height: contentHeight,
child: ClipRect(child: child),
),
)
..bg = Colors.blueGrey[700]!.withOpacity(0.2)
..opacity = opacity
..bgBlur = opacity * 30
..rounded = rounded
..shadow = shadow
..translateY = translateY,
)..useChild(
(child) => Container(
height: shellHeight,
alignment: Alignment.topCenter,
child: child,
),
),
),
);
}
}