如何解决当用户做某事时如何更新状态?
在我的代码中,用户可以将 DaTarows
添加到 DataTable
。所有数据行都保存在一个列表中。这个列表我在一个不同的类中使用,在那里我显示了列表的数据。
问题是当用户添加一行时,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(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())),]);
}
}
解决方法
要在用户执行某些操作时更新您的 UI,您应该使用 setState
方法
更多信息请查看setState
,首先,您的用户必须触发一个事件。
例如,您有 GestureDetector
widget。
因此,当用户触发事件时,您可以调用 setState
:
GestureDetector(
onTap:(){
setState((){
click++;
});
},)
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。