4 changed files with 154 additions and 59 deletions
@ -0,0 +1,85 @@
@@ -0,0 +1,85 @@
|
||||
import 'package:flutter/material.dart'; |
||||
import 'package:syncfusion_flutter_datagrid/datagrid.dart'; |
||||
import 'package:win_text_editor/modules/data_compare/controllers/data_compare_controller.dart'; |
||||
|
||||
class DataCompareDataSource extends DataGridSource { |
||||
final DataCompareController controller; |
||||
|
||||
DataCompareDataSource(this.controller) { |
||||
controller.addListener(_handleDataChange); |
||||
} |
||||
|
||||
void _handleDataChange() { |
||||
notifyListeners(); |
||||
} |
||||
|
||||
@override |
||||
void dispose() { |
||||
controller.removeListener(_handleDataChange); |
||||
super.dispose(); |
||||
} |
||||
|
||||
@override |
||||
List<DataGridRow> get rows { |
||||
return controller.comparedData.map((row) { |
||||
final isLeft = row['is_left'] as bool; |
||||
final rightData = row['right_data'] as Map<String, dynamic>?; |
||||
final status = row['match_status'] as String; |
||||
|
||||
return DataGridRow( |
||||
cells: [ |
||||
// 左表列 |
||||
DataGridCell<String>(columnName: 'left_serial', value: isLeft ? row['serial'] : ''), |
||||
DataGridCell<String>(columnName: 'left_key', value: isLeft ? row['key'] : ''), |
||||
...controller.leftColumns.map( |
||||
(col) => DataGridCell<String>(columnName: 'left_$col', value: isLeft ? row[col] : ''), |
||||
), |
||||
|
||||
// 对比状态 |
||||
DataGridCell<Icon>(columnName: 'comparison', value: _getStatusIcon(status)), |
||||
|
||||
// 右表列 |
||||
DataGridCell<String>( |
||||
columnName: 'right_serial', |
||||
value: rightData?['serial'] ?? (!isLeft ? row['serial'] : ''), |
||||
), |
||||
DataGridCell<String>( |
||||
columnName: 'right_key', |
||||
value: rightData?['key'] ?? (!isLeft ? row['key'] : ''), |
||||
), |
||||
...controller.rightColumns.map( |
||||
(col) => DataGridCell<String>( |
||||
columnName: 'right_$col', |
||||
value: rightData?[col] ?? (!isLeft ? row[col] : ''), |
||||
), |
||||
), |
||||
], |
||||
); |
||||
}).toList(); |
||||
} |
||||
|
||||
Icon _getStatusIcon(String status) { |
||||
switch (status) { |
||||
case 'full_match': |
||||
return const Icon(Icons.double_arrow, color: Colors.green); |
||||
case 'key_match': |
||||
return const Icon(Icons.arrow_forward_ios, color: Colors.blue); |
||||
default: |
||||
return const Icon(Icons.close, color: Colors.red); |
||||
} |
||||
} |
||||
|
||||
@override |
||||
DataGridRowAdapter? buildRow(DataGridRow row) { |
||||
return DataGridRowAdapter( |
||||
cells: |
||||
row.getCells().map<Widget>((cell) { |
||||
return Container( |
||||
alignment: Alignment.center, |
||||
padding: const EdgeInsets.all(8), |
||||
child: cell.value.runtimeType == Icon ? cell.value : Text(cell.value.toString()), |
||||
); |
||||
}).toList(), |
||||
); |
||||
} |
||||
} |
Loading…
Reference in new issue