0

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); }, ), ), ), 

1 Answer 1

1

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(); 
Sign up to request clarification or add additional context in comments.

1 Comment

Absolutely!! Couldn't see it myself..I guess I should take a break lol..thank you very much!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.