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

Add column title (padding, icon-size, and checkBox size to auto-fit calculation #715

Open
wants to merge 12 commits into
base: master
Choose a base branch
from
Open
13 changes: 7 additions & 6 deletions lib/src/helper/pluto_key_manager_event.dart
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@

import 'package:flutter/material.dart';
import 'package:flutter/services.dart';

class PlutoKeyManagerEvent {
FocusNode focusNode;
RawKeyEvent event;
KeyEvent event;

PlutoKeyManagerEvent({
required this.focusNode,
Expand All @@ -12,9 +13,9 @@ class PlutoKeyManagerEvent {

bool get needsThrottle => isMoving || isTab || isPageUp || isPageDown;

bool get isKeyDownEvent => event.runtimeType == RawKeyDownEvent;
bool get isKeyDownEvent => event is KeyDownEvent;

bool get isKeyUpEvent => event.runtimeType == RawKeyUpEvent;
bool get isKeyUpEvent => event is KeyUpEvent;

bool get isMoving => isHorizontal || isVertical;

Expand Down Expand Up @@ -94,15 +95,15 @@ class PlutoKeyManagerEvent {
}

bool get isShiftPressed {
return event.isShiftPressed;
return HardwareKeyboard.instance.isShiftPressed;
}

bool get isCtrlPressed {
return event.isMetaPressed || event.isControlPressed;
return HardwareKeyboard.instance.isMetaPressed || HardwareKeyboard.instance.isControlPressed;
}

bool get isAltPressed {
return event.isAltPressed;
return HardwareKeyboard.instance.isAltPressed;
}

bool get isModifierPressed {
Expand Down
2 changes: 1 addition & 1 deletion lib/src/manager/pluto_grid_key_manager.dart
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ class PlutoGridKeyManager {
if (stateManager.configuration.shortcut.handle(
keyEvent: keyEvent,
stateManager: stateManager,
state: RawKeyboard.instance,
state: HardwareKeyboard.instance,
)) {
return;
}
Expand Down
2 changes: 1 addition & 1 deletion lib/src/manager/shortcut/pluto_grid_shortcut.dart
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class PlutoGridShortcut {
bool handle({
required PlutoKeyManagerEvent keyEvent,
required PlutoGridStateManager stateManager,
required RawKeyboard state,
required HardwareKeyboard state,
}) {
for (final action in actions.entries) {
if (action.key.accepts(keyEvent.event, state)) {
Expand Down
6 changes: 3 additions & 3 deletions lib/src/manager/shortcut/pluto_grid_shortcut_action.dart
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,7 @@ class PlutoGridActionDefaultTab extends PlutoGridShortcutAction {

final saveIsEditing = stateManager.isEditing;

keyEvent.event.isShiftPressed
keyEvent.isShiftPressed
? _moveCellPrevious(stateManager)
: _moveCellNext(stateManager);

Expand Down Expand Up @@ -408,7 +408,7 @@ class PlutoGridActionDefaultEnterKey extends PlutoGridShortcutAction {
}

if (enterKeyAction.isEditingAndMoveDown) {
if (keyEvent.event.isShiftPressed) {
if (keyEvent.isShiftPressed) {
stateManager.moveCurrentCell(
PlutoMoveDirection.up,
notify: false,
Expand All @@ -420,7 +420,7 @@ class PlutoGridActionDefaultEnterKey extends PlutoGridShortcutAction {
);
}
} else if (enterKeyAction.isEditingAndMoveRight) {
if (keyEvent.event.isShiftPressed) {
if (keyEvent.isShiftPressed) {
stateManager.moveCurrentCell(
PlutoMoveDirection.left,
force: true,
Expand Down
114 changes: 75 additions & 39 deletions lib/src/manager/state/column_state.dart
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import 'dart:collection';
import 'dart:math' as math;

import 'package:flutter/material.dart';
import 'package:pluto_grid/pluto_grid.dart';
import 'package:pluto_grid/src/ui/cells/pluto_default_cell.dart';

abstract class IColumnState {
/// Columns provided at grid start.
Expand Down Expand Up @@ -304,12 +306,12 @@ mixin ColumnState implements IPlutoGridState {

@override
PlutoColumn? get currentColumn {
return currentCell == null ? null : currentCell!.column;
return currentCell?.column;
}

@override
String? get currentColumnField {
return currentCell == null ? null : currentCell!.column.field;
return currentCell?.column.field;
}

@override
Expand Down Expand Up @@ -571,47 +573,65 @@ mixin ColumnState implements IPlutoGridState {

@override
void autoFitColumn(BuildContext context, PlutoColumn column) {
final String maxValue = refRows.fold('', (previousValue, element) {
final value = column.formattedValueForDisplay(
element.cells.entries
.firstWhere((element) => element.key == column.field)
.value
.value,
);
String maxValue = '';
bool hasExpandableRowGroup = false;
for (final row in refRows) {
final cell = row.cells.entries.firstWhere((element) => element.key == column.field).value;
var value = column.formattedValueForDisplay(cell.value);
if (hasRowGroups) {
if (PlutoDefaultCell.showGroupCount(rowGroupDelegate!, cell)) {
final groupCountValue = PlutoDefaultCell.groupCountText(rowGroupDelegate!, row);
if (groupCountValue.isNotEmpty) {
value = '$value $groupCountValue';
}
}

if (previousValue.length < value.length) {
return value;
hasExpandableRowGroup |= PlutoDefaultCell.canExpand(rowGroupDelegate!, cell);
}

return previousValue;
});
if (maxValue.length < value.length) {
maxValue = value;
}
}

// Get size after rendering virtually
// https://stackoverflow.com/questions/54351655/flutter-textfield-width-should-match-width-of-contained-text
TextSpan textSpan = TextSpan(
style: DefaultTextStyle.of(context).style,
text: maxValue,
);

TextPainter textPainter = TextPainter(
text: textSpan,
textDirection: TextDirection.ltr,
);

textPainter.layout();

// todo : Apply (popup type icon, checkbox, drag indicator, renderer)
final titleTextWidth = _visualTextWidth(column.title, style.columnTextStyle);
final maxValueTextWidth = _visualTextWidth(maxValue, style.cellTextStyle);

// todo : Handle (renderer) width

final calculatedTileWidth = titleTextWidth -
column.width +
[
(column.titlePadding ?? style.defaultColumnTitlePadding).horizontal,
if (column.enableRowChecked) _getEffectiveButtonWidth(context, checkBox: true),
if (column.isShowRightIcon) style.iconSize,
8,
].reduce((acc, a) => acc + a);

final calculatedCellWidth = maxValueTextWidth -
column.width +
[
(column.cellPadding ?? style.defaultCellPadding).horizontal,
if (hasExpandableRowGroup) _getEffectiveButtonWidth(context),
if (column.enableRowChecked) _getEffectiveButtonWidth(context, checkBox: true),
if (column.isShowRightIcon) style.iconSize,
2,
].reduce((acc, a) => acc + a);

EdgeInsets cellPadding =
column.cellPadding ?? configuration.style.defaultCellPadding;
resizeColumn(column, math.max(calculatedTileWidth, calculatedCellWidth));
}

resizeColumn(
column,
textPainter.width -
column.width +
(cellPadding.left + cellPadding.right) +
2,
);
double _visualTextWidth(String text, TextStyle style) {
if (text.isEmpty) return 0;
final painter = TextPainter(
text: TextSpan(
style: style,
text: text,
),
textDirection: isRTL ? TextDirection.rtl : TextDirection.ltr,
)..layout();
return painter.width;
}

@override
Expand Down Expand Up @@ -1019,9 +1039,7 @@ mixin ColumnState implements IPlutoGridState {
return false;
}

final columns = showFrozenColumn
? leftFrozenColumns + bodyColumns + rightFrozenColumns
: refColumns;
final columns = showFrozenColumn ? leftFrozenColumns + bodyColumns + rightFrozenColumns : refColumns;

final resizeHelper = getColumnsResizeHelper(
columns: columns,
Expand All @@ -1031,4 +1049,22 @@ mixin ColumnState implements IPlutoGridState {

return resizeHelper.update();
}

double _getEffectiveButtonWidth(BuildContext context, {bool checkBox = false}) {
final theme = Theme.of(context);
late double width;
switch (theme.materialTapTargetSize) {
case MaterialTapTargetSize.padded:
width = kMinInteractiveDimension;
break;
case MaterialTapTargetSize.shrinkWrap:
width = kMinInteractiveDimension - 8.0;
break;
}
if (!checkBox) {
return width;
}
return width + theme.visualDensity.baseSizeAdjustment.dx;
}
}

18 changes: 7 additions & 11 deletions lib/src/model/pluto_column_type.dart
Original file line number Diff line number Diff line change
Expand Up @@ -224,11 +224,9 @@ extension PlutoColumnTypeExtension on PlutoColumnType {

bool get hasFormat => this is PlutoColumnTypeHasFormat;

bool get applyFormatOnInit =>
hasFormat ? (this as PlutoColumnTypeHasFormat).applyFormatOnInit : false;
bool get applyFormatOnInit => hasFormat ? (this as PlutoColumnTypeHasFormat).applyFormatOnInit : false;

dynamic applyFormat(dynamic value) =>
hasFormat ? (this as PlutoColumnTypeHasFormat).applyFormat(value) : value;
dynamic applyFormat(dynamic value) => hasFormat ? (this as PlutoColumnTypeHasFormat).applyFormat(value) : value;
}

class PlutoColumnTypeText implements PlutoColumnType {
Expand Down Expand Up @@ -351,8 +349,7 @@ class PlutoColumnTypeCurrency
late final int decimalPoint;
}

class PlutoColumnTypeSelect
implements PlutoColumnType, PlutoColumnTypeHasPopupIcon {
class PlutoColumnTypeSelect implements PlutoColumnType, PlutoColumnTypeHasPopupIcon {
@override
final dynamic defaultValue;

Expand Down Expand Up @@ -386,6 +383,8 @@ class PlutoColumnTypeSelect
}
}



class PlutoColumnTypeDate
implements
PlutoColumnType,
Expand Down Expand Up @@ -477,8 +476,7 @@ class PlutoColumnTypeDate
}
}

class PlutoColumnTypeTime
implements PlutoColumnType, PlutoColumnTypeHasPopupIcon {
class PlutoColumnTypeTime implements PlutoColumnType, PlutoColumnTypeHasPopupIcon {
@override
final dynamic defaultValue;

Expand Down Expand Up @@ -595,9 +593,7 @@ mixin PlutoColumnTypeWithNumberFormat {
match += numberFormat.symbols.MINUS_SIGN;
}

formatted = formatted
.replaceAll(RegExp('[^$match]'), '')
.replaceFirst(numberFormat.symbols.DECIMAL_SEP, '.');
formatted = formatted.replaceAll(RegExp('[^$match]'), '').replaceFirst(numberFormat.symbols.DECIMAL_SEP, '.');

final num formattedNumber = num.tryParse(formatted) ?? 0;

Expand Down
4 changes: 2 additions & 2 deletions lib/src/pluto_grid.dart
Original file line number Diff line number Diff line change
Expand Up @@ -593,7 +593,7 @@ class PlutoGridState extends PlutoStateWithChange<PlutoGrid> {
}
}

KeyEventResult _handleGridFocusOnKey(FocusNode focusNode, RawKeyEvent event) {
KeyEventResult _handleGridFocusOnKey(FocusNode focusNode, KeyEvent event) {
if (_keyManager.eventResult.isSkip == false) {
_keyManager.subject.add(PlutoKeyManagerEvent(
focusNode: focusNode,
Expand All @@ -608,7 +608,7 @@ class PlutoGridState extends PlutoStateWithChange<PlutoGrid> {
Widget build(BuildContext context) {
return FocusScope(
onFocusChange: _stateManager.setKeepFocus,
onKey: _handleGridFocusOnKey,
onKeyEvent: _handleGridFocusOnKey,
child: _GridContainer(
stateManager: _stateManager,
child: LayoutBuilder(
Expand Down
Loading