├── .gitignore ├── .metadata ├── CHANGELOG.md ├── LICENSE ├── README.md ├── android ├── app │ └── src │ │ └── main │ │ └── java │ │ └── io │ │ └── flutter │ │ └── plugins │ │ └── GeneratedPluginRegistrant.java └── local.properties ├── example └── README.md ├── example_screen1.gif ├── lib └── paging.dart ├── paging.iml ├── pubspec.lock ├── pubspec.yaml └── test └── paging_test.dart /.gitignore: -------------------------------------------------------------------------------- 1 | .DS_Store 2 | .dart_tool/ 3 | .idea/ 4 | 5 | .packages 6 | .pub/ 7 | 8 | example/* 9 | !example/README.md 10 | 11 | build/ 12 | ios/.generated/ 13 | ios/Flutter/Generated.xcconfig 14 | ios/Runner/GeneratedPluginRegistrant.* 15 | -------------------------------------------------------------------------------- /.metadata: -------------------------------------------------------------------------------- 1 | # This file tracks properties of this Flutter project. 2 | # Used by Flutter tool to assess capabilities and perform upgrades etc. 3 | # 4 | # This file should be version controlled and should not be manually edited. 5 | 6 | version: 7 | revision: 5391447fae6209bb21a89e6a5a6583cac1af9b4b 8 | channel: beta 9 | 10 | project_type: package 11 | -------------------------------------------------------------------------------- /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | ## [0.1.0] 2 | - Pagination works like a ListView.builder with all properties 3 | 4 | ## [0.0.2] 5 | - Example added 6 | 7 | ## [0.0.1] 8 | - Initial version 9 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2019 Joshua Matta 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # paging 2 | 3 | A Flutter package for paginating a list view 4 | 5 | [![pub package](https://img.shields.io/pub/v/paging.svg?style=popout)](https://pub.dartlang.org/packages/paging) 6 | 7 | ## Installation 8 | 9 | Add this to your package's pubspec.yaml file 10 | 11 | ```yaml 12 | dependencies: 13 | ... 14 | paging: ^latest.version.here 15 | ``` 16 | 17 | ## Usage 18 | First import paging.dart 19 | 20 | ```dart 21 | import 'package:paging/paging.dart'; 22 | ``` 23 | Simple to use. You can pass a type \ as a parameter to the widget, by default dynamic is assumed. 24 | 25 | There are two required parameters: 26 | - pageBuilder: requires a Future of List\ and gives you the current size of list 27 | - itemBuilder: requires a widget and gives you the item of type \ to be displayed 28 | 29 | ```dart 30 | 31 | // mocking a network call 32 | Future> pageData(int previousCount) async { 33 | await Future.delayed(Duration(seconds: 0, milliseconds: 2000)); 34 | List dummyList = List(); 35 | if (previousCount < 30) { 36 | // stop loading after 30 items 37 | for (int i = previousCount; i < previousCount + _COUNT; i++) { 38 | dummyList.add('Item $i'); 39 | } 40 | } 41 | return dummyList; 42 | } 43 | 44 | @override 45 | Widget build(BuildContext context) { 46 | return Scaffold( 47 | appBar: AppBar(title: Text('Pagination List')), 48 | body: Pagination( 49 | pageBuilder: (currentSize) => pageData(currentSize), 50 | itemBuilder: (index, item){ 51 | return Container( 52 | color: Colors.yellow, 53 | height: 48, 54 | child: Text(item), 55 | ); 56 | }, 57 | ), 58 | ); 59 | } 60 | ``` 61 | 62 | ## Screenshots 63 | 64 | 65 | 66 | ## Getting Started 67 | 68 | This project is a starting point for a Dart 69 | [package](https://flutter.io/developing-packages/), 70 | a library module containing code that can be shared easily across 71 | multiple Flutter or Dart projects. 72 | 73 | For help getting started with Flutter, view our 74 | [online documentation](https://flutter.io/docs), which offers tutorials, 75 | samples, guidance on mobile development, and a full API reference. 76 | -------------------------------------------------------------------------------- /android/app/src/main/java/io/flutter/plugins/GeneratedPluginRegistrant.java: -------------------------------------------------------------------------------- 1 | package io.flutter.plugins; 2 | 3 | import io.flutter.plugin.common.PluginRegistry; 4 | 5 | /** 6 | * Generated file. Do not edit. 7 | */ 8 | public final class GeneratedPluginRegistrant { 9 | public static void registerWith(PluginRegistry registry) { 10 | if (alreadyRegisteredWith(registry)) { 11 | return; 12 | } 13 | } 14 | 15 | private static boolean alreadyRegisteredWith(PluginRegistry registry) { 16 | final String key = GeneratedPluginRegistrant.class.getCanonicalName(); 17 | if (registry.hasPlugin(key)) { 18 | return true; 19 | } 20 | registry.registrarFor(key); 21 | return false; 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /android/local.properties: -------------------------------------------------------------------------------- 1 | sdk.dir=D:\\Programs\\Android\\sdk 2 | flutter.sdk=D:\\Programs\\flutter 3 | flutter.versionName=0.0.1 -------------------------------------------------------------------------------- /example/README.md: -------------------------------------------------------------------------------- 1 | # example 2 | 3 | Example for paging library 4 | 5 | ```dart 6 | import 'package:flutter/material.dart'; 7 | import 'package:paging/paging.dart'; 8 | 9 | void main() => runApp(MyApp()); 10 | 11 | class MyApp extends StatelessWidget { 12 | // This widget is the root of your application. 13 | @override 14 | Widget build(BuildContext context) { 15 | return MaterialApp( 16 | title: 'Flutter Demo', 17 | theme: ThemeData( 18 | primarySwatch: Colors.red, 19 | ), 20 | home: MyHomePage(title: 'Paging Example'), 21 | ); 22 | } 23 | } 24 | 25 | class MyHomePage extends StatefulWidget { 26 | MyHomePage({Key key, this.title}) : super(key: key); 27 | 28 | final String title; 29 | 30 | @override 31 | _MyHomePageState createState() => _MyHomePageState(); 32 | } 33 | 34 | class _MyHomePageState extends State { 35 | static const int _COUNT = 10; 36 | 37 | // mocking a network call 38 | Future> pageData(int previousCount) async { 39 | await Future.delayed(Duration(seconds: 0, milliseconds: 2000)); 40 | List dummyList = List(); 41 | if (previousCount < 30) { 42 | // stop loading after 30 items 43 | for (int i = previousCount; i < previousCount + _COUNT; i++) { 44 | dummyList.add('Item $i'); 45 | } 46 | } 47 | return dummyList; 48 | } 49 | 50 | @override 51 | Widget build(BuildContext context) { 52 | return Scaffold( 53 | appBar: AppBar(title: Text(widget.title)), 54 | body: Pagination( 55 | pageBuilder: (currentListSize) => pageData(currentListSize), 56 | itemBuilder: (item) => ListTile(title: Text(item)), 57 | ), 58 | ); 59 | } 60 | } 61 | ``` 62 | -------------------------------------------------------------------------------- /example_screen1.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jajpa/paging_library/8110028e3690854a52bd445db72938d85721d801/example_screen1.gif -------------------------------------------------------------------------------- /lib/paging.dart: -------------------------------------------------------------------------------- 1 | library paging; 2 | 3 | import 'dart:async'; 4 | 5 | import 'package:flutter/material.dart'; 6 | 7 | /// Signature for a function that returns a Future List of type 'T' i.e. list 8 | /// of items in a particular page that is being asynchronously called. 9 | /// 10 | /// Used by [Pagination] widget. 11 | typedef PaginationBuilder = Future> Function(int currentListSize); 12 | 13 | /// Signature for a function that creates a widget for a given item of type 'T'. 14 | typedef ItemWidgetBuilder = Widget Function(int index, T item); 15 | 16 | /// A scrollable list which implements pagination. 17 | /// 18 | /// When scrolled to the end of the list [Pagination] calls [pageBuilder] which 19 | /// must be implemented which returns a Future List of type 'T'. 20 | /// 21 | /// [itemBuilder] creates widget instances on demand. 22 | class Pagination extends StatefulWidget { 23 | /// Creates a scrollable, paginated, linear array of widgets. 24 | /// 25 | /// The arguments [pageBuilder], [itemBuilder] must not be null. 26 | Pagination({ 27 | Key key, 28 | @required this.pageBuilder, 29 | @required this.itemBuilder, 30 | this.scrollDirection = Axis.vertical, 31 | this.progress, 32 | this.onError, 33 | this.reverse = false, 34 | this.controller, 35 | this.primary, 36 | this.physics, 37 | this.padding, 38 | this.itemExtent, 39 | this.cacheExtent, 40 | this.semanticChildCount, 41 | }) : assert(pageBuilder != null), 42 | assert(itemBuilder != null), 43 | super(key: key); 44 | 45 | /// Called when the list scrolls to an end 46 | /// 47 | /// Function should return Future List of type 'T' 48 | final PaginationBuilder pageBuilder; 49 | 50 | /// Called to build children for [Pagination] 51 | /// 52 | /// Function should return a widget 53 | final ItemWidgetBuilder itemBuilder; 54 | 55 | /// Scroll direction of list view 56 | final Axis scrollDirection; 57 | 58 | /// When non-null [progress] widget is called to show loading progress 59 | final Widget progress; 60 | 61 | /// Handle error returned by the Future implemented in [pageBuilder] 62 | final Function(dynamic error) onError; 63 | 64 | final bool reverse; 65 | final ScrollController controller; 66 | final bool primary; 67 | final ScrollPhysics physics; 68 | final bool shrinkWrap = false; 69 | final EdgeInsetsGeometry padding; 70 | final double itemExtent; 71 | final bool addAutomaticKeepAlives = true; 72 | final bool addRepaintBoundaries = true; 73 | final bool addSemanticIndexes = true; 74 | final double cacheExtent; 75 | final int semanticChildCount; 76 | 77 | @override 78 | _PaginationState createState() => _PaginationState(); 79 | } 80 | 81 | class _PaginationState extends State> { 82 | final List _list = List(); 83 | bool _isLoading = false; 84 | bool _isEndOfList = false; 85 | 86 | void fetchMore() { 87 | if (!_isLoading) { 88 | _isLoading = true; 89 | widget.pageBuilder(_list.length).then((list) { 90 | _isLoading = false; 91 | if (list.isEmpty) { 92 | _isEndOfList = true; 93 | } 94 | setState(() { 95 | _list.addAll(list); 96 | }); 97 | }).catchError((error) { 98 | setState(() { 99 | _isEndOfList = true; 100 | }); 101 | print(error); 102 | if (widget.onError != null) { 103 | widget.onError(error); 104 | } 105 | }); 106 | } 107 | } 108 | 109 | @override 110 | void initState() { 111 | super.initState(); 112 | fetchMore(); 113 | } 114 | 115 | @override 116 | Widget build(BuildContext context) { 117 | return ListView.builder( 118 | padding: widget.padding, 119 | controller: widget.controller, 120 | physics: widget.physics, 121 | primary: widget.primary, 122 | reverse: widget.reverse, 123 | shrinkWrap: widget.shrinkWrap, 124 | itemExtent: widget.itemExtent, 125 | cacheExtent: widget.cacheExtent, 126 | addAutomaticKeepAlives: widget.addAutomaticKeepAlives, 127 | addRepaintBoundaries: widget.addRepaintBoundaries, 128 | addSemanticIndexes: widget.addSemanticIndexes, 129 | scrollDirection: widget.scrollDirection, 130 | itemBuilder: (context, position) { 131 | if (position < _list.length) { 132 | return widget.itemBuilder(position, _list[position]); 133 | } else if (position == _list.length && !_isEndOfList) { 134 | fetchMore(); 135 | return widget.progress ?? defaultLoading(); 136 | } 137 | return null; 138 | }, 139 | ); 140 | } 141 | 142 | Widget defaultLoading() { 143 | return Align( 144 | child: SizedBox( 145 | height: 40, 146 | width: 40, 147 | child: Padding( 148 | padding: const EdgeInsets.all(8), 149 | child: CircularProgressIndicator(), 150 | ), 151 | ), 152 | ); 153 | } 154 | } 155 | -------------------------------------------------------------------------------- /paging.iml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | -------------------------------------------------------------------------------- /pubspec.lock: -------------------------------------------------------------------------------- 1 | # Generated by pub 2 | # See https://www.dartlang.org/tools/pub/glossary#lockfile 3 | packages: 4 | async: 5 | dependency: transitive 6 | description: 7 | name: async 8 | url: "https://pub.dartlang.org" 9 | source: hosted 10 | version: "2.0.8" 11 | boolean_selector: 12 | dependency: transitive 13 | description: 14 | name: boolean_selector 15 | url: "https://pub.dartlang.org" 16 | source: hosted 17 | version: "1.0.4" 18 | charcode: 19 | dependency: transitive 20 | description: 21 | name: charcode 22 | url: "https://pub.dartlang.org" 23 | source: hosted 24 | version: "1.1.2" 25 | collection: 26 | dependency: transitive 27 | description: 28 | name: collection 29 | url: "https://pub.dartlang.org" 30 | source: hosted 31 | version: "1.14.11" 32 | flutter: 33 | dependency: "direct main" 34 | description: flutter 35 | source: sdk 36 | version: "0.0.0" 37 | flutter_test: 38 | dependency: "direct dev" 39 | description: flutter 40 | source: sdk 41 | version: "0.0.0" 42 | matcher: 43 | dependency: transitive 44 | description: 45 | name: matcher 46 | url: "https://pub.dartlang.org" 47 | source: hosted 48 | version: "0.12.3+1" 49 | meta: 50 | dependency: transitive 51 | description: 52 | name: meta 53 | url: "https://pub.dartlang.org" 54 | source: hosted 55 | version: "1.1.6" 56 | path: 57 | dependency: transitive 58 | description: 59 | name: path 60 | url: "https://pub.dartlang.org" 61 | source: hosted 62 | version: "1.6.2" 63 | quiver: 64 | dependency: transitive 65 | description: 66 | name: quiver 67 | url: "https://pub.dartlang.org" 68 | source: hosted 69 | version: "2.0.1" 70 | sky_engine: 71 | dependency: transitive 72 | description: flutter 73 | source: sdk 74 | version: "0.0.99" 75 | source_span: 76 | dependency: transitive 77 | description: 78 | name: source_span 79 | url: "https://pub.dartlang.org" 80 | source: hosted 81 | version: "1.4.1" 82 | stack_trace: 83 | dependency: transitive 84 | description: 85 | name: stack_trace 86 | url: "https://pub.dartlang.org" 87 | source: hosted 88 | version: "1.9.3" 89 | stream_channel: 90 | dependency: transitive 91 | description: 92 | name: stream_channel 93 | url: "https://pub.dartlang.org" 94 | source: hosted 95 | version: "1.6.8" 96 | string_scanner: 97 | dependency: transitive 98 | description: 99 | name: string_scanner 100 | url: "https://pub.dartlang.org" 101 | source: hosted 102 | version: "1.0.4" 103 | term_glyph: 104 | dependency: transitive 105 | description: 106 | name: term_glyph 107 | url: "https://pub.dartlang.org" 108 | source: hosted 109 | version: "1.0.1" 110 | test_api: 111 | dependency: transitive 112 | description: 113 | name: test_api 114 | url: "https://pub.dartlang.org" 115 | source: hosted 116 | version: "0.2.1" 117 | typed_data: 118 | dependency: transitive 119 | description: 120 | name: typed_data 121 | url: "https://pub.dartlang.org" 122 | source: hosted 123 | version: "1.1.6" 124 | vector_math: 125 | dependency: transitive 126 | description: 127 | name: vector_math 128 | url: "https://pub.dartlang.org" 129 | source: hosted 130 | version: "2.0.8" 131 | sdks: 132 | dart: ">=2.0.0 <3.0.0" 133 | -------------------------------------------------------------------------------- /pubspec.yaml: -------------------------------------------------------------------------------- 1 | name: paging 2 | description: A Flutter package for paginating a list view from network or local storage 3 | version: 0.1.0 4 | author: Joshua Matta 5 | homepage: https://github.com/joshmatta/paging_library 6 | 7 | environment: 8 | sdk: ">=2.0.0-dev.68.0 <3.0.0" 9 | 10 | dependencies: 11 | flutter: 12 | sdk: flutter 13 | 14 | dev_dependencies: 15 | flutter_test: 16 | sdk: flutter 17 | 18 | -------------------------------------------------------------------------------- /test/paging_test.dart: -------------------------------------------------------------------------------- 1 | import 'package:flutter_test/flutter_test.dart'; 2 | 3 | import 'package:paging/paging.dart'; 4 | 5 | void main() { 6 | //TODO add test cases 7 | } 8 | --------------------------------------------------------------------------------