Issue 1:
It is possible to select the date later than the lastDate.

Steps to reproduce:

create Date picker with firstDate set to today's date e.g. 22/09/2018, lastDate set to 1 year later -> 22/09/2019

showDatePicker, select a later date e.g. 30/09/2018

Current behavior: one can select a date that is later than the lastDate.

Expected behavior in such case the date should be set to lastDate.

It crashes if done the other way round:

Steps to reproduce:

create Date picker with firstDate set to today's date e.g. 22/09/2018, lastDate set to 1 year later -> 22/09/2019

showDatePicker, select year 2019 select an earlier date e.g. 15/09/2019

select year 2018 -> crash

That's because it tries to select 15/09/2018 which is earlier than the firstDate (22/09/2018).

Current behavior: crash.

Expected behavior in such case the date should be set to firstDate.

I/flutter ( 7018): The following assertion was thrown building _DatePickerDialog(dirty, state:
I/flutter ( 7018): _DatePickerDialogState#95992):
I/flutter ( 7018): 'package:flutter/src/material/date_picker.dart': Failed assertion: line 503 pos 15:
I/flutter ( 7018): 'selectedDate.isAfter(firstDate) || selectedDate.isAtSameMomentAs(firstDate)': is not true.
I/flutter ( 7018):
I/flutter ( 7018): Either the assertion indicates an error in the framework itself, or we should provide substantially
I/flutter ( 7018): more information in this error message to help you determine and fix the underlying cause.
I/flutter ( 7018): In either case, please report this assertion by filing a bug on GitHub:
I/flutter ( 7018): https://github.com/flutter/flutter/issues/new
I/flutter ( 7018):
I/flutter ( 7018): When the exception was thrown, this was the stack:
I/flutter ( 7018): #2 new MonthPicker (package:flutter/src/material/date_picker.dart:503:15)
I/flutter ( 7018): #3 _DatePickerDialogState._buildPicker (package:flutter/src/material/date_picker.dart:926:20)
I/flutter ( 7018): #4 _DatePickerDialogState.build (package:flutter/src/material/date_picker.dart:952:16)
I/flutter ( 7018): #5 StatefulElement.build (package:flutter/src/widgets/framework.dart:3742:27)
I/flutter ( 7018): #6 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:3654:15)
I/flutter ( 7018): #7 Element.rebuild (package:flutter/src/widgets/framework.dart:3507:5)
I/flutter ( 7018): #8 BuildOwner.buildScope (package:flutter/src/widgets/framework.dart:2255:33)
I/flutter ( 7018): #9 _WidgetsFlutterBinding&BindingBase&GestureBinding&ServicesBinding&SchedulerBinding&PaintingBinding&SemanticsBinding&RendererBinding&WidgetsBinding.drawFrame (package:flutter/src/widgets/binding.dart:653:20)
I/flutter ( 7018): #10 _WidgetsFlutterBinding&BindingBase&GestureBinding&ServicesBinding&SchedulerBinding&PaintingBinding&SemanticsBinding&RendererBinding._handlePersistentFrameCallback (package:flutter/src/rendering/binding.dart:208:5)
I/flutter ( 7018): #11 _WidgetsFlutterBinding&BindingBase&GestureBinding&ServicesBinding&SchedulerBinding._invokeFrameCallback (package:flutter/src/scheduler/binding.dart:990:15)
I/flutter ( 7018): #12 _WidgetsFlutterBinding&BindingBase&GestureBinding&ServicesBinding&SchedulerBinding.handleDrawFrame (package:flutter/src/scheduler/binding.dart:930:9)
I/flutter ( 7018): #13 _WidgetsFlutterBinding&BindingBase&GestureBinding&ServicesBinding&SchedulerBinding._handleDrawFrame (package:flutter/src/scheduler/binding.dart:842:5)
I/flutter ( 7018): #14 _invoke (dart:ui/hooks.dart:128:13)
I/flutter ( 7018): #15 _drawFrame (dart:ui/hooks.dart:117:3)
I/flutter ( 7018): (elided 2 frames from class _AssertionError)

> Doctor summary (to see all details, run flutter doctor -v):
[√] Flutter (Channel beta, v0.8.2, on Microsoft Windows [Version 10.0.17134.285], locale en-GB)
[√] Android toolchain - develop for Android devices (Android SDK 27.0.3)
[√] Android Studio (version 3.1)
[√] Connected devices (1 available)
> • No issues found!

Issue 2:
DatePicker failed on assertion when I choose past year and selected day-month is before firstDate

I have a DatePicker with first and last dates. Lets say from 6-09-2016 to 15-01-2019. Today is 4th May 2018. I choose year 2016. DatePicker changes only year in selected date. So it wants to show 4-05-2016. But it's out of range. It failed on assertions.

Another exception was thrown: 'package:flutter/src/material/date_picker.dart': Failed assertion: line 504 pos 15: 'selectedDate.isAfter(firstDate) || selectedDate.isAtSameMomentAs(firstDate)': is not true.

It can be fixed as to choose first possible date:

in _YearPickerState build()

onTap: () { DateTime dateTimeYearChanged = new DateTime(year, widget.selectedDate.month, widget.selectedDate.day); if (dateTimeYearChanged.isBefore(widget.firstDate)) { dateTimeYearChanged = widget.firstDate; } widget.onChanged(dateTimeYearChanged); }

Also, if I choose 2019 year, DatePicker scrolls from month May to month January. So user doesn't see selected date. But he can press OK and selected date is 4-05-2019, which is out of range. So this is a bug