Browse Source

稍微好一点了

master
hejl 2 months ago
parent
commit
c1ce774deb
  1. 34
      win_text_editor/lib/modules/memory_table/controllers/memory_table_controller.dart
  2. 26
      win_text_editor/lib/modules/memory_table/widgets/memory_table_left_side.dart

34
win_text_editor/lib/modules/memory_table/controllers/memory_table_controller.dart

@ -29,19 +29,20 @@ class Index {
// //
class FieldsDataSource extends DataGridSource { class FieldsDataSource extends DataGridSource {
FieldsDataSource(this.fields); FieldsDataSource(this.fields) {
_selectionNotifier = ValueNotifier<bool>(false);
}
List<Field> fields; List<Field> fields;
late ValueNotifier<bool> _selectionNotifier;
ValueNotifier<bool> get selectionNotifier => _selectionNotifier;
void toggleAllSelection(bool? value) { void toggleAllSelection(bool value) {
for (var field in fields) { for (var field in fields) {
field.isSelected = value ?? false; field.isSelected = value;
}
notifyListeners();
} }
_selectionNotifier.value = !_selectionNotifier.value; // Trigger rebuild
void toggleRowSelection(int index, bool? value) {
fields[index].isSelected = value ?? false;
notifyListeners(); notifyListeners();
} }
@ -91,18 +92,29 @@ class FieldsDataSource extends DataGridSource {
}).toList(), }).toList(),
); );
} }
void toggleRowSelection(int index, bool? value) {
fields[index].isSelected = value ?? false;
notifyListeners();
}
} }
// //
class IndexesDataSource extends DataGridSource { class IndexesDataSource extends DataGridSource {
IndexesDataSource(this.indexes); IndexesDataSource(this.indexes) {
_selectionNotifier = ValueNotifier<bool>(false);
}
List<Index> indexes; List<Index> indexes;
late ValueNotifier<bool> _selectionNotifier;
get selectionNotifier => _selectionNotifier;
void toggleAllSelection(bool? value) { void toggleAllSelection(bool value) {
for (var index in indexes) { for (var index in indexes) {
index.isSelected = value ?? false; index.isSelected = value;
} }
_selectionNotifier.value = !_selectionNotifier.value; // Trigger rebuild
notifyListeners(); notifyListeners();
} }

26
win_text_editor/lib/modules/memory_table/widgets/memory_table_left_side.dart

@ -54,10 +54,15 @@ class MemoryTableLeftSide extends StatelessWidget {
columns: [ columns: [
GridColumn( GridColumn(
columnName: 'select', columnName: 'select',
label: _buildCheckboxHeader( label: ValueListenableBuilder<bool>(
valueListenable: (fieldsSource).selectionNotifier,
builder: (context, _, __) {
return _buildCheckboxHeader(
context, context,
fieldsSource, fieldsSource,
controller.fieldsSource.rows.length, fieldsSource.fields.length,
);
},
), ),
width: 60, width: 60,
), ),
@ -111,13 +116,15 @@ class MemoryTableLeftSide extends StatelessWidget {
Widget _buildCheckboxHeader(BuildContext context, FieldsDataSource dataSource, int rowCount) { Widget _buildCheckboxHeader(BuildContext context, FieldsDataSource dataSource, int rowCount) {
final allSelected = rowCount > 0 && dataSource.fields.every((item) => item.isSelected); final allSelected = rowCount > 0 && dataSource.fields.every((item) => item.isSelected);
final someSelected =
rowCount > 0 && dataSource.fields.any((item) => item.isSelected) && !allSelected;
return Container( return Container(
alignment: Alignment.center, alignment: Alignment.center,
color: Colors.grey[200], color: Colors.grey[200],
child: Checkbox( child: Checkbox(
value: allSelected, value: allSelected,
tristate: true, tristate: someSelected,
onChanged: (value) { onChanged: (value) {
dataSource.toggleAllSelection(value ?? false); dataSource.toggleAllSelection(value ?? false);
}, },
@ -147,10 +154,15 @@ class MemoryTableLeftSide extends StatelessWidget {
columns: [ columns: [
GridColumn( GridColumn(
columnName: 'select', columnName: 'select',
label: _buildCheckboxHeaderForIndexes( label: ValueListenableBuilder<bool>(
valueListenable: (indexesSource).selectionNotifier,
builder: (context, _, __) {
return _buildCheckboxHeaderForIndexes(
context, context,
indexesSource, indexesSource,
controller.indexesSource.rows.length, indexesSource.indexes.length,
);
},
), ),
width: 60, width: 60,
), ),
@ -203,13 +215,15 @@ class MemoryTableLeftSide extends StatelessWidget {
int rowCount, int rowCount,
) { ) {
final allSelected = rowCount > 0 && dataSource.indexes.every((item) => item.isSelected); final allSelected = rowCount > 0 && dataSource.indexes.every((item) => item.isSelected);
final someSelected =
rowCount > 0 && dataSource.indexes.any((item) => item.isSelected) && !allSelected;
return Container( return Container(
alignment: Alignment.center, alignment: Alignment.center,
color: Colors.grey[200], color: Colors.grey[200],
child: Checkbox( child: Checkbox(
value: allSelected, value: allSelected,
tristate: true, tristate: someSelected,
onChanged: (value) { onChanged: (value) {
dataSource.toggleAllSelection(value ?? false); dataSource.toggleAllSelection(value ?? false);
}, },

Loading…
Cancel
Save