You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
flutter_custom_calendar/lib/widget/week_view_pager.dart

107 lines
3.8 KiB

import 'package:flutter/material.dart';
import 'package:flutter_custom_calendar/widget/week_view.dart';
import 'package:provider/provider.dart';
5 years ago
import 'package:flutter/foundation.dart';
import 'package:flutter_custom_calendar/configuration.dart';
import 'package:flutter_custom_calendar/flutter_custom_calendar.dart';
import 'package:flutter_custom_calendar/utils/LogUtil.dart';
class WeekViewPager extends StatefulWidget {
3 years ago
const WeekViewPager({Key? key}) : super(key: key);
@override
_WeekViewPagerState createState() => _WeekViewPagerState();
}
class _WeekViewPagerState extends State<WeekViewPager>
with AutomaticKeepAliveClientMixin {
3 years ago
int? lastMonth; //保存上一个月份,不然不知道月份发生了变化
late CalendarProvider calendarProvider;
// PageController newPageController;
@override
void initState() {
super.initState();
LogUtil.log(TAG: this.runtimeType, message: "WeekViewPager initState");
calendarProvider = Provider.of<CalendarProvider>(context, listen: false);
3 years ago
lastMonth = calendarProvider.lastClickDateModel!.month;
}
@override
void dispose() {
LogUtil.log(TAG: this.runtimeType, message: "WeekViewPager dispose");
super.dispose();
}
@override
Widget build(BuildContext context) {
5 years ago
super.build(context);
LogUtil.log(TAG: this.runtimeType, message: "WeekViewPager build");
// 获取到当前的CalendarProvider对象,设置listen为false不需要刷新
CalendarProvider calendarProvider =
Provider.of<CalendarProvider>(context, listen: false);
CalendarConfiguration configuration =
3 years ago
calendarProvider.calendarConfiguration!;
return Container(
height: configuration.itemSize ?? MediaQuery.of(context).size.width / 7,
child: PageView.builder(
onPageChanged: (position) {
if (calendarProvider.expandStatus.value == true) {
return;
}
LogUtil.log(
TAG: this.runtimeType,
message:
"WeekViewPager PageView onPageChanged,position:$position");
3 years ago
DateModel firstDayOfWeek = configuration.weekList![position];
int? currentMonth = firstDayOfWeek.month;
// 周视图的变化
3 years ago
configuration.weekChangeListeners!.forEach((listener) {
listener(firstDayOfWeek.year, firstDayOfWeek.month);
});
if (lastMonth != currentMonth) {
LogUtil.log(
TAG: this.runtimeType,
message:
"WeekViewPager PageView monthChange:currentMonth:$currentMonth");
3 years ago
configuration.monthChangeListeners!.forEach((listener) {
listener(firstDayOfWeek.year, firstDayOfWeek.month);
});
lastMonth = currentMonth;
if (calendarProvider.lastClickDateModel == null ||
3 years ago
calendarProvider.lastClickDateModel!.month != currentMonth) {
DateModel temp = new DateModel();
temp.year = firstDayOfWeek.year;
temp.month = firstDayOfWeek.month;
temp.day = firstDayOfWeek.day + 14;
calendarProvider.lastClickDateModel = temp;
}
}
// calendarProvider.lastClickDateModel = configuration.weekList[position]
// ..day += 4;
},
3 years ago
controller: calendarProvider.calendarConfiguration!.weekController,
itemBuilder: (context, index) {
3 years ago
DateModel dateModel = configuration.weekList![index];
return new WeekView(
year: dateModel.year,
month: dateModel.month,
firstDayOfWeek: dateModel,
configuration: calendarProvider.calendarConfiguration,
);
},
3 years ago
itemCount: configuration.weekList!.length,
),
);
}
5 years ago
@override
bool get wantKeepAlive => true;
}