diff --git a/CHANGELOG.md b/CHANGELOG.md index 8ff01c9..e3d3a59 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,26 +1,2 @@ -### 近期修改 - - -### [2.0.0] - 2019/11/01 -* 日历支持padding和margin属性,item的大小计算修改 -* 实现日历整体自适应高度 -* controller提供changeExtraDataMap的方法,可以随时动态的修改自定义数据extraDataMap -* 支持显示月视图和周视图的情况,优先显示周视图,MODE_SHOW_WEEK_AND_MONTH -* 支持verticalSpacing和itemSize属性 -### [1.0.0] - 2019/10/10 -* 重构日历的代码,进行性能优化 -* 创建configuration类,将配置的信息放到这里 -* 引入provider状态管理,避免深层嵌套传递信息 -* 实现周视图,并实现周视图和月视图之间的联动 -* DateModel增加isCurrentMonth,用于绘制月视图可以屏蔽一些非当前月份的日子,前面几天或者后面几天的isCurrentMonth是为false的。 - -### [0.0.1] - 2019/5/19. - -* 支持公历,农历,节气,传统节日,常用节假日 -* 日期范围设置,默认支持的最大日期范围为1971.01-2055.12 -* 禁用日期范围设置,比如想实现某范围的日期内可以点击,范围外的日期置灰 -* 支持单选、多选模式,提供多选超过限制个数的回调和多选超过指定范围的回调。 -* 跳转到指定日期,默认支持动画切换 -* 自定义日历Item,支持组合widget的方式和利用canvas绘制的方式 -* 自定义顶部的WeekBar -* 可以给Item添加自定义的额外数据,实现各种额外的功能。比如实现进度条风格的日历 \ No newline at end of file +## [1.0.1] +- 新增多选范围功能 diff --git a/README.md b/README.md index 06586a6..863648f 100644 --- a/README.md +++ b/README.md @@ -1,138 +1,33 @@ +## flutter_custom_calendar +> 本插件是基于[flutter_custom_calendar](https://github.com/fluttercandies/flutter_custom_calendar)做了稍微的修改进行上传的。 -## FlutterCalendarWidget +具体使用方法见[flutter_custom_calendar](https://github.com/ifgyong/flutter_custom_calendar) -Flutter上的一个日历控件,可以定制成自己想要的样子。 +新增一个选择`mode` -Language: [English](README_en.md)|中文简体 +支持选择开始和结束,选择范围内的日期,使用方法 -- [FlutterCalendarWidget](#fluttercalendarwidget) - - [概述](#%e6%a6%82%e8%bf%b0) - - [在线Demo](#%e5%9c%a8%e7%ba%bfdemo) - - [效果图](#%e6%95%88%e6%9e%9c%e5%9b%be) -- [使用](#%e4%bd%bf%e7%94%a8) -- [2.0版本](#20%e7%89%88%e6%9c%ac) -- [注意事项](#%e6%b3%a8%e6%84%8f%e4%ba%8b%e9%a1%b9) -- [主要API文档](#%e4%b8%bb%e8%a6%81api%e6%96%87%e6%a1%a3) - -### 概述 - -* 支持公历,农历,节气,传统节日,常用节假日 -* 日期范围设置,默认支持的最大日期范围为1971.01-2055.12 -* 禁用日期范围设置,比如想实现某范围的日期内可以点击,范围外的日期置灰 -* 支持单选、多选模式,提供多选超过限制个数的回调和多选超过指定范围的回调。 -* 跳转到指定日期,默认支持动画切换 -* 自定义日历Item,支持组合widget的方式和利用canvas绘制的方式 -* 自定义顶部的WeekBar -* 根据实际场景,可以给Item添加自定义的额外数据,实现各种额外的功能。比如实现进度条风格的日历,实现日历的各种标记 -* 支持周视图的展示,支持月份视图和星期视图的展示与切换联动 - -### 在线Demo - -日历支持web预览:[点击此处进入预览](https://lxd312569496.github.io/flutter_custom_calendar/#/) - - -### 效果图 - - - - - - - - - - - - - - - - - - - - - -
- - - -
- - - - - -
- - - - - -
- - -## 使用 - -1.在pubspec.yaml文件里面添加依赖: -``` -flutter_custom_calendar: - git: - url: https://github.com/LXD312569496/flutter_custom_calendar.git -``` - -2.导入flutter_custom_calendar库 -``` -import 'package:flutter_custom_calendar/flutter_custom_calendar.dart'; -``` - -3.创建CalendarViewWidget对象,配置CalendarController -``` -CalendarController controller= new CalendarController( - minYear: 2018, - minYearMonth: 1, - maxYear: 2020, - maxYearMonth: 12, - showMode: CalendarConstants.MODE_SHOW_MONTH_AND_WEEK); -CalendarViewWidget calendar= CalendarViewWidget( - calendarController: controller, - ), -``` - -* boxDecoration用来配置整体的背景 -* 利用CalendarController来配置一些数据,并且可以通过CalendarController进行一些操作或者事件监听,比如滚动到下一个月,获取当前被选中的Item等等。 - -4.操作日历 -``` -controller.toggleExpandStatus();//月视图和周视图的切换 ``` +controller = new CalendarController( + minYear: 2019, + minYearMonth: 1, + maxYear: 2021, + maxYearMonth: 12, + showMode: CalendarConstants.MODE_SHOW_MONTH_AND_WEEK, + selectedDateTimeList: _selectedDate, + selectMode: CalendarSelectedMode.mutltiStartToEndSelect) + ..addOnCalendarSelectListener((dateModel) { + _selectedModels.add(dateModel); + }) + ..addOnCalendarUnSelectListener((dateModel) { + if (_selectedModels.contains(dateModel)) { + _selectedModels.remove(dateModel); + } + }); ``` -controller.previousPage();//操作日历切换到上一页 -``` - -``` -controller.nextPage();//操作日历切换到下一页 -``` - - -## 2.0版本 -主要改动: -* UI配置相关的参数,移动到CalendarView的构造方法里面(旧版本是在controller里面配置) -* 日历支持padding和margin属性,item的大小计算修改 -* 实现日历整体自适应高度 -* controller提供changeExtraDataMap的方法,可以随时动态的修改自定义数据extraDataMap -* 支持显示月视图和周视图的情况,优先显示周视图,MODE_SHOW_WEEK_AND_MONTH -* 支持verticalSpacing和itemSize属性 - - -## 注意事项 -* 如果使用2.0之前的版本,则需要将UI配置相关的参数,移动到CalendarView的构造方法里面(旧版本是在controller里面配置) -* 暂时没有发现其他问题,如果有其他问题,可以跟我说一下。 -* 如果你用这个库做了日历,可以将展示结果分享给我,我贴到文档上进行展示 -## 主要API文档 +`CalendarSelectedMode.mutltiStartToEndSelect`这个选择模式会选择开始和结束中间的 默认选择的。 -[API Documentation](API.md) diff --git a/example/.gitignore b/example/.gitignore deleted file mode 100644 index 437cb45..0000000 --- a/example/.gitignore +++ /dev/null @@ -1,36 +0,0 @@ -# Miscellaneous -*.class -*.log -*.pyc -*.swp -.DS_Store -.atom/ -.buildlog/ -.history -.svn/ - -# IntelliJ related -*.iml -*.ipr -*.iws -.idea/ - -# The .vscode folder contains launch configuration and tasks you configure in -# VS Code which you may wish to be included in version control, so this line -# is commented out by default. -#.vscode/ - -# Flutter/Dart/Pub related -**/doc/api/ -.dart_tool/ -.flutter-plugins -.packages -.pub-cache/ -.pub/ -/build/ - -# Web related -lib/generated_plugin_registrant.dart - -# Exceptions to above rules. -!/packages/flutter_tools/test/data/dart_dependencies_test/**/.packages diff --git a/example/.idea/codeStyles/Project.xml b/example/.idea/codeStyles/Project.xml new file mode 100644 index 0000000..681f41a --- /dev/null +++ b/example/.idea/codeStyles/Project.xml @@ -0,0 +1,116 @@ + + + + + + + +
+ + + + xmlns:android + + ^$ + + + +
+
+ + + + xmlns:.* + + ^$ + + + BY_NAME + +
+
+ + + + .*:id + + http://schemas.android.com/apk/res/android + + + +
+
+ + + + .*:name + + http://schemas.android.com/apk/res/android + + + +
+
+ + + + name + + ^$ + + + +
+
+ + + + style + + ^$ + + + +
+
+ + + + .* + + ^$ + + + BY_NAME + +
+
+ + + + .* + + http://schemas.android.com/apk/res/android + + + ANDROID_ATTRIBUTE_ORDER + +
+
+ + + + .* + + .* + + + BY_NAME + +
+
+
+
+
+
\ No newline at end of file diff --git a/example/.idea/example.iml b/example/.idea/example.iml new file mode 100644 index 0000000..ae9af97 --- /dev/null +++ b/example/.idea/example.iml @@ -0,0 +1,15 @@ + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/example/.idea/libraries/Dart_Packages.xml b/example/.idea/libraries/Dart_Packages.xml new file mode 100644 index 0000000..85ee25b --- /dev/null +++ b/example/.idea/libraries/Dart_Packages.xml @@ -0,0 +1,252 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/example/.idea/libraries/Dart_SDK.xml b/example/.idea/libraries/Dart_SDK.xml new file mode 100644 index 0000000..e4dfdce --- /dev/null +++ b/example/.idea/libraries/Dart_SDK.xml @@ -0,0 +1,29 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/example/.idea/libraries/Flutter_Plugins.xml b/example/.idea/libraries/Flutter_Plugins.xml new file mode 100644 index 0000000..b0f6971 --- /dev/null +++ b/example/.idea/libraries/Flutter_Plugins.xml @@ -0,0 +1,7 @@ + + + + + + + \ No newline at end of file diff --git a/example/.idea/modules.xml b/example/.idea/modules.xml new file mode 100644 index 0000000..2a37e8d --- /dev/null +++ b/example/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/example/.idea/workspace.xml b/example/.idea/workspace.xml new file mode 100644 index 0000000..3340c0c --- /dev/null +++ b/example/.idea/workspace.xml @@ -0,0 +1,49 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + 1592272073156 + + + + \ No newline at end of file diff --git a/example/.metadata b/example/.metadata deleted file mode 100644 index 65c8fc6..0000000 --- a/example/.metadata +++ /dev/null @@ -1,10 +0,0 @@ -# This file tracks properties of this Flutter project. -# Used by Flutter tool to assess capabilities and perform upgrades etc. -# -# This file should be version controlled and should not be manually edited. - -version: - revision: 0120c414fbc5d11c503e2091f420c02ffd6dda67 - channel: master - -project_type: app diff --git a/example/README.md b/example/README.md index 292f0eb..971daed 100644 --- a/example/README.md +++ b/example/README.md @@ -1,6 +1,6 @@ -# example1 +# example -A new Flutter project. +flutter_custom_calendar/example ## Getting Started diff --git a/example/android/app/build.gradle b/example/android/app/build.gradle index 4cea380..067514d 100644 --- a/example/android/app/build.gradle +++ b/example/android/app/build.gradle @@ -22,28 +22,22 @@ if (flutterVersionName == null) { } apply plugin: 'com.android.application' -apply plugin: 'kotlin-android' apply from: "$flutterRoot/packages/flutter_tools/gradle/flutter.gradle" android { compileSdkVersion 28 - sourceSets { - main.java.srcDirs += 'src/main/kotlin' - } - lintOptions { disable 'InvalidPackage' } defaultConfig { // TODO: Specify your own unique Application ID (https://developer.android.com/studio/build/application-id.html). - applicationId "com.example.example1" + applicationId "com.example.example" minSdkVersion 16 targetSdkVersion 28 versionCode flutterVersionCode.toInteger() versionName flutterVersionName - testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" } buildTypes { @@ -58,10 +52,3 @@ android { flutter { source '../..' } - -dependencies { - implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version" - testImplementation 'junit:junit:4.12' - androidTestImplementation 'androidx.test:runner:1.1.1' - androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.1' -} diff --git a/example/android/app/src/debug/AndroidManifest.xml b/example/android/app/src/debug/AndroidManifest.xml index 5163c1f..c208884 100644 --- a/example/android/app/src/debug/AndroidManifest.xml +++ b/example/android/app/src/debug/AndroidManifest.xml @@ -1,5 +1,5 @@ + package="com.example.example"> diff --git a/example/android/app/src/main/AndroidManifest.xml b/example/android/app/src/main/AndroidManifest.xml index 28cf94a..55ca830 100644 --- a/example/android/app/src/main/AndroidManifest.xml +++ b/example/android/app/src/main/AndroidManifest.xml @@ -1,5 +1,5 @@ + package="com.example.example"> - + + android:name="io.flutter.embedding.android.NormalTheme" + android:resource="@style/NormalTheme" + /> + + + + diff --git a/example/android/app/src/main/java/com/example/example/MainActivity.java b/example/android/app/src/main/java/com/example/example/MainActivity.java new file mode 100644 index 0000000..59f336d --- /dev/null +++ b/example/android/app/src/main/java/com/example/example/MainActivity.java @@ -0,0 +1,6 @@ +package com.example.example; + +import io.flutter.embedding.android.FlutterActivity; + +public class MainActivity extends FlutterActivity { +} diff --git a/example/android/app/src/main/kotlin/com/example/example1/MainActivity.kt b/example/android/app/src/main/kotlin/com/example/example1/MainActivity.kt deleted file mode 100644 index c220bc3..0000000 --- a/example/android/app/src/main/kotlin/com/example/example1/MainActivity.kt +++ /dev/null @@ -1,12 +0,0 @@ -package com.example.example1 - -import android.os.Bundle -import io.flutter.app.FlutterActivity -import io.flutter.plugins.GeneratedPluginRegistrant - -class MainActivity: FlutterActivity() { - override fun onCreate(savedInstanceState: Bundle?) { - super.onCreate(savedInstanceState) - GeneratedPluginRegistrant.registerWith(this) - } -} diff --git a/example/android/app/src/main/res/values/styles.xml b/example/android/app/src/main/res/values/styles.xml index 00fa441..1f83a33 100644 --- a/example/android/app/src/main/res/values/styles.xml +++ b/example/android/app/src/main/res/values/styles.xml @@ -1,8 +1,18 @@ + + + diff --git a/example/android/app/src/profile/AndroidManifest.xml b/example/android/app/src/profile/AndroidManifest.xml index 5163c1f..c208884 100644 --- a/example/android/app/src/profile/AndroidManifest.xml +++ b/example/android/app/src/profile/AndroidManifest.xml @@ -1,5 +1,5 @@ + package="com.example.example"> diff --git a/example/android/build.gradle b/example/android/build.gradle index 3100ad2..e0d7ae2 100644 --- a/example/android/build.gradle +++ b/example/android/build.gradle @@ -1,5 +1,4 @@ buildscript { - ext.kotlin_version = '1.3.50' repositories { google() jcenter() @@ -7,7 +6,6 @@ buildscript { dependencies { classpath 'com.android.tools.build:gradle:3.5.0' - classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version" } } diff --git a/example/android/example_android.iml b/example/android/example_android.iml new file mode 100644 index 0000000..269c4d7 --- /dev/null +++ b/example/android/example_android.iml @@ -0,0 +1,25 @@ + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/example/example.iml b/example/example.iml new file mode 100644 index 0000000..e5c8371 --- /dev/null +++ b/example/example.iml @@ -0,0 +1,18 @@ + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/example/example/example.iml b/example/example/example.iml new file mode 100644 index 0000000..c92516a --- /dev/null +++ b/example/example/example.iml @@ -0,0 +1,17 @@ + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/example/ios/Runner.xcodeproj/project.pbxproj b/example/ios/Runner.xcodeproj/project.pbxproj index 07bc788..5f1bf4c 100644 --- a/example/ios/Runner.xcodeproj/project.pbxproj +++ b/example/ios/Runner.xcodeproj/project.pbxproj @@ -136,7 +136,7 @@ isa = PBXProject; attributes = { LastUpgradeCheck = 1020; - ORGANIZATIONNAME = "The Chromium Authors"; + ORGANIZATIONNAME = ""; TargetAttributes = { 97C146ED1CF9000F007C117D = { CreatedOnToolsVersion = 7.3.1; @@ -145,7 +145,7 @@ }; }; buildConfigurationList = 97C146E91CF9000F007C117D /* Build configuration list for PBXProject "Runner" */; - compatibilityVersion = "Xcode 3.2"; + compatibilityVersion = "Xcode 9.3"; developmentRegion = en; hasScannedForEncodings = 0; knownRegions = ( @@ -308,7 +308,7 @@ "$(inherited)", "$(PROJECT_DIR)/Flutter", ); - PRODUCT_BUNDLE_IDENTIFIER = com.example.example1; + PRODUCT_BUNDLE_IDENTIFIER = com.example.example; PRODUCT_NAME = "$(TARGET_NAME)"; SWIFT_OBJC_BRIDGING_HEADER = "Runner/Runner-Bridging-Header.h"; SWIFT_VERSION = 5.0; @@ -442,7 +442,7 @@ "$(inherited)", "$(PROJECT_DIR)/Flutter", ); - PRODUCT_BUNDLE_IDENTIFIER = com.example.example1; + PRODUCT_BUNDLE_IDENTIFIER = com.example.example; PRODUCT_NAME = "$(TARGET_NAME)"; SWIFT_OBJC_BRIDGING_HEADER = "Runner/Runner-Bridging-Header.h"; SWIFT_OPTIMIZATION_LEVEL = "-Onone"; @@ -469,7 +469,7 @@ "$(inherited)", "$(PROJECT_DIR)/Flutter", ); - PRODUCT_BUNDLE_IDENTIFIER = com.example.example1; + PRODUCT_BUNDLE_IDENTIFIER = com.example.example; PRODUCT_NAME = "$(TARGET_NAME)"; SWIFT_OBJC_BRIDGING_HEADER = "Runner/Runner-Bridging-Header.h"; SWIFT_VERSION = 5.0; diff --git a/example/ios/Runner.xcodeproj/project.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist b/example/ios/Runner.xcodeproj/project.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist new file mode 100644 index 0000000..18d9810 --- /dev/null +++ b/example/ios/Runner.xcodeproj/project.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist @@ -0,0 +1,8 @@ + + + + + IDEDidComputeMac32BitWarning + + + diff --git a/example/ios/Runner.xcodeproj/project.xcworkspace/xcshareddata/WorkspaceSettings.xcsettings b/example/ios/Runner.xcodeproj/project.xcworkspace/xcshareddata/WorkspaceSettings.xcsettings new file mode 100644 index 0000000..f9b0d7c --- /dev/null +++ b/example/ios/Runner.xcodeproj/project.xcworkspace/xcshareddata/WorkspaceSettings.xcsettings @@ -0,0 +1,8 @@ + + + + + PreviewsEnabled + + + diff --git a/example/ios/Runner.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist b/example/ios/Runner.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist new file mode 100644 index 0000000..18d9810 --- /dev/null +++ b/example/ios/Runner.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist @@ -0,0 +1,8 @@ + + + + + IDEDidComputeMac32BitWarning + + + diff --git a/example/ios/Runner.xcworkspace/xcshareddata/WorkspaceSettings.xcsettings b/example/ios/Runner.xcworkspace/xcshareddata/WorkspaceSettings.xcsettings new file mode 100644 index 0000000..f9b0d7c --- /dev/null +++ b/example/ios/Runner.xcworkspace/xcshareddata/WorkspaceSettings.xcsettings @@ -0,0 +1,8 @@ + + + + + PreviewsEnabled + + + diff --git a/example/ios/Runner/Info.plist b/example/ios/Runner/Info.plist index b5ab635..a060db6 100644 --- a/example/ios/Runner/Info.plist +++ b/example/ios/Runner/Info.plist @@ -11,7 +11,7 @@ CFBundleInfoDictionaryVersion 6.0 CFBundleName - example1 + example CFBundlePackageType APPL CFBundleShortVersionString diff --git a/example/ios/Runner/Runner-Bridging-Header.h b/example/ios/Runner/Runner-Bridging-Header.h index 7335fdf..308a2a5 100644 --- a/example/ios/Runner/Runner-Bridging-Header.h +++ b/example/ios/Runner/Runner-Bridging-Header.h @@ -1 +1 @@ -#import "GeneratedPluginRegistrant.h" \ No newline at end of file +#import "GeneratedPluginRegistrant.h" diff --git a/example/lib/blue_style_page.dart b/example/lib/blue_style_page.dart deleted file mode 100644 index 121704d..0000000 --- a/example/lib/blue_style_page.dart +++ /dev/null @@ -1,253 +0,0 @@ -import 'package:flutter/material.dart'; -import 'package:flutter_custom_calendar/flutter_custom_calendar.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() { - super.initState(); - controller = new CalendarController( - - showMode: CalendarConstants.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( - weekBarItemWidgetBuilder: () { - return CustomStyleWeekBarItem(); - }, - dayWidgetBuilder: (dateModel) { - return CustomStyleDayWidget(dateModel); - }, - 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( - mainAxisSize: MainAxisSize.max, - 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 Padding( - padding: const EdgeInsets.all(20.0), - child: 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 { - final 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); - - final TextStyle normalTextStyle = - TextStyle(fontWeight: FontWeight.w700, color: Colors.black); - - final 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_offset_page.dart b/example/lib/custom_offset_page.dart deleted file mode 100644 index 830998b..0000000 --- a/example/lib/custom_offset_page.dart +++ /dev/null @@ -1,192 +0,0 @@ -import 'package:flutter/material.dart'; -import 'package:flutter_custom_calendar/flutter_custom_calendar.dart'; - -/** - * 自定义风格+单选 - */ -class CustomOffsetPage extends StatefulWidget { - CustomOffsetPage({Key key, this.title}) : super(key: key); - - final String title; - - @override - _CustomOffsetPageState createState() => _CustomOffsetPageState(); -} - -class _CustomOffsetPageState extends State { - ValueNotifier text; - ValueNotifier selectText; - - CalendarController controller; - - @override - void initState() { - super.initState(); - controller = new CalendarController( - offset: 5 - ); - - 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) { - return Scaffold( - appBar: AppBar( - title: Text(widget.title), - ), - body: new Container( - child: new Column( - children: [ - new Row( - mainAxisAlignment: MainAxisAlignment.center, - children: [ - new IconButton( - icon: Icon(Icons.navigate_before), - onPressed: () { - controller.moveToPreviousMonth(); - }), - ValueListenableBuilder( - valueListenable: text, - builder: (context, value, child) { - return new Text(text.value); - }), - new IconButton( - icon: Icon(Icons.navigate_next), - onPressed: () { - controller.moveToNextMonth(); - }), - ], - ), - CalendarViewWidget( - calendarController: controller, - weekBarItemWidgetBuilder: () { - return CustomStyleWeekBarItem(); - }, - dayWidgetBuilder: (dateModel) { - return CustomStyleDayWidget(dateModel); - } - ), - ValueListenableBuilder( - valueListenable: selectText, - builder: (context, value, child) { - return new Text(selectText.value); - }), - ], - ), - ), - floatingActionButton: FloatingActionButton( - onPressed: () {}, - tooltip: 'Increment', - child: Icon(Icons.add), - ), // This trailing comma makes auto-formatting nicer for build methods. - ); - } -} - -class CustomStyleWeekBarItem extends BaseWeekBar { - final List weekList = ["三", "四", "五", "六", "日", "一", "二"]; - - @override - Widget getWeekBarItem(int index) { - return new Container( - child: new Center( - child: new Text(weekList[index]), - ), - ); - } -} - -class CustomStyleDayWidget extends BaseCustomDayWidget { - CustomStyleDayWidget(DateModel dateModel) : super(dateModel); - - @override - void drawNormal(DateModel dateModel, Canvas canvas, Size size) { -// if (!dateModel.isCurrentMonth) { -// return; -// } - bool isWeekend = dateModel.isWeekend; - bool isInRange = dateModel.isInRange; - - //顶部的文字 - TextPainter dayTextPainter = new TextPainter() - ..text = TextSpan( - text: dateModel.day.toString(), - style: new TextStyle( - color: !isInRange - ? Colors.grey - : isWeekend ? Colors.blue : Colors.black, - fontSize: 16)) - ..textDirection = TextDirection.ltr - ..textAlign = TextAlign.center; - - dayTextPainter.layout(minWidth: size.width, maxWidth: size.width); - dayTextPainter.paint(canvas, Offset(0, 10)); - - //下面的文字 - TextPainter lunarTextPainter = new TextPainter() - ..text = new TextSpan( - text: dateModel.lunarString, - style: new TextStyle( - color: !isInRange - ? Colors.grey - : isWeekend ? Colors.blue : Colors.grey, - fontSize: 12)) - ..textDirection = TextDirection.ltr - ..textAlign = TextAlign.center; - - lunarTextPainter.layout(minWidth: size.width, maxWidth: size.width); - lunarTextPainter.paint(canvas, Offset(0, size.height / 2)); - } - - @override - void drawSelected(DateModel dateModel, Canvas canvas, Size size) { -// if (!dateModel.isCurrentMonth) { -// return; -// } - //绘制背景 - Paint backGroundPaint = new Paint() - ..color = Colors.blue - ..strokeWidth = 2; - double padding = 8; - canvas.drawCircle(Offset(size.width / 2, size.height / 2), - (size.width - padding) / 2, backGroundPaint); - - //顶部的文字 - TextPainter dayTextPainter = new TextPainter() - ..text = TextSpan( - text: dateModel.day.toString(), - style: new TextStyle(color: Colors.white, fontSize: 16)) - ..textDirection = TextDirection.ltr - ..textAlign = TextAlign.center; - - dayTextPainter.layout(minWidth: size.width, maxWidth: size.width); - dayTextPainter.paint(canvas, Offset(0, 10)); - - //下面的文字 - TextPainter lunarTextPainter = new TextPainter() - ..text = new TextSpan( - text: dateModel.lunarString, - style: new TextStyle(color: Colors.white, fontSize: 12)) - ..textDirection = TextDirection.ltr - ..textAlign = TextAlign.center; - - lunarTextPainter.layout(minWidth: size.width, maxWidth: size.width); - lunarTextPainter.paint(canvas, Offset(0, size.height / 2)); - } -} diff --git a/example/lib/custom_sign_page.dart b/example/lib/custom_sign_page.dart deleted file mode 100644 index 63466f9..0000000 --- a/example/lib/custom_sign_page.dart +++ /dev/null @@ -1,244 +0,0 @@ -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 CustomSignPage extends StatefulWidget { - CustomSignPage({Key key, this.title}) : super(key: key); - - final String title; - - @override - _CustomSignPageState createState() => _CustomSignPageState(); -} - -class _CustomSignPageState extends State { - ValueNotifier text; - ValueNotifier selectText; - - CalendarController controller; - - Map customExtraData = { - DateModel.fromDateTime(DateTime.now().add(Duration(days: -1))): "假", - DateModel.fromDateTime(DateTime.now().add(Duration(days: -2))): "游", - DateModel.fromDateTime(DateTime.now().add(Duration(days: -3))): "事", - DateModel.fromDateTime(DateTime.now().add(Duration(days: -4))): "班", - DateModel.fromDateTime(DateTime.now().add(Duration(days: -5))): "假", - DateModel.fromDateTime(DateTime.now().add(Duration(days: -6))): "游", - DateModel.fromDateTime(DateTime.now().add(Duration(days: -7))): "事", - DateModel.fromDateTime(DateTime.now().add(Duration(days: -8))): "班", - DateModel.fromDateTime(DateTime.now()): "假", - DateModel.fromDateTime(DateTime.now().add(Duration(days: 1))): "假", - DateModel.fromDateTime(DateTime.now().add(Duration(days: 2))): "游", - DateModel.fromDateTime(DateTime.now().add(Duration(days: 3))): "事", - DateModel.fromDateTime(DateTime.now().add(Duration(days: 4))): "班", - DateModel.fromDateTime(DateTime.now().add(Duration(days: 5))): "假", - DateModel.fromDateTime(DateTime.now().add(Duration(days: 6))): "游", - DateModel.fromDateTime(DateTime.now().add(Duration(days: 7))): "事", - DateModel.fromDateTime(DateTime.now().add(Duration(days: 8))): "班", - }; - - @override - void initState() { - super.initState(); - controller = new CalendarController( - - 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) { - return Scaffold( - appBar: AppBar( - title: Text(widget.title), - ), - body: new Container( - child: new Column( - children: [ - new Row( - mainAxisAlignment: MainAxisAlignment.center, - children: [ - new IconButton( - icon: Icon(Icons.navigate_before), - onPressed: () { - controller.moveToPreviousMonth(); - }), - ValueListenableBuilder( - valueListenable: text, - builder: (context, value, child) { - return new Text(text.value); - }), - new IconButton( - icon: Icon(Icons.navigate_next), - onPressed: () { - controller.moveToNextMonth(); - }), - ], - ), - CalendarViewWidget( - calendarController: controller, - weekBarItemWidgetBuilder: () { - return CustomStyleWeekBarItem(); - }, - dayWidgetBuilder: (dateModel) { - return CustomStyleDayWidget(dateModel); - }, - ), - ValueListenableBuilder( - valueListenable: selectText, - builder: (context, value, child) { - return new Text(selectText.value); - }), - ], - ), - ), - floatingActionButton: FloatingActionButton( - onPressed: () { - controller.toggleExpandStatus(); -// controller.changeExtraData({}); - }, - tooltip: 'Increment', - child: Icon(Icons.add), - ), // This trailing comma makes auto-formatting nicer for build methods. - ); - } -} - -class CustomStyleWeekBarItem extends BaseWeekBar { - final List weekList = ["一", "二", "三", "四", "五", "六", "日"]; - - @override - Widget getWeekBarItem(int index) { - return new Container( - child: new Center( - child: new Text(weekList[index]), - ), - ); - } -} - -class CustomStyleDayWidget extends BaseCombineDayWidget { - CustomStyleDayWidget(DateModel dateModel) : super(dateModel); - - @override - Widget getNormalWidget(DateModel dateModel) { - if (!dateModel.isCurrentMonth) { - return Container(); - } - 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: currentMonthTextStyle, - ), - ), - ), - - //农历 - new Expanded( - child: Center( - child: new Text( - "${dateModel.lunarString}", - style: lunarTextStyle, - ), - ), - ), - ], - ), - dateModel.extraData != null - ? Positioned( - child: Text( - "${dateModel.extraData}", - style: TextStyle(fontSize: 10, color: RandomColor.next()), - ), - right: 0, - top: 0, - ) - : Container() - ], - ), - ); - } - - @override - Widget getSelectedWidget(DateModel dateModel) { - if (!dateModel.isCurrentMonth) { - return Container(); - } - return Container( - margin: EdgeInsets.all(8), - foregroundDecoration: - new BoxDecoration(border: Border.all(width: 2, color: Colors.blue)), - 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: currentMonthTextStyle, - ), - ), - ), - - //农历 - new Expanded( - child: Center( - child: new Text( - "${dateModel.lunarString}", - style: lunarTextStyle, - ), - ), - ), - ], - ), - dateModel.extraData != null - ? Positioned( - child: Text( - "${dateModel.extraData}", - style: TextStyle(fontSize: 10, color: RandomColor.next()), - ), - right: 0, - top: 0, - ) - : Container() - ], - ), - ); - } -} diff --git a/example/lib/custom_style_page.dart b/example/lib/custom_style_page.dart deleted file mode 100644 index 400b98d..0000000 --- a/example/lib/custom_style_page.dart +++ /dev/null @@ -1,190 +0,0 @@ -import 'package:flutter/material.dart'; -import 'package:flutter_custom_calendar/flutter_custom_calendar.dart'; - -/** - * 自定义风格+单选 - */ -class CustomStylePage extends StatefulWidget { - CustomStylePage({Key key, this.title}) : super(key: key); - - final String title; - - @override - _CustomStylePageState createState() => _CustomStylePageState(); -} - -class _CustomStylePageState extends State { - ValueNotifier text; - ValueNotifier selectText; - - CalendarController controller; - - @override - void initState() { - super.initState(); - controller = new CalendarController( - selectDateModel: DateModel.fromDateTime(DateTime.now())); - - 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) { - return Scaffold( - appBar: AppBar( - title: Text(widget.title), - ), - body: new Container( - child: new Column( - children: [ - new Row( - mainAxisAlignment: MainAxisAlignment.center, - children: [ - new IconButton( - icon: Icon(Icons.navigate_before), - onPressed: () { - controller.moveToPreviousMonth(); - }), - ValueListenableBuilder( - valueListenable: text, - builder: (context, value, child) { - return new Text(text.value); - }), - new IconButton( - icon: Icon(Icons.navigate_next), - onPressed: () { - controller.moveToNextMonth(); - }), - ], - ), - CalendarViewWidget( - calendarController: controller, - weekBarItemWidgetBuilder: () { - return CustomStyleWeekBarItem(); - }, - dayWidgetBuilder: (dateModel) { - return CustomStyleDayWidget(dateModel); - }), - ValueListenableBuilder( - valueListenable: selectText, - builder: (context, value, child) { - return new Text(selectText.value); - }), - ], - ), - ), - floatingActionButton: FloatingActionButton( - onPressed: () {}, - tooltip: 'Increment', - child: Icon(Icons.add), - ), // This trailing comma makes auto-formatting nicer for build methods. - ); - } -} - -class CustomStyleWeekBarItem extends BaseWeekBar { - final List weekList = ["一", "二", "三", "四", "五", "六", "日"]; - - @override - Widget getWeekBarItem(int index) { - return new Container( - child: new Center( - child: new Text(weekList[index]), - ), - ); - } -} - -class CustomStyleDayWidget extends BaseCustomDayWidget { - CustomStyleDayWidget(DateModel dateModel) : super(dateModel); - - @override - void drawNormal(DateModel dateModel, Canvas canvas, Size size) { - if (!dateModel.isCurrentMonth) { - return; - } - bool isWeekend = dateModel.isWeekend; - bool isInRange = dateModel.isInRange; - - //顶部的文字 - TextPainter dayTextPainter = new TextPainter() - ..text = TextSpan( - text: dateModel.day.toString(), - style: new TextStyle( - color: !isInRange - ? Colors.grey - : isWeekend ? Colors.blue : Colors.black, - fontSize: 16)) - ..textDirection = TextDirection.ltr - ..textAlign = TextAlign.center; - - dayTextPainter.layout(minWidth: size.width, maxWidth: size.width); - dayTextPainter.paint(canvas, Offset(0, 10)); - - //下面的文字 - TextPainter lunarTextPainter = new TextPainter() - ..text = new TextSpan( - text: dateModel.lunarString, - style: new TextStyle( - color: !isInRange - ? Colors.grey - : isWeekend ? Colors.blue : Colors.grey, - fontSize: 12)) - ..textDirection = TextDirection.ltr - ..textAlign = TextAlign.center; - - lunarTextPainter.layout(minWidth: size.width, maxWidth: size.width); - lunarTextPainter.paint(canvas, Offset(0, size.height / 2)); - } - - @override - void drawSelected(DateModel dateModel, Canvas canvas, Size size) { - if (!dateModel.isCurrentMonth) { - return; - } - //绘制背景 - Paint backGroundPaint = new Paint() - ..color = Colors.blue - ..strokeWidth = 2; - double padding = 8; - canvas.drawCircle(Offset(size.width / 2, size.height / 2), - (size.width - padding) / 2, backGroundPaint); - - //顶部的文字 - TextPainter dayTextPainter = new TextPainter() - ..text = TextSpan( - text: dateModel.day.toString(), - style: new TextStyle(color: Colors.white, fontSize: 16)) - ..textDirection = TextDirection.ltr - ..textAlign = TextAlign.center; - - dayTextPainter.layout(minWidth: size.width, maxWidth: size.width); - dayTextPainter.paint(canvas, Offset(0, 10)); - - //下面的文字 - TextPainter lunarTextPainter = new TextPainter() - ..text = new TextSpan( - text: dateModel.lunarString, - style: new TextStyle(color: Colors.white, fontSize: 12)) - ..textDirection = TextDirection.ltr - ..textAlign = TextAlign.center; - - lunarTextPainter.layout(minWidth: size.width, maxWidth: size.width); - lunarTextPainter.paint(canvas, Offset(0, size.height / 2)); - } -} diff --git a/example/lib/default_style_page.dart b/example/lib/default_style_page.dart deleted file mode 100644 index 5546b96..0000000 --- a/example/lib/default_style_page.dart +++ /dev/null @@ -1,102 +0,0 @@ -import 'package:flutter/material.dart'; -import 'package:flutter_custom_calendar/flutter_custom_calendar.dart'; - -/** - * 默认风格+单选 - */ -class DefaultStylePage extends StatefulWidget { - DefaultStylePage({Key key, this.title}) : super(key: key); - - final String title; - - @override - _DefaultStylePageState createState() => _DefaultStylePageState(); -} - -class _DefaultStylePageState extends State { - ValueNotifier text; - ValueNotifier selectText; - - CalendarController controller; - - @override - void initState() { - super.initState(); - DateTime now = DateTime.now(); - controller = new CalendarController( - minYear: now.year - 1, - minYearMonth: 1, - maxYear: now.year + 1, - maxYearMonth: 12, - showMode: CalendarConstants.MODE_SHOW_MONTH_AND_WEEK); - - 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) { - return Scaffold( - appBar: AppBar( - title: Text(widget.title), - ), - body: new Container( - child: new Column( - children: [ - new Row( - mainAxisAlignment: MainAxisAlignment.center, - children: [ - new IconButton( - icon: Icon(Icons.navigate_before), - onPressed: () { -// controller.moveToPreviousMonth(); - controller.previousPage(); - }), - ValueListenableBuilder( - valueListenable: text, - builder: (context, value, child) { - return new Text(text.value); - }), - new IconButton( - icon: Icon(Icons.navigate_next), - onPressed: () { -// controller.moveToNextMonth(); - controller.nextPage(); - }), - ], - ), - CalendarViewWidget( - calendarController: controller, - ), - 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. - ); - } -} diff --git a/example/lib/main.dart b/example/lib/main.dart index 0d2af75..c0e90be 100644 --- a/example/lib/main.dart +++ b/example/lib/main.dart @@ -1,19 +1,12 @@ -import 'package:example1/custom_offset_page.dart'; +import 'dart:collection'; -import 'only_week_page.dart'; -import 'red_style_page.dart'; +import 'package:flutter/cupertino.dart'; import 'package:flutter/material.dart'; -import 'blue_style_page.dart'; -import 'custom_sign_page.dart'; -import 'custom_style_page.dart'; -import 'default_style_page.dart'; -import 'multi_select_style_page.dart'; -import 'progress_style_page.dart'; +import 'package:flutter_custom_calendar/constants/constants.dart'; +import 'package:flutter_custom_calendar/controller.dart'; +import 'package:flutter_custom_calendar/flutter_custom_calendar.dart'; void main() { -// debugProfileBuildsEnabled=true; -// debugProfilePaintsEnabled=true; -// debugPrintRebuildDirtyWidgets=true; runApp(MyApp()); } @@ -22,98 +15,162 @@ class MyApp extends StatelessWidget { @override Widget build(BuildContext context) { return MaterialApp( -// checkerboardOffscreenLayers: true, // 使用了saveLayer的图形会显示为棋盘格式并随着页面刷新而闪烁 - routes: { - "/default": (context) => DefaultStylePage( - title: "默认风格+单选", - ), - "/custom": (context) => CustomStylePage( - title: "自定义风格+单选", - ), - "/multi_select": (context) => MultiSelectStylePage( - title: "自定义风格+多选", - ), - "/progress": (context) => ProgressStylePage( - title: "进度条风格+单选", - ), - "/custom_sign": (context) => CustomSignPage( - title: "自定义额外数据,实现标记功能", - ), - "/only_week_view": (context) => OnlyWeekPage( - title: "仅显示周视图", - ), - "/blue_style_page": (context) => BlueStylePage(title: "蓝色背景Demo"), - "/red_style_page": (context) => RedStylePage(title: "蓝色背景Demo"), - "/custom_offset_page": (context) => CustomOffsetPage(title: "自定义偏移量"), - }, - title: 'Flutter Demo', - theme: ThemeData( + title: 'Flutter Demo', + theme: ThemeData( primarySwatch: Colors.blue, - ), - home: HomePage()); + visualDensity: VisualDensity.adaptivePlatformDensity, + focusColor: Colors.teal), + home: MyHomePage(title: 'Flutter Demo Home Page'), + ); } } -class HomePage extends StatelessWidget { +class MyHomePage extends StatefulWidget { + MyHomePage({Key key, this.title}) : super(key: key); + + final String title; + + @override + _MyHomePageState createState() => _MyHomePageState(); +} + +class _MyHomePageState extends State { + CalendarController controller; + CalendarViewWidget calendar; + HashSet _selectedDate = new HashSet(); + HashSet _selectedModels = new HashSet(); + @override + void initState() { + _selectedDate.add(DateTime.now()); + controller = new CalendarController( + minYear: 2019, + minYearMonth: 1, + maxYear: 2021, + maxYearMonth: 12, + showMode: CalendarConstants.MODE_SHOW_MONTH_AND_WEEK, + selectedDateTimeList: _selectedDate, + selectMode: CalendarSelectedMode.singleSelect) + ..addOnCalendarSelectListener((dateModel) { + _selectedModels.add(dateModel); + setState(() { + _selectDate = _selectedModels.toString(); + }); + }) + ..addOnCalendarUnSelectListener((dateModel) { + if (_selectedModels.contains(dateModel)) { + _selectedModels.remove(dateModel); + } + setState(() { + _selectDate = ''; + }); + }); + calendar = new CalendarViewWidget( + calendarController: controller, + dayWidgetBuilder: (DateModel model) { + double wd = (MediaQuery.of(context).size.width - 20) / 7; + bool _isSelected = model.isSelected; + return ClipRRect( + borderRadius: BorderRadius.all(Radius.circular(wd / 2)), + child: Container( + color: _isSelected ? Theme.of(context).focusColor : Colors.white, + alignment: Alignment.center, + child: Column( + crossAxisAlignment: CrossAxisAlignment.center, + mainAxisAlignment: MainAxisAlignment.center, + children: [ + Text( + model.day.toString(), + style: TextStyle( + color: model.isCurrentMonth + ? (_isSelected == false + ? (model.isWeekend + ? Colors.black38 + : Colors.black87) + : Colors.white) + : Colors.black38), + ), +// Text(model.lunarDay.toString()), + ], + ), + ), + ); + }, + ); + super.initState(); + } + + String _selectDate = ''; @override Widget build(BuildContext context) { - return new Scaffold( - body: SafeArea( - child: new Column( + return Scaffold( + appBar: AppBar( + title: Text(widget.title), + ), + body: Center( + child: Column( + mainAxisAlignment: MainAxisAlignment.center, children: [ - new RaisedButton( - onPressed: () { - Navigator.pushNamed(context, "/default"); - }, - child: new Text("默认风格+单选"), - ), - new RaisedButton( - onPressed: () { - Navigator.pushNamed(context, "/custom"); - }, - child: new Text("自定义风格+单选"), - ), - new RaisedButton( - onPressed: () { - Navigator.pushNamed(context, "/multi_select"); - }, - child: new Text("自定义风格+多选"), + Wrap( + direction: Axis.vertical, + crossAxisAlignment: WrapCrossAlignment.start, + children: [ + Text('请选择mode'), + FlatButton( + child: Text( + 'singleSelect', + style: TextStyle(color: Colors.white), + ), + onPressed: () { + setState(() { + controller.calendarConfiguration.selectMode = + CalendarSelectedMode.singleSelect; + }); + }, + color: controller.calendarConfiguration.selectMode == + CalendarSelectedMode.singleSelect + ? Colors.teal + : Colors.black38, + ), + FlatButton( + child: Text( + 'multiSelect', + style: TextStyle(color: Colors.white), + ), + onPressed: () { + setState(() { + controller.calendarConfiguration.selectMode = + CalendarSelectedMode.multiSelect; + }); + }, + color: controller.calendarConfiguration.selectMode == + CalendarSelectedMode.multiSelect + ? Colors.teal + : Colors.black38, + ), + FlatButton( + child: Text( + 'mutltiStartToEndSelect', + style: TextStyle(color: Colors.white), + ), + onPressed: () { + setState(() { + controller.calendarConfiguration.selectMode = + CalendarSelectedMode.mutltiStartToEndSelect; + }); + }, + color: controller.calendarConfiguration.selectMode == + CalendarSelectedMode.mutltiStartToEndSelect + ? Colors.teal + : Colors.black38, + ) + ], ), - new RaisedButton( - onPressed: () { - Navigator.pushNamed(context, "/progress"); - }, - child: new Text("进度条风格+单选"), - ), - new RaisedButton( - onPressed: () { - Navigator.pushNamed(context, "/custom_sign"); - }, - child: new Text("自定义额外数据,实现标记功能"), - ), - new RaisedButton( - onPressed: () { - Navigator.pushNamed(context, "/only_week_view"); - }, - child: new Text("仅显示周视图"), - ), - new RaisedButton( - onPressed: () { - Navigator.pushNamed(context, "/blue_style_page"); - }, - child: new Text("蓝色Demo"), - ), - new RaisedButton( - onPressed: () { - Navigator.pushNamed(context, "/red_style_page"); - }, - child: new Text("红色Demo"), - ), - new RaisedButton( - onPressed: () { - Navigator.pushNamed(context, "/custom_offset_page"); - }, - child: new Text("自定义偏移量"), + calendar, + Expanded( + child: Text( + ' $_selectDate ', + style: TextStyle(color: Theme.of(context).focusColor), + ), ) ], ), diff --git a/example/lib/multi_select_style_page.dart b/example/lib/multi_select_style_page.dart deleted file mode 100644 index d86b06e..0000000 --- a/example/lib/multi_select_style_page.dart +++ /dev/null @@ -1,185 +0,0 @@ -import 'package:flutter/material.dart'; -import 'package:flutter_custom_calendar/flutter_custom_calendar.dart'; - -/** - * 自定义风格+多选 - */ -class MultiSelectStylePage extends StatefulWidget { - MultiSelectStylePage({Key key, this.title}) : super(key: key); - - final String title; - - @override - _MultiSelectStylePageState createState() => _MultiSelectStylePageState(); -} - -class _MultiSelectStylePageState extends State { - ValueNotifier text; - ValueNotifier selectText; - - CalendarController controller; - - @override - void initState() { - super.initState(); - controller = new CalendarController( - selectMode: CalendarConstants.MODE_MULTI_SELECT, - maxMultiSelectCount: 5, - minSelectYear: 2019, - minSelectMonth: 5, - minSelectDay: 20, - selectedDateTimeList: { - DateTime.now(), - } - ); - - controller.addMonthChangeListener( - (year, month) { - text.value = "$year年$month月"; - }, - ); - - controller.addOnCalendarSelectListener((dateModel) { - //刷新选择的时间 - selectText.value = - "多选模式\n选中的时间:\n${controller.getMultiSelectCalendar().join("\n")}"; - }); - - text = new ValueNotifier("${DateTime.now().year}年${DateTime.now().month}月"); - - selectText = new ValueNotifier( - "多选模式\n选中的时间:\n${controller.getMultiSelectCalendar().join("\n")}"); - } - - @override - Widget build(BuildContext context) { - return Scaffold( - appBar: AppBar( - title: Text(widget.title), - ), - body: new Container( - child: new Column( - children: [ - new Row( - mainAxisAlignment: MainAxisAlignment.center, - children: [ - new IconButton( - icon: Icon(Icons.navigate_before), - onPressed: () { - controller.moveToPreviousMonth(); - }), - ValueListenableBuilder( - valueListenable: text, - builder: (context, value, child) { - return new Text(text.value); - }), - new IconButton( - icon: Icon(Icons.navigate_next), - onPressed: () { - controller.moveToNextMonth(); - }), - ], - ), - CalendarViewWidget( - calendarController: controller, - weekBarItemWidgetBuilder: () { - return CustomStyleWeekBarItem(); - }, - dayWidgetBuilder: (dateModel) { - return CustomStyleDayWidget(dateModel); - }), - ValueListenableBuilder( - valueListenable: selectText, - builder: (context, value, child) { - return new Text(selectText.value); - }), - ], - ), - ), - floatingActionButton: FloatingActionButton( - onPressed: () {}, - tooltip: 'Increment', - child: Icon(Icons.add), - ), // This trailing comma makes auto-formatting nicer for build methods. - ); - } -} - -class CustomStyleWeekBarItem extends BaseWeekBar { - final List weekList = ["一", "二", "三", "四", "五", "六", "日"]; - - @override - Widget getWeekBarItem(int index) { - return new Container( - child: new Center( - child: new Text(weekList[index]), - ), - ); - } -} - -class CustomStyleDayWidget extends BaseCustomDayWidget { - CustomStyleDayWidget(DateModel dateModel) : super(dateModel); - - @override - void drawNormal(DateModel dateModel, Canvas canvas, Size size) { - bool isInRange = dateModel.isInRange; - - //顶部的文字 - TextPainter dayTextPainter = new TextPainter() - ..text = TextSpan( - text: dateModel.day.toString(), - style: new TextStyle( - color: !isInRange ? Colors.grey : Colors.black, fontSize: 16)) - ..textDirection = TextDirection.ltr - ..textAlign = TextAlign.center; - - dayTextPainter.layout(minWidth: size.width, maxWidth: size.width); - dayTextPainter.paint(canvas, Offset(0, 10)); - - //下面的文字 - TextPainter lunarTextPainter = new TextPainter() - ..text = new TextSpan( - text: dateModel.lunarString, - style: new TextStyle( - color: !isInRange ? Colors.grey : Colors.grey, fontSize: 12)) - ..textDirection = TextDirection.ltr - ..textAlign = TextAlign.center; - - lunarTextPainter.layout(minWidth: size.width, maxWidth: size.width); - lunarTextPainter.paint(canvas, Offset(0, size.height / 2)); - } - - @override - void drawSelected(DateModel dateModel, Canvas canvas, Size size) { - //绘制背景 - Paint backGroundPaint = new Paint() - ..color = Colors.blue - ..strokeWidth = 2; - double padding = 8; - canvas.drawCircle(Offset(size.width / 2, size.height / 2), - (size.width - padding) / 2, backGroundPaint); - - //顶部的文字 - TextPainter dayTextPainter = new TextPainter() - ..text = TextSpan( - text: dateModel.day.toString(), - style: new TextStyle(color: Colors.white, fontSize: 16)) - ..textDirection = TextDirection.ltr - ..textAlign = TextAlign.center; - - dayTextPainter.layout(minWidth: size.width, maxWidth: size.width); - dayTextPainter.paint(canvas, Offset(0, 10)); - - //下面的文字 - TextPainter lunarTextPainter = new TextPainter() - ..text = new TextSpan( - text: dateModel.lunarString, - style: new TextStyle(color: Colors.white, fontSize: 12)) - ..textDirection = TextDirection.ltr - ..textAlign = TextAlign.center; - - lunarTextPainter.layout(minWidth: size.width, maxWidth: size.width); - lunarTextPainter.paint(canvas, Offset(0, size.height / 2)); - } -} diff --git a/example/lib/only_week_page.dart b/example/lib/only_week_page.dart deleted file mode 100644 index 68ec4ac..0000000 --- a/example/lib/only_week_page.dart +++ /dev/null @@ -1,102 +0,0 @@ -import 'package:flutter/material.dart'; -import 'package:flutter_custom_calendar/flutter_custom_calendar.dart'; - -/** - * 默认风格+单选 - */ -class OnlyWeekPage extends StatefulWidget { - OnlyWeekPage({Key key, this.title}) : super(key: key); - - final String title; - - @override - _OnlyWeekPageState createState() => _OnlyWeekPageState(); -} - -class _OnlyWeekPageState extends State { - ValueNotifier text; - ValueNotifier selectText; - - CalendarController controller; - - @override - void initState() { - super.initState(); - DateTime now = DateTime.now(); - controller = new CalendarController( - minYear: now.year, - minYearMonth: now.month - 2, - maxYear: now.year, - maxYearMonth: now.month + 1, - showMode: CalendarConstants.MODE_SHOW_ONLY_WEEK); - - 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) { - return Scaffold( - appBar: AppBar( - title: Text(widget.title), - ), - body: new Container( - child: new Column( - children: [ - new Row( - mainAxisAlignment: MainAxisAlignment.center, - children: [ - new IconButton( - icon: Icon(Icons.navigate_before), - onPressed: () { -// controller.moveToPreviousMonth(); - controller.previousPage(); - }), - ValueListenableBuilder( - valueListenable: text, - builder: (context, value, child) { - return new Text(text.value); - }), - new IconButton( - icon: Icon(Icons.navigate_next), - onPressed: () { -// controller.moveToNextMonth(); - controller.nextPage(); - }), - ], - ), - CalendarViewWidget( - calendarController: controller, - ), - 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. - ); - } -} diff --git a/example/lib/progress_style_page.dart b/example/lib/progress_style_page.dart deleted file mode 100644 index f9d597e..0000000 --- a/example/lib/progress_style_page.dart +++ /dev/null @@ -1,224 +0,0 @@ -import 'package:flutter/material.dart'; -import 'package:flutter_custom_calendar/flutter_custom_calendar.dart'; -import 'dart:math'; - -/** - * 进度条风格+单选 - */ -class ProgressStylePage extends StatefulWidget { - ProgressStylePage({Key key, this.title}) : super(key: key); - - final String title; - - @override - _ProgressStylePageState createState() => _ProgressStylePageState(); -} - -class _ProgressStylePageState extends State { - ValueNotifier text; - ValueNotifier selectText; - - CalendarController controller; - - @override - void initState() { - super.initState(); - DateTime now = DateTime.now(); - DateTime temp = DateTime(now.year, now.month, now.day); - - Map progressMap = { - DateModel.fromDateTime(temp.add(Duration(days: -1))): 0, - DateModel.fromDateTime(temp.add(Duration(days: -2))): 20, - DateModel.fromDateTime(temp.add(Duration(days: -3))): 40, - DateModel.fromDateTime(temp.add(Duration(days: -4))): 60, - DateModel.fromDateTime(temp.add(Duration(days: -5))): 80, - DateModel.fromDateTime(temp.add(Duration(days: -6))): 100, - DateModel.fromDateTime(temp.add(Duration(days: 1))): 0, - DateModel.fromDateTime(temp.add(Duration(days: 2))): 20, - DateModel.fromDateTime(temp.add(Duration(days: 3))): 40, - DateModel.fromDateTime(temp.add(Duration(days: 4))): 60, - DateModel.fromDateTime(temp.add(Duration(days: 5))): 80, - DateModel.fromDateTime(temp.add(Duration(days: 6))): 100, - }; - - controller = new CalendarController( - extraDataMap: progressMap, - ); - - 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) { - return Scaffold( - appBar: AppBar( - title: Text(widget.title), - ), - body: new Container( - child: new Column( - children: [ - new Row( - mainAxisAlignment: MainAxisAlignment.center, - children: [ - new IconButton( - icon: Icon(Icons.navigate_before), - onPressed: () { - controller.moveToPreviousMonth(); - }), - ValueListenableBuilder( - valueListenable: text, - builder: (context, value, child) { - return new Text(text.value); - }), - new IconButton( - icon: Icon(Icons.navigate_next), - onPressed: () { - controller.moveToNextMonth(); - }), - ], - ), - CalendarViewWidget( - calendarController: controller, - weekBarItemWidgetBuilder: () { - return CustomStyleWeekBarItem(); - }, - dayWidgetBuilder: (dateModel) { - return ProgressStyleDayWidget(dateModel); - }), - ValueListenableBuilder( - valueListenable: selectText, - builder: (context, value, child) { - return new Text(selectText.value); - }), - ], - ), - ), - floatingActionButton: FloatingActionButton( - onPressed: () {}, - tooltip: 'Increment', - child: Icon(Icons.add), - ), // This trailing comma makes auto-formatting nicer for build methods. - ); - } -} - -class CustomStyleWeekBarItem extends BaseWeekBar { - final List weekList = ["一", "二", "三", "四", "五", "六", "日"]; - - @override - Widget getWeekBarItem(int index) { - return new Container( - child: new Center( - child: new Text(weekList[index]), - ), - ); - } -} - -class ProgressStyleDayWidget extends BaseCustomDayWidget { - ProgressStyleDayWidget(DateModel dateModel) : super(dateModel); - - @override - void drawNormal(DateModel dateModel, Canvas canvas, Size size) { - bool isInRange = dateModel.isInRange; - - //进度条 - int progress = dateModel.extraData; - if (progress != null && progress != 0) { - double padding = 8; - Paint paint = Paint() - ..color = Colors.grey - ..style = PaintingStyle.stroke - ..strokeWidth = 2; - - canvas.drawCircle(Offset(size.width / 2, size.height / 2), - (size.width - padding) / 2, paint); - - paint.color = Colors.blue; - - double startAngle = -90 * pi / 180; - double sweepAngle = pi / 180 * (360 * progress / 100); - - canvas.drawArc( - Rect.fromCircle( - center: Offset(size.width / 2, size.height / 2), - radius: (size.width - padding) / 2), - startAngle, - sweepAngle, - false, - paint); - } - - //顶部的文字 - TextPainter dayTextPainter = new TextPainter() - ..text = TextSpan( - text: dateModel.day.toString(), - style: new TextStyle( - color: !isInRange ? Colors.grey : Colors.black, fontSize: 16)) - ..textDirection = TextDirection.ltr - ..textAlign = TextAlign.center; - - dayTextPainter.layout(minWidth: size.width, maxWidth: size.width); - dayTextPainter.paint(canvas, Offset(0, 10)); - - //下面的文字 - TextPainter lunarTextPainter = new TextPainter() - ..text = new TextSpan( - text: dateModel.lunarString, - style: new TextStyle( - color: !isInRange ? Colors.grey : Colors.grey, fontSize: 12)) - ..textDirection = TextDirection.ltr - ..textAlign = TextAlign.center; - - lunarTextPainter.layout(minWidth: size.width, maxWidth: size.width); - lunarTextPainter.paint(canvas, Offset(0, size.height / 2)); - } - - @override - void drawSelected(DateModel dateModel, Canvas canvas, Size size) { - //绘制背景 - Paint backGroundPaint = new Paint() - ..color = Colors.blue - ..strokeWidth = 2; - double padding = 8; - canvas.drawCircle(Offset(size.width / 2, size.height / 2), - (size.width - padding) / 2, backGroundPaint); - - //顶部的文字 - TextPainter dayTextPainter = new TextPainter() - ..text = TextSpan( - text: dateModel.day.toString(), - style: new TextStyle(color: Colors.white, fontSize: 16)) - ..textDirection = TextDirection.ltr - ..textAlign = TextAlign.center; - - dayTextPainter.layout(minWidth: size.width, maxWidth: size.width); - dayTextPainter.paint(canvas, Offset(0, 10)); - - //下面的文字 - TextPainter lunarTextPainter = new TextPainter() - ..text = new TextSpan( - text: dateModel.lunarString, - style: new TextStyle(color: Colors.white, fontSize: 12)) - ..textDirection = TextDirection.ltr - ..textAlign = TextAlign.center; - - lunarTextPainter.layout(minWidth: size.width, maxWidth: size.width); - lunarTextPainter.paint(canvas, Offset(0, size.height / 2)); - } -} diff --git a/example/lib/red_style_page.dart b/example/lib/red_style_page.dart deleted file mode 100644 index e3801c7..0000000 --- a/example/lib/red_style_page.dart +++ /dev/null @@ -1,238 +0,0 @@ -import 'package:flutter/material.dart'; -import 'package:flutter_custom_calendar/flutter_custom_calendar.dart'; - -class RedStylePage extends StatefulWidget { - RedStylePage({Key key, this.title}) : super(key: key); - - final String title; - - @override - _RedStylePageState createState() => _RedStylePageState(); -} - -class _RedStylePageState extends State { - ValueNotifier text; - ValueNotifier selectText; - - CalendarController controller; - - Map customExtraData = {}; - - Color pinkColor = Color(0xffFF8291); - - @override - void initState() { - super.initState(); - - controller = new CalendarController( - showMode: CalendarConstants.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, - margin: EdgeInsets.only(top: 20), - weekBarItemWidgetBuilder: () { - return CustomStyleWeekBarItem(); - }, - dayWidgetBuilder: (dateModel) { - return CustomStyleDayWidget(dateModel); - }, - ); - - return SafeArea( - child: Scaffold( - appBar: AppBar(), - body: new Container( - color: Colors.white, - padding: EdgeInsets.symmetric(horizontal: 20), - child: new Column( - crossAxisAlignment:CrossAxisAlignment.stretch , - children: [ - SizedBox( - height: 20, - ), - Stack( - alignment: Alignment.center, - children: [ - ValueListenableBuilder( - valueListenable: text, - builder: (context, value, child) { - return new Text( - "${text.value}", - style: TextStyle( - fontSize: 20, - color: Colors.black, - fontWeight: FontWeight.w700), - ); - }), - Positioned( - left: 0, - child: Icon( - Icons.notifications, - color: pinkColor, - ), - ), - Positioned( - right: 40, - child: Icon( - Icons.search, - color: pinkColor, - ), - ), - Positioned( - right: 0, - child: Icon( - Icons.add, - color: pinkColor, - ), - ), - ], - ), - calendarWidget, - ValueListenableBuilder( - valueListenable: selectText, - builder: (context, value, child) { - return Padding( - padding: const EdgeInsets.all(20.0), - child: 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 { - final List weekList = ["M", "T", "W", "T", "F", "S", "S"]; - - //可以直接重写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(0xffBBC0C6)), - ), - ), - ); - } -} - -class CustomStyleDayWidget extends BaseCombineDayWidget { - CustomStyleDayWidget(DateModel dateModel) : super(dateModel); - - final TextStyle normalTextStyle = - TextStyle(fontWeight: FontWeight.w700, color: Colors.black); - - final 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( - shape: BoxShape.circle, - color: Color(0xffFF8291), - ), - 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: TextStyle(color: Colors.white), - ), - ), - ), - ], - ), - ], - ), - ); - } -} diff --git a/example/pubspec.lock b/example/pubspec.lock index ddf3787..413304b 100644 --- a/example/pubspec.lock +++ b/example/pubspec.lock @@ -63,7 +63,7 @@ packages: name: cupertino_icons url: "https://pub.flutter-io.cn" source: hosted - version: "0.1.2" + version: "0.1.3" flutter: dependency: "direct main" description: flutter @@ -72,10 +72,10 @@ packages: flutter_custom_calendar: dependency: "direct main" description: - path: ".." - relative: true - source: path - version: "0.0.1" + name: flutter_custom_calendar + url: "https://pub.flutter-io.cn" + source: hosted + version: "1.0.2" flutter_test: dependency: "direct dev" description: flutter @@ -137,13 +137,6 @@ packages: url: "https://pub.flutter-io.cn" source: hosted version: "2.1.3" - random_pk: - dependency: "direct main" - description: - name: random_pk - url: "https://pub.flutter-io.cn" - source: hosted - version: "0.0.3" sky_engine: dependency: transitive description: flutter diff --git a/example/pubspec.yaml b/example/pubspec.yaml index 99bb92a..2444baa 100644 --- a/example/pubspec.yaml +++ b/example/pubspec.yaml @@ -1,5 +1,9 @@ -name: example1 -description: A new Flutter project. +name: example +description: flutter_custom_calendar example + +# The following line prevents the package from being accidentally published to +# pub.dev using `pub publish`. This is preferred for private packages. +publish_to: 'none' # Remove this line if you wish to publish to pub.dev # The following defines the version and build number for your application. # A version number is three numbers separated by dots, like 1.2.43 @@ -14,23 +18,23 @@ description: A new Flutter project. version: 1.0.0+1 environment: - sdk: ">=2.1.0 <3.0.0" + sdk: ">=2.7.0 <3.0.0" dependencies: flutter: sdk: flutter + + # The following adds the Cupertino Icons font to your application. # Use with the CupertinoIcons class for iOS style icons. - cupertino_icons: ^0.1.2 - random_pk: ^0.0.3 - flutter_custom_calendar: - path: ../ + cupertino_icons: ^0.1.3 + flutter_custom_calendar: ^1.0.2 + dev_dependencies: flutter_test: sdk: flutter - # For information on the generic Dart part of this file, see the # following page: https://dart.dev/tools/pub/pubspec @@ -44,8 +48,8 @@ flutter: # To add assets to your application, add an assets section, like this: # assets: - # - images/a_dot_burr.jpeg - # - images/a_dot_ham.jpeg + # - images/a_dot_burr.jpeg + # - images/a_dot_ham.jpeg # An image asset can refer to one or more resolution-specific "variants", see # https://flutter.dev/assets-and-images/#resolution-aware. diff --git a/example/test/widget_test.dart b/example/test/widget_test.dart index f0b07d5..22130b5 100644 --- a/example/test/widget_test.dart +++ b/example/test/widget_test.dart @@ -8,23 +8,21 @@ import 'package:flutter/material.dart'; import 'package:flutter_test/flutter_test.dart'; -import 'package:example1/main.dart'; - void main() { - testWidgets('Counter increments smoke test', (WidgetTester tester) async { - // Build our app and trigger a frame. - await tester.pumpWidget(MyApp()); - - // Verify that our counter starts at 0. - expect(find.text('0'), findsOneWidget); - expect(find.text('1'), findsNothing); - - // Tap the '+' icon and trigger a frame. - await tester.tap(find.byIcon(Icons.add)); - await tester.pump(); - - // Verify that our counter has incremented. - expect(find.text('0'), findsNothing); - expect(find.text('1'), findsOneWidget); - }); +// testWidgets('Counter increments smoke test', (WidgetTester tester) async { +// // Build our app and trigger a frame. +// await tester.pumpWidget(MyApp()); +// +// // Verify that our counter starts at 0. +// expect(find.text('0'), findsOneWidget); +// expect(find.text('1'), findsNothing); +// +// // Tap the '+' icon and trigger a frame. +// await tester.tap(find.byIcon(Icons.add)); +// await tester.pump(); +// +// // Verify that our counter has incremented. +// expect(find.text('0'), findsNothing); +// expect(find.text('1'), findsOneWidget); +// }); } diff --git a/example/web/index.html b/example/web/index.html deleted file mode 100644 index 237c424..0000000 --- a/example/web/index.html +++ /dev/null @@ -1,10 +0,0 @@ - - - - - example1 - - - - - diff --git a/fluttercustomcalendar.iml b/fluttercustomcalendar.iml new file mode 100644 index 0000000..666d196 --- /dev/null +++ b/fluttercustomcalendar.iml @@ -0,0 +1,26 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/lib/cache_data.dart b/lib/cache_data.dart index 5362177..ad5d5b4 100644 --- a/lib/cache_data.dart +++ b/lib/cache_data.dart @@ -1,4 +1,4 @@ -import 'package:flutter_custom_calendar/model/date_model.dart'; +import 'model/date_model.dart'; /** * 保存一些缓存数据,不用再次去计算日子 @@ -12,6 +12,7 @@ class CacheData { static CacheData get instance => _instance; Map> monthListCache = Map(); + Map> weekListCache = Map(); static CacheData getInstance() { @@ -21,14 +22,8 @@ class CacheData { return _instance; } - - void clearData(){ + void clearData() { monthListCache.clear(); weekListCache.clear(); } - - } - - - diff --git a/lib/calendar_provider.dart b/lib/calendar_provider.dart index 0dd700a..d34353e 100644 --- a/lib/calendar_provider.dart +++ b/lib/calendar_provider.dart @@ -2,13 +2,13 @@ import 'dart:collection'; import 'package:flutter/material.dart'; import 'package:flutter_custom_calendar/cache_data.dart'; -import 'package:flutter_custom_calendar/configuration.dart'; -import 'package:flutter_custom_calendar/constants/constants.dart'; -import 'package:flutter_custom_calendar/controller.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'; import 'package:flutter_custom_calendar/widget/month_view.dart'; +import 'configuration.dart'; +import 'constants/constants.dart'; +import 'flutter_custom_calendar.dart'; +import 'utils/LogUtil.dart'; +import 'utils/date_util.dart'; +import 'model/date_model.dart'; /** * 引入provider的状态管理,保存一些临时信息 @@ -173,7 +173,9 @@ class CalendarProvider extends ChangeNotifier { calendarConfiguration.showMode == CalendarConstants.MODE_SHOW_MONTH_AND_WEEK) { int lineCount = DateUtil.getMonthViewLineCount( - calendarConfiguration.nowYear, calendarConfiguration.nowMonth, calendarConfiguration.offset); + calendarConfiguration.nowYear, + calendarConfiguration.nowMonth, + calendarConfiguration.offset); totalHeight = calendarConfiguration.itemSize * (lineCount) + calendarConfiguration.verticalSpacing * (lineCount - 1); } else { diff --git a/lib/configuration.dart b/lib/configuration.dart index ab3d0a7..cf40a34 100644 --- a/lib/configuration.dart +++ b/lib/configuration.dart @@ -1,16 +1,19 @@ import 'dart:collection'; 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'; +import 'constants/constants.dart'; +import 'flutter_custom_calendar.dart'; + +import 'model/date_model.dart'; + /** * 配置信息类 */ class CalendarConfiguration { //默认是单选,可以配置为MODE_SINGLE_SELECT,MODE_MULTI_SELECT - int selectMode; + CalendarSelectedMode selectMode; //仅展示月视图,仅展示周视图,支持月视图和周视图切换 int showMode; @@ -60,6 +63,7 @@ class CalendarConfiguration { //各种事件回调 OnMonthChange monthChange; //月份切换事件 (已弃用,交给multiMonthChanges来实现) OnCalendarSelect calendarSelect; //点击选择事件 + OnCalendarSelect unCalendarSelect; //点击选择事件 OnMultiSelectOutOfRange multiSelectOutOfRange; //多选超出指定范围 OnMultiSelectOutOfSize multiSelectOutOfSize; //多选超出限制个数 diff --git a/lib/constants/constants.dart b/lib/constants/constants.dart index fe0b86d..290c1af 100644 --- a/lib/constants/constants.dart +++ b/lib/constants/constants.dart @@ -1,3 +1,5 @@ +enum CalendarSelectedMode { singleSelect, multiSelect, mutltiStartToEndSelect } + class CalendarConstants { //单选或者多选模式 // 单选 @@ -9,7 +11,7 @@ class CalendarConstants { /// 选择开始和结束 中间的自动选择 - static const int MODE_MULTI_SELECT_FROM_TO_END = 3; + static const int MODE_MULTI_SELECT_START_TO_END = 3; //展示模式 static const int MODE_SHOW_ONLY_MONTH = 1; //仅支持月视图 diff --git a/lib/controller.dart b/lib/controller.dart index f284fbe..6f2df13 100644 --- a/lib/controller.dart +++ b/lib/controller.dart @@ -1,15 +1,17 @@ import 'dart:collection'; 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'; -import 'package:flutter_custom_calendar/widget/default_combine_day_view.dart'; -import 'package:flutter_custom_calendar/widget/default_custom_day_view.dart'; -import 'package:flutter_custom_calendar/widget/default_week_bar.dart'; +import 'calendar_provider.dart'; +import 'configuration.dart'; +import 'constants/constants.dart'; +import 'flutter_custom_calendar.dart'; +import 'utils/LogUtil.dart'; +import 'utils/date_util.dart'; +import 'widget/default_combine_day_view.dart'; +import 'widget/default_custom_day_view.dart'; +import 'widget/default_week_bar.dart'; + +import 'model/date_model.dart'; /** * 利用controller来控制视图 @@ -33,7 +35,7 @@ class CalendarController { PageController weekController; //星期的controller CalendarController( - {int selectMode = CalendarConstants.MODE_SINGLE_SELECT, + {CalendarSelectedMode selectMode = CalendarSelectedMode.singleSelect, int showMode = CalendarConstants.MODE_SHOW_ONLY_MONTH, int minYear = 1971, int maxYear = 2055, @@ -52,7 +54,7 @@ class CalendarController { int maxMultiSelectCount = 9999, Map extraDataMap = EMPTY_MAP, int offset = 0 // 首日偏移量 - }) { + }) { assert(offset >= 0 && offset <= 6); LogUtil.log(TAG: this.runtimeType, message: "init CalendarConfiguration"); //如果没有指定当前月份和年份,默认是当年时间 @@ -189,7 +191,7 @@ class CalendarController { } //周视图切换 - void addWeekChangeListener(OnWeekChange listener){ + void addWeekChangeListener(OnWeekChange listener) { this.calendarConfiguration.weekChangeListeners.add(listener); } @@ -204,6 +206,11 @@ class CalendarController { this.calendarConfiguration.calendarSelect = listener; } + //点击选择取消监听 + void addOnCalendarUnSelectListener(OnCalendarUnSelect listener) { + this.calendarConfiguration.unCalendarSelect = listener; + } + //多选超出指定范围 void addOnMultiSelectOutOfRangeListener(OnMultiSelectOutOfRange listener) { this.calendarConfiguration.multiSelectOutOfRange = listener; @@ -506,8 +513,8 @@ class CalendarController { monthList.clear(); weekList.clear(); calendarProvider.clearData(); - calendarConfiguration.weekChangeListeners=null; - calendarConfiguration.monthChangeListeners=null; + calendarConfiguration.weekChangeListeners = null; + calendarConfiguration.monthChangeListeners = null; } } @@ -557,6 +564,10 @@ typedef void OnMonthChange(int year, int month); * 日期选择事件 */ typedef void OnCalendarSelect(DateModel dateModel); +/** + * 取消选择 + */ +typedef void OnCalendarUnSelect(DateModel dateModel); /** * 多选超出指定范围 diff --git a/lib/flutter_custom_calendar.dart b/lib/flutter_custom_calendar.dart index d9d34c6..63407bf 100644 --- a/lib/flutter_custom_calendar.dart +++ b/lib/flutter_custom_calendar.dart @@ -1,12 +1,17 @@ library flutter_custom_calendar; -export 'package:flutter_custom_calendar/controller.dart'; -export 'package:flutter_custom_calendar/widget/calendar_view.dart'; -export 'package:flutter_custom_calendar/widget/base_day_view.dart'; -export 'package:flutter_custom_calendar/widget/base_week_bar.dart'; -export 'package:flutter_custom_calendar/constants/constants.dart'; -export 'package:flutter_custom_calendar/model/date_model.dart'; -export 'package:flutter_custom_calendar/widget/default_combine_day_view.dart'; -export 'package:flutter_custom_calendar/widget/default_custom_day_view.dart'; -export 'package:flutter_custom_calendar/widget/default_week_bar.dart'; -export 'package:flutter_custom_calendar/configuration.dart'; +export 'controller.dart'; +export 'widget/calendar_view.dart'; +export 'widget/base_day_view.dart'; +export 'widget/base_week_bar.dart'; +export 'constants/constants.dart'; +export 'model/date_model.dart'; +export 'widget/default_combine_day_view.dart'; +export 'widget/default_custom_day_view.dart'; +export 'widget/default_week_bar.dart'; +export 'configuration.dart'; + +export 'configuration.dart'; +export 'calendar_provider.dart'; +export 'constants/constants.dart'; +export 'widget/base_day_view.dart'; diff --git a/lib/utils/date_util.dart b/lib/utils/date_util.dart index 554a8eb..5a5e21c 100644 --- a/lib/utils/date_util.dart +++ b/lib/utils/date_util.dart @@ -1,6 +1,6 @@ import 'package:flutter/cupertino.dart'; -import 'package:flutter_custom_calendar/model/date_model.dart'; -import 'package:flutter_custom_calendar/utils/LogUtil.dart'; +import 'package:flutter_custom_calendar/flutter_custom_calendar.dart'; +import 'LogUtil.dart'; /** * 工具类 @@ -117,7 +117,8 @@ class DateUtil { print('initCalendarForMonthView start'); weekStart = DateTime.monday; //获取月视图真实偏移量 - int mPreDiff = getIndexOfFirstDayInMonth(new DateTime(year, month), offset: offset); + int mPreDiff = + getIndexOfFirstDayInMonth(new DateTime(year, month), offset: offset); //获取该月的天数 int monthDayCount = getMonthDaysCount(year, month); @@ -126,7 +127,6 @@ class DateUtil { message: "initCalendarForMonthView:$year年$month月,有$monthDayCount天,第一天的index为${mPreDiff}"); - List result = new List(); int size = 42; @@ -171,11 +171,11 @@ class DateUtil { if (extraDataMap?.isNotEmpty == true) { if (extraDataMap.containsKey(dateModel)) { dateModel.extraData = extraDataMap[dateModel]; - }else{ - dateModel.extraData=null; + } else { + dateModel.extraData = null; } - }else{ - dateModel.extraData=null; + } else { + dateModel.extraData = null; } result.add(dateModel); @@ -197,8 +197,7 @@ class DateUtil { int lineCount = ((preIndex + monthDayCount) / 7).ceil(); LogUtil.log( TAG: "DateUtil", - message: - "getMonthViewLineCount:$year年$month月:有$lineCount行"); + message: "getMonthViewLineCount:$year年$month月:有$lineCount行"); return lineCount; } diff --git a/lib/utils/lunar_util.dart b/lib/utils/lunar_util.dart index ce18e9a..dbd46c0 100644 --- a/lib/utils/lunar_util.dart +++ b/lib/utils/lunar_util.dart @@ -1,6 +1,4 @@ -import 'package:flutter_custom_calendar/constants/constants.dart'; -import 'package:flutter_custom_calendar/model/date_model.dart'; -import 'package:flutter_custom_calendar/utils/date_util.dart'; +import 'package:flutter_custom_calendar/flutter_custom_calendar.dart'; import 'package:flutter_custom_calendar/utils/solar_term_util.dart'; /** diff --git a/lib/widget/base_day_view.dart b/lib/widget/base_day_view.dart index 6ff32ec..348c5b4 100644 --- a/lib/widget/base_day_view.dart +++ b/lib/widget/base_day_view.dart @@ -1,6 +1,8 @@ import 'package:flutter/material.dart'; -import 'package:flutter_custom_calendar/controller.dart'; import 'package:flutter_custom_calendar/model/date_model.dart'; +import 'package:flutter_custom_calendar/flutter_custom_calendar.dart'; + +import '../controller.dart'; /** * 通过canvas自定义item,只需实现相关的方法就可以 diff --git a/lib/widget/calendar_view.dart b/lib/widget/calendar_view.dart index 4dbd34c..13463e9 100644 --- a/lib/widget/calendar_view.dart +++ b/lib/widget/calendar_view.dart @@ -1,14 +1,17 @@ import 'package:flutter/foundation.dart'; import 'package:flutter/material.dart'; -import 'package:flutter_custom_calendar/calendar_provider.dart'; + +import 'package:flutter/material.dart'; import 'package:flutter_custom_calendar/constants/constants.dart'; -import 'package:flutter_custom_calendar/controller.dart'; import 'package:flutter_custom_calendar/utils/LogUtil.dart'; import 'package:flutter_custom_calendar/utils/date_util.dart'; -import 'package:flutter_custom_calendar/widget/month_view_pager.dart'; import 'package:flutter_custom_calendar/widget/week_view_pager.dart'; import 'package:provider/provider.dart'; +import '../calendar_provider.dart'; +import '../controller.dart'; +import 'month_view_pager.dart'; + /** * 暂时默认是周一开始的 */ @@ -167,7 +170,8 @@ class CalendarContainerState extends State .showMode != CalendarConstants.MODE_SHOW_ONLY_WEEK) { //月份切换的时候,如果高度发生变化的话,需要setState使高度整体自适应 - int lineCount = DateUtil.getMonthViewLineCount(year, month, widget.calendarController.calendarConfiguration.offset); + int lineCount = DateUtil.getMonthViewLineCount(year, month, + widget.calendarController.calendarConfiguration.offset); double newHeight = itemHeight * (lineCount) + calendarProvider.calendarConfiguration.verticalSpacing * (lineCount - 1); diff --git a/lib/widget/default_combine_day_view.dart b/lib/widget/default_combine_day_view.dart index f68c696..c841490 100644 --- a/lib/widget/default_combine_day_view.dart +++ b/lib/widget/default_combine_day_view.dart @@ -1,8 +1,9 @@ import 'package:flutter/material.dart'; -import 'package:flutter_custom_calendar/widget/base_day_view.dart'; -import 'package:flutter_custom_calendar/model/date_model.dart'; +import 'package:flutter_custom_calendar/flutter_custom_calendar.dart'; import 'package:flutter_custom_calendar/style/style.dart'; +import 'base_day_view.dart'; + /** * 默认的利用组合widget的方式构造item */ diff --git a/lib/widget/default_custom_day_view.dart b/lib/widget/default_custom_day_view.dart index d972d03..f3fe492 100644 --- a/lib/widget/default_custom_day_view.dart +++ b/lib/widget/default_custom_day_view.dart @@ -1,8 +1,9 @@ import 'package:flutter/material.dart'; -import 'package:flutter_custom_calendar/widget/base_day_view.dart'; -import 'package:flutter_custom_calendar/model/date_model.dart'; +import 'package:flutter_custom_calendar/flutter_custom_calendar.dart'; import 'package:flutter_custom_calendar/style/style.dart'; +import 'base_day_view.dart'; + /** * 这里定义成一个StatelessWidget,状态是外部的父控件传进来参数控制就行,自己不弄state类 */ diff --git a/lib/widget/default_week_bar.dart b/lib/widget/default_week_bar.dart index 0b8935e..c30c99a 100644 --- a/lib/widget/default_week_bar.dart +++ b/lib/widget/default_week_bar.dart @@ -1,8 +1,9 @@ import 'package:flutter/material.dart'; import 'package:flutter_custom_calendar/constants/constants.dart'; -import 'package:flutter_custom_calendar/widget/base_week_bar.dart'; import 'package:flutter_custom_calendar/style/style.dart'; +import 'base_week_bar.dart'; + ///** // * 顶部的固定的周显示 // */ diff --git a/lib/widget/month_view.dart b/lib/widget/month_view.dart index b08dc79..429a3c6 100644 --- a/lib/widget/month_view.dart +++ b/lib/widget/month_view.dart @@ -1,10 +1,8 @@ import 'package:flutter/foundation.dart'; import 'package:flutter/material.dart'; import 'package:flutter_custom_calendar/cache_data.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/flutter_custom_calendar.dart'; import 'package:flutter_custom_calendar/utils/LogUtil.dart'; import 'package:flutter_custom_calendar/utils/date_util.dart'; import 'package:provider/provider.dart'; @@ -113,7 +111,7 @@ class _MonthViewState extends State switch (configuration.selectMode) { /// 多选 - case CalendarConstants.MODE_MULTI_SELECT: + case CalendarSelectedMode.multiSelect: if (calendarProvider.selectedDateList.contains(dateModel)) { dateModel.isSelected = true; } else { @@ -123,7 +121,7 @@ class _MonthViewState extends State /// 选择开始和结束 中间的自动选择 - case CalendarConstants.MODE_MULTI_SELECT_FROM_TO_END: + case CalendarSelectedMode.mutltiStartToEndSelect: if (calendarProvider.selectedDateList.contains(dateModel)) { dateModel.isSelected = true; } else { @@ -132,7 +130,7 @@ class _MonthViewState extends State break; /// 单选 - case CalendarConstants.MODE_SINGLE_SELECT: + case CalendarSelectedMode.singleSelect: if (calendarProvider.selectDateModel == dateModel) { dateModel.isSelected = true; } else { @@ -143,8 +141,15 @@ class _MonthViewState extends State return ItemContainer( dateModel: dateModel, - key: ObjectKey( - dateModel), //这里使用objectKey,保证可以刷新。原因1:跟flutter的刷新机制有关。原因2:statefulElement持有state。 + key: ObjectKey(dateModel), + clickCall: () { + setState(() {}); +// if (configuration.selectMode == +// CalendarSelectedMode.mutltiStartToEndSelect) + + /// 如果是选择开始和结束则进行刷新日历 + }, + //这里使用objectKey,保证可以刷新。原因1:跟flutter的刷新机制有关。原因2:statefulElement持有state。 ); }); } @@ -159,10 +164,9 @@ class _MonthViewState extends State class ItemContainer extends StatefulWidget { final DateModel dateModel; - const ItemContainer({ - Key key, - this.dateModel, - }) : super(key: key); + final GestureTapCallback clickCall; + const ItemContainer({Key key, this.dateModel, this.clickCall}) + : super(key: key); @override ItemContainerState createState() => ItemContainerState(); @@ -180,14 +184,6 @@ class ItemContainerState extends State { super.initState(); dateModel = widget.dateModel; isSelected = ValueNotifier(dateModel.isSelected); - -// 先注释掉这段代码 -// WidgetsBinding.instance.addPostFrameCallback((callback) { -// if (configuration.selectMode == CalendarConstants.MODE_SINGLE_SELECT && -// dateModel.isSelected) { -// calendarProvider.lastClickItemState = this; -// } -// }); } /** @@ -199,12 +195,28 @@ class ItemContainerState extends State { The following assertion was thrown while handling a gesture: setState() called after dispose() */ + v ??= false; if (mounted) { setState(() { dateModel.isSelected = v; -// isSelected.value = !isSelected.value; }); } + + if (widget.clickCall != null) { + widget.clickCall(); + } + } + + void _notifiCationUnCalendarSelect(DateModel element) { + if (configuration.unCalendarSelect != null) { + configuration.unCalendarSelect(element); + } + } + + void _notifiCationCalendarSelect(DateModel element) { + if (configuration.calendarSelect != null) { + configuration.calendarSelect(element); + } } @override @@ -224,7 +236,7 @@ class ItemContainerState extends State { //范围外不可点击 if (!dateModel.isInRange) { //多选回调 - if (configuration.selectMode == CalendarConstants.MODE_MULTI_SELECT) { + if (configuration.selectMode == CalendarSelectedMode.multiSelect) { configuration.multiSelectOutOfRange(); } return; @@ -234,7 +246,7 @@ class ItemContainerState extends State { switch (configuration.selectMode) { //简单多选 - case CalendarConstants.MODE_MULTI_SELECT: + case CalendarSelectedMode.multiSelect: if (calendarProvider.selectedDateList.contains(dateModel)) { calendarProvider.selectedDateList.remove(dateModel); } else { @@ -248,17 +260,21 @@ class ItemContainerState extends State { } calendarProvider.selectedDateList.add(dateModel); } - if (configuration.calendarSelect != null) { - configuration.calendarSelect(dateModel); - } //多选也可以弄这些单选的代码 calendarProvider.selectDateModel = dateModel; break; /// 单选 - case CalendarConstants.MODE_SINGLE_SELECT: - calendarProvider.selectDateModel = dateModel; + case CalendarSelectedMode.singleSelect: + + /// 加入已经选择了多个 则进行取消操作 + calendarProvider.selectedDateList.forEach((element) { + if (configuration.unCalendarSelect != null) { + configuration.unCalendarSelect(element); + } + element.isSelected = false; + }); if (configuration.calendarSelect != null) { configuration.calendarSelect(dateModel); } @@ -268,14 +284,28 @@ class ItemContainerState extends State { calendarProvider.lastClickItemState?.refreshItem(false); calendarProvider.lastClickItemState = this; } + _notifiCationUnCalendarSelect(calendarProvider.selectDateModel); + + calendarProvider.selectDateModel = dateModel; + + setState(() {}); + break; /// 选择范围 - case CalendarConstants.MODE_MULTI_SELECT_FROM_TO_END: + case CalendarSelectedMode.mutltiStartToEndSelect: if (calendarProvider.selectedDateList.length == 0) { calendarProvider.selectedDateList.add(dateModel); } else if (calendarProvider.selectedDateList.length == 1) { - DateModel d2 = calendarProvider.selectedDateList.last; + DateModel d2 = calendarProvider.selectedDateList.first; + if (calendarProvider.selectedDateList.contains(dateModel)) { + /// 选择同一个第二次则进行取消 + dateModel.isSelected = false; + _notifiCationUnCalendarSelect(dateModel); + + setState(() {}); + return; + } DateTime t1, t2; if (d2.getDateTime().isAfter(dateModel.getDateTime())) { t2 = d2.getDateTime(); @@ -289,13 +319,24 @@ class ItemContainerState extends State { .add(DateModel.fromDateTime(t1)); t1 = t1.add(Duration(days: 1)); } + calendarProvider.selectedDateList.add(DateModel.fromDateTime(t1)); } else { /// 加入已经选择了多个 则进行取消操作 + calendarProvider.selectedDateList.forEach((element) { + element.isSelected = false; + _notifiCationUnCalendarSelect(element); + }); + + /// 清空删除的 数组 calendarProvider.selectedDateList.clear(); + setState(() {}); } - if (configuration.calendarSelect != null) { + + /// 所有数组操作完了 进行通知分发 + if (configuration.calendarSelect != null && + calendarProvider.selectedDateList.length > 0) { calendarProvider.selectedDateList.forEach((element) { - configuration.calendarSelect(element); + _notifiCationCalendarSelect(element); }); } break; diff --git a/lib/widget/month_view_pager.dart b/lib/widget/month_view_pager.dart index a35e24f..f93e265 100644 --- a/lib/widget/month_view_pager.dart +++ b/lib/widget/month_view_pager.dart @@ -1,9 +1,10 @@ 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/model/date_model.dart'; +import 'package:flutter_custom_calendar/flutter_custom_calendar.dart'; import 'package:flutter_custom_calendar/utils/LogUtil.dart'; import 'package:flutter_custom_calendar/widget/month_view.dart'; + import 'package:provider/provider.dart'; class MonthViewPager extends StatefulWidget { diff --git a/lib/widget/week_view.dart b/lib/widget/week_view.dart index 5bb0b3f..d54f443 100644 --- a/lib/widget/week_view.dart +++ b/lib/widget/week_view.dart @@ -1,8 +1,8 @@ import 'package:flutter/material.dart'; -import 'package:flutter_custom_calendar/calendar_provider.dart'; + +import 'package:flutter/foundation.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/flutter_custom_calendar.dart'; import 'package:flutter_custom_calendar/utils/date_util.dart'; import 'package:flutter_custom_calendar/widget/month_view.dart'; import 'package:provider/provider.dart'; diff --git a/lib/widget/week_view_pager.dart b/lib/widget/week_view_pager.dart index 98b1b71..d7bf948 100644 --- a/lib/widget/week_view_pager.dart +++ b/lib/widget/week_view_pager.dart @@ -1,10 +1,10 @@ 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/model/date_model.dart'; -import 'package:flutter_custom_calendar/utils/LogUtil.dart'; import 'package:flutter_custom_calendar/widget/week_view.dart'; import 'package:provider/provider.dart'; +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 { const WeekViewPager({Key key}) : super(key: key); diff --git a/test/fluttercustomcalendar_test.dart b/test/fluttercustomcalendar_test.dart new file mode 100644 index 0000000..2f7e49c --- /dev/null +++ b/test/fluttercustomcalendar_test.dart @@ -0,0 +1,9 @@ +void main() { +// test('adds one to input values', () { +// final calculator = Calculator(); +// expect(calculator.addOne(2), 3); +// expect(calculator.addOne(-7), -6); +// expect(calculator.addOne(0), 1); +// expect(() => calculator.addOne(null), throwsNoSuchMethodError); +// }); +}