I have a widget WeekdayCell
that is used to build a ListView. 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. Can you spot what I'm doing wrong?
As always many thanks for your time and help.
Widget:
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;
});
},
)
],
),
);
}
}
ListView:
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);
},
),
),
),
You forgot to add the parenthesis. widget.onTap and widget.onTapCancel gives you a reference to the function. For calling/executing it, you need to write:
widget.isSelected == false ? widget.onTap() : widget.onTapCancel();
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.