diff --git a/lib/pickers/as_date_range_picker_part.dart b/lib/pickers/as_date_range_picker_part.dart index 68ed9db..c36bbc7 100644 --- a/lib/pickers/as_date_range_picker_part.dart +++ b/lib/pickers/as_date_range_picker_part.dart @@ -9,7 +9,10 @@ class RangeDate { }); } -Future show2DatePicker(BuildContext context) async { +///返回值为`bool`且为`true`时,为一小时内 +/// +///否则返回`RangeDate` +Future show2DatePicker(BuildContext context) async { return await showModalBottomSheet( context: context, builder: (context) { diff --git a/lib/pickers/as_two_date_picker.dart b/lib/pickers/as_two_date_picker.dart index 961606a..6b728d9 100644 --- a/lib/pickers/as_two_date_picker.dart +++ b/lib/pickers/as_two_date_picker.dart @@ -18,6 +18,8 @@ class _AS2DatePickerState extends State { start: now, end: now.add(Duration(hours: 1)), ); + + bool _inAnHour = false; Widget _buildDayButton(int index, String title) { bool sameDay = _selectedDay == index; return MaterialButton( @@ -92,20 +94,28 @@ class _AS2DatePickerState extends State { itemBuilder: (context, index) { bool sameItem = now.day == _selectedDate.day && _selectedDate.hour == startHour + index; - if (index == 0) + if (index == 0) { return _renderButton( '一小时内', - () => setState(() => _selectedDate = now), + () { + _inAnHour = true; + setState(() => {_selectedDate = now}); + }, sameItem, ); + } + _inAnHour = false; return _renderButton( '${startHour + index}:00-${startHour + index + 1}:00', - () => setState(() => _selectedDate = DateTime( - now.year, - now.month, - now.day, - startHour + index, - )), + () { + _inAnHour = false; + setState(() => _selectedDate = DateTime( + now.year, + now.month, + now.day, + startHour + index, + )); + }, sameItem, ); }, @@ -121,12 +131,15 @@ class _AS2DatePickerState extends State { _selectedDate.hour == index; return _renderButton( '$index\:00-${index + 1}:00', - () => setState(() => _selectedDate = DateTime( - now.year, - now.month, - now.day + offsetDay, - index, - )), + () { + _inAnHour = false; + setState(() => _selectedDate = DateTime( + now.year, + now.month, + now.day + offsetDay, + index, + )); + }, sameItem, ); }, @@ -191,12 +204,15 @@ class _AS2DatePickerState extends State { child: ASLongButton.solid( title: '确认', onPressed: () { - Navigator.pop( - context, - RangeDate( - start: _selectedDate, - end: _selectedDate.add(Duration(hours: 1)), - )); + if (_inAnHour) + Navigator.pop(context, true); + else + Navigator.pop( + context, + RangeDate( + start: _selectedDate, + end: _selectedDate.add(Duration(hours: 1)), + )); }, ), ),