-
Notifications
You must be signed in to change notification settings - Fork 485
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
Changes from 3 commits
b30e050
549cae8
29ea403
a61bb26
68f1370
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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'; | ||
|
@@ -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 | ||
|
@@ -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; | ||
|
@@ -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 | ||
|
@@ -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; | ||
late DateTime _minDate, _maxDate; | ||
DateTime? _currentStartSelectedDate, _currentEndSelectedDate; | ||
|
||
@override | ||
void initState() { | ||
|
@@ -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))); | ||
|
@@ -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++) { | ||
|
@@ -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, | ||
|
@@ -228,10 +223,7 @@ class _CustomCalendarViewState extends State<BrnCalendarView> { | |
), | ||
); | ||
} | ||
return Container( | ||
height: 0, | ||
width: 0, | ||
); | ||
return SizedBox.shrink(); | ||
} | ||
|
||
bool _isIconEnable(bool isPre) { | ||
|
@@ -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) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 这里为什么改成 right 了 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 使用 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ooo ,看差了,cool~ |
||
? const Radius.circular(24.0) | ||
: const Radius.circular(0.0), | ||
), | ||
|
@@ -409,7 +395,7 @@ class _CustomCalendarViewState extends State<BrnCalendarView> { | |
} | ||
}, | ||
child: Padding( | ||
padding: const EdgeInsets.all(0), | ||
padding: EdgeInsets.zero, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. cool~ |
||
child: Container( | ||
child: Center( | ||
child: Text( | ||
|
@@ -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; | ||
|
@@ -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 && | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 从语义上来看它貌似是 不可空的 应该是late的 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. emmm 这个 |
||
_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; | ||
|
@@ -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; | ||
|
@@ -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; | ||
|
@@ -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; | ||
} | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
这个不是也可以用late,不为空
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
已修改~