today_date_strip.dart
3.5 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
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import '../../controllers/today_controller.dart';
/// Figma: 横向 7 日日期条,当前选中日高亮品牌色圆角背景
/// 支持左右滑动切换周次(PageView)
class TodayDateStrip extends GetView<TodayController> {
const TodayDateStrip({super.key});
static const _brandColor = Color(0xFF845EEE);
static const _h1Color = Color(0xFF0F0F11);
static const _h5Color = Color(0xFFCCCCCC);
/// 获取以本周一为起点的 7 天列表
List<DateTime> _getWeekDays(DateTime base) {
final monday = base.subtract(Duration(days: base.weekday - 1));
return List.generate(7, (i) => monday.add(Duration(days: i)));
}
String _weekdayLabel(int weekday) {
const labels = ['一', '二', '三', '四', '五', '六', '日'];
return labels[weekday - 1];
}
@override
Widget build(BuildContext context) {
return Obx(() {
final selected = controller.selectedDate.value;
final today = DateTime.now();
final days = _getWeekDays(selected);
return Container(
height: 72,
decoration: BoxDecoration(
color: _brandColor,
borderRadius: BorderRadius.circular(22),
),
child: Row(
children: days.map((day) {
final isSelected = _isSameDay(day, selected);
final isToday = _isSameDay(day, today);
final isFuture = day.isAfter(today);
return Expanded(
child: GestureDetector(
onTap: isFuture ? null : () => controller.changeDate(day),
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
// 周几
Text(
_weekdayLabel(day.weekday),
style: TextStyle(
fontSize: 11,
fontWeight: FontWeight.w400,
color: isFuture
? _h5Color
: (isSelected ? _h1Color : Colors.white70),
),
),
const SizedBox(height: 4),
// 日期数字
AnimatedContainer(
duration: const Duration(milliseconds: 180),
curve: Curves.easeOut,
width: 28,
height: 28,
decoration: BoxDecoration(
color: isSelected ? Colors.white : Colors.transparent,
borderRadius: BorderRadius.circular(14),
),
alignment: Alignment.center,
child: Text(
'${day.day}',
style: TextStyle(
fontSize: 15,
fontWeight: isSelected || isToday
? FontWeight.w600
: FontWeight.w400,
color: isSelected
? _brandColor
: isFuture
? _h5Color
: Colors.white,
),
),
),
],
),
),
);
}).toList(),
),
);
});
}
bool _isSameDay(DateTime a, DateTime b) {
return a.year == b.year && a.month == b.month && a.day == b.day;
}
}