today_date_strip.dart 3.5 KB
import 'package:flutter/material.dart';
import 'package:get/get.dart';

import '../../controllers/home_controller.dart';

/// Figma: 横向 7 日日期条,当前选中日高亮品牌色圆角背景
/// 支持左右滑动切换周次(PageView)
class TodayDateStrip extends GetView<HomeController> {
  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;
  }
}