Browse Source

Map Gallery: Removed rebuilds on resizing bottom sheet caused by ScrollablePositionedList.builder

ashilkn 2 years ago
parent
commit
a6d979905b

+ 56 - 42
lib/ui/huge_listview/huge_listview.dart

@@ -62,6 +62,8 @@ class HugeListView<T> extends StatefulWidget {
 
   final bool disableScroll;
 
+  final bool isScrollablePositionedList;
+
   const HugeListView({
     Key? key,
     this.controller,
@@ -80,6 +82,7 @@ class HugeListView<T> extends StatefulWidget {
     this.isDraggableScrollbarEnabled = true,
     this.thumbPadding,
     this.disableScroll = false,
+    this.isScrollablePositionedList = true,
   }) : super(key: key);
 
   @override
@@ -96,7 +99,9 @@ class HugeListViewState<T> extends State<HugeListView<T>> {
   void initState() {
     super.initState();
 
-    listener.itemPositions.addListener(_sendScroll);
+    widget.isScrollablePositionedList
+        ? listener.itemPositions.addListener(_sendScroll)
+        : null;
   }
 
   @override
@@ -131,47 +136,56 @@ class HugeListViewState<T> extends State<HugeListView<T>> {
       return widget.emptyResultBuilder!(context);
     }
 
-    return DraggableScrollbar(
-      key: scrollKey,
-      totalCount: widget.totalCount,
-      initialScrollIndex: widget.startIndex,
-      onChange: (position) {
-        final int currentIndex = _currentFirst();
-        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,
-      backgroundColor: widget.thumbBackgroundColor,
-      drawColor: widget.thumbDrawColor,
-      heightScrollThumb: widget.thumbHeight,
-      bottomSafeArea: widget.bottomSafeArea,
-      currentFirstIndex: _currentFirst(),
-      isEnabled: widget.isDraggableScrollbarEnabled,
-      padding: widget.thumbPadding,
-      child: ScrollablePositionedList.builder(
-        physics:
-            widget.disableScroll ? const NeverScrollableScrollPhysics() : null,
-        itemScrollController: widget.controller,
-        itemPositionsListener: listener,
-        initialScrollIndex: widget.startIndex,
-        itemCount: max(widget.totalCount, 0),
-        itemBuilder: (context, index) {
-          return widget.itemBuilder(context, index);
-        },
-      ),
-    );
+    return widget.isScrollablePositionedList
+        ? DraggableScrollbar(
+            key: scrollKey,
+            totalCount: widget.totalCount,
+            initialScrollIndex: widget.startIndex,
+            onChange: (position) {
+              final int currentIndex = _currentFirst();
+              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,
+            backgroundColor: widget.thumbBackgroundColor,
+            drawColor: widget.thumbDrawColor,
+            heightScrollThumb: widget.thumbHeight,
+            bottomSafeArea: widget.bottomSafeArea,
+            currentFirstIndex: _currentFirst(),
+            isEnabled: widget.isDraggableScrollbarEnabled,
+            padding: widget.thumbPadding,
+            child: ScrollablePositionedList.builder(
+              physics: widget.disableScroll
+                  ? const NeverScrollableScrollPhysics()
+                  : null,
+              itemScrollController: widget.controller,
+              itemPositionsListener: listener,
+              initialScrollIndex: widget.startIndex,
+              itemCount: max(widget.totalCount, 0),
+              itemBuilder: (context, index) {
+                return widget.itemBuilder(context, index);
+              },
+            ),
+          )
+        : ListView.builder(
+            itemCount: max(widget.totalCount, 0),
+            itemBuilder: (context, index) {
+              return widget.itemBuilder(context, index);
+            },
+          );
   }
 
   /// Jump to the [position] in the list. [position] is between 0.0 (first item) and 1.0 (last item), practically currentIndex / totalCount.

+ 4 - 2
lib/ui/map/map_pull_up_gallery.dart

@@ -12,13 +12,14 @@ import "package:photos/ui/common/loading_widget.dart";
 import "package:photos/ui/viewer/gallery/gallery.dart";
 
 class MapPullUpGallery extends StatelessWidget {
+  final _selectedFiles = SelectedFiles();
   final StreamController<List<File>> visibleImages;
   final double bottomSheetDraggableAreaHeight;
   static const gridCrossAxisSpacing = 4.0;
   static const gridMainAxisSpacing = 4.0;
   static const gridPadding = 2.0;
   static const gridCrossAxisCount = 4;
-  const MapPullUpGallery(
+  MapPullUpGallery(
     this.visibleImages,
     this.bottomSheetDraggableAreaHeight, {
     Key? key,
@@ -105,7 +106,8 @@ class MapPullUpGallery extends StatelessWidget {
                         reloadEvent: Bus.instance.on<LocalPhotosUpdatedEvent>(),
                         tagPrefix: "map_gallery",
                         showSelectAllByDefault: true,
-                        selectedFiles: SelectedFiles(),
+                        selectedFiles: _selectedFiles,
+                        isScrollablePositionedList: false,
                       );
                     },
                   ),

+ 3 - 0
lib/ui/viewer/gallery/component/multiple_groups_gallery_view.dart

@@ -40,6 +40,7 @@ class MultipleGroupsGalleryView extends StatelessWidget {
   final String logTag;
   final Logger logger;
   final bool showSelectAllByDefault;
+  final bool isScrollablePositionedList;
 
   const MultipleGroupsGalleryView({
     required this.hugeListViewKey,
@@ -60,6 +61,7 @@ class MultipleGroupsGalleryView extends StatelessWidget {
     required this.logTag,
     required this.logger,
     required this.showSelectAllByDefault,
+    required this.isScrollablePositionedList,
     super.key,
   });
 
@@ -72,6 +74,7 @@ class MultipleGroupsGalleryView extends StatelessWidget {
       totalCount: groupedFiles.length,
       isDraggableScrollbarEnabled: groupedFiles.length > 10,
       disableScroll: disableScroll,
+      isScrollablePositionedList: isScrollablePositionedList,
       waitBuilder: (_) {
         return const EnteLoadingWidget();
       },

+ 3 - 0
lib/ui/viewer/gallery/gallery.dart

@@ -46,6 +46,7 @@ class Gallery extends StatefulWidget {
   final bool disableScroll;
   final bool limitSelectionToOne;
   final bool showSelectAllByDefault;
+  final bool isScrollablePositionedList;
 
   // add a Function variable to get sort value in bool
   final SortAscFn? sortAsyncFn;
@@ -69,6 +70,7 @@ class Gallery extends StatefulWidget {
     this.limitSelectionToOne = false,
     this.sortAsyncFn,
     this.showSelectAllByDefault = true,
+    this.isScrollablePositionedList = true,
     Key? key,
   }) : super(key: key);
 
@@ -242,6 +244,7 @@ class _GalleryState extends State<Gallery> {
         footer: widget.footer,
         selectedFiles: widget.selectedFiles,
         showSelectAllByDefault: widget.showSelectAllByDefault,
+        isScrollablePositionedList: widget.isScrollablePositionedList,
       ),
     );
   }