瀏覽代碼

Improve scrollBar behaviour

Neeraj Gupta 2 年之前
父節點
當前提交
d3a7cb060a
共有 1 個文件被更改,包括 14 次插入3 次删除
  1. 14 3
      lib/ui/huge_listview/huge_listview.dart

+ 14 - 3
lib/ui/huge_listview/huge_listview.dart

@@ -88,6 +88,7 @@ class HugeListView<T> extends StatefulWidget {
 class HugeListViewState<T> extends State<HugeListView<T>> {
   final scrollKey = GlobalKey<DraggableScrollbarState>();
   final listener = ItemPositionsListener.create();
+  int lastIndexJump = -1;
   dynamic error;
 
   @override
@@ -136,10 +137,20 @@ class HugeListViewState<T> extends State<HugeListView<T>> {
           totalCount: widget.totalCount,
           initialScrollIndex: widget.startIndex,
           onChange: (position) {
-            final int newIndex = (position * widget.totalCount).floor();
             final int currentIndex = _currentFirst();
-            if (newIndex != currentIndex) {
-              widget.controller?.jumpTo(index: newIndex);
+            final int floorIndex = (position * widget.totalCount).floor();
+            final int cielIndex = (position * widget.totalCount).ceil();
+            int nextIndexToJump;
+            if (floorIndex != currentIndex && floorIndex > currentIndex) {
+              nextIndexToJump = floorIndex;
+            } else if (cielIndex != currentIndex && cielIndex < currentIndex) {
+              nextIndexToJump = floorIndex;
+            } else {
+              return;
+            }
+            if (lastIndexJump != nextIndexToJump) {
+              lastIndexJump = nextIndexToJump;
+              widget.controller?.jumpTo(index: nextIndexToJump);
             }
           },
           labelTextBuilder: widget.labelTextBuilder,