Skip to content

Commit

Permalink
Fixed bug where new chunks were not auto-synced horizontally
Browse files Browse the repository at this point in the history
  • Loading branch information
andreped committed Jan 5, 2025
1 parent 238ceaf commit c9e6fa4
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 14 deletions.
22 changes: 14 additions & 8 deletions lib/core/controllers.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@ import 'package:flutter/material.dart';
class ClampingScrollController extends ScrollController {
@override
void jumpTo(double value) {
final double maxScrollExtent = position.maxScrollExtent;
final double minScrollExtent = position.minScrollExtent;
final double clampedValue = value.clamp(minScrollExtent, maxScrollExtent);
super.jumpTo(clampedValue);
if (hasClients) {
final double maxScrollExtent = position.maxScrollExtent;
final double minScrollExtent = position.minScrollExtent;
final double clampedValue = value.clamp(minScrollExtent, maxScrollExtent);
super.jumpTo(clampedValue);
}
}

@override
Expand All @@ -15,9 +17,13 @@ class ClampingScrollController extends ScrollController {
required Duration duration,
required Curve curve,
}) {
final double maxScrollExtent = position.maxScrollExtent;
final double minScrollExtent = position.minScrollExtent;
final double clampedOffset = offset.clamp(minScrollExtent, maxScrollExtent);
return super.animateTo(clampedOffset, duration: duration, curve: curve);
if (hasClients) {
final double maxScrollExtent = position.maxScrollExtent;
final double minScrollExtent = position.minScrollExtent;
final double clampedOffset =
offset.clamp(minScrollExtent, maxScrollExtent);
return super.animateTo(clampedOffset, duration: duration, curve: curve);
}
return Future.value();
}
}
32 changes: 26 additions & 6 deletions lib/widgets/table/table_widget.dart
Original file line number Diff line number Diff line change
Expand Up @@ -35,21 +35,34 @@ class TableWidgetState extends State<TableWidget> {
final int _limit = 50;
bool _isLoading = false;
bool _hasMoreData = true;
final bool _isSyncing = false;
bool _isSyncing = false;
ScrollController? _activeRowController;
final List<ScrollController> _rowControllers = [];
String _searchQuery = ''; // Add search query state
double _globalHorizontalScrollPosition =
0.0; // Global horizontal scroll position

@override
void initState() {
super.initState();
_verticalScrollController
.addListener(() => onScroll(_verticalScrollController, _loadNextChunk));
_horizontalScrollController.addListener(() => onHorizontalScroll(
_horizontalScrollController,
_rowControllers,
_isSyncing,
_activeRowController));
_horizontalScrollController.addListener(() {
_globalHorizontalScrollPosition =
_horizontalScrollController.position.pixels;
onHorizontalScroll(_horizontalScrollController, _rowControllers,
_isSyncing, _activeRowController);
});

// Add listener to sync horizontal scroll position on vertical scroll
_verticalScrollController.addListener(() {
for (final rowController in _rowControllers) {
if (rowController.hasClients) {
rowController.jumpTo(_horizontalScrollController.position.pixels);
}
}
});

loadData(widget.selectedTable);
}

Expand Down Expand Up @@ -110,6 +123,13 @@ class TableWidgetState extends State<TableWidget> {
if (newData.length < _limit) {
_hasMoreData = false;
}

// Add new row controllers and set their scroll position immediately
for (int i = _data.length - newData.length; i < _data.length; i++) {
final rowController = ClampingScrollController();
_rowControllers.add(rowController);
rowController.jumpTo(_horizontalScrollController.position.pixels);
}
});
}

Expand Down

0 comments on commit c9e6fa4

Please sign in to comment.