创建configuration类,将配置的信息放到这里

引入provider状态管理,避免深层嵌套传递信息
周视图和月视图,联动
增加日志输出类LogUtil,方便查看调试
增加example例子
性能优化
develop
xiaodong 5 years ago
parent 6d9d752d16
commit d0da3b9e90

@ -315,3 +315,4 @@ bool operator ==(Object other)|重写==方法可以判断两个dateModel是
* 优化代码实现 * 优化代码实现
* 继续写几个不同风格的Demo * 继续写几个不同风格的Demo
* 支持手势操作 * 支持手势操作
* 实现高度自适应

@ -10,6 +10,7 @@ import 'progress_style_page.dart';
void main() { void main() {
// debugProfileBuildsEnabled=true; // debugProfileBuildsEnabled=true;
// debugProfilePaintsEnabled=true; // debugProfilePaintsEnabled=true;
// debugPrintRebuildDirtyWidgets=true;
runApp(MyApp()); runApp(MyApp());
} }

@ -13,12 +13,23 @@ import 'package:flutter_custom_calendar/widget/month_view.dart';
* rebuild * rebuild
*/ */
class CalendarProvider extends ChangeNotifier { class CalendarProvider extends ChangeNotifier {
double _totalHeight; //
Set<DateModel> selectedDateList = new Set(); //, Set<DateModel> selectedDateList = new Set(); //,
DateModel _selectDateModel; // DateModel _selectDateModel; //
ItemContainerState lastClickItemState; ItemContainerState lastClickItemState;
DateModel _lastClickDateModel; DateModel _lastClickDateModel;
double get totalHeight => _totalHeight;
set totalHeight(double value) {
_totalHeight = value;
}
changeTotalHeight(double value) {
_totalHeight = value;
notifyListeners();
}
DateModel get lastClickDateModel => DateModel get lastClickDateModel =>
_lastClickDateModel; // _lastClickDateModel; //

@ -158,8 +158,8 @@ class CalendarController {
dateTime = dateTime.add(Duration(days: 7))) { dateTime = dateTime.add(Duration(days: 7))) {
DateModel dateModel = DateModel.fromDateTime(dateTime); DateModel dateModel = DateModel.fromDateTime(dateTime);
weekList.add(dateModel); weekList.add(dateModel);
print("nowTime.isBefore(dateTime)"); // print("nowTime.isBefore(dateTime)");
print("$nowTime,,,,$dateTime"); // print("$nowTime,,,,$dateTime");
if (nowTime.isAfter(dateTime)) { if (nowTime.isAfter(dateTime)) {
temp++; temp++;
@ -230,7 +230,7 @@ class CalendarController {
calendarProvider.calendarConfiguration.monthController calendarProvider.calendarConfiguration.monthController
.previousPage(duration: DEFAULT_DURATION, curve: Curves.ease); .previousPage(duration: DEFAULT_DURATION, curve: Curves.ease);
calendarProvider.calendarConfiguration.monthChange( calendarProvider.calendarConfiguration.monthChange(
monthList[currentIndex].year, monthList[currentIndex].month); monthList[currentIndex-1].year, monthList[currentIndex-1].month);
DateModel temp = new DateModel(); DateModel temp = new DateModel();
temp.year = monthList[currentIndex].year; temp.year = monthList[currentIndex].year;
temp.month = monthList[currentIndex].month; temp.month = monthList[currentIndex].month;
@ -268,7 +268,7 @@ class CalendarController {
calendarProvider.calendarConfiguration.monthController calendarProvider.calendarConfiguration.monthController
.nextPage(duration: DEFAULT_DURATION, curve: Curves.ease); .nextPage(duration: DEFAULT_DURATION, curve: Curves.ease);
calendarProvider.calendarConfiguration.monthChange( calendarProvider.calendarConfiguration.monthChange(
monthList[currentIndex].year, monthList[currentIndex].month); monthList[currentIndex+1].year, monthList[currentIndex+1].month);
DateModel temp = new DateModel(); DateModel temp = new DateModel();
temp.year = monthList[currentIndex].year; temp.year = monthList[currentIndex].year;
temp.month = monthList[currentIndex].month; temp.month = monthList[currentIndex].month;

@ -124,27 +124,36 @@ class CalendarContainerState extends State<CalendarContainer>
}); });
} }
widget.calendarController.addMonthChangeListener((year, month) { // widget.calendarController.addMonthChangeListener((year, month) {
if (widget.calendarController.calendarProvider.calendarConfiguration // if (widget.calendarController.calendarProvider.calendarConfiguration
.showMode != // .showMode !=
Constants.MODE_SHOW_ONLY_WEEK) { // Constants.MODE_SHOW_ONLY_WEEK) {
//setState使 // //setState使
int lineCount = DateUtil.getMonthViewLineCount(year, month); // int lineCount = DateUtil.getMonthViewLineCount(year, month);
double newHeight = itemHeight * lineCount + // double newHeight = itemHeight * lineCount +
calendarProvider.calendarConfiguration.verticalSpacing * // calendarProvider.calendarConfiguration.verticalSpacing *
(lineCount - 1); // (lineCount - 1);
if (totalHeight.toInt() != newHeight.toInt()) { // if (totalHeight.toInt() != newHeight.toInt()) {
LogUtil.log( // LogUtil.log(
TAG: this.runtimeType, // TAG: this.runtimeType,
message: "totalHeight:$totalHeight,newHeight:$newHeight"); // message: "totalHeight:$totalHeight,newHeight:$newHeight");
//
LogUtil.log(TAG: this.runtimeType, message: "月份视图高度发生变化"); // LogUtil.log(TAG: this.runtimeType, message: "月份视图高度发生变化");
setState(() { // setState(() {
totalHeight = newHeight; // totalHeight = newHeight;
}); // });
} // }
// }
// });
//,,:a horizontal viewport was given an unlimited amount of I/flutter ( 6759): vertical space in which to expand.
itemHeight = calendarProvider.calendarConfiguration.itemSize ??
MediaQueryData.fromWindow(WidgetsBinding.instance.window).size.width /
7;
if (calendarProvider.totalHeight == null) {
calendarProvider.totalHeight = itemHeight * 6 +
calendarProvider.calendarConfiguration.verticalSpacing * (6 - 1);
} }
});
} }
@override @override

@ -1,6 +1,7 @@
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:flutter_custom_calendar/calendar_provider.dart'; import 'package:flutter_custom_calendar/calendar_provider.dart';
import 'package:flutter_custom_calendar/configuration.dart'; import 'package:flutter_custom_calendar/configuration.dart';
import 'package:flutter_custom_calendar/constants/constants.dart';
import 'package:flutter_custom_calendar/model/date_model.dart'; import 'package:flutter_custom_calendar/model/date_model.dart';
import 'package:flutter_custom_calendar/utils/LogUtil.dart'; import 'package:flutter_custom_calendar/utils/LogUtil.dart';
import 'package:flutter_custom_calendar/utils/date_util.dart'; import 'package:flutter_custom_calendar/utils/date_util.dart';
@ -17,10 +18,6 @@ class MonthViewPager extends StatefulWidget {
class _MonthViewPagerState extends State<MonthViewPager> { class _MonthViewPagerState extends State<MonthViewPager> {
CalendarProvider calendarProvider; CalendarProvider calendarProvider;
var totalHeight;
// PageController newPageController;
@override @override
void initState() { void initState() {
LogUtil.log(TAG: this.runtimeType, message: "MonthViewPager initState"); LogUtil.log(TAG: this.runtimeType, message: "MonthViewPager initState");
@ -84,19 +81,22 @@ class _MonthViewPagerState extends State<MonthViewPager> {
} }
//使PageView //使PageView
if (calendarProvider.calendarConfiguration.showMode !=
// double itemHeight = MediaQuery.of(context).size.width / 7; Constants.MODE_SHOW_ONLY_WEEK) {
// //setState使
// int lineCount = int lineCount =
// DateUtil.getMonthViewLineCount(dateModel.year, dateModel.month); DateUtil.getMonthViewLineCount(dateModel.year, dateModel.month);
// double newHeight = itemHeight * lineCount + double newHeight = (calendarProvider.calendarConfiguration.itemSize ??
// configuration.verticalSpacing * (lineCount - 1); MediaQuery.of(context).size.width / 7) *
// if(totalHeight!=newHeight){ lineCount +
// totalHeight=newHeight; calendarProvider.calendarConfiguration.verticalSpacing *
// setState(() { (lineCount - 1);
// if (calendarProvider.totalHeight.toInt() != newHeight.toInt()) {
// }); LogUtil.log(TAG: this.runtimeType, message: "月份视图高度发生变化");
// } calendarProvider.totalHeight = newHeight;
// calendarProvider.changeTotalHeight(newHeight);
}
}
}, },
controller: configuration.monthController, controller: configuration.monthController,
itemBuilder: (context, index) { itemBuilder: (context, index) {

Loading…
Cancel
Save