Git Product home page Git Product logo

stellapath / very_good_infinite_list Goto Github PK

View Code? Open in Web Editor NEW

This project forked from verygoodopensource/very_good_infinite_list

0.0 0.0 0.0 1.64 MB

A Very Good Infinite List Widget created by Very Good Ventures. Great for activity feeds, news feeds, and more. ๐Ÿฆ„

Home Page: https://pub.dev/packages/very_good_infinite_list

License: MIT License

C++ 19.74% C 1.35% Objective-C 0.04% Kotlin 0.12% Dart 59.06% Swift 1.10% HTML 3.71% CMake 14.89%

very_good_infinite_list's Introduction

Very Good Infinite List

Very Good Ventures Very Good Ventures

Developed with ๐Ÿ’™ by Very Good Ventures ๐Ÿฆ„

ci coverage pub package License: MIT style: very good analysis


A library for easily displaying paginated data, created by Very Good Ventures.

InfiniteList comes in handy when building features like activity feeds, news feeds, or anywhere else where you need to lazily fetch and render content for users to consume.

Example

Usage

The InfiniteList API is very similar to that of ListView.builder. A basic implementation requires four parameters:

  • An itemCount that represents the amount of items that should be rendered using the itemBuilder.
  • An itemBuilder that is responsible for returning a widget for every index of the itemCount.
  • A hasReachedMax flag that indicates if any more data is available.
  • An onFetchData callback that's triggered whenever new data should be fetched.

Example

import 'package:flutter/material.dart';
import 'package:very_good_infinite_list/very_good_infinite_list.dart';

void main() => runApp(MyApp());

class MyApp extends StatefulWidget {
  @override
  _MyAppState createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  var _items = <String>[];
  var _isLoading = false;

  void _fetchData() async {
    setState(() {
      _isLoading = true;
    });

    await Future.delayed(const Duration(seconds: 1));

    if (!mounted) {
      return;
    }

    setState(() {
      _isLoading = false;
      _items = List.generate(_items.length + 10, (i) => 'Item $i');
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('Simple Example'),
      ),
      body: InfiniteList(
        itemCount: _items.length,
        isLoading: _isLoading,
        onFetchData: _fetchData,
        separatorBuilder: (context, index) => const Divider(),
        itemBuilder: (context, index) {
          return ListTile(
            dense: true,
            title: Text(_items[index]),
          );
        },
      ),
    );
  }
}

Customizations

InfiniteList

InfiniteList has multiple optional parameters which allow you to customize the loading and error behavior.

InfiniteList<String>(
  itemCount: 3,
  hasReachedMax: false,
  onFetchData: () => _fetchData(),
  itemBuilder: (context, index) => ListTile(title: Text('$index')),

  // An optional [ScrollController] this [InfiniteList] will attach to.
  // It's used to detect when the list has scrolled to the appropriate position
  // to call [onFetchData].
  //
  // Is optional and mostly used only for testing. If set to `null`, an
  // internal [ScrollController] is used instead.
  scrollController: _scrollController,

  // Indicates if new items are currently being loaded.
  //
  // While set to `true`, the [onFetchData] callback will not be triggered
  // and the [loadingBuilder] will be rendered.
  //
  // Is set to `false` by default.
  isLoading: false,

  // Indicates if an error has occurred.
  //
  // While set to `true`, the [onFetchData] callback will not be triggered
  // and the [errorBuilder] will be rendered.
  //
  // Is set to `false` by default.
  hasError: false,

  // Indicates if the list should be reversed.
  //
  // If set to `true`, the list of items, [loadingBuilder] and [errorBuilder]
  // will be rendered from bottom to top.
  //
  // Is set to `false` by default.
  reverse: false,

  // The duration with which calls to [onFetchData] will be debounced.
  //
  // Is set to a duration of 100 milliseconds by default.
  debounceDuration: const Duration(milliseconds: 100),

  // The offset, in pixels, that the internal [ScrollView] must be scrolled over
  // to trigger [onFetchData].
  // 
  // Defaults to the same as [RenderAbstractViewport.defaultCacheExtent], which is 250.
  cacheExtent: 250.0,

  // The amount of space by which to inset the list of items.
  //
  // Is optional and can be `null`.
  padding: const EdgeInsets.all(16.0),

  // An optional builder that's shown when the list of items is empty.
  //
  // If `null`, nothing is shown.
  emptyBuilder: (context) => const Center(child: Text('No items.')),
  
  // Flag used to center the empty builder, it is optional and defaults to false
  centerEmpty: true,

  // An optional builder that's shown at the end of the list when [isLoading]
  // is `true`.
  //
  // If `null`, a default builder is used that renders a centered
  // [CircularProgressIndicator].
  loadingBuilder: (context) => const Center(child: CircularProgressIndicator()),

  // Flag used to center the loading builder, it is optional and defaults to false
  centerLoading: true,

  // An optional builder that's shown when [hasError] is not `null`.
  //
  // If `null`, a default builder is used that renders the text `"Error"`.
  errorBuilder: (context) => const Center(child: Text('Error')),

  // Flag used to center the error builder, it is optional and defaults to false
  centerError: true,

  // An optional builder that, when provided, is used to show a widget in
  // between every pair of items.
  //
  // If the [itemBuilder] returns a [ListTile], this is commonly used to render
  // a [Divider] between every tile.
  //
  // Is optional and can be `null`.
  separatorBuilder: (context, index) => const Divider(),

  // An optional [Axis] to be used by the internal [ScrollView] that defines
  // the axis of scroll. 
  //
  // Is set to `Axis.vertical` by default.
  scrollDirection: Axis.vertical,

  // An optional [ScrollPhysics] to be used by the internal [ScrollView].
  //
  // Default to tha same as [ScrollView].
  physics: const AlwaysScrollableScrollPhysics(),
);

Refer to the example to see both basic and complex usage of InfiniteList.

very_good_infinite_list's People

Contributors

felangel avatar renancaraujo avatar alestiago avatar alardizabal avatar jeroen-meijer avatar tomarra avatar erickzanardo avatar henriquenas avatar cteq3132 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.