瀏覽代碼

Simplify huge listview by removing the notion of pages

Vishnu Mohandas 4 年之前
父節點
當前提交
d426aabb1b
共有 2 個文件被更改,包括 10 次插入34 次删除
  1. 7 18
      lib/ui/gallery.dart
  2. 3 16
      lib/ui/huge_listview/huge_listview.dart

+ 7 - 18
lib/ui/gallery.dart

@@ -1,5 +1,4 @@
 import 'dart:async';
-import 'dart:math';
 
 import 'package:flutter/cupertino.dart';
 import 'package:flutter/material.dart';
@@ -42,13 +41,12 @@ class Gallery extends StatefulWidget {
 }
 
 class _GalleryState extends State<Gallery> {
-  static const int kPageSize = 10;
   static const int kInitialLoadLimit = 100;
 
   final _hugeListViewKey = GlobalKey<HugeListViewState>();
 
   Logger _logger;
-  int _pageIndex = 0;
+  int _index = 0;
   List<List<File>> _collatedFiles = [];
   bool _hasLoadedFiles = false;
   StreamSubscription<FilesUpdatedEvent> _reloadEventSubscription;
@@ -126,26 +124,17 @@ class _GalleryState extends State<Gallery> {
     return HugeListView<List<File>>(
       key: _hugeListViewKey,
       controller: ItemScrollController(),
-      pageSize: kPageSize,
-      startIndex: _pageIndex,
+      startIndex: _index,
       totalCount: _collatedFiles.length,
       isDraggableScrollbarEnabled: _collatedFiles.length > 30,
-      page: (pageIndex) {
-        _pageIndex = pageIndex;
-        final endTimeIndex =
-            min(pageIndex * kPageSize, _collatedFiles.length - 1);
-        final startTimeIndex =
-            min((pageIndex + 1) * kPageSize, _collatedFiles.length - 1);
-        return _collatedFiles.sublist(endTimeIndex, startTimeIndex);
-      },
-      placeholderBuilder: (context, pageIndex) {
-        var day = getDayWidget(_collatedFiles[pageIndex][0].creationTime);
+      placeholderBuilder: (context, index) {
+        var day = getDayWidget(_collatedFiles[index][0].creationTime);
         return Padding(
           padding: const EdgeInsets.only(bottom: 12),
           child: Column(
             children: [
               day,
-              PlaceHolderWidget(_collatedFiles[pageIndex].length),
+              PlaceHolderWidget(_collatedFiles[index].length),
             ],
           ),
         );
@@ -156,10 +145,10 @@ class _GalleryState extends State<Gallery> {
       emptyResultBuilder: (_) {
         return nothingToSeeHere;
       },
-      itemBuilder: (context, index, files) {
+      itemBuilder: (context, index) {
         var gallery;
         gallery = LazyLoadingGallery(
-          files,
+          _collatedFiles[index],
           widget.reloadEvent,
           widget.asyncLoader,
           widget.selectedFiles,

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

@@ -5,9 +5,8 @@ import 'package:flutter/scheduler.dart';
 import 'package:photos/ui/huge_listview/draggable_scrollbar.dart';
 import 'package:scrollable_positioned_list/scrollable_positioned_list.dart';
 
-typedef HugeListViewPageFuture<T> = List<T> Function(int pageIndex);
 typedef HugeListViewItemBuilder<T> = Widget Function(
-    BuildContext context, int index, T entry);
+    BuildContext context, int index);
 typedef HugeListViewErrorBuilder = Widget Function(
     BuildContext context, dynamic error);
 
@@ -15,18 +14,12 @@ class HugeListView<T> extends StatefulWidget {
   /// A [ScrollablePositionedList] controller for jumping or scrolling to an item.
   final ItemScrollController controller;
 
-  /// Size of the page. [HugeListView] only keeps a few pages of items in memory any time.
-  final int pageSize;
-
   /// Index of an item to initially align within the viewport.
   final int startIndex;
 
   /// Total number of items in the list.
   final int totalCount;
 
-  /// Called to build items for the list with the specified [pageIndex].
-  final HugeListViewPageFuture<T> page;
-
   /// Called to build the thumb. One of [DraggableScrollbarThumbs.RoundedRectThumb], [DraggableScrollbarThumbs.ArrowThumb]
   /// or [DraggableScrollbarThumbs.SemicircleThumb], or build your own.
   final String Function(int) labelTextBuilder;
@@ -67,10 +60,8 @@ class HugeListView<T> extends StatefulWidget {
   HugeListView({
     Key key,
     this.controller,
-    @required this.pageSize,
     @required this.startIndex,
     @required this.totalCount,
-    @required this.page,
     @required this.labelTextBuilder,
     @required this.itemBuilder,
     @required this.placeholderBuilder,
@@ -83,8 +74,7 @@ class HugeListView<T> extends StatefulWidget {
     this.thumbDrawColor = Colors.grey,
     this.thumbHeight = 48.0,
     this.isDraggableScrollbarEnabled = true,
-  })  : assert(pageSize > 0),
-        assert(velocityThreshold >= 0),
+  })  : assert(velocityThreshold >= 0),
         super(key: key);
 
   @override
@@ -158,10 +148,7 @@ class HugeListViewState<T> extends State<HugeListView<T>> {
             itemCount: max(widget.totalCount, 0),
             itemBuilder: (context, index) {
               if (!Scrollable.recommendDeferredLoadingForContext(context)) {
-                final page = index ~/ widget.pageSize;
-                final pageResult = widget.page(page);
-                final value = pageResult?.elementAt(index % widget.pageSize);
-                return widget.itemBuilder(context, index, value);
+                return widget.itemBuilder(context, index);
               } else if (!_frameCallbackInProgress) {
                 _frameCallbackInProgress = true;
                 SchedulerBinding.instance