From cac3b44b33b2310def92e761fe2f3df450f74d27 Mon Sep 17 00:00:00 2001 From: LXD312569496 <450468291@qq.com> Date: Thu, 31 Oct 2019 14:04:44 +0800 Subject: [PATCH] =?UTF-8?q?=E6=97=A5=E5=8E=86=E5=A2=9E=E5=8A=A0padding?= =?UTF-8?q?=E5=92=8Cmargin=E5=B1=9E=E6=80=A7=20=E6=97=A5=E5=8E=86=E9=AB=98?= =?UTF-8?q?=E5=BA=A6=E8=87=AA=E9=80=82=E5=BA=94=20=E6=97=A5=E5=8E=86item?= =?UTF-8?q?=E5=A4=A7=E5=B0=8F=E8=AE=A1=E7=AE=97=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- example/lib/blue_style_page.dart | 253 ++++++++++++++++++++++++++++ example/lib/custom_sign_page.dart | 1 + example/lib/default_style_page.dart | 6 +- example/lib/main.dart | 69 ++++++++ lib/calendar_provider.dart | 54 +++++- lib/configuration.dart | 13 +- lib/controller.dart | 31 +++- lib/widget/calendar_view.dart | 82 +++++---- lib/widget/month_view.dart | 1 + lib/widget/month_view_pager.dart | 25 +-- lib/widget/week_view_pager.dart | 8 +- 11 files changed, 458 insertions(+), 85 deletions(-) create mode 100644 example/lib/blue_style_page.dart diff --git a/example/lib/blue_style_page.dart b/example/lib/blue_style_page.dart new file mode 100644 index 0000000..ab4a71f --- /dev/null +++ b/example/lib/blue_style_page.dart @@ -0,0 +1,253 @@ +import 'package:flutter/material.dart'; +import 'package:flutter_custom_calendar/flutter_custom_calendar.dart'; +import 'package:flutter_custom_calendar/style/style.dart'; +import 'package:random_pk/random_pk.dart'; + +/** + * 自定义一些额外的数据,实现标记功能 + */ +class BlueStylePage extends StatefulWidget { + BlueStylePage({Key key, this.title}) : super(key: key); + + final String title; + + @override + _BlueStylePageState createState() => _BlueStylePageState(); +} + +class _BlueStylePageState extends State { + ValueNotifier text; + ValueNotifier selectText; + + CalendarController controller; + + Map customExtraData = {}; + + @override + void initState() { + controller = new CalendarController( + weekBarItemWidgetBuilder: () { + return CustomStyleWeekBarItem(); + }, + dayWidgetBuilder: (dateModel) { + return CustomStyleDayWidget(dateModel); + }, + showMode: Constants.MODE_SHOW_MONTH_AND_WEEK, + extraDataMap: customExtraData); + + controller.addMonthChangeListener( + (year, month) { + text.value = "$year年$month月"; + }, + ); + + controller.addOnCalendarSelectListener((dateModel) { + //刷新选择的时间 + selectText.value = + "单选模式\n选中的时间:\n${controller.getSingleSelectCalendar()}"; + }); + + text = new ValueNotifier("${DateTime.now().year}年${DateTime.now().month}月"); + + selectText = new ValueNotifier( + "单选模式\n选中的时间:\n${controller.getSingleSelectCalendar()}"); + } + + @override + Widget build(BuildContext context) { + var calendarWidget = CalendarViewWidget( + calendarController: controller, + boxDecoration: BoxDecoration( + borderRadius: BorderRadius.only( + bottomLeft: Radius.circular(20), bottomRight: Radius.circular(20)), + color: Colors.white, + ), + padding: EdgeInsets.only(left: 20, right: 20, bottom: 10), + margin: const EdgeInsets.only( + left: 15, + right: 15, + ), + ); + + return SafeArea( + child: Scaffold( + appBar: AppBar( + backgroundColor: Color(0xff6219EC), + ), + backgroundColor: Color(0xff6219EC), + body: new Container( + child: new Column( + children: [ + SizedBox( + height: 20, + ), + Column( + crossAxisAlignment: CrossAxisAlignment.stretch, + mainAxisSize: MainAxisSize.min, + children: [ + Container( + height: 100, + margin: const EdgeInsets.only(left: 15, right: 15), + decoration: BoxDecoration( + borderRadius: BorderRadius.only( + topLeft: Radius.circular(20), + topRight: Radius.circular(20), + ), + color: Colors.white), + child: Row( + children: [ + Padding( + padding: const EdgeInsets.only(left: 30), + child: RichText( + text: TextSpan( + children: [ + TextSpan( + text: "Februaly", + style: TextStyle( + color: Colors.black, + fontWeight: FontWeight.w700, + fontSize: 30), + ), + TextSpan( + text: " 2019", + style: TextStyle( + color: Colors.black, fontSize: 30), + ), + ], + ), + ), + ), + ], + ), + ), + calendarWidget + ], + ), + ValueListenableBuilder( + valueListenable: selectText, + builder: (context, value, child) { + return new Text(selectText.value); + }), + ], + ), + ), + floatingActionButton: FloatingActionButton( + onPressed: () { + controller.toggleExpandStatus(); + }, + tooltip: 'Increment', + child: Icon(Icons.add), + ), // This trailing comma makes auto-formatting nicer for build methods. + ), + ); + } +} + +class CustomStyleWeekBarItem extends BaseWeekBar { + List weekList = ["mo", "tu", "we", "th", "fr", "sa", "su"]; + + //可以直接重写build方法 + @override + Widget build(BuildContext context) { + List children = List(); + + var items = getWeekDayWidget(); + children.add(Row( + children: items, + )); + children.add(Divider( + color: Colors.grey, + )); + return Column( + children: children, + ); + } + + @override + Widget getWeekBarItem(int index) { + return new Container( + margin: EdgeInsets.only(top: 10, bottom: 10), + child: new Center( + child: new Text( + weekList[index], + style: + TextStyle(fontWeight: FontWeight.w700, color: Color(0xffC5BCDC)), + ), + ), + ); + } +} + +class CustomStyleDayWidget extends BaseCombineDayWidget { + CustomStyleDayWidget(DateModel dateModel) : super(dateModel); + + TextStyle normalTextStyle = + TextStyle(fontWeight: FontWeight.w700, color: Colors.black); + + TextStyle noIsCurrentMonthTextStyle = + TextStyle(fontWeight: FontWeight.w700, color: Colors.grey); + + @override + Widget getNormalWidget(DateModel dateModel) { + return Container( + margin: EdgeInsets.all(8), + child: new Stack( + alignment: Alignment.center, + children: [ + new Column( + mainAxisSize: MainAxisSize.max, + crossAxisAlignment: CrossAxisAlignment.center, + children: [ + //公历 + new Expanded( + child: Center( + child: new Text( + dateModel.day.toString(), + style: dateModel.isCurrentMonth + ? normalTextStyle + : noIsCurrentMonthTextStyle, + ), + ), + ), + ], + ), + ], + ), + ); + } + + @override + Widget getSelectedWidget(DateModel dateModel) { + return Container( +// margin: EdgeInsets.all(8), + decoration: new BoxDecoration( + borderRadius: BorderRadius.all( + Radius.circular(5), + ), + color: Color(0xffFED32B), + ), + child: new Stack( + alignment: Alignment.center, + children: [ + new Column( + mainAxisSize: MainAxisSize.max, + crossAxisAlignment: CrossAxisAlignment.center, + children: [ + //公历 + new Expanded( + child: Center( + child: new Text( + dateModel.day.toString(), + style: dateModel.isCurrentMonth + ? normalTextStyle + : noIsCurrentMonthTextStyle, + ), + ), + ), + ], + ), + ], + ), + ); + } +} diff --git a/example/lib/custom_sign_page.dart b/example/lib/custom_sign_page.dart index 4ea59cb..74ad880 100644 --- a/example/lib/custom_sign_page.dart +++ b/example/lib/custom_sign_page.dart @@ -113,6 +113,7 @@ class _CustomSignPageState extends State { floatingActionButton: FloatingActionButton( onPressed: () { controller.toggleExpandStatus(); +// controller.changeExtraData({}); }, tooltip: 'Increment', child: Icon(Icons.add), diff --git a/example/lib/default_style_page.dart b/example/lib/default_style_page.dart index 455782f..39945b9 100644 --- a/example/lib/default_style_page.dart +++ b/example/lib/default_style_page.dart @@ -24,10 +24,10 @@ class _DefaultStylePageState extends State { DateTime now = DateTime.now(); controller = new CalendarController( minYear: now.year, - minYearMonth: now.month - 2, + minYearMonth: now.month - 5, maxYear: now.year, - maxYearMonth: now.month + 1, - showMode: Constants.MODE_SHOW_WEEK_AND_MONTH); + maxYearMonth: now.month + 5, + showMode: Constants.MODE_SHOW_MONTH_AND_WEEK); controller.addMonthChangeListener( (year, month) { diff --git a/example/lib/main.dart b/example/lib/main.dart index 9453339..4bcbd68 100644 --- a/example/lib/main.dart +++ b/example/lib/main.dart @@ -1,6 +1,7 @@ import 'package:example/only_week_page.dart'; import 'package:flutter/material.dart'; import 'package:flutter/rendering.dart'; +import 'blue_style_page.dart'; import 'custom_sign_page.dart'; import 'custom_style_page.dart'; import 'default_style_page.dart'; @@ -39,6 +40,9 @@ class MyApp extends StatelessWidget { "/only_week_view": (context) => OnlyWeekPage( title: "仅显示周视图", ), + "/blue_style_page":(context)=>BlueStylePage( + title:"蓝色背景Demo" + ) }, title: 'Flutter Demo', theme: ThemeData( @@ -91,9 +95,74 @@ class HomePage extends StatelessWidget { }, child: new Text("仅显示周视图"), ), + new RaisedButton(onPressed: (){ + Navigator.pushNamed(context, "/blue_style_page"); + },child: new Text("蓝色Demo"),) ], ), ), ); } } + +// +//# Desktop Flutter Example +// +//This is the standard Flutter template application, modified to run on desktop. +// +//The `linux` and `windows` directories serve as early prototypes of +//what will eventually become the `flutter create` templates for desktop, and will +//be evolving over time to better reflect that goal. The `macos` directory has +//now become a `flutter create` template, so is largely identical to what that +//command creates. +// +//## Building and Running +// +//See [the main project README](../README.md). +// +//To build without running, use `flutter build macos`/`windows`/`linux` rather than `flutter run`, as with +//a standard Flutter project. +// +//## Dart Differences from Flutter Template +// +//The `main.dart` and `pubspec.yaml` have minor changes to support desktop: +//* `debugDefaultTargetPlatformOverride` is set to avoid 'Unknown platform' +//exceptions. +//* The font is explicitly set to Roboto, and Roboto is bundled via +//`pubspec.yaml`, to ensure that text displays on all platforms. +// +//See the [Flutter Application Requirements section of the Flutter page on +//desktop support](https://github.com/flutter/flutter/wiki/Desktop-shells#flutter-application-requirements) +//for more information. +// +//## Adapting for Another Project +// +//Since `flutter create` is not yet supported for Windows and Linux, the easiest +//way to try out desktop support with an existing Flutter application on those +//platforms is to copy the platform directories from this example; see below for +//details. For macOS, just run `flutter create --macos .` in your project. +// +//Be sure to read the [Flutter page on desktop +//support](https://github.com/flutter/flutter/wiki/Desktop-shells) before trying to +//run an existing project on desktop, especially the [Flutter Application Requirements +//section](https://github.com/flutter/flutter/wiki/Desktop-shells#flutter-application-requirements). +// +//### Coping the Desktop Runners +// +//The 'linux' and 'windows' directories are self-contained, and can be copied to +//an existing Flutter project, enabling `flutter run` for those platforms. +// +//**Be aware that neither the API surface of the Flutter desktop libraries nor the +//interaction between the `flutter` tool and the platform directories is stable, +//and no attempt will be made to provide supported migration paths as things +//change.** You should expect that every time you update Flutter you may have +//to delete your copies of the platform directories and re-copy them from an +//updated version of flutter-desktop-embedding. +// +//### Customizing the Runners +// +//See [Application Customization](App-Customization.md) for premilinary +//documenation on modifying basic application information like name and icon. +// +//If you are building for macOS, you should also read about [managing macOS +//security configurations](../macOS-Security.md). diff --git a/lib/calendar_provider.dart b/lib/calendar_provider.dart index 888ec43..846e4a1 100644 --- a/lib/calendar_provider.dart +++ b/lib/calendar_provider.dart @@ -21,6 +21,15 @@ class CalendarProvider extends ChangeNotifier { double get totalHeight => _totalHeight; + ValueNotifier _generation = + new ValueNotifier(0); //生成的int值,每次变化,都会刷新整个日历。 + + ValueNotifier get generation => _generation; + + set generation(ValueNotifier value) { + _generation = value; + } + set totalHeight(double value) { _totalHeight = value; } @@ -95,27 +104,64 @@ class CalendarProvider extends ChangeNotifier { //配置类也放这里吧,这样的话,所有子树,都可以拿到配置的信息 CalendarConfiguration calendarConfiguration; - void initData( - {Set selectedDateList, - DateModel selectDateModel, - CalendarConfiguration calendarConfiguration}) { + void initData({ + Set selectedDateList, + DateModel selectDateModel, + CalendarConfiguration calendarConfiguration, + EdgeInsetsGeometry padding, + EdgeInsetsGeometry margin, + }) { LogUtil.log(TAG: this.runtimeType, message: "CalendarProvider initData"); if (selectedDateList != null) { this.selectedDateList.addAll(selectedDateList); } this.selectDateModel = selectDateModel; this.calendarConfiguration = calendarConfiguration; + this.calendarConfiguration.padding = padding; + this.calendarConfiguration.margin = margin; //lastClickDateModel,默认是选中的item,如果为空的话,默认是当前的时间 this.lastClickDateModel = selectDateModel != null ? selectDateModel : DateModel.fromDateTime(DateTime.now()) ..day = 15; + //初始化展示状态 if (calendarConfiguration.showMode == Constants.MODE_SHOW_ONLY_WEEK || calendarConfiguration.showMode == Constants.MODE_SHOW_WEEK_AND_MONTH) { expandStatus = ValueNotifier(false); } else { expandStatus = ValueNotifier(true); } + //初始化item的大小。如果itemSize为空,默认是宽度/7。网页版的话是高度/7。需要减去padding和margin值 + if (calendarConfiguration.itemSize == null) { + MediaQueryData mediaQueryData = + MediaQueryData.fromWindow(WidgetsBinding.instance.window); + if (mediaQueryData.orientation == Orientation.landscape) { + calendarConfiguration.itemSize = (mediaQueryData.size.height - + calendarConfiguration.padding.horizontal - + calendarConfiguration.margin.horizontal) / + 7; + } else { + calendarConfiguration.itemSize = (mediaQueryData.size.width - + calendarConfiguration.padding.horizontal - + calendarConfiguration.margin.horizontal) / + 7; + } + } else { + //如果指定了itemSize的大小,那就按照itemSize的大小去绘制 + } + + //如果第一个页面展示的是月视图,需要计算下初始化的高度 + if (calendarConfiguration.showMode == Constants.MODE_SHOW_ONLY_MONTH || + calendarConfiguration.showMode == Constants.MODE_SHOW_MONTH_AND_WEEK) { + int lineCount = DateUtil.getMonthViewLineCount( + calendarConfiguration.nowYear, calendarConfiguration.nowMonth); + totalHeight = calendarConfiguration.itemSize * (lineCount) + + calendarConfiguration.verticalSpacing * (lineCount - 1); + print( + "1111111:totalHeight:$totalHeight,lineCount:$lineCount,calendarConfiguration.nowYear:${calendarConfiguration.nowYear},calendarConfiguration.nowMonth:${calendarConfiguration.nowMonth}"); + } else { + totalHeight = calendarConfiguration.itemSize; + } } //退出的时候,清除数据 diff --git a/lib/configuration.dart b/lib/configuration.dart index eeca2fc..798c3cd 100644 --- a/lib/configuration.dart +++ b/lib/configuration.dart @@ -1,6 +1,7 @@ import 'package:flutter/material.dart'; import 'package:flutter_custom_calendar/controller.dart'; import 'package:flutter_custom_calendar/model/date_model.dart'; +import 'package:flutter/foundation.dart'; /** * 配置信息类 @@ -43,6 +44,8 @@ class CalendarConfiguration { double verticalSpacing; //日历item之间的竖直方向间距,默认10 BoxDecoration boxDecoration; //整体的背景设置 double itemSize; //默认是屏幕宽度/7 + EdgeInsetsGeometry padding; + EdgeInsetsGeometry margin; //支持自定义绘制 DayWidgetBuilder dayWidgetBuilder; //创建日历item @@ -52,11 +55,14 @@ class CalendarConfiguration { * 监听变化 */ //各种事件回调 - OnMonthChange monthChange; //月份切换事件 + OnMonthChange monthChange; //月份切换事件 (已弃用,交给multiMonthChanges来实现) OnCalendarSelect calendarSelect; //点击选择事件 OnMultiSelectOutOfRange multiSelectOutOfRange; //多选超出指定范围 OnMultiSelectOutOfSize multiSelectOutOfSize; //多选超出限制个数 + ObserverList monthChangeListeners = + ObserverList(); //保存多个月份监听的事件 + /** * 下面的信息不是配置的,是根据配置信息进行计算出来的 */ @@ -87,7 +93,10 @@ class CalendarConfiguration { this.monthController, this.weekController, this.verticalSpacing, - this.itemSize,this.showMode}); + this.itemSize, + this.showMode, + this.padding, + this.margin}); @override String toString() { diff --git a/lib/controller.dart b/lib/controller.dart index d61432b..d131673 100644 --- a/lib/controller.dart +++ b/lib/controller.dart @@ -39,8 +39,8 @@ class CalendarController { int maxYear = 2055, int minYearMonth = 1, int maxYearMonth = 12, - int nowYear = -1, - int nowMonth = -1, + int nowYear, + int nowMonth, int minSelectYear = 1971, int minSelectMonth = 1, int minSelectDay = 1, @@ -54,6 +54,13 @@ class CalendarController { double itemSize, Map extraDataMap = EMPTY_MAP}) { LogUtil.log(TAG: this.runtimeType, message: "init CalendarConfiguration"); + //如果没有指定当前月份和年份,默认是当年时间 + if (nowYear == null) { + nowYear = DateTime.now().year; + } + if (nowMonth == null) { + nowMonth = DateTime.now().month; + } calendarConfiguration = CalendarConfiguration( selectMode: selectMode, showMode: showMode, @@ -181,7 +188,8 @@ class CalendarController { //月份切换监听 void addMonthChangeListener(OnMonthChange listener) { - this.calendarConfiguration.monthChange = listener; +// this.calendarConfiguration.monthChange = listener; + this.calendarConfiguration.monthChangeListeners.add(listener); } //点击选择监听 @@ -233,9 +241,11 @@ class CalendarController { } else { calendarProvider.calendarConfiguration.monthController .previousPage(duration: DEFAULT_DURATION, curve: Curves.ease); - calendarProvider.calendarConfiguration.monthChange( - monthList[currentIndex - 1].year, - monthList[currentIndex - 1].month); + calendarProvider.calendarConfiguration.monthChangeListeners + .forEach((listener) { + listener(monthList[currentIndex - 1].year, + monthList[currentIndex - 1].month); + }); DateModel temp = new DateModel(); temp.year = monthList[currentIndex].year; temp.month = monthList[currentIndex].month; @@ -272,9 +282,12 @@ class CalendarController { } else { calendarProvider.calendarConfiguration.monthController .nextPage(duration: DEFAULT_DURATION, curve: Curves.ease); - calendarProvider.calendarConfiguration.monthChange( - monthList[currentIndex + 1].year, - monthList[currentIndex + 1].month); + calendarProvider.calendarConfiguration.monthChangeListeners + .forEach((listener) { + listener(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 b403203..f5d6c7b 100644 --- a/lib/widget/calendar_view.dart +++ b/lib/widget/calendar_view.dart @@ -19,11 +19,19 @@ class CalendarViewWidget extends StatefulWidget { //整体的背景设置 BoxDecoration boxDecoration; + //日历的padding和margin + EdgeInsetsGeometry padding; + EdgeInsetsGeometry margin; + //控制器 final CalendarController calendarController; CalendarViewWidget( - {Key key, @required this.calendarController, this.boxDecoration}) + {Key key, + @required this.calendarController, + this.boxDecoration, + this.padding = EdgeInsets.zero, + this.margin = EdgeInsets.zero}) : super(key: key); @override @@ -35,7 +43,9 @@ class _CalendarViewWidgetState extends State { void initState() { //初始化一些数据,一些跟状态有关的要放到provider中 widget.calendarController.calendarProvider.initData( - calendarConfiguration: widget.calendarController.calendarConfiguration); + calendarConfiguration: widget.calendarController.calendarConfiguration, + padding: widget.padding, + margin: widget.margin); super.initState(); } @@ -53,6 +63,8 @@ class _CalendarViewWidgetState extends State { child: Container( //外部可以自定义背景设置 decoration: widget.boxDecoration, + padding: widget.padding, + margin: widget.margin, //使用const,保证外界的setState不会刷新日历这个widget child: CalendarContainer(widget.calendarController)), ); @@ -128,47 +140,32 @@ class CalendarContainerState extends State } }); }); - }else{ - index=0; - + } else { + index = 0; } -// 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. - - MediaQueryData mediaQueryData = - MediaQueryData.fromWindow(WidgetsBinding.instance.window); - - //如果itemSize为空,默认是宽度/7。网页版的话是高度/7 - itemHeight = calendarProvider.calendarConfiguration.itemSize != null - ? calendarProvider.calendarConfiguration.itemSize - : mediaQueryData.orientation == Orientation.landscape - ? mediaQueryData.size.height / 7 - : mediaQueryData.size.width / 7; - - calendarProvider.totalHeight = itemHeight * 6 + - calendarProvider.calendarConfiguration.verticalSpacing * (6 - 1); + 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); + LogUtil.log( + TAG: this.runtimeType, + message: "totalHeight:$totalHeight,newHeight:$newHeight"); + if (totalHeight.toInt() != newHeight.toInt()) { + LogUtil.log(TAG: this.runtimeType, message: "月份视图高度发生变化"); + setState(() { + totalHeight = newHeight; + }); + } + } + }); + + itemHeight = calendarProvider.calendarConfiguration.itemSize; totalHeight = calendarProvider.totalHeight; } @@ -180,9 +177,6 @@ class CalendarContainerState extends State @override Widget build(BuildContext context) { LogUtil.log(TAG: this.runtimeType, message: "CalendarContainerState build"); - //暂时先这样写死,提前计算布局的高度,不然会出现问题:a horizontal viewport was given an unlimited amount of I/flutter ( 6759): vertical space in which to expand. -// itemHeight = calendarProvider.calendarConfiguration.itemSize ?? -// MediaQuery.of(context).size.width / 7; return Container( width: itemHeight * 7, child: new Column( diff --git a/lib/widget/month_view.dart b/lib/widget/month_view.dart index 019ea69..6b3a8d5 100644 --- a/lib/widget/month_view.dart +++ b/lib/widget/month_view.dart @@ -117,6 +117,7 @@ class _MonthViewState extends State return new GridView.builder( addAutomaticKeepAlives: true, + padding: EdgeInsets.zero, physics: const NeverScrollableScrollPhysics(), gridDelegate: SliverGridDelegateWithFixedCrossAxisCount( crossAxisCount: 7, mainAxisSpacing: configuration.verticalSpacing), diff --git a/lib/widget/month_view_pager.dart b/lib/widget/month_view_pager.dart index 9fceaab..358d442 100644 --- a/lib/widget/month_view_pager.dart +++ b/lib/widget/month_view_pager.dart @@ -15,7 +15,8 @@ class MonthViewPager extends StatefulWidget { _MonthViewPagerState createState() => _MonthViewPagerState(); } -class _MonthViewPagerState extends State with AutomaticKeepAliveClientMixin{ +class _MonthViewPagerState extends State + with AutomaticKeepAliveClientMixin { CalendarProvider calendarProvider; @override @@ -71,7 +72,9 @@ class _MonthViewPagerState extends State with AutomaticKeepAlive } //月份的变化 DateModel dateModel = configuration.monthList[position]; - configuration.monthChange(dateModel.year, dateModel.month); + configuration.monthChangeListeners.forEach((listener) { + listener(dateModel.year, dateModel.month); + }); //用来保存临时变量,用于月视图切换到周视图的时候,默认是显示中间的一周 if (calendarProvider.lastClickDateModel != null || calendarProvider.lastClickDateModel.month != dateModel.month) { @@ -81,24 +84,6 @@ class _MonthViewPagerState extends State with AutomaticKeepAlive temp.day = configuration.monthList[position].day + 14; calendarProvider.lastClickDateModel = temp; } - - //计算下高度,使PageView自适应高度 - 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) { diff --git a/lib/widget/week_view_pager.dart b/lib/widget/week_view_pager.dart index 0ba52e8..ef9f06d 100644 --- a/lib/widget/week_view_pager.dart +++ b/lib/widget/week_view_pager.dart @@ -14,7 +14,8 @@ class WeekViewPager extends StatefulWidget { _WeekViewPagerState createState() => _WeekViewPagerState(); } -class _WeekViewPagerState extends State with AutomaticKeepAliveClientMixin{ +class _WeekViewPagerState extends State + with AutomaticKeepAliveClientMixin { int lastMonth; //保存上一个月份,不然不知道月份发生了变化 CalendarProvider calendarProvider; @@ -64,8 +65,9 @@ class _WeekViewPagerState extends State with AutomaticKeepAliveCl TAG: this.runtimeType, message: "WeekViewPager PageView monthChange:currentMonth:${currentMonth}"); - configuration.monthChange( - firstDayOfWeek.year, firstDayOfWeek.month); + configuration.monthChangeListeners.forEach((listener) { + listener(firstDayOfWeek.year, firstDayOfWeek.month); + }); lastMonth = currentMonth; if (calendarProvider.lastClickDateModel == null || calendarProvider.lastClickDateModel.month != currentMonth) {