文档修改,代码优化

develop^2
LXD312569496 5 years ago committed by xiaodong
parent c223b3ba78
commit d0b0d88ca5

318
API.md

@ -0,0 +1,318 @@
## 主要Api文档
### 配置日历的UI
日历UI相关的配置是在CalendarViewWidget的构造函数里面进行配置就行了。
```
CalendarViewWidget(
{Key key,
this.dayWidgetBuilder = defaultCustomDayWidget,
this.weekBarItemWidgetBuilder = defaultWeekBarWidget,
@required this.calendarController,
this.boxDecoration,
this.padding = EdgeInsets.zero,
this.margin = EdgeInsets.zero,
this.verticalSpacing = 10,
this.itemSize})
: super(key: key);
```
#### 参数说明
属性 | 含义 | 默认值
:-: | :-: | :-:
weekBarItemWidgetBuilder | 创建顶部的weekbar | 默认样式
dayWidgetBuilder | 创建日历item | 默认样式
verticalSpacing|日历item之间的竖直方向间距|默认10
boxDecoration |整体的背景设置|默认为空
itemSize| 每个item的边长|手机默认是屏幕宽度/7网页默认屏幕高度/7|
padding|日历的padding |默认为EdgeInsets.zero
margin| 日历的padding|默认为EdgeInsets.zero
### 配置Controller
两个作用:
一个是显示日历所需要的相关数据是在controller里面进行配置的。
一个是可以利用controller添加事件监听使用controller进行操作日历
```
//构造函数
CalendarController(
{int selectMode = CalendarConstants.MODE_SINGLE_SELECT,
int showMode = CalendarConstants.MODE_SHOW_ONLY_MONTH,
int minYear = 1971,
int maxYear = 2055,
int minYearMonth = 1,
int maxYearMonth = 12,
int nowYear,
int nowMonth,
int minSelectYear = 1971,
int minSelectMonth = 1,
int minSelectDay = 1,
int maxSelectYear = 2055,
int maxSelectMonth = 12,
int maxSelectDay = 30,
Set<DateTime> selectedDateTimeList = EMPTY_SET,
DateModel selectDateModel,
int maxMultiSelectCount = 9999,
Map<DateModel, Object> extraDataMap = EMPTY_MAP})
```
#### 通用参数说明
属性 | 含义 | 默认值
:-: | :-: | :-:
selectMode | 选择模式,表示单选或者多选 | 默认是单选<br>static const int MODE_SINGLE_SELECT = 1;<br>static const int MODE_MULTI_SELECT = 2;
showMode|展示模式| 默认是只展示月视图<br>static const int MODE_SHOW_ONLY_MONTH=1;//仅支持月视图<br>static const int MODE_SHOW_ONLY_WEEK=2;//仅支持星期视图<br>static const int MODE_SHOW_WEEK_AND_MONTH=3;//支持两种视图,先显示周视图<br>static const int MODE_SHOW_MONTH_AND_WEEK=4;//支持两种视图,先显示月视图
minYear | 日历显示的最小年份| 1971
maxYear | 日历显示的最大年份| 2055
minYearMonth | 日历显示的最小年份的月份| 1
maxYearMonth | 日历显示的最大年份的月份| 12
nowYear | 日历显示的当前的年份| -1
nowMonth | 日历显示的当前的月份| -1
minSelectYear | 可以选择的最小年份| 1971
minSelectMonth | 可以选择的最小年份的月份| 1
minSelectDay | 可以选择的最小月份的日子| 1
maxSelectYear | 可以选择的最大年份| 2055
maxSelectMonth | 可以选择的最大年份的月份| 12
maxSelectDay | 可以选择的最大月份的日子| 30注意不能超过对应月份的总天数
selectedDateList | 被选中的日期,用于多选| 默认为空Set, Set<DateModel> selectedDateList = new Set()
selectDateModel | 当前选择项,用于单选| 默认为空
maxMultiSelectCount | 多选,最多选多少个| hhh
extraDataMap | 自定义额外的数据| 默认为空MapMap<DateTime, Object> extraDataMap = new Map()
#### 给controller添加事件监听
方法 | 含义 | 默认值
:-: | :-: | :-:
void addMonthChangeListener(OnMonthChange listener) | 月份切换事件 |
void addOnCalendarSelectListener(OnCalendarSelect listener) | 点击选择事件 |
void addOnMultiSelectOutOfRangeListener(OnMultiSelectOutOfRange listener) | 多选超出指定范围 |
void addOnMultiSelectOutOfSizeListener(OnMultiSelectOutOfSize listener) | 多选超出限制个数 |
void addExpandChangeListener(ValueChanged<bool> expandChange)|监听日历的展开收缩状态|
#### 利用controller来控制日历的切换支持配置动画
方法 | 含义 | 默认值
:-: | :-: | :-:
Future<bool> previousPage()|滑动到上一个页面会自动根据当前的展开状态滑动到上一个月或者上一个星期。如果已经在第一个页面没有上一个页面就会返回false其他情况返回true|
Future<bool> nextPage()|滑动到下一个页面会自动根据当前的展开状态滑动到下一个月或者下一个星期。如果已经在最后一个页面没有下一个页面就会返回false其他情况返回true|
void moveToCalendar(int year, int month, int day, {bool needAnimation = false,Duration duration = const Duration(milliseconds: 500),Curve curve = Curves.ease}) | 到指定日期 |
void moveToNextYear()|切换到下一年|
void moveToPreviousYear()|切换到上一年|
void moveToNextMonth()|切换到下一个月份|
void moveToPreviousMonth()|切换到上一个月份|
void toggleExpandStatus()|切换展开状态|
void changeExtraData(Map<DateModel, Object> newMap)|修改自定义的额外数据并刷新日历|
#### 利用controller来获取当前日历的状态和数据
方法 | 含义 | 默认值
:-: | :-: | :-:
DateTime getCurrentMonth()|获取当前的月份|
Set<DateModel> getMultiSelectCalendar()|获取被选中的日期,多选|
DateModel getSingleSelectCalendar()|获取被选中的日期,单选|
### 如何自定义UI
包括自定义WeekBar、自定义日历Item默认使用的都是DefaultXXXWidget。
只要继承对应的Base类实现相应的方法然后只需要在配置Controller的时候实现相应的Builder方法就可以了。
```
//支持自定义绘制
DayWidgetBuilder dayWidgetBuilder; //创建日历item
WeekBarItemWidgetBuilder weekBarItemWidgetBuilder; //创建顶部的weekbar
```
#### 自定义WeekBar
第一种做法继承BaseWeekBar重写getWeekBarItem(index)方法就可以。随便你怎么实现只需要返回一个Widget就可以了。
```
class DefaultWeekBar extends BaseWeekBar {
const DefaultWeekBar({Key key}) : super(key: key);
@override
Widget getWeekBarItem(int index) {
/**
* 自定义Widget
*/
return new Container(
height: 40,
alignment: Alignment.center,
child: new Text(
Constants.WEEK_LIST[index],
style: topWeekTextStyle,
),
);
}
}
```
第二种做法你也可以直接重写build方法进行更加的自定义绘制。
```
class CustomStyleWeekBarItem extends BaseWeekBar {
List<String> weekList = ["mo", "tu", "we", "th", "fr", "sa", "su"];
//可以直接重写build方法weekbar底部添加下划线
@override
Widget build(BuildContext context) {
List<Widget> 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)),
),
),
);
}
}
```
#### 自定义日历Item
提供两种方法一种是利用组合widget的方式来创建一种是利用Canvas来自定义绘制Item。最后只需要在CalendarController的构造参数中进行配置就可以了。
* 利用组合widget的方式来创建继承BaseCombineDayWidget重写getNormalWidget(DateModel dateModel)
和getSelectedWidget(DateModel dateModel)就可以了返回对应的widget就行。
```
class DefaultCombineDayWidget extends BaseCombineDayWidget {
DefaultCombineDayWidget(DateModel dateModel) : super(dateModel);
@override
Widget getNormalWidget(DateModel dateModel) {
//实现默认状态下的UI
}
@override
Widget getSelectedWidget(DateModel dateModel) {
//绘制被选中的UI
}
}
```
* 利用Canvas来自定义绘制继承BaseCustomDayWidget重写drawNormal和drawSelected的两个方法就可以了利用canvas自己绘制Item。
```
class DefaultCustomDayWidget extends BaseCustomDayWidget {
DefaultCustomDayWidget(DateModel dateModel) : super(dateModel);
@override
void drawNormal(DateModel dateModel, Canvas canvas, Size size) {
//实现默认状态下的UI
defaultDrawNormal(dateModel, canvas, size);
}
@override
void drawSelected(DateModel dateModel, Canvas canvas, Size size) {
//绘制被选中的UI
defaultDrawSelected(dateModel, canvas, size);
}
}
```
### 根据实际场景自定义额外的数据extraData
#### 实现进度条样式的日历
我们可以自定义每个item的进度条数据保存到map中最后赋值给controller的extraDataMap。
```
//外部处理每个dateModel所对应的进度
Map<DateModel, int> 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,
};
//创建CalendarController对象的时候将extraDataMap赋值就行了
new CalendarController(
extraDataMap: progressMap)
//绘制DayWidget的时候可以直接从dateModel的extraData对象中拿到想要的数据
int progress = dateModel.extraData;
```
#### 实现自定义各种标记的日历
```
//外部处理每个dateModel所对应的标记
Map<DateModel, String> 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: 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))): "班",
};
//创建CalendarController对象的时候将extraDataMap赋值就行了
new CalendarController(
extraDataMap: customExtraData)
//绘制DayWidget的时候可以直接从dateModel的extraData对象中拿到想要的数据
String data = dateModel.extraData;
```
#### 手动修改自定义的数据
当自定义的数据发生变化的时候可以使用controller的changeExtraData(Map<DateModel, Object> newMap)方法,
日历会自动刷新,根据新的额外数据进行绘制。
```
//可以动态修改extraDataMap
void changeExtraData(Map<DateModel, Object> newMap) {
this.calendarConfiguration.extraDataMap = newMap;
this.calendarProvider.generation.value++;
}
```
### DateModel实体类
日历所用的日期的实体类DateModel有下面这些属性。可以在自定义绘制DayWidget的时候根据相应的属性进行判断后绘制相应的UI。
属性|含义|类型|默认值
:-: | :-: | :-: |:-:
year|年份|int|
month|月份|int|
day|日期|int|默认为1
lunarYear|农历年份|int|
lunarMonth|农历月份|int|
lunarDay|农历日期|int|
lunarString|农历字符串|String|
solarTerm|24节气|String|
gregorianFestival|gregorianFestival|String|
traditionFestival|传统农历节日|String|
isCurrentDay|是否是今天|bool|false
isLeapYear|是否是闰年|bool|false
isWeekend|是否是周末|bool|false
isInRange|是否在范围内,比如可以实现在某个范围外,设置置灰的功能|bool|false
isSelected|是否被选中,用来实现一些标记或者选择功能|bool|false
extraData|自定义的额外数据|Object|默认为空
isCurrentMonth|是否是当前月份|bool|
方法|含义|
:-: | :-: |
DateTime getDateTime()|将DateModel转化成DateTime
DateModel fromDateTime(DateTime dateTime)|根据DateTime创建对应的model并初始化农历和传统节日等信息
bool operator ==(Object other)|重写==方法可以判断两个dateModel是否是同一天

@ -3,9 +3,7 @@ import 'package:flutter_custom_calendar/flutter_custom_calendar.dart';
import 'package:flutter_custom_calendar/style/style.dart';
import 'package:random_pk/random_pk.dart';
/**
*
*/
class BlueStylePage extends StatefulWidget {
BlueStylePage({Key key, this.title}) : super(key: key);
@ -26,13 +24,8 @@ class _BlueStylePageState extends State<BlueStylePage> {
@override
void initState() {
controller = new CalendarController(
weekBarItemWidgetBuilder: () {
return CustomStyleWeekBarItem();
},
dayWidgetBuilder: (dateModel) {
return CustomStyleDayWidget(dateModel);
},
showMode: Constants.MODE_SHOW_MONTH_AND_WEEK,
showMode: CalendarConstants.MODE_SHOW_MONTH_AND_WEEK,
extraDataMap: customExtraData);
controller.addMonthChangeListener(
@ -56,6 +49,12 @@ class _BlueStylePageState extends State<BlueStylePage> {
@override
Widget build(BuildContext context) {
var calendarWidget = CalendarViewWidget(
weekBarItemWidgetBuilder: () {
return CustomStyleWeekBarItem();
},
dayWidgetBuilder: (dateModel) {
return CustomStyleDayWidget(dateModel);
},
calendarController: controller,
boxDecoration: BoxDecoration(
borderRadius: BorderRadius.only(
@ -126,7 +125,10 @@ class _BlueStylePageState extends State<BlueStylePage> {
ValueListenableBuilder(
valueListenable: selectText,
builder: (context, value, child) {
return new Text(selectText.value);
return Padding(
padding: const EdgeInsets.all(20.0),
child: new Text(selectText.value),
);
}),
],
),

@ -44,12 +44,7 @@ class _CustomSignPageState extends State<CustomSignPage> {
@override
void initState() {
controller = new CalendarController(
weekBarItemWidgetBuilder: () {
return CustomStyleWeekBarItem();
},
dayWidgetBuilder: (dateModel) {
return CustomStyleDayWidget(dateModel);
},
extraDataMap: customExtraData);
controller.addMonthChangeListener(
@ -101,6 +96,12 @@ class _CustomSignPageState extends State<CustomSignPage> {
),
CalendarViewWidget(
calendarController: controller,
weekBarItemWidgetBuilder: () {
return CustomStyleWeekBarItem();
},
dayWidgetBuilder: (dateModel) {
return CustomStyleDayWidget(dateModel);
},
),
ValueListenableBuilder(
valueListenable: selectText,

@ -21,11 +21,7 @@ class _CustomStylePageState extends State<CustomStylePage> {
@override
void initState() {
controller = new CalendarController(weekBarItemWidgetBuilder: () {
return CustomStyleWeekBarItem();
}, dayWidgetBuilder: (dateModel) {
return CustomStyleDayWidget(dateModel);
});
controller = new CalendarController();
controller.addMonthChangeListener(
(year, month) {
@ -76,7 +72,12 @@ class _CustomStylePageState extends State<CustomStylePage> {
),
CalendarViewWidget(
calendarController: controller,
),
weekBarItemWidgetBuilder: () {
return CustomStyleWeekBarItem();
},
dayWidgetBuilder: (dateModel) {
return CustomStyleDayWidget(dateModel);
}),
ValueListenableBuilder(
valueListenable: selectText,
builder: (context, value, child) {

@ -27,7 +27,7 @@ class _DefaultStylePageState extends State<DefaultStylePage> {
minYearMonth: now.month - 5,
maxYear: now.year,
maxYearMonth: now.month + 5,
showMode: Constants.MODE_SHOW_MONTH_AND_WEEK);
showMode: CalendarConstants.MODE_SHOW_MONTH_AND_WEEK);
controller.addMonthChangeListener(
(year, month) {

@ -1,4 +1,5 @@
import 'package:example/only_week_page.dart';
import 'package:example/red_style_page.dart';
import 'package:flutter/material.dart';
import 'package:flutter/rendering.dart';
import 'blue_style_page.dart';
@ -40,9 +41,8 @@ class MyApp extends StatelessWidget {
"/only_week_view": (context) => OnlyWeekPage(
title: "仅显示周视图",
),
"/blue_style_page":(context)=>BlueStylePage(
title:"蓝色背景Demo"
)
"/blue_style_page": (context) => BlueStylePage(title: "蓝色背景Demo"),
"/red_style_page": (context) => RedStylePage(title: "蓝色背景Demo"),
},
title: 'Flutter Demo',
theme: ThemeData(
@ -95,9 +95,18 @@ class HomePage extends StatelessWidget {
},
child: new Text("仅显示周视图"),
),
new RaisedButton(onPressed: (){
new RaisedButton(
onPressed: () {
Navigator.pushNamed(context, "/blue_style_page");
},child: new Text("蓝色Demo"),)
},
child: new Text("蓝色Demo"),
),
new RaisedButton(
onPressed: () {
Navigator.pushNamed(context, "/red_style_page");
},
child: new Text("红色Demo"),
)
],
),
),

@ -22,17 +22,12 @@ class _MultiSelectStylePageState extends State<MultiSelectStylePage> {
@override
void initState() {
controller = new CalendarController(
selectMode: Constants.MODE_MULTI_SELECT,
selectMode: CalendarConstants.MODE_MULTI_SELECT,
maxMultiSelectCount: 5,
minSelectYear: 2019,
minSelectMonth: 5,
minSelectDay: 20,
weekBarItemWidgetBuilder: () {
return CustomStyleWeekBarItem();
},
dayWidgetBuilder: (dateModel) {
return CustomStyleDayWidget(dateModel);
});
);
controller.addMonthChangeListener(
(year, month) {
@ -83,7 +78,12 @@ class _MultiSelectStylePageState extends State<MultiSelectStylePage> {
),
CalendarViewWidget(
calendarController: controller,
),
weekBarItemWidgetBuilder: () {
return CustomStyleWeekBarItem();
},
dayWidgetBuilder: (dateModel) {
return CustomStyleDayWidget(dateModel);
}),
ValueListenableBuilder(
valueListenable: selectText,
builder: (context, value, child) {

@ -27,7 +27,7 @@ class _OnlyWeekPageState extends State<OnlyWeekPage> {
minYearMonth: now.month - 2,
maxYear: now.year,
maxYearMonth: now.month + 1,
showMode: Constants.MODE_SHOW_ONLY_WEEK);
showMode: CalendarConstants.MODE_SHOW_ONLY_WEEK);
controller.addMonthChangeListener(
(year, month) {

@ -42,12 +42,7 @@ class _ProgressStylePageState extends State<ProgressStylePage> {
controller = new CalendarController(
extraDataMap: progressMap,
weekBarItemWidgetBuilder: () {
return CustomStyleWeekBarItem();
},
dayWidgetBuilder: (dateModel) {
return ProgressStyleDayWidget(dateModel);
});
);
controller.addMonthChangeListener(
(year, month) {
@ -98,7 +93,12 @@ class _ProgressStylePageState extends State<ProgressStylePage> {
),
CalendarViewWidget(
calendarController: controller,
),
weekBarItemWidgetBuilder: () {
return CustomStyleWeekBarItem();
},
dayWidgetBuilder: (dateModel) {
return ProgressStyleDayWidget(dateModel);
}),
ValueListenableBuilder(
valueListenable: selectText,
builder: (context, value, child) {

@ -0,0 +1,236 @@
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<RedStylePage> {
ValueNotifier<String> text;
ValueNotifier<String> selectText;
CalendarController controller;
Map<DateModel, String> customExtraData = {};
Color pinkColor = Color(0xffFF8291);
@override
void 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: <Widget>[
SizedBox(
height: 20,
),
Stack(
alignment: Alignment.center,
children: <Widget>[
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 {
List<String> weekList = ["M", "T", "W", "T", "F", "S", "S"];
//build
@override
Widget build(BuildContext context) {
List<Widget> 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);
TextStyle normalTextStyle =
TextStyle(fontWeight: FontWeight.w700, color: Colors.black);
TextStyle noIsCurrentMonthTextStyle =
TextStyle(fontWeight: FontWeight.w700, color: Colors.grey);
@override
Widget getNormalWidget(DateModel dateModel) {
return Container(
margin: EdgeInsets.all(8),
child: new Stack(
alignment: Alignment.center,
children: <Widget>[
new Column(
mainAxisSize: MainAxisSize.max,
crossAxisAlignment: CrossAxisAlignment.center,
children: <Widget>[
//
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: <Widget>[
new Column(
mainAxisSize: MainAxisSize.max,
crossAxisAlignment: CrossAxisAlignment.center,
children: <Widget>[
//
new Expanded(
child: Center(
child: new Text(
dateModel.day.toString(),
style: TextStyle(color: Colors.white),
),
),
),
],
),
],
),
);
}
}

@ -2,6 +2,7 @@ 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';
@ -110,6 +111,10 @@ class CalendarProvider extends ChangeNotifier {
CalendarConfiguration calendarConfiguration,
EdgeInsetsGeometry padding,
EdgeInsetsGeometry margin,
double itemSize,
double verticalSpacing,
DayWidgetBuilder dayWidgetBuilder,
WeekBarItemWidgetBuilder weekBarItemWidgetBuilder,
}) {
LogUtil.log(TAG: this.runtimeType, message: "CalendarProvider initData");
if (selectedDateList != null) {
@ -119,14 +124,21 @@ class CalendarProvider extends ChangeNotifier {
this.calendarConfiguration = calendarConfiguration;
this.calendarConfiguration.padding = padding;
this.calendarConfiguration.margin = margin;
this.calendarConfiguration.itemSize = itemSize;
this.calendarConfiguration.verticalSpacing = verticalSpacing;
this.calendarConfiguration.dayWidgetBuilder=dayWidgetBuilder;
this.calendarConfiguration.weekBarItemWidgetBuilder=weekBarItemWidgetBuilder;
//lastClickDateModelitem
this.lastClickDateModel = selectDateModel != null
? selectDateModel
: DateModel.fromDateTime(DateTime.now())
..day = 15;
//
if (calendarConfiguration.showMode == Constants.MODE_SHOW_ONLY_WEEK ||
calendarConfiguration.showMode == Constants.MODE_SHOW_WEEK_AND_MONTH) {
if (calendarConfiguration.showMode ==
CalendarConstants.MODE_SHOW_ONLY_WEEK ||
calendarConfiguration.showMode ==
CalendarConstants.MODE_SHOW_WEEK_AND_MONTH) {
expandStatus = ValueNotifier(false);
} else {
expandStatus = ValueNotifier(true);
@ -151,8 +163,10 @@ class CalendarProvider extends ChangeNotifier {
}
//
if (calendarConfiguration.showMode == Constants.MODE_SHOW_ONLY_MONTH ||
calendarConfiguration.showMode == Constants.MODE_SHOW_MONTH_AND_WEEK) {
if (calendarConfiguration.showMode ==
CalendarConstants.MODE_SHOW_ONLY_MONTH ||
calendarConfiguration.showMode ==
CalendarConstants.MODE_SHOW_MONTH_AND_WEEK) {
int lineCount = DateUtil.getMonthViewLineCount(
calendarConfiguration.nowYear, calendarConfiguration.nowMonth);
totalHeight = calendarConfiguration.itemSize * (lineCount) +

@ -41,9 +41,9 @@ class CalendarConfiguration {
/**
* UI
*/
double itemSize; ///7
double verticalSpacing; //item10
BoxDecoration boxDecoration; //
double itemSize; ///7
EdgeInsetsGeometry padding;
EdgeInsetsGeometry margin;

@ -1,4 +1,4 @@
class Constants {
class CalendarConstants {
//
static const int MODE_SINGLE_SELECT = 1;

@ -31,10 +31,8 @@ class CalendarController {
PageController weekController; //controller
CalendarController(
{int selectMode = Constants.MODE_SINGLE_SELECT,
int showMode = Constants.MODE_SHOW_ONLY_MONTH,
DayWidgetBuilder dayWidgetBuilder = defaultCustomDayWidget,
WeekBarItemWidgetBuilder weekBarItemWidgetBuilder = defaultWeekBarWidget,
{int selectMode = CalendarConstants.MODE_SINGLE_SELECT,
int showMode = CalendarConstants.MODE_SHOW_ONLY_MONTH,
int minYear = 1971,
int maxYear = 2055,
int minYearMonth = 1,
@ -50,8 +48,6 @@ class CalendarController {
Set<DateTime> selectedDateTimeList = EMPTY_SET,
DateModel selectDateModel,
int maxMultiSelectCount = 9999,
double verticalSpacing = 10,
double itemSize,
Map<DateModel, Object> extraDataMap = EMPTY_MAP}) {
LogUtil.log(TAG: this.runtimeType, message: "init CalendarConfiguration");
//
@ -77,11 +73,7 @@ class CalendarController {
maxSelectMonth: maxSelectMonth,
extraDataMap: extraDataMap,
maxSelectDay: maxSelectDay,
verticalSpacing: verticalSpacing,
itemSize: itemSize);
calendarConfiguration.dayWidgetBuilder = dayWidgetBuilder;
calendarConfiguration.weekBarItemWidgetBuilder = weekBarItemWidgetBuilder;
);
if (selectedDateTimeList != null && selectedDateTimeList.isNotEmpty) {
calendarConfiguration.defaultSelectedDateList
@ -100,7 +92,7 @@ class CalendarController {
maxYearMonth,
))}");
if (showMode != Constants.MODE_SHOW_ONLY_WEEK) {
if (showMode != CalendarConstants.MODE_SHOW_ONLY_WEEK) {
//pageController,initialPage
int initialPage = 0;
int nowMonthIndex = 0;
@ -138,7 +130,7 @@ class CalendarController {
"初始化月份视图的信息:一共有${monthList.length}个月initialPage为${nowMonthIndex}");
}
if (showMode != Constants.MODE_SHOW_ONLY_MONTH) {
if (showMode != CalendarConstants.MODE_SHOW_ONLY_MONTH) {
//
///72
int initialWeekPage = 0;
@ -182,8 +174,6 @@ class CalendarController {
calendarConfiguration.weekList = weekList;
calendarConfiguration.monthController = monthController;
calendarConfiguration.weekController = weekController;
calendarConfiguration.dayWidgetBuilder = dayWidgetBuilder;
calendarConfiguration.weekBarItemWidgetBuilder = weekBarItemWidgetBuilder;
}
//

@ -908,7 +908,7 @@ class LunarUtil {
if (day == 1) {
return numToChineseMonth(month, leap);
}
return Constants.LUNAR_DAY_TEXT[day - 1];
return CalendarConstants.LUNAR_DAY_TEXT[day - 1];
}
/**
@ -920,9 +920,9 @@ class LunarUtil {
*/
static String numToChineseMonth(int month, int leap) {
if (leap == 1) {
return "" + Constants.LUNAR_MONTH_TEXT[month - 1];
return "" + CalendarConstants.LUNAR_MONTH_TEXT[month - 1];
}
return Constants.LUNAR_MONTH_TEXT[month - 1];
return CalendarConstants.LUNAR_MONTH_TEXT[month - 1];
}
static String getString(int month, int day) {

@ -3,7 +3,6 @@ import 'package:flutter/material.dart';
import 'package:flutter_custom_calendar/calendar_provider.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_pager.dart';
@ -23,15 +22,28 @@ class CalendarViewWidget extends StatefulWidget {
EdgeInsetsGeometry padding;
EdgeInsetsGeometry margin;
///7
double itemSize;
//item10
double verticalSpacing;
DayWidgetBuilder dayWidgetBuilder;
WeekBarItemWidgetBuilder weekBarItemWidgetBuilder;
//
final CalendarController calendarController;
CalendarViewWidget(
{Key key,
this.dayWidgetBuilder = defaultCustomDayWidget,
this.weekBarItemWidgetBuilder = defaultWeekBarWidget,
@required this.calendarController,
this.boxDecoration,
this.padding = EdgeInsets.zero,
this.margin = EdgeInsets.zero})
this.margin = EdgeInsets.zero,
this.verticalSpacing = 10,
this.itemSize})
: super(key: key);
@override
@ -45,7 +57,11 @@ class _CalendarViewWidgetState extends State<CalendarViewWidget> {
widget.calendarController.calendarProvider.initData(
calendarConfiguration: widget.calendarController.calendarConfiguration,
padding: widget.padding,
margin: widget.margin);
margin: widget.margin,
itemSize: widget.itemSize,
verticalSpacing: widget.verticalSpacing,
dayWidgetBuilder: widget.dayWidgetBuilder,
weekBarItemWidgetBuilder: widget.weekBarItemWidgetBuilder);
super.initState();
}
@ -99,15 +115,15 @@ class CalendarContainerState extends State<CalendarContainer>
expand = calendarProvider.expandStatus.value;
if (calendarProvider.calendarConfiguration.showMode ==
Constants.MODE_SHOW_ONLY_WEEK) {
CalendarConstants.MODE_SHOW_ONLY_WEEK) {
widgets.add(const WeekViewPager());
} else if (calendarProvider.calendarConfiguration.showMode ==
Constants.MODE_SHOW_WEEK_AND_MONTH) {
CalendarConstants.MODE_SHOW_WEEK_AND_MONTH) {
widgets.add(const MonthViewPager());
widgets.add(const WeekViewPager());
index = 1;
} else if (calendarProvider.calendarConfiguration.showMode ==
Constants.MODE_SHOW_MONTH_AND_WEEK) {
CalendarConstants.MODE_SHOW_MONTH_AND_WEEK) {
widgets.add(const MonthViewPager());
widgets.add(const WeekViewPager());
index = 0;
@ -119,9 +135,9 @@ class CalendarContainerState extends State<CalendarContainer>
//
if (calendarProvider.calendarConfiguration.showMode ==
Constants.MODE_SHOW_WEEK_AND_MONTH ||
CalendarConstants.MODE_SHOW_WEEK_AND_MONTH ||
calendarProvider.calendarConfiguration.showMode ==
Constants.MODE_SHOW_MONTH_AND_WEEK) {
CalendarConstants.MODE_SHOW_MONTH_AND_WEEK) {
calendarProvider.expandStatus.addListener(() {
setState(() {
print(
@ -147,7 +163,7 @@ class CalendarContainerState extends State<CalendarContainer>
widget.calendarController.addMonthChangeListener((year, month) {
if (widget.calendarController.calendarProvider.calendarConfiguration
.showMode !=
Constants.MODE_SHOW_ONLY_WEEK) {
CalendarConstants.MODE_SHOW_ONLY_WEEK) {
//setState使
int lineCount = DateUtil.getMonthViewLineCount(year, month);
double newHeight = itemHeight * (lineCount) +

@ -47,7 +47,7 @@ class DefaultWeekBar extends BaseWeekBar {
height: 40,
alignment: Alignment.center,
child: new Text(
Constants.WEEK_LIST[index],
CalendarConstants.WEEK_LIST[index],
style: topWeekTextStyle,
),
);

@ -125,7 +125,7 @@ class _MonthViewState extends State<MonthView>
itemBuilder: (context, index) {
DateModel dateModel = items[index];
//
if (configuration.selectMode == Constants.MODE_MULTI_SELECT) {
if (configuration.selectMode == CalendarConstants.MODE_MULTI_SELECT) {
if (calendarProvider.selectedDateList.contains(dateModel)) {
dateModel.isSelected = true;
} else {
@ -213,7 +213,7 @@ class ItemContainerState extends State<ItemContainer> {
//
if (!dateModel.isInRange) {
//
if (configuration.selectMode == Constants.MODE_MULTI_SELECT) {
if (configuration.selectMode == CalendarConstants.MODE_MULTI_SELECT) {
configuration.multiSelectOutOfRange();
}
return;
@ -221,7 +221,7 @@ class ItemContainerState extends State<ItemContainer> {
calendarProvider.lastClickDateModel = dateModel;
if (configuration.selectMode == Constants.MODE_MULTI_SELECT) {
if (configuration.selectMode == CalendarConstants.MODE_MULTI_SELECT) {
//
if (calendarProvider.selectedDateList.length ==
configuration.maxMultiSelectCount) {

@ -72,7 +72,7 @@ class _WeekViewState extends State<WeekView> {
itemBuilder: (context, index) {
DateModel dateModel = items[index];
//
if (configuration.selectMode == Constants.MODE_MULTI_SELECT) {
if (configuration.selectMode == CalendarConstants.MODE_MULTI_SELECT) {
if (calendarProvider.selectedDateList.contains(dateModel)) {
dateModel.isSelected = true;
} else {

Loading…
Cancel
Save