app_home_tab.dart
9.97 KB
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
import 'package:cached_network_image/cached_network_image.dart';
import 'package:doublefeel_flutter/app/modules/home/controllers/app_home_controller.dart';
import 'package:doublefeel_flutter/app/modules/home/controllers/today_controller.dart';
import 'package:doublefeel_flutter/app/modules/home/views/tabs/today_tab.dart';
import 'package:doublefeel_flutter/core/theme/app_theme.dart';
import 'package:doublefeel_flutter/app/modules/home/widgets/today/app_home_stress_card.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
class AppHomeTab extends GetView<AppHomeController> {
const AppHomeTab({super.key});
@override
Widget build(BuildContext context) => Obx(() {
final friend = controller.watchedFriend.value;
final children = <Widget>[
_todayBody(
controller.selfTodayController,
showPeopleSwitcher: friend != null,
),
if (friend != null)
_todayBody(
Get.find<TodayController>(
tag: 'app_home_friend_${friend.friendItem.friendUserId}',
),
showPeopleSwitcher: true,
),
];
final index = controller.isShowingSelf || friend == null ? 0 : 1;
return IndexedStack(index: index, children: children);
});
Widget _todayBody(
TodayController todayController, {
required bool showPeopleSwitcher,
}) => TodayTabBody(
controller: todayController,
useHomeHeader: true,
headerFooter: showPeopleSwitcher
? _PeopleSwitcher(controller: controller)
: null,
stressCard: showPeopleSwitcher
? AppHomeStressCard(controller: todayController)
: null,
showInteractionEntry: controller.hasAnyFriend,
);
}
class _PeopleSwitcher extends StatelessWidget {
const _PeopleSwitcher({required this.controller});
final AppHomeController controller;
@override
Widget build(BuildContext context) => Obx(() {
final friend = controller.watchedFriend.value;
if (friend == null) return const SizedBox.shrink();
return Padding(
padding: const EdgeInsets.symmetric(horizontal: 16),
child: LayoutBuilder(
builder: (context, constraints) {
final height = constraints.maxWidth * 252 / 1029;
final scale = height / 84;
return SizedBox(
height: height,
child: DecoratedBox(
decoration: BoxDecoration(
image: DecorationImage(
image: AssetImage(
controller.isShowingSelf
? 'assets/images/today/today_switch_right@3x.png'
: 'assets/images/today/today_switch_left@3x.png',
),
fit: BoxFit.fill,
),
),
child: Row(
children: [
Expanded(
child: _PersonTab(
selected: !controller.isShowingSelf,
name: _friendName(friend.remark, friend.name),
avatarUrl: friend.avatarUrl,
isMe: false,
status: _statusText(
isSelected: !controller.isShowingSelf,
todayController: controller.selectedTodayController,
),
scale: scale,
onTap: controller.selectFriend,
),
),
Expanded(
child: _PersonTab(
selected: controller.isShowingSelf,
name: _selfName(controller.selfName),
avatarUrl: controller.selfAvatar,
isMe: true,
status: _statusText(
isSelected: controller.isShowingSelf,
todayController: controller.selectedTodayController,
),
scale: scale,
onTap: controller.selectSelf,
),
),
],
),
),
);
},
),
);
});
String _friendName(String? remark, String nickname) {
final value = remark?.trim();
return value == null || value.isEmpty ? nickname : '$value($nickname)';
}
String _selfName(String nickname) => '$nickname(我)';
_StatusText? _statusText({
required bool isSelected,
required TodayController todayController,
}) {
if (!isSelected) return null;
final state = todayController.v2StressScore.value?.state;
final text = todayController.v2StressScore.value?.stateString();
if (text == null || text.isEmpty) return null;
return _StatusText(text, _statusColor(state));
}
Color _statusColor(int? state) => switch (state) {
1 => const Color(0xFFFF5279),
2 => const Color(0xFFFF9A6E),
3 => const Color(0xFF7B9BFB),
4 => const Color(0xFF3BD49D),
_ => const Color(0xFF78787D),
};
}
class _StatusText {
const _StatusText(this.value, this.color);
final String value;
final Color color;
}
class _PersonTab extends StatelessWidget {
const _PersonTab({
required this.isMe,
required this.selected,
required this.name,
required this.avatarUrl,
required this.status,
required this.scale,
required this.onTap,
});
final bool isMe;
final bool selected;
final String name;
final String? avatarUrl;
final _StatusText? status;
final double scale;
final VoidCallback onTap;
@override
Widget build(BuildContext context) => GestureDetector(
behavior: HitTestBehavior.opaque,
onTap: onTap,
child: AnimatedContainer(
duration: const Duration(milliseconds: 160), // 透明背景,确保点击区域覆盖整个卡片
padding: EdgeInsets.only(
left: (isMe && !selected ? 16 : 10) * scale,
right: (isMe || (!isMe && selected) ? 10 : 16) * scale,
),
margin: EdgeInsets.only(
top: (selected ? 0 : 10) * scale,
bottom: (selected ? 24 : 26) * scale,
),
child: Row(
crossAxisAlignment: CrossAxisAlignment.center,
children: [
SizedBox(
width: (selected ? 36 : 32) * scale,
height: (selected ? 36 : 32) * scale,
child: _Avatar(url: avatarUrl, name: name),
),
SizedBox(width: 8),
Expanded(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.start,
children: [
_PersonName(name: name, selected: selected),
if (status != null)
Text(
status!.value,
maxLines: 1,
overflow: TextOverflow.ellipsis,
style: TextStyle(
color: status!.color,
fontSize: 9,
fontWeight: FontWeight.w500,
),
),
],
),
),
],
),
),
);
}
// class _PersonalTabCard extends StatelessWidget {
// final bool selected;
// final String name;
// final String? avatarUrl;
// final _StatusText? status;
// _PersonalTabCard({
// required this.selected,
// required this.name,
// required this.avatarUrl,
// required this.status,
// })
// @override
// Widget build(BuildContext context) {
// return Row(
// children:[
// SizedBox(
// width: selected ? 36 : 32,
// height: selected ? 36 : 32,
// child: _Avatar(url: avatarUrl, name: name),
// ),
// const SizedBox(width: 8),
// Expanded(
// child: Column(
// mainAxisAlignment: MainAxisAlignment.center,
// crossAxisAlignment: CrossAxisAlignment.start,
// children: [
// _PersonName(name: name, selected: selected),
// if (status != null)
// Text(
// status!.value,
// maxLines: 1,
// overflow: TextOverflow.ellipsis,
// style: TextStyle(
// color: status!.color,
// fontSize: 9,
// fontWeight: FontWeight.w500,
// ),
// ),
// ],
// ),
// )
// ]
// );
// }
// }
class _PersonName extends StatelessWidget {
const _PersonName({required this.name, required this.selected});
final String name;
final bool selected;
@override
Widget build(BuildContext context) {
final bracketIndex = name.indexOf('(');
final primary = bracketIndex < 0 ? name : name.substring(0, bracketIndex);
final suffix = bracketIndex < 0 ? '' : name.substring(bracketIndex);
return RichText(
maxLines: 1,
overflow: TextOverflow.ellipsis,
text: TextSpan(
children: [
TextSpan(
text: primary,
style: TextStyle(
color: context.colors.textPrimary,
fontSize: selected ? 14 : 13,
fontWeight: FontWeight.w600,
),
),
TextSpan(
text: suffix,
style: const TextStyle(
color: Color(0xFF78787D),
fontSize: 13,
fontWeight: FontWeight.w500,
),
),
],
),
);
}
}
class _Avatar extends StatelessWidget {
const _Avatar({required this.url, required this.name});
final String? url;
final String name;
@override
Widget build(BuildContext context) {
final avatarUrl = url?.trim();
return CircleAvatar(
backgroundColor: const Color(0xFFF0EEF8),
backgroundImage: avatarUrl == null || avatarUrl.isEmpty
? null
: CachedNetworkImageProvider(avatarUrl),
child: avatarUrl == null || avatarUrl.isEmpty
? Text(
name.isEmpty ? '?' : name.characters.first,
style: TextStyle(color: context.colors.textPrimary),
)
: null,
);
}
}