在颤振码初始项中,我如何通过与所选择的下拉列表中的_year值相同的年份 ================================================== =====================
Container(
height: 100,
width: 48,
child: CupertinoPicker(
squeeze: 1.3,
itemExtent: 30,
backgroundColor: Colors.white,
children: _getYearList(),
scrollController: FixedExtentScrollController(
initialItem: DateTime.now().year.compareTo(_year),
),
onSelectedItemChanged: (item) {
setState(() {
_year = DateTime.now().year - 30 + item;
});
},
),
),
List<Widget> _getYearList() {
List list = new List<Widget>();
int currentYear = DateTime.now().year - 30;
print("yearrr : " + currentYear.toString());
list.contains(_year);
_year.compareTo(currentYear);
print("currentYear :" + currentYear.toString());
//for (int index = currentYear; index <= currentYear + 30; index++) {
for (int index = currentYear; index <= currentYear + 30; index++) {
list.add(Container(
alignment: Alignment.center,
child: Text(
'$index',
//'$index - ${index + 1}',
style: TextStyle(fontSize: 16),
),
));
}
return list;
}
答案 0 :(得分:0)
如果我的问题正确的话...
识别CupertinoPicker / DropdownButton中的值更改的方法是在代码中使用的onSelectedItemChanged / onChanged的回调函数。
回调函数有一个自变量void Function(T value)
,它是选定的值。您可以拥有T类型的成员,并通过setState(){...}在onSelectedItemChanged / onChanged的情况下保持更改后的值。