Coder Social home page Coder Social logo

flutter_long_list's Introduction

flutter_long_list

A Flutter LongList with Provider which supports ListView, GridView and Slivers

Features

  1. Support refresh & loadmore & error

  2. Use Provider4.x to manage list data, and use Selector can improve performanc

  3. Support ListView, GridView and Slivers

  4. Easy api, custom loading & nomore style

  5. Support List item exposure listener

  6. Support ScrollToIndex

Get started

Add it to your pubspec.yaml file:

  dependencies:
     flutter_long_list: 0.1.6

Install packages from the command line

  flutter packages get

If you like this package, consider supporting it by giving it a star on Github and a like on pub.dev ❤️

Basic Usage

Create a GridView By flutter_long_list

Step1 - Use ChangeNotifierProvider

 ChangeNotifierProvider<LongListProvider<T>>(
   create: (_) => LongListProvider<T>(),
   child: GridViewDemo(),
 );

Step2 - Init LongListProvider

·param id: list custom id is required.

·param pageSize: list load more need pagesize to request.

·param request: list load more function, offset = page * pageSize(page initialValue = 0).

@override
initState() {
  longListProvider.init(
    id: id,
    pageSize: 5,
    request: (int offset) async => await _getList(offset),
  );
  // your code...
}
// 'list' & 'total' & 'error' can not be modified. These field will use to loadmore.
Future<LongListData> _getList(int offset) async{
  final result = await api(0, 10);
  if (result['list'] != null) {
    return LongListData(
      list: result['list'],
      total: result['total']
    );
  } else {
    return LongListData(error: 'error');
  }
}

Step3 - Use LongList

·param id: list custom id you have inited.

·param gridDelegate: gridView property.

·param mode: enum LongListMode {list, grid, sliver_list, sliver_grid}

·param itemWidget: (BuildContext context, LongListProvider<T> provider, String id, int index, T data) { return your custom widget }

LongList<T>(
  id: id,
  gridDelegate: const SliverGridDelegateWithFixedCrossAxisCount(
    crossAxisCount: 2,
    crossAxisSpacing: 10.0,
    mainAxisSpacing: 10.0,
  ),
  padding: EdgeInsets.only(left: 10, right: 10),
  mode: LongListMode.grid,
  itemWidget: itemWidget,
)

Then you have finished to create a LongList GridView Widget easily!

Feature Use

1. You can use it to make your list data shared. Need to be determined the list has inited before use these functions. Please see ListView example

prvider.addItem(id, index, data); // add item
prvider.addItems(id, data); // add list
prvider.changeItem(id, index, data); // delete item
prvider.removeItem(id, index); // remove item
  • your data must have clone method, because provider Selector need.

2. If you want use exposure listener, only add exposureCallback

LongList<T>(
  id: id,
  gridDelegate: const SliverGridDelegateWithFixedCrossAxisCount(
    crossAxisCount: 2,
    crossAxisSpacing: 10.0,
    mainAxisSpacing: 10.0,
  ),
  padding: EdgeInsets.only(left: 10, right: 10),
  mode: LongListMode.grid,
  itemWidget: itemWidget,
  exposureCallback: (LongListProvider<T> provider, List<ToExposureItem> exposureList) {
    exposureList.forEach((item) {
      print('上报数据:${provider.list[item.index]} ${item.index} ${item.time}');
    });
  },
)

if use sliver mode, please must add 'sliverHeadHeight' param. It equals your sliverHead's expandedHeight.

3. Use ScrollToIndex

ScrollToIndexController scrollController = ScrollToIndexController();
scrollController.scrollToIndex(LongListProvider provider, String id, int index);

flutter_long_list's People

Stargazers

Abdullah Al Mashmoum avatar  avatar  avatar godaangel avatar  avatar

Watchers

James Cloos avatar  avatar

Forkers

godaangel

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.