[英]how to supply data to a class extending DataTableSource
使用PaginatedDataTable在抖動上顯示數據
我必須提供一個從DataTableSource擴展的類的源,但是我無法像在通過錯誤嘗試擴展DataTableSource的類上提供任何數據
“只能在初始值設定項flutter中訪問靜態成員”
那么如何從外部提供DataTableSource的初始數據?
class TransactionAcrossMonthsItem {
final String transactionName;
final int monthOf1;
final int monthOf2;
final int monthOf3;
final int monthOf4;
final int monthOf5;
final int monthOf6;
final int monthOf7;
final int monthOf8;
final int monthOf9;
final int monthOf10;
final int monthOf11;
final int monthOf12;
final int totalCount;
final double percent;
bool selected = false;
TransactionAcrossMonthsItem(
this.transactionName,
this.monthOf1,
this.monthOf2,
this.monthOf3,
this.monthOf4,
this.monthOf5,
this.monthOf6,
this.monthOf7,
this.monthOf8,
this.monthOf9,
this.monthOf10,
this.monthOf11,
this.monthOf12,
this.totalCount,
this.percent);
}
class TransactionsAcrossDataSource extends DataTableSource {
final List<TransactionAcrossMonthsItem> _data;
int _selectedCount = 0;
TransactionsAcrossDataSource(this._data);
...........
}
class DetailedReportPage extends StatefulWidget {
final int yearOf;
DetailedReportPage({this.yearOf});
@override
State<StatefulWidget> createState() {
return _DetailedReportPageState();
}
}
class _DetailedReportPageState extends State<DetailedReportPage> {
int _rowsPerPage = PaginatedDataTable.defaultRowsPerPage;
int _sortColumnIndex;
bool _sortAscending = true;
List<TransactionAcrossMonthsItem> _data = [
TransactionAcrossMonthsItem(
'eDirham', 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 1000, 70),
TransactionAcrossMonthsItem(
'tasheel', 120, 120, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 2000, 20),
TransactionAcrossMonthsItem(
'immgration', 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 3000, 10)
];
var _dataSource = TransactionsAcrossDataSource(_data);
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: .....
body: createReport(),
);
}
Widget createReport() {
return ListView(padding: const EdgeInsets.all(20.0), children: <Widget>[
PaginatedDataTable(
header: Text('Year of ${widget.yearOf}'),
rowsPerPage: _rowsPerPage,
onRowsPerPageChanged: (int value) {
setState(() {
_rowsPerPage = value;
});
},
sortColumnIndex: _sortColumnIndex,
sortAscending: _sortAscending,
// onSelectAll: _dataSource._selectAll,
columns: getColumns(),
source: _dataSource,
)
]);
}
List<DataColumn> getColumns() {
return [
DataColumn(
label: Text('Name'),
......
),
DataColumn(
.....
),
......
];
}
}
更改
var _dataSource = TransactionsAcrossDataSource(_data);
至
TransactionsAcrossDataSource _dataSource;
並移動
_dataSource = TransactionsAcrossDataSource(_data);
到構造函數。
因為您不能在類的方法或構造函數之外具有隨機代碼。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.