在我的CupertinoDatePickerMode.time
中,我需要滑动选择器以设置onDateTimeChanged
当前在选择器上的时间。我点击按钮,拾取器就会弹出。我可以像我说的那样在滑动选择器之后设置时间。
所以我的问题是: 如何设置只弹出拾取器的时间?
这是我的变量:DateTime? selectedDayStart;
这是我的选择器:
return CupertinoDatePicker(
mode: CupertinoDatePickerMode.time,
minuteInterval: 15,
initialDateTime: _initialTime,
use24hFormat: true,
onDateTimeChanged: (time) {
setState(() {
selectedDayStart = DateTime(
selectedDayStart!.year,
selectedDayStart!.month,
selectedDayStart!.day,
time.hour,
time.minute);
});
},
);
显示时间:
Text(
"Başlangıç Saati ${DateFormat("HH:mm").format(selectedDayStart!)}",
style: const TextStyle(fontSize: 20),
)
可以在initialDateTime
上设置时间
DateTime _initialTime = DateTime.now().copyWith(
hour: 11,
minute: 40,
);
CupertinoDatePicker(
mode: CupertinoDatePickerMode.time,
minuteInterval: 10,
initialDateTime: _initialTime,
minuteInterval应为
_initialTime.minute%minuteInterval==0
。请确保满足这个(%==0)条件。
我使用CupertinoDatePicker在有状态的小部件和调用initState解决了这个问题
class MyCupertinoDatePicker extends StatefulWidget {
const MyCupertinoDatePicker({Key? key}) : super(key: key);
@override
_MyCupertinoDatePickerState createState() => _MyCupertinoDatePickerState();
}
class _MyCupertinoDatePickerState extends State<MyCupertinoDatePicker> {
@override
void initState() {
super.initState();
// set the initial date to the current date and time
selectedDate = DateTime.now();
print(selectedDate);
}
@override
Widget build(BuildContext context) {
return CupertinoDatePicker(
mode: CupertinoDatePickerMode.time,
initialDateTime: selectedDate,
onDateTimeChanged: (DateTime newDateTime) {
setState(() {
selectedDate = newDateTime;
print(selectedDate);
});
},
);
}
}