我正在尝试将DateTimePicker
添加到dropdown menu
,以便用户可以通过它选择特定的date
。 DateTimePicker
为dropDropdownMenuItem
后如何显示clicked
?我已经在下面添加了代码。
import 'package:flutter/material.dart';
import 'dart:async';
class CustomDropDown extends StatefulWidget {
DateTime selectedEndDate = new DateTime.now();
@override
_CustomDropDownState createState() => _CustomDropDownState();
}
class _CustomDropDownState extends State<CustomDropDown> {
var _value = '1';
DateTime selectedDate = DateTime.now();
Future<Null> _selectDate(BuildContext context) async {
final DateTime picked = await showDatePicker(
context: context,
initialDate: selectedDate,
firstDate: DateTime(2015, 8),
lastDate: DateTime(2101));
if (picked != null && picked != selectedDate)
setState(() {
selectedDate = picked;
widget.selectedEndDate = picked;
});
}
@override
Widget build(BuildContext context) {
return Container(
child: DropdownButton(
items: [
DropdownMenuItem(
value: "1",
child: InkWell(
child: Text(
'Forever',
style: TextStyle(color: Colors.teal, fontSize: 23),
),
onTap: () => (widget.selectedEndDate = DateTime(2100)),
),
),
DropdownMenuItem(
value: "2",
child: InkWell(
onTap: () => _selectDate(context),
child: Text(
'Select end date: ${selectedDate.day}/${selectedDate.month}/${selectedDate.year}',
style: TextStyle(color: Colors.teal, fontSize: 23),
),
),
),
],
onChanged: (val) => setState(() {
_value = val;
}),
value: _value,
style: TextStyle(color: Colors.teal, fontSize: 23),
isExpanded: true,
));
}
}
答案 0 :(得分:0)
您可以检查在onChange()中选择的值:
onChanged: (val) =>
setState(() {
if (val == "2") {
//Display select date
}
_value = val;
})