很遗憾,互联网上没有关于如何更新PaginatedDataTable中条目的示例。
我认为我即将解决此问题,但不确定如何做到,也许总有更好的方法。
我有这个WeightDataSource
,WeightDialog
只会返回用户输入的数字。这些值已正确存储在内存中,但屏幕不会更新,因为其他类的状态没有更改(如果重新加载应用程序,则可以看到更改)。
class WeightDataSource extends DataTableSource {
final List<Weight> _weights;
final BuildContext context;
WeightDataSource(this.context, this._weights);
int _selectedCount = 0;
_editCell(index, Weight _weight) async {
print('$index $_weight');
var updatedWeight = await Navigator.push(
context,
MaterialPageRoute(
builder: (BuildContext context) =>
WeightDialog(
color: Colors.greenAccent,
saveUpdate: 'update',
),
fullscreenDialog: true,
));
if (updatedWeight != null) {
print('received weight: $updatedWeight');
final prefs = await SharedPreferences.getInstance();
var tempWeights = json.decode(prefs.getString('weights')) ?? {};
tempWeights[_weight.date.toString()] = updatedWeight;
prefs.setString('weights', json.encode(tempWeights));
tempWeights = json.decode(prefs.getString('weights')) ?? {};
print('updated weights $tempWeights');
}
}
// ...
我的想法是,如果我可以以某种方式调用我的_loadAsyncData
函数,屏幕将会更新,但是我不确定如何从其他类中进行操作:
class HistoryPageState extends State<HistoryPage> {
var calories = {};
var weights = {};
@override
void initState() {
super.initState();
_loadAsyncData();
}
_loadAsyncData() async {
SharedPreferences prefs = await SharedPreferences.getInstance();
setState(() {
calories = json.decode(prefs.getString('calories')) ?? {};
weights = json.decode(prefs.getString('weights')) ?? {};
});
}
// ...
_updateWeights(BuildContext context, _weightsDataSource) async {
var inputCalories = await Navigator.push(
context,
MaterialPageRoute(
builder: (context) =>
Scaffold(
appBar: AppBar(title: Text('Update weights')),
body: PaginatedDataTable(
header: Text('Weight Log'),
dataRowHeight: 30,
headingRowHeight: 30,
rowsPerPage: 3,
columns: <DataColumn>[
DataColumn(
label: Text('Date'),
),
DataColumn(label: Text('Weight'))
],
source: _weightsDataSource,
),
),
fullscreenDialog: true,
));
if (inputCalories != null) {
print(inputCalories);
}
}