在弹出菜单被关闭后,我在更新 UI 时遇到了问题。实际单击的项目正在被删除,但更新它是问题所在。我知道我是从扩展 DatatableSource 的无状态类调用此方法。
这是代码:1:数据源
class ResultDataSource extends DataTableSource{
//Dependency injection
var brandsController = BrandsController(BrandsRepository());
ResultDataSource({
@required List<Result> resultData,
@required this.onRowSelect,
}) : _resultData = resultData,
assert(resultData != null);
final List<Result> _resultData;
final OnRowSelect onRowSelect;
@override
DataRow getRow(int index) {
assert(index >= 0);
if (index >= _resultData.length) {
return null;
}
final _result = _resultData[index];
return DataRow.byIndex(
index: index, // DONT MISS THIS
cells: <DataCell>[
DataCell(Text('${_result.name}',style: TextStyle(fontSize: 13.0, fontWeight: FontWeight.w600, color: secondaryColor))),
DataCell(Text('${_result.country}',style: TextStyle(fontSize: 13.0, color: secondaryColor))),
DataCell(Text('${_result.isActive.toString() == "true" ? "Active" : "Inactive"}',style: TextStyle(fontSize: 13.0, fontWeight: FontWeight.w500, color: primaryColor),)),
DataCell(
FutureBuilder<List<Result>>(
future: brandsController.fetchBrandList(),
builder: (context, snapshot){
return PopupMenuButton(
elevation: 20.0,
icon: Icon(
Icons.more_horiz,
color: secondaryColor.withOpacity(0.5),
),
itemBuilder: (context) =>[
PopupMenuItem(
child: Row(
children: [
SvgPicture.asset("assets/icons/view.svg", height: 18.0,color: Colors.blueGrey, ),
SizedBox(width:10.0),
Text("View Details", style: TextStyle(color: Colors.blueGrey, fontSize: 13.0),)
],
),
value: 1,
),
PopupMenuItem(
child: Row(
children: [
SvgPicture.asset("assets/icons/repeat.svg", height: 18.0,color: Colors.blueGrey, ),
SizedBox(width:10.0),
Text("Orders", style: TextStyle(color: Colors.blueGrey, fontSize: 13.0),)
],
),
value: 2,
),
PopupMenuItem(
child: Row(
children: [
SvgPicture.asset("assets/icons/activities.svg", height: 18.0,color: Colors.blueGrey, ),
SizedBox(width:10.0),
Text("Activities", style: TextStyle(color: Colors.blueGrey, fontSize: 13.0),)
],
),
value: 3,
),
PopupMenuItem(
onTap: () {
//Here is the delete functionality
brandsController.deleteBrand(_result.id.toString());
},
child:Row(
children: [
SvgPicture.asset("assets/icons/garbage.svg", height: 18.0,color: Colors.blueGrey, ),
SizedBox(width:10.0),
Text("Delete", style: TextStyle(color: Colors.blueGrey, fontSize: 13.0),)
],
),
value: 4,
),
],
);
},
)
),
],
);
}
@override
bool get isRowCountApproximate => false;
@override
int get rowCount => _resultData.length;
@override
int get selectedRowCount => 0;
}
2:Statefull 小部件类的代码
ListView(
padding: const EdgeInsets.all(16),
shrinkWrap: true,
children: [
Theme(
data: Theme.of(context).copyWith(
dividerColor: Colors.blueGrey.shade100.withOpacity(0.4),
),
child: PaginatedDataTable(
rowsPerPage: snapshot.data.length ?? 1,
showCheckboxColumn: true,
dataRowHeight: 60,
columns: [
DataColumn(label: Text(
"Name",
style: TextStyle(
fontSize: 13.5,
color: secondaryColor.withOpacity(0.4)),
),),
DataColumn(label: Text(
"Country",
style: TextStyle(
fontSize: 13.5,
color: secondaryColor.withOpacity(0.4)),
),),
DataColumn(label: Text(
"Status",
style: TextStyle(
fontSize: 13.5,
color: secondaryColor.withOpacity(0.4)),
),),
DataColumn(label: PopupMenuButton(
elevation: 20.0,
icon: Icon(
Icons.more_horiz,
color: secondaryColor.withOpacity(0.5),
),
itemBuilder: (context) =>
[
PopupMenuItem(
child: Row(
children: [
SvgPicture.asset(
"assets/icons/view.svg", height: 18.0,
color: Colors.blueGrey,),
SizedBox(width: 10.0),
Text("View Details", style: TextStyle(
color: Colors.blueGrey, fontSize: 13.0),)
],
),
value: 1,
),
PopupMenuItem(
child: Row(
children: [
SvgPicture.asset(
"assets/icons/repeat.svg", height: 18.0,
color: Colors.blueGrey,),
SizedBox(width: 10.0),
Text("Orders", style: TextStyle(
color: Colors.blueGrey, fontSize: 13.0),)
],
),
value: 2,
),
PopupMenuItem(
child: Row(
children: [
SvgPicture.asset(
"assets/icons/activities.svg", height: 18.0,
color: Colors.blueGrey,),
SizedBox(width: 10.0),
Text("Activities", style: TextStyle(
color: Colors.blueGrey, fontSize: 13.0),)
],
),
value: 3,
),
PopupMenuItem(
child: Row(
children: [
SvgPicture.asset(
"assets/icons/garbage.svg", height: 18.0,
color: Colors.blueGrey,),
SizedBox(width: 10.0),
Text("Delete", style: TextStyle(
color: Colors.blueGrey, fontSize: 13.0),)
],
),
value: 4,
),
],
)),
],
source: ResultDataSource(
onRowSelect: (index) => () {},
resultData: snapshot.data,
),
),
),
],
),