From e9314cf399bec83152e9bcb93b5ea999437facb0 Mon Sep 17 00:00:00 2001 From: zhangmeng <494089941@qq.com> Date: Tue, 3 Aug 2021 16:29:54 +0800 Subject: [PATCH] =?UTF-8?q?=E9=87=8D=E6=9E=84datetime=20picker?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/pickers/as_date_range_picker_part.dart | 14 ++++++------- lib/pickers/as_two_date_picker.dart | 23 +++++++++++----------- 2 files changed, 19 insertions(+), 18 deletions(-) diff --git a/lib/pickers/as_date_range_picker_part.dart b/lib/pickers/as_date_range_picker_part.dart index 3f4ebe7..0e92840 100644 --- a/lib/pickers/as_date_range_picker_part.dart +++ b/lib/pickers/as_date_range_picker_part.dart @@ -3,22 +3,22 @@ import 'package:ansu_ui/pickers/as_two_date_picker.dart'; class RangeDate { DateTime? start; - DateTime? end; + DateTime end; RangeDate({ this.start, - this.end, + required this.end, }); } -///返回值为`bool`且为`true`时,为一小时内 +///一个小时内 则 start 为null /// -///否则返回`RangeDate` -Future show2DatePicker(BuildContext context, - {bool? isAnHour, RangeDate? date}) async { +/// +Future show2DatePicker(BuildContext context, + { RangeDate? date}) async { return await showModalBottomSheet( context: context, builder: (context) { - return AS2DatePicker(isAnHour: isAnHour, date: date); + return AS2DatePicker( date: date); }, ); } diff --git a/lib/pickers/as_two_date_picker.dart b/lib/pickers/as_two_date_picker.dart index 401de80..a54a200 100644 --- a/lib/pickers/as_two_date_picker.dart +++ b/lib/pickers/as_two_date_picker.dart @@ -8,9 +8,8 @@ import 'package:ansu_ui/buttons/as_long_button.dart'; import 'package:velocity_x/velocity_x.dart'; class AS2DatePicker extends StatefulWidget { - final bool? isAnHour; final RangeDate? date; - AS2DatePicker({Key? key, this.isAnHour, this.date}) : super(key: key); + AS2DatePicker({Key? key, this.date}) : super(key: key); @override _AS2DatePickerState createState() => _AS2DatePickerState(); @@ -28,7 +27,6 @@ class _AS2DatePickerState extends State { end: now.add(Duration(hours: 1)), ); - bool _inAnHour = false; Widget _buildDayButton(int index, String title) { bool sameDay = _selectedDay == index; return MaterialButton( @@ -105,17 +103,14 @@ class _AS2DatePickerState extends State { return _renderButton( '一小时内', () { - _inAnHour = true; setState(() => {_selectedDate = now}); }, sameItem, ); } - _inAnHour = false; return _renderButton( '${startHour + index}:00-${startHour + index + 1}:00', () { - _inAnHour = false; setState(() => _selectedDate = DateTime( now.year, now.month, @@ -139,7 +134,6 @@ class _AS2DatePickerState extends State { return _renderButton( '$index\:00-${index + 1}:00', () { - _inAnHour = false; setState(() => _selectedDate = DateTime( now.year, now.month, @@ -158,8 +152,6 @@ class _AS2DatePickerState extends State { void initState() { super.initState(); _pageController = PageController(); - _selectedDate = now; - _inAnHour = widget.isAnHour ?? true; _selectedDate = widget.date?.start ?? now; } @@ -213,8 +205,17 @@ class _AS2DatePickerState extends State { child: ASLongButton.solid( title: '确认', onPressed: () { - if (_inAnHour) - Navigator.pop(context, true); + if (_selectedDate!.hour==now.hour) + Navigator.pop( + context, + RangeDate( + start: null, + end: DateTime( + _selectedDate!.year, + _selectedDate!.month, + _selectedDate!.day, + _selectedDate!.hour + 1), + )); else Navigator.pop( context,