Skip to content

Commit 34a4f21

Browse files
authored
Migration to null safety + little code shorten (#51)
* updated intl and dart sdk versions * intl version fix * migrated date widget to null safety * migrated date picker widget * update pubspec yaml autogenerated by migration tool
1 parent b71392e commit 34a4f21

File tree

3 files changed

+58
-66
lines changed

3 files changed

+58
-66
lines changed

lib/date_picker_widget.dart

Lines changed: 46 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
2+
13
import 'package:date_picker_timeline/date_widget.dart';
24
import 'package:date_picker_timeline/extra/color.dart';
35
import 'package:date_picker_timeline/extra/style.dart';
@@ -17,7 +19,7 @@ class DatePicker extends StatefulWidget {
1719
final double height;
1820

1921
/// DatePicker Controller
20-
final DatePickerController controller;
22+
final DatePickerController? controller;
2123

2224
/// Text color for the selected Date
2325
final Color selectedTextColor;
@@ -38,18 +40,18 @@ class DatePicker extends StatefulWidget {
3840
final TextStyle dateTextStyle;
3941

4042
/// Current Selected Date
41-
final DateTime initialSelectedDate;
43+
final DateTime?/*?*/ initialSelectedDate;
4244

4345
/// Contains the list of inactive dates.
4446
/// All the dates defined in this List will be deactivated
45-
final List<DateTime> inactiveDates;
47+
final List<DateTime>? inactiveDates;
4648

4749
/// Contains the list of active dates.
4850
/// Only the dates in this list will be activated.
49-
final List<DateTime> activeDates;
51+
final List<DateTime>? activeDates;
5052

5153
/// Callback function for when a different date is selected
52-
final DateChangeListener onDateChange;
54+
final DateChangeListener? onDateChange;
5355

5456
/// Max limit up to which the dates are shown.
5557
/// Days are counted from the startDate
@@ -60,7 +62,7 @@ class DatePicker extends StatefulWidget {
6062

6163
DatePicker(
6264
this.startDate, {
63-
Key key,
65+
Key? key,
6466
this.width = 60,
6567
this.height = 80,
6668
this.controller,
@@ -86,17 +88,17 @@ class DatePicker extends StatefulWidget {
8688
}
8789

8890
class _DatePickerState extends State<DatePicker> {
89-
DateTime _currentDate;
91+
DateTime? _currentDate;
9092

9193
ScrollController _controller = ScrollController();
9294

93-
TextStyle selectedDateStyle;
94-
TextStyle selectedMonthStyle;
95-
TextStyle selectedDayStyle;
95+
late final TextStyle selectedDateStyle;
96+
late final TextStyle selectedMonthStyle;
97+
late final TextStyle selectedDayStyle;
9698

97-
TextStyle deactivatedDateStyle;
98-
TextStyle deactivatedMonthStyle;
99-
TextStyle deactivatedDayStyle;
99+
late final TextStyle deactivatedDateStyle;
100+
late final TextStyle deactivatedMonthStyle;
101+
late final TextStyle deactivatedDayStyle;
100102

101103
@override
102104
void initState() {
@@ -106,42 +108,26 @@ class _DatePickerState extends State<DatePicker> {
106108
_currentDate = widget.initialSelectedDate;
107109

108110
if (widget.controller != null) {
109-
widget.controller.setDatePickerState(this);
111+
widget.controller!.setDatePickerState(this);
110112
}
111113

112114
this.selectedDateStyle =
113-
createTextStyle(widget.dateTextStyle, widget.selectedTextColor);
115+
widget.dateTextStyle.copyWith(color: widget.selectedTextColor);
114116
this.selectedMonthStyle =
115-
createTextStyle(widget.monthTextStyle, widget.selectedTextColor);
117+
widget.monthTextStyle.copyWith(color: widget.selectedTextColor);
116118
this.selectedDayStyle =
117-
createTextStyle(widget.dayTextStyle, widget.selectedTextColor);
119+
widget.dayTextStyle.copyWith(color: widget.selectedTextColor);
118120

119121
this.deactivatedDateStyle =
120-
createTextStyle(widget.dateTextStyle, widget.deactivatedColor);
122+
widget.dateTextStyle.copyWith(color: widget.deactivatedColor);
121123
this.deactivatedMonthStyle =
122-
createTextStyle(widget.monthTextStyle, widget.deactivatedColor);
124+
widget.monthTextStyle.copyWith(color: widget.deactivatedColor);
123125
this.deactivatedDayStyle =
124-
createTextStyle(widget.dayTextStyle, widget.deactivatedColor);
126+
widget.dayTextStyle.copyWith(color: widget.deactivatedColor);
125127

126128
super.initState();
127129
}
128130

129-
/// This will return a text style for the Selected date Text Values
130-
/// the only change will be the color provided
131-
TextStyle createTextStyle(TextStyle style, Color color) {
132-
if (color != null) {
133-
return TextStyle(
134-
color: color,
135-
fontSize: style.fontSize,
136-
fontWeight: style.fontWeight,
137-
fontFamily: style.fontFamily,
138-
letterSpacing: style.letterSpacing,
139-
);
140-
} else {
141-
return style;
142-
}
143-
}
144-
145131
@override
146132
Widget build(BuildContext context) {
147133
return Container(
@@ -162,7 +148,7 @@ class _DatePickerState extends State<DatePicker> {
162148
// check if this date needs to be deactivated for only DeactivatedDates
163149
if (widget.inactiveDates != null) {
164150
// print("Inside Inactive dates.");
165-
for (DateTime inactiveDate in widget.inactiveDates) {
151+
for (DateTime inactiveDate in widget.inactiveDates!) {
166152
if (_compareDate(date, inactiveDate)) {
167153
isDeactivated = true;
168154
break;
@@ -173,7 +159,7 @@ class _DatePickerState extends State<DatePicker> {
173159
// check if this date needs to be deactivated for only ActivatedDates
174160
if (widget.activeDates != null) {
175161
isDeactivated = true;
176-
for (DateTime activateDate in widget.activeDates) {
162+
for (DateTime activateDate in widget.activeDates!) {
177163
// Compare the date if it is in the
178164
if (_compareDate(date, activateDate)) {
179165
isDeactivated = false;
@@ -184,20 +170,26 @@ class _DatePickerState extends State<DatePicker> {
184170

185171
// Check if this date is the one that is currently selected
186172
bool isSelected =
187-
_currentDate != null ? _compareDate(date, _currentDate) : false;
173+
_currentDate != null ? _compareDate(date, _currentDate!) : false;
188174

189175
// Return the Date Widget
190176
return DateWidget(
191177
date: date,
192178
monthTextStyle: isDeactivated
193179
? deactivatedMonthStyle
194-
: isSelected ? selectedMonthStyle : widget.monthTextStyle,
180+
: isSelected
181+
? selectedMonthStyle
182+
: widget.monthTextStyle,
195183
dateTextStyle: isDeactivated
196184
? deactivatedDateStyle
197-
: isSelected ? selectedDateStyle : widget.dateTextStyle,
185+
: isSelected
186+
? selectedDateStyle
187+
: widget.dateTextStyle,
198188
dayTextStyle: isDeactivated
199189
? deactivatedDayStyle
200-
: isSelected ? selectedDayStyle : widget.dayTextStyle,
190+
: isSelected
191+
? selectedDayStyle
192+
: widget.dayTextStyle,
201193
width: widget.width,
202194
locale: widget.locale,
203195
selectionColor:
@@ -207,8 +199,8 @@ class _DatePickerState extends State<DatePicker> {
207199
if (isDeactivated) return;
208200

209201
// A date is selected
210-
if (widget.onDateChange != null ) {
211-
widget.onDateChange(selectedDate);
202+
if (widget.onDateChange != null) {
203+
widget.onDateChange!(selectedDate);
212204
}
213205
setState(() {
214206
_currentDate = selectedDate;
@@ -230,7 +222,7 @@ class _DatePickerState extends State<DatePicker> {
230222
}
231223

232224
class DatePickerController {
233-
_DatePickerState _datePickerState;
225+
_DatePickerState? _datePickerState;
234226

235227
void setDatePickerState(_DatePickerState state) {
236228
_datePickerState = state;
@@ -241,8 +233,8 @@ class DatePickerController {
241233
'DatePickerController is not attached to any DatePicker View.');
242234

243235
// jump to the current Date
244-
_datePickerState._controller
245-
.jumpTo(_calculateDateOffset(_datePickerState._currentDate));
236+
_datePickerState!._controller
237+
.jumpTo(_calculateDateOffset(_datePickerState!._currentDate!));
246238
}
247239

248240
/// This function will animate the Timeline to the currently selected Date
@@ -252,8 +244,8 @@ class DatePickerController {
252244
'DatePickerController is not attached to any DatePicker View.');
253245

254246
// animate to the current date
255-
_datePickerState._controller.animateTo(
256-
_calculateDateOffset(_datePickerState._currentDate),
247+
_datePickerState!._controller.animateTo(
248+
_calculateDateOffset(_datePickerState!._currentDate!),
257249
duration: duration,
258250
curve: curve);
259251
}
@@ -265,19 +257,19 @@ class DatePickerController {
265257
assert(_datePickerState != null,
266258
'DatePickerController is not attached to any DatePicker View.');
267259

268-
_datePickerState._controller.animateTo(_calculateDateOffset(date),
260+
_datePickerState!._controller.animateTo(_calculateDateOffset(date),
269261
duration: duration, curve: curve);
270262
}
271263

272264
/// Calculate the number of pixels that needs to be scrolled to go to the
273265
/// date provided in the argument
274266
double _calculateDateOffset(DateTime date) {
275267
final startDate = new DateTime(
276-
_datePickerState.widget.startDate.year,
277-
_datePickerState.widget.startDate.month,
278-
_datePickerState.widget.startDate.day);
268+
_datePickerState!.widget.startDate.year,
269+
_datePickerState!.widget.startDate.month,
270+
_datePickerState!.widget.startDate.day);
279271

280272
int offset = date.difference(startDate).inDays;
281-
return (offset * _datePickerState.widget.width) + (offset * 6);
273+
return (offset * _datePickerState!.widget.width) + (offset * 6);
282274
}
283275
}

lib/date_widget.dart

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,19 +10,19 @@ import 'package:flutter/material.dart';
1010
import 'package:intl/intl.dart';
1111

1212
class DateWidget extends StatelessWidget {
13-
final double width;
13+
final double? width;
1414
final DateTime date;
15-
final TextStyle monthTextStyle, dayTextStyle, dateTextStyle;
15+
final TextStyle? monthTextStyle, dayTextStyle, dateTextStyle;
1616
final Color selectionColor;
17-
final DateSelectionCallback onDateSelected;
18-
final String locale;
17+
final DateSelectionCallback? onDateSelected;
18+
final String? locale;
1919

2020
DateWidget({
21-
@required this.date,
22-
@required this.monthTextStyle,
23-
@required this.dayTextStyle,
24-
@required this.dateTextStyle,
25-
@required this.selectionColor,
21+
required this.date,
22+
required this.monthTextStyle,
23+
required this.dayTextStyle,
24+
required this.dateTextStyle,
25+
required this.selectionColor,
2626
this.width,
2727
this.onDateSelected,
2828
this.locale,
@@ -58,7 +58,7 @@ class DateWidget extends StatelessWidget {
5858
// Check if onDateSelected is not null
5959
if (onDateSelected != null) {
6060
// Call the onDateSelected Function
61-
onDateSelected(this.date);
61+
onDateSelected!(this.date);
6262
}
6363
},
6464
);

pubspec.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,12 @@ version: 1.2.1
44
homepage: https://github.com/iamvivekkaushik/DatePickerTimelineFlutter
55

66
environment:
7-
sdk: ">=2.1.0 <3.0.0"
7+
sdk: '>=2.12.0 <3.0.0'
88

99
dependencies:
1010
flutter:
1111
sdk: flutter
12-
intl: ^0.16.1
12+
intl: ^0.17.0
1313

1414
dev_dependencies:
1515
flutter_test:

0 commit comments

Comments
 (0)