在我的代码中,用户可以添加DataRows
到DataTable
. 所有 DataRows 都保存在一个列表中。我在显示列表数据的不同类中使用此列表。问题是当用户添加行时,UI 没有更新,因此用户没有看到新添加的 DataRow。用户添加新 DataRow 后如何更新 UI?
class ExerciseTable extends StatefulWidget {
ExerciseTable({Key key, this.title}) : super(key: key);
final String title;
@override
_ExerciseTableState createState() => _ExerciseTableState();
}
class _ExerciseTableState extends State<ExerciseTable> {
ExerciseDataSource _rowsDataSource;
@override
void initState() {
_rowsDataSource = ExerciseDataSource(list: _rowList);
super.initState();
}
List<DataRow> _rowList = [
DataRow(cells: <DataCell>[
DataCell(Datum(key: GlobalKey())),
DataCell(ExerciseWidget(key: GlobalKey())),
DataCell(ExerciseWidget(key: GlobalKey())),
DataCell(ExerciseWidget(key: GlobalKey())),
DataCell(ExerciseWidget(key: GlobalKey())),
DataCell(ExerciseWidget(key: GlobalKey())),
DataCell(ExerciseWidget(key: GlobalKey())),
DataCell(Notes(key: GlobalKey())),
]),
];
class ExerciseDataSource extends DataTableSource {
ExerciseDataSource({Key key, this.list});
final List<DataRow> list;
int _selectedCount = 0;
@override
int get rowCount => list.length;
@override
bool get isRowCountApproximate => false;
@override
int get selectedRowCount => _selectedCount;
@override
DataRow getRow(int index) {
return DataRow.byIndex(
index: index,
cells: <DataCell>[
DataCell(Datum(key: GlobalKey())),
DataCell(ExerciseWidget(key: GlobalKey())),
DataCell(ExerciseWidget(key: GlobalKey())),
DataCell(ExerciseWidget(key: GlobalKey())),
DataCell(ExerciseWidget(key: GlobalKey())),
DataCell(ExerciseWidget(key: GlobalKey())),
DataCell(ExerciseWidget(key: GlobalKey())),
DataCell(Notes(key: GlobalKey())),
]);
}
}