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

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

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

Loading…
Cancel
Save