From d0da3b9e90839e95fb29b8d11d1d8d83f14225b3 Mon Sep 17 00:00:00 2001 From: xiaodong <450468291@qq.com> Date: Wed, 9 Oct 2019 18:39:27 +0800 Subject: [PATCH] =?UTF-8?q?=E5=88=9B=E5=BB=BAconfiguration=E7=B1=BB?= =?UTF-8?q?=EF=BC=8C=E5=B0=86=E9=85=8D=E7=BD=AE=E7=9A=84=E4=BF=A1=E6=81=AF?= =?UTF-8?q?=E6=94=BE=E5=88=B0=E8=BF=99=E9=87=8C=20=E5=BC=95=E5=85=A5provid?= =?UTF-8?q?er=E7=8A=B6=E6=80=81=E7=AE=A1=E7=90=86,=E9=81=BF=E5=85=8D?= =?UTF-8?q?=E6=B7=B1=E5=B1=82=E5=B5=8C=E5=A5=97=E4=BC=A0=E9=80=92=E4=BF=A1?= =?UTF-8?q?=E6=81=AF=20=E5=91=A8=E8=A7=86=E5=9B=BE=E5=92=8C=E6=9C=88?= =?UTF-8?q?=E8=A7=86=E5=9B=BE=EF=BC=8C=E8=81=94=E5=8A=A8=20=E5=A2=9E?= =?UTF-8?q?=E5=8A=A0=E6=97=A5=E5=BF=97=E8=BE=93=E5=87=BA=E7=B1=BBLogUtil?= =?UTF-8?q?=EF=BC=8C=E6=96=B9=E4=BE=BF=E6=9F=A5=E7=9C=8B=E8=B0=83=E8=AF=95?= =?UTF-8?q?=20=E5=A2=9E=E5=8A=A0example=E4=BE=8B=E5=AD=90=20=E6=80=A7?= =?UTF-8?q?=E8=83=BD=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 1 + example/lib/main.dart | 1 + lib/calendar_provider.dart | 13 +++++++- lib/controller.dart | 8 ++--- lib/widget/calendar_view.dart | 51 +++++++++++++++++++------------- lib/widget/month_view_pager.dart | 34 ++++++++++----------- 6 files changed, 65 insertions(+), 43 deletions(-) diff --git a/README.md b/README.md index 0f36d93..175c43a 100644 --- a/README.md +++ b/README.md @@ -315,3 +315,4 @@ bool operator ==(Object other)|重写==方法,可以判断两个dateModel是 * 优化代码实现 * 继续写几个不同风格的Demo * 支持手势操作 +* 实现高度自适应 diff --git a/example/lib/main.dart b/example/lib/main.dart index 0428860..9453339 100644 --- a/example/lib/main.dart +++ b/example/lib/main.dart @@ -10,6 +10,7 @@ import 'progress_style_page.dart'; void main() { // debugProfileBuildsEnabled=true; // debugProfilePaintsEnabled=true; +// debugPrintRebuildDirtyWidgets=true; runApp(MyApp()); } diff --git a/lib/calendar_provider.dart b/lib/calendar_provider.dart index 4d30ca6..b98daab 100644 --- a/lib/calendar_provider.dart +++ b/lib/calendar_provider.dart @@ -13,12 +13,23 @@ import 'package:flutter_custom_calendar/widget/month_view.dart'; * 目前的情况:只需要获取状态,不需要监听rebuild */ class CalendarProvider extends ChangeNotifier { + double _totalHeight; //当前月视图的整体高度 Set selectedDateList = new Set(); //被选中的日期,用于多选 DateModel _selectDateModel; //当前选中的日期,用于单选 ItemContainerState lastClickItemState; - DateModel _lastClickDateModel; + double get totalHeight => _totalHeight; + + set totalHeight(double value) { + _totalHeight = value; + } + + changeTotalHeight(double value) { + _totalHeight = value; + notifyListeners(); + } + DateModel get lastClickDateModel => _lastClickDateModel; //保存最后点击的一个日期,用于周视图与月视图之间的切换和同步 diff --git a/lib/controller.dart b/lib/controller.dart index a24d8f2..386aa86 100644 --- a/lib/controller.dart +++ b/lib/controller.dart @@ -158,8 +158,8 @@ class CalendarController { dateTime = dateTime.add(Duration(days: 7))) { DateModel dateModel = DateModel.fromDateTime(dateTime); weekList.add(dateModel); - print("nowTime.isBefore(dateTime)"); - print("$nowTime,,,,$dateTime"); +// print("nowTime.isBefore(dateTime)"); +// print("$nowTime,,,,$dateTime"); if (nowTime.isAfter(dateTime)) { temp++; @@ -230,7 +230,7 @@ class CalendarController { calendarProvider.calendarConfiguration.monthController .previousPage(duration: DEFAULT_DURATION, curve: Curves.ease); calendarProvider.calendarConfiguration.monthChange( - monthList[currentIndex].year, monthList[currentIndex].month); + monthList[currentIndex-1].year, monthList[currentIndex-1].month); DateModel temp = new DateModel(); temp.year = monthList[currentIndex].year; temp.month = monthList[currentIndex].month; @@ -268,7 +268,7 @@ class CalendarController { calendarProvider.calendarConfiguration.monthController .nextPage(duration: DEFAULT_DURATION, curve: Curves.ease); calendarProvider.calendarConfiguration.monthChange( - monthList[currentIndex].year, monthList[currentIndex].month); + monthList[currentIndex+1].year, monthList[currentIndex+1].month); DateModel temp = new DateModel(); temp.year = monthList[currentIndex].year; temp.month = monthList[currentIndex].month; diff --git a/lib/widget/calendar_view.dart b/lib/widget/calendar_view.dart index d04cd0f..e0a1ffa 100644 --- a/lib/widget/calendar_view.dart +++ b/lib/widget/calendar_view.dart @@ -124,27 +124,36 @@ class CalendarContainerState extends State }); } - widget.calendarController.addMonthChangeListener((year, month) { - if (widget.calendarController.calendarProvider.calendarConfiguration - .showMode != - Constants.MODE_SHOW_ONLY_WEEK) { - //月份切换的时候,如果高度发生变化的话,需要setState使高度整体自适应 - int lineCount = DateUtil.getMonthViewLineCount(year, month); - double newHeight = itemHeight * lineCount + - calendarProvider.calendarConfiguration.verticalSpacing * - (lineCount - 1); - if (totalHeight.toInt() != newHeight.toInt()) { - LogUtil.log( - TAG: this.runtimeType, - message: "totalHeight:$totalHeight,newHeight:$newHeight"); - - LogUtil.log(TAG: this.runtimeType, message: "月份视图高度发生变化"); - setState(() { - totalHeight = newHeight; - }); - } - } - }); +// widget.calendarController.addMonthChangeListener((year, month) { +// if (widget.calendarController.calendarProvider.calendarConfiguration +// .showMode != +// Constants.MODE_SHOW_ONLY_WEEK) { +// //月份切换的时候,如果高度发生变化的话,需要setState使高度整体自适应 +// int lineCount = DateUtil.getMonthViewLineCount(year, month); +// double newHeight = itemHeight * lineCount + +// calendarProvider.calendarConfiguration.verticalSpacing * +// (lineCount - 1); +// if (totalHeight.toInt() != newHeight.toInt()) { +// LogUtil.log( +// TAG: this.runtimeType, +// message: "totalHeight:$totalHeight,newHeight:$newHeight"); +// +// LogUtil.log(TAG: this.runtimeType, message: "月份视图高度发生变化"); +// setState(() { +// 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 diff --git a/lib/widget/month_view_pager.dart b/lib/widget/month_view_pager.dart index ca47e92..4e140bd 100644 --- a/lib/widget/month_view_pager.dart +++ b/lib/widget/month_view_pager.dart @@ -1,6 +1,7 @@ import 'package:flutter/material.dart'; import 'package:flutter_custom_calendar/calendar_provider.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/utils/LogUtil.dart'; import 'package:flutter_custom_calendar/utils/date_util.dart'; @@ -17,10 +18,6 @@ class MonthViewPager extends StatefulWidget { class _MonthViewPagerState extends State { CalendarProvider calendarProvider; - var totalHeight; - -// PageController newPageController; - @override void initState() { LogUtil.log(TAG: this.runtimeType, message: "MonthViewPager initState"); @@ -84,19 +81,22 @@ class _MonthViewPagerState extends State { } //计算下高度,使PageView自适应高度 - -// double itemHeight = MediaQuery.of(context).size.width / 7; -// -// int lineCount = -// DateUtil.getMonthViewLineCount(dateModel.year, dateModel.month); -// double newHeight = itemHeight * lineCount + -// configuration.verticalSpacing * (lineCount - 1); -// if(totalHeight!=newHeight){ -// totalHeight=newHeight; -// setState(() { -// -// }); -// } + if (calendarProvider.calendarConfiguration.showMode != + Constants.MODE_SHOW_ONLY_WEEK) { + //月份切换的时候,如果高度发生变化的话,需要setState使高度整体自适应 + int lineCount = + DateUtil.getMonthViewLineCount(dateModel.year, dateModel.month); + double newHeight = (calendarProvider.calendarConfiguration.itemSize ?? + MediaQuery.of(context).size.width / 7) * + lineCount + + calendarProvider.calendarConfiguration.verticalSpacing * + (lineCount - 1); + if (calendarProvider.totalHeight.toInt() != newHeight.toInt()) { + LogUtil.log(TAG: this.runtimeType, message: "月份视图高度发生变化"); + calendarProvider.totalHeight = newHeight; +// calendarProvider.changeTotalHeight(newHeight); + } + } }, controller: configuration.monthController, itemBuilder: (context, index) {