Flutter DataTable-点击行 [英] Flutter DataTable - Tap on row
本文介绍了Flutter DataTable-点击行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在使用Flutter DataTables显示购物车中的项目列表。现在,我要编辑任何选定行的数量。有没有办法获取用户点击的行的信息?
I am using Flutter DataTables to display list of items in cart. Now I want to edit the quantity of any selected row. Is there a way to get the information of the row user has tapped?
下面是我的DataTable的完整代码:
Following is complete code of my DataTable:
class _DataTableSampleState extends State<DataTableSample> {
void _getSelectedRowInfo() {
print('Selected Item Row Name Here...')
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('DataTable Sample'),
),
body: Container(
child: DataTable(
onSelectAll: (b) {},
sortAscending: true,
columns: <DataColumn>[
DataColumn(
label: Text('Item'),
),
DataColumn(
label: Text('Price'),
),
],
rows: items
.map(
(itemRow) => DataRow(
cells: [
DataCell(
Text(itemRow.itemName),
showEditIcon: false,
placeholder: false,
),
DataCell(
Text(itemRow.itemPrice),
showEditIcon: true,
placeholder: false,
onTap: _getSelectedRowInfo,
),
],
),
)
.toList(),
),
),
);
}
}
class ItemInfo {
String itemName;
String itemPrice;
ItemInfo({
this.itemName,
this.itemPrice,
});
}
var items = <ItemInfo>[
ItemInfo(
itemName: 'Item A',
itemPrice: '250',
),
ItemInfo(
itemName: 'Item B',
itemPrice: '100',
),
ItemInfo(
itemName: 'Item C',
itemPrice: '150',
),
];
单击编辑图标时,将调用 _getSelectedRowInfo方法。我想在此函数中获得所选行/点击行的完整详细信息。
When edit icon is clicked "_getSelectedRowInfo" method is called. I want to get complete detail of selected/tapped row in this function.
推荐答案
您可以使用 onSelectChanged 来自DataRow的属性。
you can use onSelectChanged property from DataRow.
rows: items
.map(
(itemRow) => DataRow(
onSelectChanged: (bool selected) {
if (selected) {
log.add('row-selected: ${itemRow.index}');
}
},
cells: [
// ..
],
),
这篇关于Flutter DataTable-点击行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文