Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: cellColorCallback implemented #1000

Open
wants to merge 19 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions lib/src/manager/pluto_grid_state_manager.dart
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ class PlutoGridStateChangeNotifier extends PlutoChangeNotifier
this.onRowsMoved,
this.onColumnsMoved,
this.rowColorCallback,
this.cellColorCallback,
this.createHeader,
this.createFooter,
PlutoColumnMenuDelegate? columnMenuDelegate,
Expand Down Expand Up @@ -142,6 +143,9 @@ class PlutoGridStateChangeNotifier extends PlutoChangeNotifier
@override
final PlutoRowColorCallback? rowColorCallback;

@override
final PlutoCellColorCallback? cellColorCallback;

@override
final CreateHeaderCallBack? createHeader;

Expand Down Expand Up @@ -223,6 +227,7 @@ class PlutoGridStateManager extends PlutoGridStateChangeNotifier {
super.onRowsMoved,
super.onColumnsMoved,
super.rowColorCallback,
super.cellColorCallback,
super.createHeader,
super.createFooter,
super.columnMenuDelegate,
Expand Down
2 changes: 2 additions & 0 deletions lib/src/manager/state/cell_state.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ abstract class ICellState {
/// currently selected cell.
PlutoCell? get currentCell;

PlutoCellColorCallback? get cellColorCallback;

/// The position index value of the currently selected cell.
PlutoGridCellPosition? get currentCellPosition;

Expand Down
32 changes: 24 additions & 8 deletions lib/src/manager/state/editing_state.dart
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ abstract class IEditingState {
bool callOnChangedEvent = true,
bool force = false,
bool notify = true,
bool eachChange = false,
String? validationError,
});
}

Expand Down Expand Up @@ -212,6 +214,8 @@ mixin EditingState implements IPlutoGridState {
bool callOnChangedEvent = true,
bool force = false,
bool notify = true,
bool eachChange = false,
String? validationError,
}) {
final currentColumn = cell.column;

Expand Down Expand Up @@ -239,16 +243,28 @@ mixin EditingState implements IPlutoGridState {
currentRow.setState(PlutoRowState.updated);

cell.value = value;
cell.validationError = validationError;

if (callOnChangedEvent == true && onChanged != null) {
onChanged!(PlutoGridOnChangedEvent(
columnIdx: columnIndex(currentColumn)!,
column: currentColumn,
rowIdx: refRows.indexOf(currentRow),
row: currentRow,
value: value,
oldValue: oldValue,
));
onChanged!(
eachChange
? PlutoGridOnEachChangedEvent(
columnIdx: columnIndex(currentColumn)!,
column: currentColumn,
rowIdx: refRows.indexOf(currentRow),
row: currentRow,
value: value,
oldValue: oldValue,
)
: PlutoGridOnChangedEvent(
columnIdx: columnIndex(currentColumn)!,
column: currentColumn,
rowIdx: refRows.indexOf(currentRow),
row: currentRow,
value: value,
oldValue: oldValue,
),
);
}

notifyListeners(notify, changeCellValue.hashCode);
Expand Down
34 changes: 34 additions & 0 deletions lib/src/manager/state/layout_state.dart
Original file line number Diff line number Diff line change
Expand Up @@ -440,6 +440,40 @@ mixin LayoutState implements IPlutoGridState {
notifyListeners(notify, setShowColumnFooter.hashCode);
}

bool validate({bool notify = true}) {
bool isValid = true;
for (var rowElement in refRows.originalList) {
for (var cellElement in rowElement.cells.values) {
if (cellElement.initialized) {
if (cellElement.column.validator != null &&
!cellElement.column.hide) {
final validationError = cellElement.column.validator?.call(
rowElement,
cellElement,
cellElement.value,
);

changeCellValue(
cellElement,
cellElement.value,
force: true,
eachChange: true,
callOnChangedEvent: true,
validationError: validationError,
);

if (validationError != null) {
isValid = false;
}
}
}
}
}

notifyListeners(notify, validate.hashCode);
return isValid;
}

@override
void setShowColumnFilter(bool flag, {bool notify = true}) {
if (showColumnFilter == flag) {
Expand Down
21 changes: 21 additions & 0 deletions lib/src/model/pluto_cell.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ class PlutoCell {

final Key _key;

String? _validationError;

dynamic _value;

dynamic _valueForSorting;
Expand Down Expand Up @@ -52,6 +54,18 @@ class PlutoCell {
return _value;
}

String? get validationError {
if (_needToApplyFormatOnInit) {
_applyFormatOnInit();
}

return _validationError;
}

bool get hasValidationError {
return validationError != null && validationError!.trim().isNotEmpty;
}

set value(dynamic changed) {
if (_value == changed) {
return;
Expand All @@ -62,6 +76,13 @@ class PlutoCell {
_valueForSorting = null;
}

set validationError(String? changed) {
if (_validationError == changed) {
return;
}
_validationError = changed;
}

dynamic get valueForSorting {
_valueForSorting ??= _getValueForSorting();

Expand Down
17 changes: 17 additions & 0 deletions lib/src/model/pluto_column.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@ import 'package:flutter/material.dart';
import 'package:pluto_grid/pluto_grid.dart';

typedef PlutoColumnValueFormatter = String Function(dynamic value);
typedef PlutoColumnValueValidator = String? Function(
PlutoRow row,
PlutoCell cell,
dynamic value,
);

typedef PlutoColumnRenderer = Widget Function(
PlutoColumnRendererContext rendererContext);
Expand Down Expand Up @@ -67,6 +72,9 @@ class PlutoColumn {
/// It takes precedence over defaultCellPadding in PlutoGridConfiguration.
EdgeInsets? cellPadding;

/// Customisable cellField padding.
EdgeInsets? cellInternalPadding;

/// Text alignment in Cell. (Left, Right, Center)
PlutoColumnTextAlign textAlign;

Expand Down Expand Up @@ -192,6 +200,12 @@ class PlutoColumn {
/// Hide the column.
bool hide;

/// Trigger [PlutoGridOnEachChangedEvent] for each character change
bool enablePlutoGridOnEachChangedEvent;

/// Field Validator for the column
PlutoColumnValueValidator? validator;

PlutoColumn({
required this.title,
required this.field,
Expand All @@ -204,6 +218,7 @@ class PlutoColumn {
this.filterPadding,
this.titleSpan,
this.cellPadding,
this.cellInternalPadding,
this.textAlign = PlutoColumnTextAlign.start,
this.titleTextAlign = PlutoColumnTextAlign.start,
this.frozen = PlutoColumnFrozen.none,
Expand All @@ -226,6 +241,8 @@ class PlutoColumn {
this.enableAutoEditing = false,
this.enableEditingMode = true,
this.hide = false,
this.enablePlutoGridOnEachChangedEvent = false,
this.validator,
}) : _key = UniqueKey(),
_checkReadOnly = checkReadOnly;

Expand Down
49 changes: 49 additions & 0 deletions lib/src/pluto_grid.dart
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@ typedef CreateFooterCallBack = Widget Function(
typedef PlutoRowColorCallback = Color Function(
PlutoRowColorContext rowColorContext);

typedef PlutoCellColorCallback = Color? Function(
PlutoCellColorContext cellColorContext);

/// [PlutoGrid] is a widget that receives columns and rows and is expressed as a grid-type UI.
///
/// [PlutoGrid] supports movement and editing with the keyboard,
Expand Down Expand Up @@ -72,6 +75,7 @@ class PlutoGrid extends PlutoStatefulWidget {
this.createFooter,
this.noRowsWidget,
this.rowColorCallback,
this.cellColorCallback,
this.columnMenuDelegate,
this.configuration = const PlutoGridConfiguration(),
this.notifierFilterResolver,
Expand Down Expand Up @@ -291,6 +295,8 @@ class PlutoGrid extends PlutoStatefulWidget {
/// {@endtemplate}
final PlutoRowColorCallback? rowColorCallback;

final PlutoCellColorCallback? cellColorCallback;

/// {@template pluto_grid_property_columnMenuDelegate}
/// Column menu can be customized.
///
Expand Down Expand Up @@ -515,6 +521,7 @@ class PlutoGridState extends PlutoStateWithChange<PlutoGrid> {
onRowsMoved: widget.onRowsMoved,
onColumnsMoved: widget.onColumnsMoved,
rowColorCallback: widget.rowColorCallback,
cellColorCallback: widget.cellColorCallback,
createHeader: widget.createHeader,
createFooter: widget.createFooter,
columnMenuDelegate: widget.columnMenuDelegate,
Expand Down Expand Up @@ -1288,6 +1295,26 @@ class PlutoGridOnChangedEvent {
}
}

class PlutoGridOnEachChangedEvent extends PlutoGridOnChangedEvent {
const PlutoGridOnEachChangedEvent({
required super.columnIdx,
required super.column,
required super.rowIdx,
required super.row,
super.value,
super.oldValue,
});

@override
String toString() {
String out = '[PlutoGridOnEachChangedEvent] ';
out += 'ColumnIndex : $columnIdx, RowIndex : $rowIdx\n';
out += '::: oldValue : $oldValue\n';
out += '::: newValue : $value';
return out;
}
}

/// This is the argument value of the [PlutoGrid.onSelected] callback
/// that is called when the [PlutoGrid.mode] value is in select mode.
///
Expand Down Expand Up @@ -1470,6 +1497,28 @@ class PlutoRowColorContext {
});
}

/// Argument of [PlutoGrid.cellColorCallback] callback
/// to dynamically change the background color of a row.
class PlutoCellColorContext {
final PlutoColumn column;

final int rowIdx;

final PlutoRow row;

final PlutoCell cell;

final PlutoGridStateManager stateManager;

PlutoCellColorContext({
required this.column,
required this.rowIdx,
required this.row,
required this.cell,
required this.stateManager,
});
}

/// Extension class for [ScrollConfiguration.behavior] of [PlutoGrid].
class PlutoScrollBehavior extends MaterialScrollBehavior {
const PlutoScrollBehavior({
Expand Down
5 changes: 5 additions & 0 deletions lib/src/pluto_grid_configuration.dart
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,7 @@ class PlutoGridConfiguration {

class PlutoGridStyleConfig {
const PlutoGridStyleConfig({
this.bodyPadding = EdgeInsets.zero,
this.enableGridBorderShadow = false,
this.enableColumnBorderVertical = true,
this.enableColumnBorderHorizontal = true,
Expand Down Expand Up @@ -248,6 +249,7 @@ class PlutoGridStyleConfig {
});

const PlutoGridStyleConfig.dark({
this.bodyPadding = EdgeInsets.zero,
this.enableGridBorderShadow = false,
this.enableColumnBorderVertical = true,
this.enableColumnBorderHorizontal = true,
Expand Down Expand Up @@ -303,6 +305,9 @@ class PlutoGridStyleConfig {
this.gridPopupBorderRadius = BorderRadius.zero,
});

/// body padding
final EdgeInsets bodyPadding;

/// Enable borderShadow in [PlutoGrid].
final bool enableGridBorderShadow;

Expand Down
Loading