创建configuration类,将配置的信息放到这里

引入provider状态管理,避免深层嵌套传递信息
周视图和月视图,联动
增加日志输出类LogUtil,方便查看调试
增加example例子
develop
xiaodong 5 years ago
parent 95f2ca12ac
commit 025be98263

@ -0,0 +1,225 @@
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<CustomSignPage> {
String text;
CalendarController controller;
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: -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() {
text = "${DateTime.now().year}${DateTime.now().month}";
controller = new CalendarController(
weekBarItemWidgetBuilder: () {
return CustomStyleWeekBarItem();
},
dayWidgetBuilder: (dateModel) {
return CustomStyleDayWidget(dateModel);
},
extraDataMap: customExtraData);
controller.addMonthChangeListener(
(year, month) {
setState(() {
text = "$year$month";
});
},
);
controller.addOnCalendarSelectListener((dateModel) {
//
setState(() {});
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: new Container(
child: new Column(
children: <Widget>[
new Row(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
new IconButton(
icon: Icon(Icons.navigate_before),
onPressed: () {
controller.moveToPreviousMonth();
}),
new Text(text),
new IconButton(
icon: Icon(Icons.navigate_next),
onPressed: () {
controller.moveToNextMonth();
}),
],
),
CalendarViewWidget(
calendarController: controller,
),
new Text(
"自定义创建Item\n选中的时间:\n${controller.getSingleSelectCalendar().toString()}"),
],
),
),
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 = ["", "", "", "", "", "", ""];
@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) {
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: 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) {
return Container(
margin: EdgeInsets.all(8),
foregroundDecoration:
new BoxDecoration(border: Border.all(width: 2, color: Colors.blue)),
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: 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()
],
),
);
}
}

@ -1,4 +1,5 @@
import 'package:flutter/material.dart';
import 'custom_sign_page.dart';
import 'custom_style_page.dart';
import 'default_style_page.dart';
import 'multi_select_style_page.dart';
@ -24,6 +25,9 @@ class MyApp extends StatelessWidget {
"/progress": (context) => ProgressStylePage(
title: "进度条风格+单选",
),
"/custom_sign": (context) => CustomSignPage(
title: "自定义额外数据,实现标记功能",
)
},
title: 'Flutter Demo',
theme: ThemeData(
@ -64,6 +68,12 @@ class HomePage extends StatelessWidget {
},
child: new Text("进度条风格+单选"),
),
new RaisedButton(
onPressed: () {
Navigator.pushNamed(context, "/custom_sign");
},
child: new Text("自定义额外数据,实现标记功能"),
),
],
),
),

@ -81,8 +81,8 @@ class _MultiSelectStylePageState extends State<MultiSelectStylePage> {
CalendarViewWidget(
calendarController: controller,
),
new Text(
"多选模式\n选中的时间:\n${controller.getMultiSelectCalendar().toString()}"),
// new Text(
// "多选模式\n选中的时间:\n${controller.getMultiSelectCalendar().toString()}"),
],
),
),

@ -23,13 +23,13 @@ class _ProgressStylePageState extends State<ProgressStylePage> {
DateTime now = DateTime.now();
DateTime temp = DateTime(now.year, now.month, now.day);
Map<DateTime, int> progressMap = {
temp.add(Duration(days: 1)): 0,
temp.add(Duration(days: 2)): 20,
temp.add(Duration(days: 3)): 40,
temp.add(Duration(days: 4)): 60,
temp.add(Duration(days: 5)): 80,
temp.add(Duration(days: 6)): 100,
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,
};
controller = new CalendarController(
@ -90,8 +90,8 @@ class _ProgressStylePageState extends State<ProgressStylePage> {
CalendarViewWidget(
calendarController: controller,
),
new Text(
"单选模式\n选中的时间:\n${controller.getSingleSelectCalendar().toString()}"),
// new Text(
// "单选模式\n选中的时间:\n${controller.getSingleSelectCalendar().toString()}"),
],
),
),

@ -1,5 +1,5 @@
# Generated by pub
# See https://www.dartlang.org/tools/pub/glossary#lockfile
# See https://dart.dev/tools/pub/glossary#lockfile
packages:
async:
dependency: transitive
@ -7,14 +7,14 @@ packages:
name: async
url: "https://pub.flutter-io.cn"
source: hosted
version: "2.1.0"
version: "2.3.0"
boolean_selector:
dependency: transitive
description:
name: boolean_selector
url: "https://pub.flutter-io.cn"
source: hosted
version: "1.0.4"
version: "1.0.5"
charcode:
dependency: transitive
description:
@ -66,28 +66,42 @@ packages:
name: meta
url: "https://pub.flutter-io.cn"
source: hosted
version: "1.1.6"
version: "1.1.7"
path:
dependency: transitive
description:
name: path
url: "https://pub.flutter-io.cn"
source: hosted
version: "1.6.2"
version: "1.6.4"
pedantic:
dependency: transitive
description:
name: pedantic
url: "https://pub.flutter-io.cn"
source: hosted
version: "1.5.0"
version: "1.8.0+1"
provider:
dependency: transitive
description:
name: provider
url: "https://pub.flutter-io.cn"
source: hosted
version: "3.0.0+1"
quiver:
dependency: transitive
description:
name: quiver
url: "https://pub.flutter-io.cn"
source: hosted
version: "2.0.2"
version: "2.0.5"
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
@ -120,7 +134,7 @@ packages:
name: string_scanner
url: "https://pub.flutter-io.cn"
source: hosted
version: "1.0.4"
version: "1.0.5"
term_glyph:
dependency: transitive
description:
@ -134,7 +148,7 @@ packages:
name: test_api
url: "https://pub.flutter-io.cn"
source: hosted
version: "0.2.4"
version: "0.2.5"
typed_data:
dependency: transitive
description:
@ -150,4 +164,4 @@ packages:
source: hosted
version: "2.0.8"
sdks:
dart: ">=2.2.0 <3.0.0"
dart: ">=2.2.2 <3.0.0"

@ -27,7 +27,7 @@ dependencies:
# 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: any
flutter_custom_calendar:
path: ../

@ -9,8 +9,8 @@ class CalendarConfiguration {
//,MODE_SINGLE_SELECTMODE_MULTI_SELECT
int selectMode;
//
bool defaultExpandStatus;
bool defaultExpandStatus; //,truefalse
bool enableExpand; //
//
int minYear;
@ -35,13 +35,13 @@ class CalendarConfiguration {
Set<DateModel> defaultSelectedDateList = new Set(); //
int maxMultiSelectCount; //
Map<DateTime, Object> extraDataMap = new Map(); //
Map<DateModel, Object> extraDataMap = new Map(); //
/**
* UI
*/
double verticalSpacing ;//item10
double verticalSpacing; //item10
BoxDecoration boxDecoration; //
//
DayWidgetBuilder dayWidgetBuilder; //item
@ -86,7 +86,8 @@ class CalendarConfiguration {
this.pageController,
this.weekController,
this.verticalSpacing,
bool defaultExpandStatus = true}) {
this.enableExpand,
bool defaultExpandStatus}) {
this.defaultExpandStatus = defaultExpandStatus;
}

@ -1,16 +1,13 @@
import 'dart:developer';
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/model/date_model.dart';
import 'package:flutter_custom_calendar/widget/default_week_bar.dart';
import 'package:flutter_custom_calendar/constants/constants.dart';
import 'package:provider/provider.dart';
/**
* controller
@ -18,7 +15,7 @@ import 'package:provider/provider.dart';
class CalendarController {
static const Set<DateTime> EMPTY_SET = {};
static const Map<DateTime, Object> EMPTY_MAP = {};
static const Map<DateModel, Object> EMPTY_MAP = {};
static const Duration DEFAULT_DURATION = const Duration(milliseconds: 500);
CalendarConfiguration calendarConfiguration;
@ -54,7 +51,8 @@ class CalendarController {
DateModel selectDateModel,
int maxMultiSelectCount = 9999,
double verticalSpacing = 10,
Map<DateTime, Object> extraDataMap = EMPTY_MAP}) {
bool enableExpand = true,
Map<DateModel, Object> extraDataMap = EMPTY_MAP}) {
LogUtil.log(TAG: this.runtimeType, message: "init CalendarConfiguration");
calendarConfiguration = CalendarConfiguration(
selectMode: selectMode,
@ -70,7 +68,9 @@ class CalendarController {
maxSelectYear: maxSelectYear,
maxSelectMonth: maxSelectMonth,
defaultExpandStatus: expandStatus,
extraDataMap: extraDataMap,
maxSelectDay: maxSelectDay,
enableExpand: enableExpand,
verticalSpacing: verticalSpacing);
calendarConfiguration.dayWidgetBuilder = dayWidgetBuilder;

@ -9,3 +9,4 @@ 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';

@ -17,10 +17,11 @@ class DateModel {
String gregorianFestival; //
String traditionFestival; //
bool isCurrentMonth;//
bool isCurrentDay; //
bool isLeapYear; //
bool isWeekend; //
int leapMonth; //
// int leapMonth; //
Object extraData; //

@ -1,18 +1,21 @@
import 'package:flutter/material.dart';
//7
TextStyle topWeekTextStyle=new TextStyle(fontSize: 12);
TextStyle topWeekTextStyle = new TextStyle(fontSize: 12);
//
TextStyle currentMonthTextStyle =
new TextStyle(color: Colors.black, fontSize: 16);
new TextStyle(color: Colors.black, fontSize: 16);
//
TextStyle preOrNextMonthTextStyle =
new TextStyle(color: Colors.grey, fontSize: 18);
new TextStyle(color: Colors.grey, fontSize: 18);
//
TextStyle lunarTextStyle = new TextStyle(color: Colors.grey, fontSize: 12);
//
TextStyle notCurrentMonthTextStyle =
new TextStyle(color: Colors.grey, fontSize: 16);
TextStyle currentDayTextStyle = new TextStyle(color: Colors.red, fontSize: 16);

@ -65,8 +65,8 @@ class DateUtil {
*
*/
static bool isCurrentDay(int year, int month, int day) {
return new DateTime(year, month, day).difference(DateTime.now()).inDays ==
0;
DateTime now = DateTime.now();
return now.year == year && now.month == month && now.day == day;
}
/**
@ -113,7 +113,7 @@ class DateUtil {
int year, int month, DateTime currentDate, int weekStart,
{DateModel minSelectDate,
DateModel maxSelectDate,
Map<DateTime, Object> extraDataMap}) {
Map<DateModel, Object> extraDataMap}) {
weekStart = DateTime.monday;
//
int mPreDiff = getIndexOfFirstDayInMonth(new DateTime(year, month));
@ -136,19 +136,22 @@ class DateUtil {
DateTime temp;
DateModel dateModel;
if (i < mPreDiff - 1) {
//
temp = firstDayOfMonth.subtract(Duration(days: mPreDiff - i - 1));
dateModel = DateModel.fromDateTime(temp);
//
dateModel.isCurrentMonth = false;
} else if (i >= monthDayCount + (mPreDiff - 1)) {
//
temp = lastDayOfMonth
.add(Duration(days: i - mPreDiff - monthDayCount + 2));
dateModel = DateModel.fromDateTime(temp);
//
dateModel.isCurrentMonth = false;
} else {
//
temp = new DateTime(year, month, i - mPreDiff + 2);
dateModel = DateModel.fromDateTime(temp);
dateModel.isCurrentMonth = true;
}
//
@ -160,9 +163,8 @@ class DateUtil {
}
//model
if (extraDataMap != null && extraDataMap.isNotEmpty) {
DateTime dateTime = dateModel.getDateTime();
if (extraDataMap.containsKey(dateTime)) {
dateModel.extraData = extraDataMap[dateTime];
if (extraDataMap.containsKey(dateModel)) {
dateModel.extraData = extraDataMap[dateModel];
}
}
@ -197,7 +199,7 @@ class DateUtil {
int year, int month, DateTime currentDate, int weekStart,
{DateModel minSelectDate,
DateModel maxSelectDate,
Map<DateTime, Object> extraDataMap}) {
Map<DateModel, Object> extraDataMap}) {
LogUtil.log(TAG: "DateUtil", message: "initCalendarForWeekView");
List<DateModel> items = List();
@ -217,11 +219,16 @@ class DateUtil {
} else {
dateModel.isInRange = false;
}
if (month == dateModel.month) {
dateModel.isCurrentMonth = true;
} else {
dateModel.isCurrentMonth = false;
}
//model
if (extraDataMap != null && extraDataMap.isNotEmpty) {
DateTime dateTime = dateModel.getDateTime();
if (extraDataMap.containsKey(dateTime)) {
dateModel.extraData = extraDataMap[dateTime];
if (extraDataMap.containsKey(dateModel)) {
dateModel.extraData = extraDataMap[dateModel];
}
}

@ -766,16 +766,16 @@ class LunarUtil {
dateModel.isLeapYear = DateUtil.isLeapYear(year);
dateModel.isCurrentDay = DateUtil.isCurrentDay(year, month, day);
List<int> lunar = LunarUtil.solarToLunar(year, month, day);
List<int> lunar = LunarUtil.solarToLunar(2020, 2, day);
dateModel.lunarYear = (lunar[0]);
dateModel.lunarMonth = (lunar[1]);
dateModel.lunarDay = (lunar[2]);
if (lunar[3] == 1) {
//
dateModel.leapMonth = lunar[1];
}
// if (lunar[3] == 1) {
// //
// dateModel.leapMonth = lunar[1];
// }
//24
String solarTerm = getSolarTerm(year, month, day);
dateModel.solarTerm=solarTerm;

@ -72,8 +72,11 @@ class DefaultCustomDayWidget extends BaseCustomDayWidget {
void defaultDrawNormal(DateModel dateModel, Canvas canvas, Size size) {
//
TextPainter dayTextPainter = new TextPainter()
..text =
TextSpan(text: dateModel.day.toString(), style: currentMonthTextStyle)
..text = TextSpan(
text: dateModel.day.toString(),
style: dateModel.isCurrentDay
? currentDayTextStyle
: currentMonthTextStyle)
..textDirection = TextDirection.ltr
..textAlign = TextAlign.center;

@ -18,7 +18,7 @@ class MonthView extends StatefulWidget {
final DateModel minSelectDate;
final DateModel maxSelectDate;
final Map<DateTime, Object> extraDataMap; //
final Map<DateModel, Object> extraDataMap; //
const MonthView({
@required this.year,

@ -18,7 +18,7 @@ class WeekView extends StatefulWidget {
final DateModel minSelectDate;
final DateModel maxSelectDate;
final Map<DateTime, Object> extraDataMap; //
final Map<DateModel, Object> extraDataMap; //
const WeekView(
{@required this.year,
@ -39,7 +39,7 @@ class _WeekViewState extends State<WeekView> {
void initState() {
super.initState();
items = DateUtil.initCalendarForWeekView(
2019, 9, widget.firstDayOfWeek.getDateTime(), 0,
widget.year, widget.month, widget.firstDayOfWeek.getDateTime(), 0,
minSelectDate: widget.minSelectDate,
maxSelectDate: widget.maxSelectDate,
extraDataMap: widget.extraDataMap);

Loading…
Cancel
Save