diff --git a/.idea/libraries/Dart_Packages.xml b/.idea/libraries/Dart_Packages.xml
index e7a63b8..bbf8a98 100644
--- a/.idea/libraries/Dart_Packages.xml
+++ b/.idea/libraries/Dart_Packages.xml
@@ -72,13 +72,6 @@
-
-
-
-
-
-
-
@@ -224,7 +217,6 @@
-
diff --git a/.idea/workspace.xml b/.idea/workspace.xml
index f69ab1c..135a399 100644
--- a/.idea/workspace.xml
+++ b/.idea/workspace.xml
@@ -9,26 +9,31 @@
+
-
-
+
+
+
-
-
+
+
+
+
+
@@ -46,18 +51,6 @@
-
-
-
@@ -99,4 +92,22 @@
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/CHANGELOG.md b/CHANGELOG.md
index e3d3a59..6cfc5e7 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -1,2 +1,5 @@
+## [1.0.4+0.4]
+- 新增三个模式相互且含数据不影响
+
## [1.0.1]
- 新增多选范围功能
diff --git a/README.md b/README.md
index fba59b0..473ef5a 100644
--- a/README.md
+++ b/README.md
@@ -37,7 +37,7 @@ Add this to your package's pubspec.yaml file:
```
dependencies:
- flutter_custom_calendar: ^1.0.3
+ flutter_custom_calendar: ^1.0.4+0.5
```
2. Install it
@@ -57,10 +57,34 @@ Now in your Dart code, you can use:
```
import 'package:flutter_custom_calendar/flutter_custom_calendar.dart';
```
+### 监听月视图和周视图状态
+```dart WidgetsBinding.instance.addPostFrameCallback((timeStamp) {
+ controller.addExpandChangeListener((value) {
+ /// 添加改变 月视图和 周视图的监听
+ _isMonthSelected = value;
+ setState(() {});
+ });
+});
+```
+### 变更月视图和周视图
+> 前提条件是`showModel`是`CalendarConstants.MODE_SHOW_MONTH_AND_WEEK`或者`CalendarConstants.MODE_SHOW_WEEK_AND_MONTH`.
+
+#### 变更到周视图
+```dart
+ setState(() {
+ controller.weekAndMonthViewChange(CalendarConstants.MODE_SHOW_ONLY_WEEK);
+ });
+```
+
+#### 变更到月视图
+```dart
+setState(() {controller.weekAndMonthViewChange(CalendarConstants.MODE_SHOW_ONLY_MONTH);
+ });
+```
### 动画演示
-![](https://github.com/ifgyong/flutter_custom_calendar/blob/master/img.gif)
+![](img.gif)
### [查看API](https://github.com/ifgyong/flutter_custom_calendar/blob/master/API.md)
### [查看一个例子 如何使用](https://github.com/ifgyong/flutter_custom_calendar/blob/master/example/lib/main.dart)
diff --git a/example/lib/main.dart b/example/lib/main.dart
index 7f31223..e0c57ae 100644
--- a/example/lib/main.dart
+++ b/example/lib/main.dart
@@ -39,6 +39,8 @@ class _MyHomePageState extends State {
CalendarViewWidget calendar;
HashSet _selectedDate = new HashSet();
HashSet _selectedModels = new HashSet();
+
+ GlobalKey _globalKey = new GlobalKey();
@override
void initState() {
_selectedDate.add(DateTime.now());
@@ -47,7 +49,7 @@ class _MyHomePageState extends State {
minYearMonth: 1,
maxYear: 2021,
maxYearMonth: 12,
- showMode: CalendarConstants.MODE_SHOW_MONTH_AND_WEEK,
+ showMode: CalendarConstants.MODE_SHOW_WEEK_AND_MONTH,
selectedDateTimeList: _selectedDate,
selectMode: CalendarSelectedMode.singleSelect)
..addOnCalendarSelectListener((dateModel) {
@@ -65,6 +67,7 @@ class _MyHomePageState extends State {
});
});
calendar = new CalendarViewWidget(
+ key: _globalKey,
calendarController: controller,
dayWidgetBuilder: (DateModel model) {
double wd = (MediaQuery.of(context).size.width - 20) / 7;
@@ -101,9 +104,19 @@ class _MyHomePageState extends State {
);
},
);
+ WidgetsBinding.instance.addPostFrameCallback((timeStamp) {
+ controller.addExpandChangeListener((value) {
+ /// 添加改变 月视图和 周视图的监听
+ _isMonthSelected = value;
+ setState(() {});
+ });
+ });
+
super.initState();
}
+ bool _isMonthSelected = false;
+
String _selectDate = '';
@override
Widget build(BuildContext context) {
@@ -115,6 +128,7 @@ class _MyHomePageState extends State {
child: CustomScrollView(
slivers: [
_topButtons(),
+ _topMonths(),
SliverToBoxAdapter(
child: calendar,
),
@@ -139,54 +153,104 @@ class _MyHomePageState extends State {
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,
+ Wrap(
+ spacing: 10,
+ runSpacing: 10,
+ children: [
+ FlatButton(
+ child: Text(
+ '单选',
+ 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(
+ '多选',
+ 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(
+ '多选 选择开始和结束',
+ style: TextStyle(color: Colors.white),
+ ),
+ onPressed: () {
+ setState(() {
+ controller.calendarConfiguration.selectMode =
+ CalendarSelectedMode.mutltiStartToEndSelect;
+ });
+ },
+ color: controller.calendarConfiguration.selectMode ==
+ CalendarSelectedMode.mutltiStartToEndSelect
+ ? 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,
+ ],
+ ),
+ );
+ }
+
+ Widget _topMonths() {
+ return SliverToBoxAdapter(
+ child: Wrap(
+ direction: Axis.vertical,
+ crossAxisAlignment: WrapCrossAlignment.start,
+ children: [
+ Text('月视图和周视图'),
+ Wrap(
+ spacing: 10,
+ runSpacing: 10,
+ children: [
+ FlatButton(
+ child: Text(
+ '月视图',
+ style: TextStyle(color: Colors.white),
+ ),
+ onPressed: () {
+ setState(() {
+ controller.weekAndMonthViewChange(
+ CalendarConstants.MODE_SHOW_ONLY_WEEK);
+ });
+ },
+ color: _isMonthSelected ? Colors.teal : Colors.black38,
+ ),
+ FlatButton(
+ child: Text(
+ '周视图',
+ style: TextStyle(color: Colors.white),
+ ),
+ onPressed: () {
+ setState(() {
+ controller.weekAndMonthViewChange(
+ CalendarConstants.MODE_SHOW_ONLY_MONTH);
+ });
+ },
+ color: _isMonthSelected == false ? 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,
- )
],
),
);
diff --git a/example/pubspec.lock b/example/pubspec.lock
index 5c76dc8..ba88015 100644
--- a/example/pubspec.lock
+++ b/example/pubspec.lock
@@ -72,10 +72,10 @@ packages:
flutter_custom_calendar:
dependency: "direct main"
description:
- name: flutter_custom_calendar
- url: "https://pub.flutter-io.cn"
- source: hosted
- version: "1.0.4+0.2"
+ path: ".."
+ relative: true
+ source: path
+ version: "1.0.4+0.3"
flutter_test:
dependency: "direct dev"
description: flutter
diff --git a/example/pubspec.yaml b/example/pubspec.yaml
index 825d3b3..9a4db9a 100644
--- a/example/pubspec.yaml
+++ b/example/pubspec.yaml
@@ -29,7 +29,8 @@ dependencies:
# The following adds the Cupertino Icons font to your application.
# Use with the CupertinoIcons class for iOS style icons.
cupertino_icons: ^0.1.3
- flutter_custom_calendar: ^1.0.4+0.3
+ flutter_custom_calendar:
+ path: ../
dev_dependencies:
flutter_test:
diff --git a/img.gif b/img.gif
index 47a6b57..f8db49b 100644
Binary files a/img.gif and b/img.gif differ
diff --git a/lib/calendar_provider.dart b/lib/calendar_provider.dart
index d34353e..fdc8570 100644
--- a/lib/calendar_provider.dart
+++ b/lib/calendar_provider.dart
@@ -106,6 +106,7 @@ class CalendarProvider extends ChangeNotifier {
//配置类也放这里吧,这样的话,所有子树,都可以拿到配置的信息
CalendarConfiguration calendarConfiguration;
+ void weekAndMonthViewChange(int mode) {}
void initData({
Set selectedDateList,
diff --git a/lib/controller.dart b/lib/controller.dart
index 6f2df13..116bc09 100644
--- a/lib/controller.dart
+++ b/lib/controller.dart
@@ -111,6 +111,17 @@ class CalendarController {
maxYear,
maxYearMonth,
))}");
+ _weekAndMonthViewChange(showMode);
+ }
+ void _weekAndMonthViewChange(
+ int showMode,
+ ) {
+ int minYear = calendarConfiguration.minYear;
+ int maxYear = calendarConfiguration.maxYear;
+ int minYearMonth = calendarConfiguration.minYearMonth;
+ int maxYearMonth = calendarConfiguration.maxYearMonth;
+ int nowYear = calendarConfiguration.nowYear;
+ int nowMonth = calendarConfiguration.nowMonth;
if (showMode != CalendarConstants.MODE_SHOW_ONLY_WEEK) {
//初始化pageController,initialPage默认是当前时间对于的页面
@@ -183,11 +194,18 @@ class CalendarController {
"初始化星期视图的信息:一共有${weekList.length}个星期,initialPage为$initialWeekPage");
this.weekController = new PageController(initialPage: initialWeekPage);
}
-
calendarConfiguration.monthList = monthList;
calendarConfiguration.weekList = weekList;
calendarConfiguration.monthController = monthController;
calendarConfiguration.weekController = weekController;
+ calendarProvider.weekAndMonthViewChange(showMode);
+ }
+
+ void weekAndMonthViewChange(
+ int showMode,
+ ) {
+ calendarProvider.expandStatus.value =
+ showMode == CalendarConstants.MODE_SHOW_ONLY_WEEK ? true : false;
}
//周视图切换
diff --git a/lib/widget/month_view.dart b/lib/widget/month_view.dart
index d4dcee1..26ca2c8 100644
--- a/lib/widget/month_view.dart
+++ b/lib/widget/month_view.dart
@@ -200,10 +200,10 @@ class ItemContainerState extends State {
setState(() {
dateModel.isSelected = v;
});
- }
- if (widget.clickCall != null) {
- widget.clickCall();
+ if (widget.clickCall != null) {
+ widget.clickCall();
+ }
}
}
@@ -261,7 +261,6 @@ class ItemContainerState extends State {
}
dateModel.isSelected = !dateModel.isSelected;
calendarProvider.selectedDateList.add(dateModel);
-// _notifiCationCalendarSelect(dateModel);
}
//多选也可以弄这些单选的代码
@@ -277,6 +276,7 @@ class ItemContainerState extends State {
element.isSelected = false;
_notifiCationUnCalendarSelect(element);
});
+ calendarProvider.selectedDateList.clear();
//单选需要刷新上一个item
if (calendarProvider.lastClickItemState != this) {
diff --git a/lib/widget/week_view.dart b/lib/widget/week_view.dart
index d54f443..ae9efb3 100644
--- a/lib/widget/week_view.dart
+++ b/lib/widget/week_view.dart
@@ -76,25 +76,35 @@ class _WeekViewState extends State {
itemBuilder: (context, index) {
DateModel dateModel = items[index];
//判断是否被选择
- if (configuration.selectMode == CalendarConstants.MODE_MULTI_SELECT) {
- if (calendarProvider.selectedDateList.contains(dateModel)) {
- dateModel.isSelected = true;
- } else {
- dateModel.isSelected = false;
- }
- } else {
- if (calendarProvider.selectDateModel == dateModel) {
- dateModel.isSelected = true;
- } else {
- dateModel.isSelected = false;
- }
+ switch (configuration.selectMode) {
+ case CalendarSelectedMode.multiSelect:
+ if (calendarProvider.selectedDateList.contains(dateModel)) {
+ dateModel.isSelected = true;
+ } else {
+ dateModel.isSelected = false;
+ }
+ break;
+ case CalendarSelectedMode.singleSelect:
+ if (calendarProvider.selectDateModel == dateModel) {
+ dateModel.isSelected = true;
+ } else {
+ dateModel.isSelected = false;
+ }
+ break;
+ case CalendarSelectedMode.mutltiStartToEndSelect:
+ if (calendarProvider.selectedDateList.contains(dateModel)) {
+ dateModel.isSelected = true;
+ } else {
+ dateModel.isSelected = false;
+ }
+ break;
}
return ItemContainer(
- dateModel: dateModel,
-// configuration: configuration,
-// calendarProvider: calendarProvider,
- );
+ dateModel: dateModel,
+ clickCall: () {
+ setState(() {});
+ });
});
}
}
diff --git a/pubspec.yaml b/pubspec.yaml
index 6387f3a..0fd3f31 100644
--- a/pubspec.yaml
+++ b/pubspec.yaml
@@ -1,6 +1,6 @@
name: flutter_custom_calendar
description: A calendar control of flutter that supports three selection modes。
-version: 1.0.4+0.3
+version: 1.0.4+0.5
homepage: http://www.fgyong.cn
author: fgyong
repository: https://github.com/ifgyong/flutter_custom_calendar