Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ class TDCalendar extends StatefulWidget {
this.isTimeUnit = true,
this.animateTo = false,
this.cellWidget,
this.onMonthChange,
}) : super(key: key);

/// 第一天从星期几开始,默认 0 = 周日
Expand Down Expand Up @@ -112,6 +113,9 @@ class TDCalendar extends StatefulWidget {
String week,
)? onHeaderClick;

/// 月份变化时触发
final ValueChanged<DateTime>? onMonthChange;

/// 是否使用安全区域,默认true
final bool? useSafeArea;

Expand Down Expand Up @@ -168,7 +172,6 @@ class _TDCalendarState extends State<TDCalendar> {
late TDCalendarInherited? inherited;
late TDCalendarStyle _style;
final List<DatePickerModel> timePickerModelList = [];

@override
void didChangeDependencies() {
super.didChangeDependencies();
Expand Down Expand Up @@ -243,6 +246,7 @@ class _TDCalendarState extends State<TDCalendar> {
monthTitleHeight: widget.monthTitleHeight ?? 22,
monthTitleBuilder: widget.monthTitleBuilder,
animateTo: widget.animateTo ?? false,
onMonthChange: widget.onMonthChange,
builder: (date, dateList, data, rowIndex, colIndex) {
return TDCalendarCell(
height: widget.cellHeight ?? 60,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ class TDCalendarBody extends StatelessWidget {
required this.monthTitleHeight,
required this.verticalGap,
required this.animateTo,
this.onMonthChange,
}) : super(key: key);

final int? maxDate;
Expand All @@ -50,15 +51,37 @@ class TDCalendarBody extends StatelessWidget {
final double verticalGap;
final double cellHeight;
final bool animateTo;
final ValueChanged<DateTime>? onMonthChange;

@override
Widget build(BuildContext context) {
final scrollController = ScrollController();
final scrollController = TrackingScrollController();
final min = _getDefDate(minDate);
final max = _getDefDate(maxDate, 6);
final months = _monthsBetween(min, max);
final data = <DateTime, List<TDate?>>{};
final monthHeight = <int, double>{};
DateTime? _lastPrintMonth;
scrollController.addListener(() {
// 根据滚动位置判断当前是几月
var currentOffset = 0.0;
for (var i = 0; i < months.length; i++) {
final mh = _getMonthHeight(months, i, monthHeight);
if (scrollController.offset >= currentOffset &&
scrollController.offset < currentOffset + mh) {
//只返回下一个月
DateTime currentMonth = months[i + 1];
// 缓存上一次打印的月份,只有变更时才打印
if (_lastPrintMonth == null ||
!_lastPrintMonth!.isAtSameMomentAs(currentMonth)) {
_lastPrintMonth = currentMonth;
onMonthChange?.call(currentMonth);
}
break;
}
currentOffset += mh;
}
});
_scrollToItem(scrollController, months, monthHeight);
return ListView.builder(
padding: EdgeInsets.all(bodyPadding),
Expand Down