import 'package:wanisclient/utils/colors.dart'; import 'package:flutter/cupertino.dart'; import 'package:flutter/material.dart'; import 'package:intl/intl.dart'; class monthGrid extends StatelessWidget { final DateTime monthStart; final DateTime selected; final ValueChanged onSelect; const monthGrid({ required this.monthStart, required this.selected, required this.onSelect, }); @override Widget build(BuildContext context) { final daysInMonth = DateUtils.getDaysInMonth(monthStart.year, monthStart.month); final firstWeekday = _mondayFirstOffset(monthStart); final totalCells = firstWeekday + daysInMonth; final rows = (totalCells / 7).ceil(); final now = DateTime.now(); final today = DateTime(now.year, now.month, now.day); return Padding( padding: const EdgeInsets.only(bottom: 12), child: Column( crossAxisAlignment: CrossAxisAlignment.start, children: [ // month title Padding( padding: const EdgeInsets.symmetric(vertical: 8), child: Text( DateFormat('MMMM').format(monthStart), style: const TextStyle(fontSize: 16, fontWeight: FontWeight.w600, color: Colors.black87), ), ), // grid LayoutBuilder( builder: (context, c) { final cellW = (c.maxWidth) / 7; final cellH = cellW * .9; return Column( children: List.generate(rows, (r) { return Row( children: List.generate(7, (col) { final idx = r * 7 + col; final dayNum = idx - firstWeekday + 1; DateTime? date; bool enabled = false; if (dayNum >= 1 && dayNum <= daysInMonth) { date = DateTime(monthStart.year, monthStart.month, dayNum); enabled = !date.isBefore(today); } final isSelected = date != null && date.year == selected.year && date.month == selected.month && date.day == selected.day; return SizedBox( width: cellW, height: cellH, child: Center( child: date == null ? const SizedBox.shrink() : InkWell( borderRadius: BorderRadius.circular(20), onTap: enabled ? () => onSelect(date!) : null, child: Container( width: 34, height: 34, decoration: BoxDecoration( color: isSelected ? AppNewColors.primaryBtnColor : Colors.transparent, borderRadius: BorderRadius.circular(18), ), alignment: Alignment.center, child: Text( '$dayNum', style: TextStyle( fontSize: 14, fontWeight: FontWeight.w600, color: enabled ? (isSelected ? Colors.white : Colors.black87) : const Color(0xFFBDBDBD), ), ), ), ), ), ); }), ); }), ); }, ), ], ), ); } // convert DateTime.weekday (Mon=1..Sun=7) to 0-based offset with Monday first int _mondayFirstOffset(DateTime firstOfMonth) { final w = firstOfMonth.weekday; // 1..7 return (w + 6) % 7; } }