I have built a ListView.builder() in lets call it StatefulWidget1 as seen below that is as long as the index of the List listTile is:
StatefulWidget1 below:
ListView.builder(
itemCount: listTile.length,
itemBuilder: (context, index) {
// CUSTOM WIDGET
return CustomTile(
custom: listTile.reversed.toList()[index]);
},
)
CustomTile contains a ListTile() in StatefulWidget2 and I want to be able to have an onPressed event inside of the ListTile() that calls a function in StatefulWidget1 that will delete the CustomTile from the screen and remove itself from the List listTile index.
How do I call a function from StatefulWidget1 inside of StatefulWidget2? and How do I ensure that I'm deleting the CustomTile of the correct index?
Edit: or is there another way to delete the CustomTile with the leading: onPressed() {}
StatefulWidget2 Below:
Card(
margin: EdgeInsets.fromLTRB(20.0, 6.0, 20.0, 0.0),
child: ListTile(
leading: IconButton(
onPressed: () {},
icon: Icon(
Icons.close,
color: Colors.red,
),
),
title: Text(
'${widget.produce.name} - \$${widget.produce.tilePrice}',
style: TextStyle(fontWeight: FontWeight.bold),
),
subtitle: Padding(
padding: const EdgeInsets.fromLTRB(0.0, 8.0, 0.0, 3.0),
child: Text(
'Weight: ${widget.produce.weight} lbs,\nPrice per lb: \$${widget.produce.pricePerLb}'),
),
trailing: Column(
children: [
TextButton(
onPressed: () {},
child: Text(
'edit',
style: TextStyle(color: Colors.green[600]),
),
),
],
),
),
),
CodePudding user response:
try this solution
ListView.builder(
itemCount: listTile.length,
itemBuilder: (context, index) {
// CUSTOM WIDGET
return InkWell(
onTap: (){
setState(() {
listTile.remove(index);
});
},
child: CustomTile(
custom: listTile.reversed.toList()[index]),
);
},
),
CodePudding user response:
ListView.builder(
itemCount: listTile.length,
itemBuilder: (context, index) {
// CUSTOM WIDGET
return InkWell(
child:Inkwell(
onTap: () => setState(() {
listTile.reversed.toList()
[index])).remove(index);
});
CustomTile(
custom:
listTile.reversed.toList()[index])),
);
},
),
CodePudding user response:
first create a function in your StatefulWidget1 just like this
void _removetile(int index){
setState(() {
listTile.reversed.toList().removeAt(index);
});
}
Send this function as argument in your StatefulWidget2 just like
CustomTile(custom: listTile.reversed.toList()[index], removeFunct :
_removetile, index: index)
declar in your customTile just like this
class CustomTile extends StatefulWidget{
final List<YorList> custom;
final void Function removeFunc;
final int index;
CustomTile(this.custom,this.removeFunc,this.index);
}
use this function in your leading button just like
leading: IconButton(
onPressed: () {
widget.removeFunc(widget.index);
},
