|
|
@ -103,17 +103,25 @@ class _DataGridViewState extends State<DataGridView> { |
|
|
|
eol: '\n', |
|
|
|
eol: '\n', |
|
|
|
).convert(content, fieldDelimiter: _delimiter); |
|
|
|
).convert(content, fieldDelimiter: _delimiter); |
|
|
|
|
|
|
|
|
|
|
|
// 清理数据 |
|
|
|
// 清理数据并添加序号列 |
|
|
|
final cleanedData = |
|
|
|
final cleanedData = csvTable |
|
|
|
csvTable |
|
|
|
.where((row) => row.isNotEmpty && row.any((cell) => cell.toString().trim().isNotEmpty)) |
|
|
|
.where( |
|
|
|
.toList(); // 先转换为List |
|
|
|
(row) => row.isNotEmpty && row.any((cell) => cell.toString().trim().isNotEmpty), |
|
|
|
|
|
|
|
) |
|
|
|
// 添加序号列 |
|
|
|
.map((row) => row.map((cell) => cell.toString().trim()).toList()) |
|
|
|
final dataWithIndex = cleanedData.asMap().entries.map((entry) { |
|
|
|
.toList(); |
|
|
|
final index = entry.key; |
|
|
|
|
|
|
|
final row = entry.value; |
|
|
|
|
|
|
|
// 如果是标题行(第一行),添加"序号"列标题 |
|
|
|
|
|
|
|
if (index == 0) { |
|
|
|
|
|
|
|
return ['序号', ...row.map((cell) => cell.toString().trim()).toList()]; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
// 数据行添加序号(从1开始) |
|
|
|
|
|
|
|
return [index, ...row.map((cell) => cell.toString().trim()).toList()]; |
|
|
|
|
|
|
|
}).toList(); |
|
|
|
|
|
|
|
|
|
|
|
setState(() { |
|
|
|
setState(() { |
|
|
|
_csvData = cleanedData; |
|
|
|
_csvData = dataWithIndex; |
|
|
|
}); |
|
|
|
}); |
|
|
|
} |
|
|
|
} |
|
|
|
} catch (e) { |
|
|
|
} catch (e) { |
|
|
@ -129,8 +137,20 @@ class _DataGridViewState extends State<DataGridView> { |
|
|
|
final headers = _csvData.first; |
|
|
|
final headers = _csvData.first; |
|
|
|
final dataRows = _csvData.sublist(1); |
|
|
|
final dataRows = _csvData.sublist(1); |
|
|
|
|
|
|
|
|
|
|
|
final columns = |
|
|
|
final columns = [ |
|
|
|
headers.map<GridColumn>((header) { |
|
|
|
// 添加序号列 |
|
|
|
|
|
|
|
GridColumn( |
|
|
|
|
|
|
|
columnName: '序号', |
|
|
|
|
|
|
|
width: 60, // 序号列宽度较小 |
|
|
|
|
|
|
|
label: Container( |
|
|
|
|
|
|
|
padding: const EdgeInsets.all(8.0), |
|
|
|
|
|
|
|
color: Colors.grey[200], |
|
|
|
|
|
|
|
alignment: Alignment.center, |
|
|
|
|
|
|
|
child: const Text('序号', overflow: TextOverflow.ellipsis, maxLines: 1), |
|
|
|
|
|
|
|
), |
|
|
|
|
|
|
|
), |
|
|
|
|
|
|
|
// 原有列 |
|
|
|
|
|
|
|
...headers.sublist(1).map<GridColumn>((header) { |
|
|
|
return GridColumn( |
|
|
|
return GridColumn( |
|
|
|
columnName: header.toString(), |
|
|
|
columnName: header.toString(), |
|
|
|
width: 150, // 固定列宽 |
|
|
|
width: 150, // 固定列宽 |
|
|
@ -141,7 +161,8 @@ class _DataGridViewState extends State<DataGridView> { |
|
|
|
child: Text(header.toString(), overflow: TextOverflow.ellipsis, maxLines: 1), |
|
|
|
child: Text(header.toString(), overflow: TextOverflow.ellipsis, maxLines: 1), |
|
|
|
), |
|
|
|
), |
|
|
|
); |
|
|
|
); |
|
|
|
}).toList(); |
|
|
|
}).toList(), |
|
|
|
|
|
|
|
]; |
|
|
|
|
|
|
|
|
|
|
|
final dataSource = _CsvDataSource(headers: headers, rows: dataRows); |
|
|
|
final dataSource = _CsvDataSource(headers: headers, rows: dataRows); |
|
|
|
|
|
|
|
|
|
|
@ -150,7 +171,7 @@ class _DataGridViewState extends State<DataGridView> { |
|
|
|
columns: columns, |
|
|
|
columns: columns, |
|
|
|
gridLinesVisibility: GridLinesVisibility.both, |
|
|
|
gridLinesVisibility: GridLinesVisibility.both, |
|
|
|
headerGridLinesVisibility: GridLinesVisibility.both, |
|
|
|
headerGridLinesVisibility: GridLinesVisibility.both, |
|
|
|
columnWidthMode: ColumnWidthMode.fitByCellValue, // 使用固定列宽模式 |
|
|
|
columnWidthMode: ColumnWidthMode.fitByCellValue, |
|
|
|
); |
|
|
|
); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|