In flutter, I have dynamic widget data table. rows are added on submitting values (via edittextbox) with button. add and remove row on actions within row field(on clicking particular row). below code gives me output that removes last row from list. i need to remove row that I clicked/selected row from table. it should found index of that row to remove that one. is there alternative way to achieve the expected result?
I am using List<DataRow> row =[];
Row(
mainAxisAlignment: MainAxisAlignment.end,
children: <Widget>[
FlatButton.icon(
color: Colors.orange,
icon: Icon(FontAwesomeIcons.shoppingBag),
label: Text('Add to List'),
onPressed: () async{
ttl = int.parse(rate) * int.parse(noInputController.text);
setState(() {
this.row.add(
new DataRow(
cells: [
DataCell( Icon(Icons.remove_circle, color: Colors.red,),
//Text('remove', style: TextStyle(color: Colors.blue,decoration: TextDecoration.underline,),
onTap: () {
setState(() {
//remove item
if(amt > 0){
amt = amt - ttl;
}
print(amt);
if (index != -1){
print("before: $index $rowindex");
rowindex.removeAt(index);
//row.removeAt(index);
index = index - 1;
print("after: $index $rowindex");
}else{
print('no rows');
}
});
}),
DataCell(Text(prd),
onTap: () {
setState(() {
});
}),
DataCell(Text(noInputController.text),
onTap: () {
setState(() {
});
}),
DataCell(Text(rate),
onTap: () {
setState(() {
});
}),
DataCell(Text(ttl.toString()),
onTap: () {
setState(() {
});
}),
DataCell(Text(stype),
onTap: () {
setState(() {
});
}),
]));
print("before: $index $rowindex");
index = index + 1;
this.rowindex.add(index);
print("after: $index $rowindex");
// print(this.row.length);
return this.row;
});
rstitem(); //get total
}
),]),
SizedBox(
height: ScreenUtil.getInstance().setHeight(20),
),
new SingleChildScrollView(
scrollDirection: Axis.horizontal,
child: DataTable(
columns: [
DataColumn(label: Text("Remove",style: TextStyle(fontWeight: FontWeight.bold, fontSize: 15.0,color: Colors.blue))),
DataColumn( //tooltip: "item code for products",
label: Text("Name",style: TextStyle(fontWeight: FontWeight.bold,fontSize: 15.0,color: Colors.blue))),
DataColumn(label: Text("Quantity",style: TextStyle(fontWeight: FontWeight.bold, fontSize: 15.0,color: Colors.blue))),
DataColumn(label: Text("Rate",style: TextStyle(fontWeight: FontWeight.bold, fontSize: 15.0,color: Colors.blue))),
DataColumn(label: Text("Total",style: TextStyle(fontWeight: FontWeight.bold, fontSize: 15.0,color: Colors.blue))),
DataColumn(label: Text("Item Type",style: TextStyle(fontWeight: FontWeight.bold, fontSize: 15.0,color: Colors.blue)))
],
rows: row,
),),
help me fix this issue.
