重构datetime picker

master
张萌 3 years ago
parent 4837412652
commit e9314cf399

@ -3,22 +3,22 @@ import 'package:ansu_ui/pickers/as_two_date_picker.dart';
class RangeDate { class RangeDate {
DateTime? start; DateTime? start;
DateTime? end; DateTime end;
RangeDate({ RangeDate({
this.start, this.start,
this.end, required this.end,
}); });
} }
///`bool``true`, /// start null
/// ///
///`RangeDate` ///
Future<dynamic> show2DatePicker(BuildContext context, Future<RangeDate> show2DatePicker(BuildContext context,
{bool? isAnHour, RangeDate? date}) async { { RangeDate? date}) async {
return await showModalBottomSheet( return await showModalBottomSheet(
context: context, context: context,
builder: (context) { builder: (context) {
return AS2DatePicker(isAnHour: isAnHour, date: date); return AS2DatePicker( date: date);
}, },
); );
} }

@ -8,9 +8,8 @@ import 'package:ansu_ui/buttons/as_long_button.dart';
import 'package:velocity_x/velocity_x.dart'; import 'package:velocity_x/velocity_x.dart';
class AS2DatePicker extends StatefulWidget { class AS2DatePicker extends StatefulWidget {
final bool? isAnHour;
final RangeDate? date; final RangeDate? date;
AS2DatePicker({Key? key, this.isAnHour, this.date}) : super(key: key); AS2DatePicker({Key? key, this.date}) : super(key: key);
@override @override
_AS2DatePickerState createState() => _AS2DatePickerState(); _AS2DatePickerState createState() => _AS2DatePickerState();
@ -28,7 +27,6 @@ class _AS2DatePickerState extends State<AS2DatePicker> {
end: now.add(Duration(hours: 1)), end: now.add(Duration(hours: 1)),
); );
bool _inAnHour = false;
Widget _buildDayButton(int index, String title) { Widget _buildDayButton(int index, String title) {
bool sameDay = _selectedDay == index; bool sameDay = _selectedDay == index;
return MaterialButton( return MaterialButton(
@ -105,17 +103,14 @@ class _AS2DatePickerState extends State<AS2DatePicker> {
return _renderButton( return _renderButton(
'一小时内', '一小时内',
() { () {
_inAnHour = true;
setState(() => {_selectedDate = now}); setState(() => {_selectedDate = now});
}, },
sameItem, sameItem,
); );
} }
_inAnHour = false;
return _renderButton( return _renderButton(
'${startHour + index}:00-${startHour + index + 1}:00', '${startHour + index}:00-${startHour + index + 1}:00',
() { () {
_inAnHour = false;
setState(() => _selectedDate = DateTime( setState(() => _selectedDate = DateTime(
now.year, now.year,
now.month, now.month,
@ -139,7 +134,6 @@ class _AS2DatePickerState extends State<AS2DatePicker> {
return _renderButton( return _renderButton(
'$index\:00-${index + 1}:00', '$index\:00-${index + 1}:00',
() { () {
_inAnHour = false;
setState(() => _selectedDate = DateTime( setState(() => _selectedDate = DateTime(
now.year, now.year,
now.month, now.month,
@ -158,8 +152,6 @@ class _AS2DatePickerState extends State<AS2DatePicker> {
void initState() { void initState() {
super.initState(); super.initState();
_pageController = PageController(); _pageController = PageController();
_selectedDate = now;
_inAnHour = widget.isAnHour ?? true;
_selectedDate = widget.date?.start ?? now; _selectedDate = widget.date?.start ?? now;
} }
@ -213,8 +205,17 @@ class _AS2DatePickerState extends State<AS2DatePicker> {
child: ASLongButton.solid( child: ASLongButton.solid(
title: '确认', title: '确认',
onPressed: () { onPressed: () {
if (_inAnHour) if (_selectedDate!.hour==now.hour)
Navigator.pop(context, true); Navigator.pop(
context,
RangeDate(
start: null,
end: DateTime(
_selectedDate!.year,
_selectedDate!.month,
_selectedDate!.day,
_selectedDate!.hour + 1),
));
else else
Navigator.pop( Navigator.pop(
context, context,

Loading…
Cancel
Save