Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Null safe calendar #64

Merged
merged 5 commits into from
Jan 12, 2022
Merged
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
100 changes: 43 additions & 57 deletions lib/src/components/calendar/brn_calendar_view.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
// @dart=2.9

import 'package:bruno/bruno.dart';
import 'package:bruno/src/constants/brn_asset_constants.dart';
import 'package:bruno/src/theme/brn_theme_configurator.dart';
import 'package:bruno/src/utils/brn_tools.dart';
Expand All @@ -20,18 +17,18 @@ const List<String> _defaultWeekNames = ['日', '一', '二', '三', '四', '五'
/// 2、日历组件支持时间范围展示,仅展示范围内的日历视图,范围外日期置灰不可点击。日期范围边界后不可再翻页。
class BrnCalendarView extends StatefulWidget {
const BrnCalendarView(
{Key key,
{Key? key,
this.selectMode = SelectMode.SINGLE,
this.displayMode = DisplayMode.Month,
this.weekNames = _defaultWeekNames,
this.showControllerBar = true,
this.initStartSelectedDate,
this.initEndSelectedDate,
this.initDisplayDate,
this.startEndDateChange,
required this.startEndDateChange,
this.minDate,
this.maxDate})
: assert(weekNames != null && weekNames.length > 0),
: assert(weekNames.length == 7),
super(key: key);

/// 展示模式, Week, Month
Expand All @@ -41,16 +38,20 @@ class BrnCalendarView extends StatefulWidget {
final SelectMode selectMode;

/// 日历日期选择范围最小值
final DateTime minDate;
///
/// 默认 `DateTime(1970)`
final DateTime? minDate;

/// 日历日期选择范围最大值
final DateTime maxDate;
///
/// 默认 `DateTime(2100)`
final DateTime? maxDate;

/// 日历日期初始选中最小值
final DateTime initStartSelectedDate;
final DateTime? initStartSelectedDate;

/// 日历日期初始选中最大值
final DateTime initEndSelectedDate;
final DateTime? initEndSelectedDate;

/// 是否展示顶部控制按钮
final bool showControllerBar;
Expand All @@ -59,9 +60,11 @@ class BrnCalendarView extends StatefulWidget {
final List<String> weekNames;

/// 初始展示月份
final DateTime initDisplayDate;
///
/// 默认当前时间
final DateTime? initDisplayDate;

final Function(DateTime startSelectedDate, DateTime endSelectedDate)
final Function(DateTime? startSelectedDate, DateTime? endSelectedDate)
startEndDateChange;

@override
Expand All @@ -70,12 +73,10 @@ class BrnCalendarView extends StatefulWidget {

class _CustomCalendarViewState extends State<BrnCalendarView> {
List<DateTime> dateList = <DateTime>[];
DateTime _currentDate;
DisplayMode _displayMode;
DateTime _minDate,
_maxDate,
_currentStartSelectedDate,
_currentEndSelectedDate;
late DateTime _currentDate;
DisplayMode? _displayMode;
Copy link
Collaborator

@zhoujuanjuan zhoujuanjuan Jan 11, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这个不是也可以用late,不为空

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

已修改~

late DateTime _minDate, _maxDate;
DateTime? _currentStartSelectedDate, _currentEndSelectedDate;

@override
void initState() {
Expand All @@ -94,14 +95,9 @@ class _CustomCalendarViewState extends State<BrnCalendarView> {
super.initState();
}

@override
void dispose() {
super.dispose();
}

void _setListOfWeekDate(DateTime weekDate) {
dateList.clear();
List<DateTime> tmpDateList = List();
List<DateTime> tmpDateList = [];
int previousDay = weekDate.weekday % 7;
for (int i = 0; i < weekDate.weekday; i++) {
tmpDateList.add(weekDate.subtract(Duration(days: previousDay - i)));
Expand All @@ -116,7 +112,7 @@ class _CustomCalendarViewState extends State<BrnCalendarView> {

void _setListOfMonthDate(DateTime monthDate) {
dateList.clear();
List<DateTime> tmpDateList = List();
List<DateTime> tmpDateList = [];
final DateTime newDate = DateTime(monthDate.year, monthDate.month, 0);
int previousMonthDay = (newDate.weekday + 1) % 7;
for (int i = 1; i <= previousMonthDay; i++) {
Expand Down Expand Up @@ -217,8 +213,7 @@ class _CustomCalendarViewState extends State<BrnCalendarView> {
color: Colors.transparent,
padding: EdgeInsets.only(right: 15),
child: isNextIconEnable
? BrunoTools.getAssetImage(
BrnAsset.iconCalendarNextMonth)
? BrunoTools.getAssetImage(BrnAsset.iconCalendarNextMonth)
: BrunoTools.getAssetImageWithColor(
BrnAsset.iconCalendarNextMonth, Color(0xFFCCCCCC)),
alignment: Alignment.center,
Expand All @@ -228,10 +223,7 @@ class _CustomCalendarViewState extends State<BrnCalendarView> {
),
);
}
return Container(
height: 0,
width: 0,
);
return SizedBox.shrink();
}

bool _isIconEnable(bool isPre) {
Expand Down Expand Up @@ -341,17 +333,11 @@ class _CustomCalendarViewState extends State<BrnCalendarView> {
.withOpacity(0.14)
: Colors.transparent)
: Colors.transparent,
borderRadius: BorderRadius.only(
bottomLeft: _isStartDateRadius(date)
? const Radius.circular(24.0)
: const Radius.circular(0.0),
topLeft: _isStartDateRadius(date)
? const Radius.circular(24.0)
: const Radius.circular(0.0),
topRight: _isEndDateRadius(date)
borderRadius: BorderRadius.horizontal(
left: _isStartDateRadius(date)
? const Radius.circular(24.0)
: const Radius.circular(0.0),
bottomRight: _isEndDateRadius(date)
right: _isEndDateRadius(date)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这里为什么改成 right 了

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

使用BorderRadius.horizontal感觉更简洁一点

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ooo ,看差了,cool~

? const Radius.circular(24.0)
: const Radius.circular(0.0),
),
Expand Down Expand Up @@ -409,7 +395,7 @@ class _CustomCalendarViewState extends State<BrnCalendarView> {
}
},
child: Padding(
padding: const EdgeInsets.all(0),
padding: EdgeInsets.zero,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

cool~

child: Container(
child: Center(
child: Text(
Expand Down Expand Up @@ -493,8 +479,8 @@ class _CustomCalendarViewState extends State<BrnCalendarView> {

bool _getIsInRange(DateTime date) {
if (_currentStartSelectedDate != null && _currentEndSelectedDate != null) {
if (date.isAfter(_currentStartSelectedDate) &&
date.isBefore(_currentEndSelectedDate)) {
if (date.isAfter(_currentStartSelectedDate!) &&
date.isBefore(_currentEndSelectedDate!)) {
return true;
} else {
return false;
Expand All @@ -506,14 +492,14 @@ class _CustomCalendarViewState extends State<BrnCalendarView> {

bool _getIsItStartAndEndDate(DateTime date) {
if (_currentStartSelectedDate != null &&
_currentStartSelectedDate.day == date.day &&
_currentStartSelectedDate.month == date.month &&
_currentStartSelectedDate.year == date.year) {
_currentStartSelectedDate!.day == date.day &&
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

从语义上来看它貌似是 不可空的 应该是late的
不知道是不是🤷‍♀️

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

emmm 这个_currentStartSelectedDate吗,当前选中的日期传参数进去是有可能为空的 L87

_currentStartSelectedDate!.month == date.month &&
_currentStartSelectedDate!.year == date.year) {
return true;
} else if (_currentEndSelectedDate != null &&
_currentEndSelectedDate.day == date.day &&
_currentEndSelectedDate.month == date.month &&
_currentEndSelectedDate.year == date.year) {
_currentEndSelectedDate!.day == date.day &&
_currentEndSelectedDate!.month == date.month &&
_currentEndSelectedDate!.year == date.year) {
return true;
} else {
return false;
Expand All @@ -522,8 +508,8 @@ class _CustomCalendarViewState extends State<BrnCalendarView> {

bool _isStartDateRadius(DateTime date) {
if (_currentStartSelectedDate != null &&
_currentStartSelectedDate.day == date.day &&
_currentStartSelectedDate.month == date.month) {
_currentStartSelectedDate!.day == date.day &&
_currentStartSelectedDate!.month == date.month) {
return true;
} else if (date.weekday == 7) {
return true;
Expand All @@ -534,8 +520,8 @@ class _CustomCalendarViewState extends State<BrnCalendarView> {

bool _isEndDateRadius(DateTime date) {
if (_currentEndSelectedDate != null &&
_currentEndSelectedDate.day == date.day &&
_currentEndSelectedDate.month == date.month) {
_currentEndSelectedDate!.day == date.day &&
_currentEndSelectedDate!.month == date.month) {
return true;
} else if (date.weekday == 6) {
return true;
Expand Down Expand Up @@ -571,15 +557,15 @@ class _CustomCalendarViewState extends State<BrnCalendarView> {
}

if (_currentStartSelectedDate != null && _currentEndSelectedDate != null) {
if (!_currentEndSelectedDate.isAfter(_currentStartSelectedDate)) {
final DateTime d = _currentStartSelectedDate;
if (!_currentEndSelectedDate!.isAfter(_currentStartSelectedDate!)) {
final DateTime d = _currentStartSelectedDate!;
_currentStartSelectedDate = _currentEndSelectedDate;
_currentEndSelectedDate = d;
}
if (date.isBefore(_currentStartSelectedDate)) {
if (date.isBefore(_currentStartSelectedDate!)) {
_currentStartSelectedDate = date;
}
if (date.isAfter(_currentEndSelectedDate)) {
if (date.isAfter(_currentEndSelectedDate!)) {
_currentEndSelectedDate = date;
}
}
Expand Down