Bring projects, wikis, and teams together with AI. AppFlowy is the AI collaborative workspace where you achieve more without losing control of your data. The leading open source Notion alternative.
Dart blogconfluence-alternativecontent-managementcontent-servicesdocumentationflutternote-takingnotion-alternativeproject-managementtask-managementteam-collaborationwiki
feat: resize table column width on mobile (#7174)
Lucasdd3b4dc5fb69
diff --git a/frontend/appflowy_flutter/integration_test/mobile/document/simple_table_test.dart b/frontend/appflowy_flutter/integration_test/mobile/document/simple_table_test.dartindex 9fa8be3a9c..546baebb31 100644--- a/frontend/appflowy_flutter/integration_test/mobile/document/simple_table_test.dart+++ b/frontend/appflowy_flutter/integration_test/mobile/document/simple_table_test.dart@@ -494,5 +494,61 @@ void main() {expect(heading.level, equals(1));}});++ testWidgets('''+1. insert a simple table via + menu+2. resize column+''', (tester) async {+ await tester.launchInAnonymousMode();+ await tester.createNewDocumentOnMobile('simple table');++ final editorState = tester.editor.getCurrentEditorState();+ // focus on the editor+ unawaited(+ editorState.updateSelectionWithReason(+ Selection.collapsed(Position(path: [0])),+ reason: SelectionUpdateReason.uiEvent,+ ),+ );+ await tester.pumpAndSettle();++ final beforeWidth = editorState.getNodeAtPath([0, 0, 0])!.columnWidth;++ // find the first cell+ {+ final resizeHandle = find.byType(SimpleTableColumnResizeHandle).first;+ final offset = tester.getCenter(resizeHandle);+ final gesture = await tester.startGesture(offset, pointer: 7);+ await tester.pumpAndSettle();++ await gesture.moveBy(const Offset(100, 0));+ await tester.pumpAndSettle();++ await gesture.up();+ await tester.pumpAndSettle();+ }++ // check the table is updated+ final afterWidth1 = editorState.getNodeAtPath([0, 0, 0])!.columnWidth;+ expect(afterWidth1, greaterThan(beforeWidth));++ // resize back to the original width+ {+ final resizeHandle = find.byType(SimpleTableColumnResizeHandle).first;+ final offset = tester.getCenter(resizeHandle);+ final gesture = await tester.startGesture(offset, pointer: 7);+ await tester.pumpAndSettle();++ await gesture.moveBy(const Offset(-100, 0));+ await tester.pumpAndSettle();++ await gesture.up();+ await tester.pumpAndSettle();+ }++ // check the table is updated+ final afterWidth2 = editorState.getNodeAtPath([0, 0, 0])!.columnWidth;+ expect(afterWidth2, equals(beforeWidth));+ });});}diff --git a/frontend/appflowy_flutter/lib/plugins/document/presentation/editor_plugins/simple_table/simple_table_cell_block_component.dart b/frontend/appflowy_flutter/lib/plugins/document/presentation/editor_plugins/simple_table/simple_table_cell_block_component.dartindex f234440169..2b691da637 100644--- a/frontend/appflowy_flutter/lib/plugins/document/presentation/editor_plugins/simple_table/simple_table_cell_block_component.dart+++ b/frontend/appflowy_flutter/lib/plugins/document/presentation/editor_plugins/simple_table/simple_table_cell_block_component.dart@@ -282,9 +282,34 @@ class SimpleTableCellBlockWidgetState extends State<SimpleTableCellBlockWidget>return ValueListenableBuilder(valueListenable: isReorderingHitCellNotifier,builder: (context, isReorderingHitCellNotifier, _) {- return DecoratedBox(- decoration: _buildDecoration(),- child: child!,+ final previousCell = node.getPreviousCellInSameRow();+ return Stack(+ children: [+ DecoratedBox(+ decoration: _buildDecoration(),+ child: child!,+ ),+ Positioned(+ right: 0,+ top: 0,+ bottom: 0,+ child: SimpleTableColumnResizeHandle(+ node: node,+ ),+ ),+ if (node.columnIndex != 0 && previousCell != null)+ Positioned(+ left: 0,+ top: 0,+ bottom: 0,+ // pass the previous node to the resize handle+ // to make the resize handle work correctly+ child: SimpleTableColumnResizeHandle(+ node: previousCell,+ isPreviousCell: true,+ ),+ ),+ ],);},);diff --git a/frontend/appflowy_flutter/lib/plugins/document/presentation/editor_plugins/simple_table/simple_table_constants.dart b/frontend/appflowy_flutter/lib/plugins/document/presentation/editor_plugins/simple_table/simple_table_constants.dartindex bc6e3450e5..6bdbcd516a 100644--- a/frontend/appflowy_flutter/lib/plugins/document/presentation/editor_plugins/simple_table/simple_table_constants.dart+++ b/frontend/appflowy_flutter/lib/plugins/document/presentation/editor_plugins/simple_table/simple_table_constants.dart@@ -86,6 +86,11 @@ class SimpleTableContext {/// This value is available on mobile onlyfinal ValueNotifier<int?> isReorderingHitIndex = ValueNotifier(null);+ /// resizingCell is the cell that the user is resizing+ ///+ /// This value is available on mobile only+ final ValueNotifier<Node?> resizingCell = ValueNotifier(null);+/// Scroll controller for the tableScrollController? horizontalScrollController;@@ -180,6 +185,7 @@ class SimpleTableContext {reorderingOffset.dispose();isEditingCell.dispose();isReorderingHitIndex.dispose();+ resizingCell.dispose();}}diff --git a/frontend/appflowy_flutter/lib/plugins/document/presentation/editor_plugins/simple_table/simple_table_operations/simple_table_style_operation.dart b/frontend/appflowy_flutter/lib/plugins/document/presentation/editor_plugins/simple_table/simple_table_operations/simple_table_style_operation.dartindex 59352af0ac..7afa7da66b 100644--- a/frontend/appflowy_flutter/lib/plugins/document/presentation/editor_plugins/simple_table/simple_table_operations/simple_table_style_operation.dart+++ b/frontend/appflowy_flutter/lib/plugins/document/presentation/editor_plugins/simple_table/simple_table_operations/simple_table_style_operation.dart@@ -15,11 +15,6 @@ extension TableOptionOperation on EditorState {required Node tableCellNode,required double deltaX,}) async {- // Disable in mobile- if (UniversalPlatform.isMobile) {- return;- }-assert(tableCellNode.type == SimpleTableCellBlockKeys.type);if (tableCellNode.type != SimpleTableCellBlockKeys.type) {@@ -65,11 +60,6 @@ extension TableOptionOperation on EditorState {required Node tableCellNode,required double width,}) async {- // Disable in mobile- if (UniversalPlatform.isMobile) {- return;- }-assert(tableCellNode.type == SimpleTableCellBlockKeys.type);if (tableCellNode.type != SimpleTableCellBlockKeys.type) {diff --git a/frontend/appflowy_flutter/lib/plugins/document/presentation/editor_plugins/simple_table/simple_table_widgets/simple_table_column_resize_handle.dart b/frontend/appflowy_flutter/lib/plugins/document/presentation/editor_plugins/simple_table/simple_table_widgets/simple_table_column_resize_handle.dartindex bfa1f316d7..ab941cb4d1 100644--- a/frontend/appflowy_flutter/lib/plugins/document/presentation/editor_plugins/simple_table/simple_table_widgets/simple_table_column_resize_handle.dart+++ b/frontend/appflowy_flutter/lib/plugins/document/presentation/editor_plugins/simple_table/simple_table_widgets/simple_table_column_resize_handle.dart@@ -3,15 +3,19 @@ import 'dart:ui';import 'package:appflowy/plugins/document/presentation/editor_plugins/plugins.dart';import 'package:appflowy_editor/appflowy_editor.dart';import 'package:flutter/material.dart';+import 'package:flutter/services.dart';import 'package:provider/provider.dart';+import 'package:universal_platform/universal_platform.dart';class SimpleTableColumnResizeHandle extends StatefulWidget {const SimpleTableColumnResizeHandle({super.key,required this.node,+ this.isPreviousCell = false,});final Node node;+ final bool isPreviousCell;@overrideState<SimpleTableColumnResizeHandle> createState() =>@@ -24,8 +28,17 @@ class _SimpleTableColumnResizeHandleStatebool isStartDragging = false;+ // record the previous position of the drag, only used on mobile+ double previousDx = 0;+@overrideWidget build(BuildContext context) {+ return UniversalPlatform.isMobile+ ? _buildMobileResizeHandle()+ : _buildDesktopResizeHandle();+ }++ Widget _buildDesktopResizeHandle() {return MouseRegion(cursor: SystemMouseCursors.resizeColumn,onEnter: (_) => _onEnterHoverArea(),@@ -56,6 +69,40 @@ class _SimpleTableColumnResizeHandleState);}+ Widget _buildMobileResizeHandle() {+ return GestureDetector(+ behavior: HitTestBehavior.opaque,+ onLongPressStart: _onLongPressStart,+ onLongPressMoveUpdate: _onLongPressMoveUpdate,+ onLongPressEnd: _onLongPressEnd,+ onLongPressCancel: _onLongPressCancel,+ child: ValueListenableBuilder(+ valueListenable: simpleTableContext.resizingCell,+ builder: (context, resizingCell, child) {+ final isSameColumnIndex =+ widget.node.columnIndex == resizingCell?.columnIndex;+ if (!isSameColumnIndex) {+ return child!;+ }+ return Container(+ width: 10,+ alignment: !widget.isPreviousCell+ ? Alignment.centerRight+ : Alignment.centerLeft,+ child: Container(+ width: 2,+ color: Theme.of(context).colorScheme.primary,+ ),+ );+ },+ child: Container(+ width: 10,+ color: Colors.transparent,+ ),+ ),+ );+ }+void _onEnterHoverArea() {simpleTableContext.hoveringOnResizeHandle.value = widget.node;}@@ -79,6 +126,13 @@ class _SimpleTableColumnResizeHandleStateisStartDragging = true;}+ void _onLongPressStart(LongPressStartDetails details) {+ isStartDragging = true;+ simpleTableContext.resizingCell.value = widget.node;++ HapticFeedback.lightImpact();+ }+void _onHorizontalDragUpdate(DragUpdateDetails details) {if (!isStartDragging) {return;@@ -92,6 +146,21 @@ class _SimpleTableColumnResizeHandleState);}+ void _onLongPressMoveUpdate(LongPressMoveUpdateDetails details) {+ if (!isStartDragging) {+ return;+ }++ // only update the column width in memory,+ // the actual update will be applied in _onHorizontalDragEnd+ context.read<EditorState>().updateColumnWidthInMemory(+ tableCellNode: widget.node,+ deltaX: details.offsetFromOrigin.dx - previousDx,+ );++ previousDx = details.offsetFromOrigin.dx;+ }+void _onHorizontalDragEnd(DragEndDetails details) {if (!isStartDragging) {return;@@ -106,4 +175,27 @@ class _SimpleTableColumnResizeHandleStatewidth: widget.node.columnWidth,);}++ void _onLongPressEnd(LongPressEndDetails details) {+ if (!isStartDragging) {+ return;+ }++ isStartDragging = false;++ // apply the updated column width+ context.read<EditorState>().updateColumnWidth(+ tableCellNode: widget.node,+ width: widget.node.columnWidth,+ );++ previousDx = 0;++ simpleTableContext.resizingCell.value = null;+ }++ void _onLongPressCancel() {+ isStartDragging = false;+ simpleTableContext.resizingCell.value = null;+ }}