Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
30 changes: 30 additions & 0 deletions example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,36 @@ class _MyHomePageState extends State<MyHomePage> {
},
),
),
Padding(
padding: EdgeInsets.all(30),
),
Text("DatePicker from predefined list:"),
Padding(
padding: EdgeInsets.all(20),
),
Container(
child: DatePicker.fromList(
[
DateTime(2020, 1, 1),
DateTime(2020, 2, 1),
DateTime(2020, 3, 1),
DateTime(2020, 4, 1),
DateTime(2020, 5, 1),
DateTime(2020, 6, 1),
DateTime(2020, 7, 1),
DateTime(2020, 8, 1),
DateTime(2020, 9, 1),
DateTime(2020, 10, 1),
DateTime(2020, 11, 1),
DateTime(2020, 12, 1),
],
width: 60,
height: 80,
initialSelectedDate: DateTime(2020, 4, 1),
selectionColor: Colors.black,
selectedTextColor: Colors.white,
),
),
],
),
));
Expand Down
32 changes: 30 additions & 2 deletions lib/date_picker_widget.dart
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,9 @@ class DatePicker extends StatefulWidget {
/// Locale for the calendar default: en_us
final String locale;

/// Predefined date list
List<DateTime> dateList;

DatePicker(
this.startDate, {
Key key,
Expand All @@ -64,6 +67,26 @@ class DatePicker extends StatefulWidget {
this.locale = "en_US",
}) : super(key: key);


// Create date picker from predefined list
DatePicker.fromList(this.dateList, {
Key key,
this.width = 60,
this.height = 80,
this.controller,
this.monthTextStyle = defaultMonthTextStyle,
this.dayTextStyle = defaultDayTextStyle,
this.dateTextStyle = defaultDateTextStyle,
this.selectedTextColor = Colors.white,
this.selectionColor = AppColors.defaultSelectionColor,
this.initialSelectedDate,
this.onDateChange,
this.locale = "en_US",
}) : assert(dateList != null && dateList.length > 0),
this.startDate = dateList[0],
this.daysCount = dateList.length,
super(key: key);

@override
State<StatefulWidget> createState() => new _DatePickerState();
}
Expand Down Expand Up @@ -123,8 +146,13 @@ class _DatePickerState extends State<DatePicker> {
// get the date object based on the index position
// if widget.startDate is null then use the initialDateValue
DateTime date;
DateTime _date = widget.startDate.add(Duration(days: index));
date = new DateTime(_date.year, _date.month, _date.day);

if(widget.dateList != null && widget.dateList.length > 0) {
date = widget.dateList[index];
} else {
DateTime _date = widget.startDate.add(Duration(days: index));
date = new DateTime(_date.year, _date.month, _date.day);
}

// Check if this date is the one that is currently selected
bool isSelected = _currentDate != null? _compareDate(date, _currentDate) : false;
Expand Down