diff --git a/example/lib/main.dart b/example/lib/main.dart index b35e193..2af3f09 100644 --- a/example/lib/main.dart +++ b/example/lib/main.dart @@ -78,6 +78,36 @@ class _MyHomePageState extends State { }, ), ), + 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, + ), + ), ], ), )); diff --git a/lib/date_picker_widget.dart b/lib/date_picker_widget.dart index b6de39a..144bc1a 100644 --- a/lib/date_picker_widget.dart +++ b/lib/date_picker_widget.dart @@ -47,6 +47,9 @@ class DatePicker extends StatefulWidget { /// Locale for the calendar default: en_us final String locale; + /// Predefined date list + List dateList; + DatePicker( this.startDate, { Key key, @@ -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 createState() => new _DatePickerState(); } @@ -123,8 +146,13 @@ class _DatePickerState extends State { // 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;