[英]How to delete a row from a DataTable in Flutter?
I'm new to flutter.我是 flutter 的新手。 I'm creating a app with a table which a button in every row.
我正在创建一个带有表格的应用程序,每行都有一个按钮。 I need to delete the row when that button is pressed.
按下该按钮时,我需要删除该行。
This is the code of the table.这是表的代码。
DataTable(
columns: const <DataColumn>[
DataColumn(label: Text('Medications')),
DataColumn(label: Text('Amount')),
DataColumn(label: Text('When')),
DataColumn(label: Text(' '))
],
rows:
_data // Loops through dataColumnText, each iteration assigning the value to element
.map(
((element) => DataRow(
cells: <DataCell>[
DataCell(Text(element[
"drug"])), //Extracting from Map element the value
DataCell(Text(element["amount"])),
DataCell(Text(element["when"])),
DataCell(new RButton(
id: bid,// bid variable increments by 1 every t
onPressed: onPressed,
))
],
)),
)
.toList(),
),
This is the code of RButton这是 RButton 的代码
class RButton extends StatelessWidget {
final int id;
final Function onPressed;
const RButton({this.id, this.onPressed});
@override
Widget build(BuildContext context) {
return SizedBox(
width: 30,
child: FlatButton(
onPressed: () {
onPressed(this.id);
print(this.id);
},
textColor: Colors.red,
child: Text("-"),
));
}
}
This is the code of the function button run when pressed.这是按下时运行的 function 按钮的代码。
onPressed(id) {
setState() {
_data.remove(_data[id]);
}
}
Assuming that you are properly using a StatefulWidget
to create this example.假设您正确使用
StatefulWidget
来创建此示例。
setState
is a function
that takes another function
as it's single parameter. setState
是一个function
,它采用另一个function
作为它的单个参数。
So, it must used like this,所以,它必须像这样使用,
onPressed(id) {
setState(() {
_data.remove(_data[id]);
});
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.