[英]ListView onTap callback not working Flutter
我有一個用於構建 ListView 的小部件WeekdayCell
。 All the logic that makes the text changing color when selected calling widget's onTap works es expected but onTap:
and onTapCancel:
callbacks from the ListView don't execute as I don't get any print in console when tapping a cell. 你能看出我做錯了什么嗎?
一如既往地感謝您的時間和幫助。
小部件:
class WeedayCell extends StatefulWidget {
String day;
bool isSelected;
Function onTap;
Function onTapCancel;
WeedayCell(
{@required this.day,
@required this.isSelected,
@required this.onTap,
@required this.onTapCancel});
@override
List<Object> get props => [day, isSelected];
@override
_WeedayCellState createState() => new _WeedayCellState();
}
class _WeedayCellState extends State<WeedayCell> {
@override
Widget build(BuildContext context) {
return Container(
child: new Column(
children: [
new ListTile(
selected: widget.isSelected,
title: Text(
'${widget.day}',
style: TextStyle(
color: widget.isSelected == false
? Colors.black
: Colors.redAccent,
fontSize: 20,
fontWeight: FontWeight.w500),
),
onTap: () {
setState(() {
widget.isSelected == false ? widget.onTap : widget.onTapCancel;
widget.isSelected = !widget.isSelected;
});
},
)
],
),
);
}
}
列表顯示:
body: Container(
padding: EdgeInsets.all(20),
child: ListView.builder(
itemCount: weekdays.length,
itemBuilder: (BuildContext context, int index) => WeedayCell(
day: '${weekdays[index]}',
isSelected: false,
onTap: () {
// repeatWeekdays.add(weekdays[index]);
repeatWeekdays.add(index);
print(repeatWeekdays);
},
onTapCancel: () {
// int indexToRemove = repeatWeekdays.indexOf(weekdays[index]);
// repeatWeekdays.removeAt(indexToRemove);
repeatWeekdays.remove(index);
print(repeatWeekdays);
},
),
),
),
你忘了加上括號。 widget.onTap和widget.onTapCancel為您提供了對 function 的引用。 要調用/執行它,您需要編寫:
widget.isSelected == false ? widget.onTap() : widget.onTapCancel();
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.