6 changed files with 151 additions and 109 deletions
@ -0,0 +1,52 @@
@@ -0,0 +1,52 @@
|
||||
import 'package:flutter/material.dart'; |
||||
import 'package:syncfusion_flutter_datagrid/datagrid.dart'; |
||||
import 'package:win_text_editor/modules/memory_table/controllers/base_data_source.dart'; |
||||
import 'package:win_text_editor/modules/memory_table/models/memory_table.dart'; |
||||
|
||||
class FieldsDataSource extends SelectableDataSource<Field> { |
||||
FieldsDataSource( |
||||
List<Field> fields, { |
||||
required Null Function(dynamic index, dynamic isSelected) onSelectionChanged, |
||||
}) : super(fields, onSelectionChanged: onSelectionChanged); |
||||
|
||||
@override |
||||
List<DataGridRow> get rows => |
||||
items |
||||
.map( |
||||
(field) => DataGridRow( |
||||
cells: [ |
||||
DataGridCell<bool>(columnName: 'select', value: field.isSelected), |
||||
DataGridCell<String>(columnName: 'id', value: field.id), |
||||
DataGridCell<String>(columnName: 'name', value: field.name), |
||||
DataGridCell<String>(columnName: 'chineseName', value: field.chineseName), |
||||
DataGridCell<String>(columnName: 'type', value: field.type), |
||||
], |
||||
), |
||||
) |
||||
.toList(); |
||||
|
||||
get data => items; |
||||
|
||||
@override |
||||
DataGridRowAdapter buildRow(DataGridRow row) { |
||||
final rowIndex = effectiveRows.indexOf(row); |
||||
return DataGridRowAdapter( |
||||
cells: |
||||
row.getCells().map<Widget>((cell) { |
||||
if (cell.columnName == 'select') { |
||||
return Center( |
||||
child: Checkbox( |
||||
value: items[rowIndex].isSelected, |
||||
onChanged: (value) => toggleRowSelection(rowIndex, value), |
||||
), |
||||
); |
||||
} |
||||
return Container( |
||||
alignment: Alignment.centerLeft, |
||||
padding: const EdgeInsets.symmetric(horizontal: 8), |
||||
child: Text(cell.value.toString(), overflow: TextOverflow.ellipsis), |
||||
); |
||||
}).toList(), |
||||
); |
||||
} |
||||
} |
@ -0,0 +1,80 @@
@@ -0,0 +1,80 @@
|
||||
import 'package:flutter/material.dart'; |
||||
import 'package:syncfusion_flutter_datagrid/datagrid.dart'; |
||||
import 'package:win_text_editor/modules/memory_table/controllers/base_data_source.dart'; |
||||
import 'package:win_text_editor/modules/memory_table/models/memory_table.dart'; |
||||
|
||||
// 索引数据源 |
||||
class IndexesDataSource extends SelectableDataSource<Index> { |
||||
// 新增:跟踪当前选中的索引ID |
||||
String? _selectedId; |
||||
|
||||
IndexesDataSource( |
||||
List<Index> indexes, { |
||||
required Null Function(dynamic index, dynamic isSelected) onSelectionChanged, |
||||
}) : super(indexes, onSelectionChanged: onSelectionChanged); |
||||
|
||||
@override |
||||
List<DataGridRow> get rows => |
||||
items |
||||
.map( |
||||
(index) => DataGridRow( |
||||
cells: [ |
||||
DataGridCell<bool>(columnName: 'select', value: index.isSelected), |
||||
DataGridCell<String>(columnName: 'indexName', value: index.indexName), |
||||
DataGridCell<bool>(columnName: 'isPrimary', value: index.isPrimary), |
||||
DataGridCell<String>(columnName: 'indexFields', value: index.indexFields), |
||||
DataGridCell<String>(columnName: 'rule', value: index.rule), |
||||
], |
||||
), |
||||
) |
||||
.toList(); |
||||
|
||||
get data => items; |
||||
|
||||
@override |
||||
DataGridRowAdapter buildRow(DataGridRow row) { |
||||
final rowIndex = effectiveRows.indexOf(row); |
||||
final index = items[rowIndex]; |
||||
|
||||
return DataGridRowAdapter( |
||||
cells: |
||||
row.getCells().map<Widget>((cell) { |
||||
if (cell.columnName == 'select') { |
||||
return Center( |
||||
child: Radio<String>( |
||||
value: index.id, // 使用唯一标识作为value |
||||
groupValue: _selectedId, // 当前选中的ID |
||||
onChanged: (String? value) { |
||||
_selectedId = value; |
||||
toggleRowSelection(rowIndex, value != null); |
||||
notifyListeners(); // 必须通知更新 |
||||
}, |
||||
), |
||||
); |
||||
} |
||||
return Container( |
||||
alignment: Alignment.centerLeft, |
||||
padding: const EdgeInsets.symmetric(horizontal: 8), |
||||
child: Text(cell.value.toString(), overflow: TextOverflow.ellipsis), |
||||
); |
||||
}).toList(), |
||||
); |
||||
} |
||||
|
||||
// 修改单选逻辑 |
||||
@override |
||||
void toggleRowSelection(int index, bool? value) { |
||||
final newValue = value ?? false; |
||||
if (newValue) { |
||||
// 单选时先取消所有选择 |
||||
for (var item in items) { |
||||
item.isSelected = false; |
||||
} |
||||
_selectedId = items[index].id; // 记录选中的ID |
||||
} |
||||
items[index].isSelected = newValue; |
||||
updateSelectionState(); |
||||
notifyListeners(); |
||||
onSelectionChanged?.call(index, newValue); |
||||
} |
||||
} |
Loading…
Reference in new issue