Giter Site home page Giter Site logo

nain93 / flat_list Goto Github PK

View Code? Open in Web Editor NEW

This project forked from hyochan/flat_list

0.0 0.0 0.0 342 KB

Flutter's [FlatList] widget for React Native friendly developers

License: MIT License

Shell 1.27% C++ 27.83% C 2.46% Objective-C 0.78% Java 0.68% Kotlin 0.17% Dart 38.53% Swift 1.58% HTML 2.23% CMake 24.48%

flat_list's Introduction

Pub Version CI

[FlatList] widget in Flutter which will be familiar to React Native developers.

Motivation

While there are opinionated ways to build listviews in React Native, there are many ways to build listviews in Flutter. In Flutter, we can use ListView, ListView.builder(), SliverList and also when you want to make list with more than one column, you need to use GridView, SliverGrid and so on.

By providing FlatList widget in Flutter, we can move faster on implementing the ListView we want.

Installation

flutter pub add flat_list

Usage

You'll easily understand by looking at below code.

FlatList(
  onEndReached: () async {
    loading.value = true;
    await Future.delayed(const Duration(seconds: 2));
    if (context.mounted) {
      items.value += getMoreData();
      loading.value = false;
    }
  },
  onRefresh: () async {
    await Future.delayed(const Duration(seconds: 2));

    if (context.mounted) {
      items.value = data;
    }
  },
  loading: loading.value,
  listHeaderWidget: const Header(),
  listFooterWidget: const Footer(),
  listEmptyWidget: Container(
    alignment: Alignment.center,
    padding: const EdgeInsets.all(12),
    child: const Text('List is empty!'),
  ),
  data: items.value,
  buildItem: (item, index) {
    var person = items.value[index];

    return ListItemView(person: person);
  },
)

More about the differences in props compared to React Native's FlatList are listed below.

Flutter React Native Required
data data โœ“
buildItem renderItem โœ“
listHeaderWidget ListHeaderComponent
listFooterWidget ListFooterComponent
listEmptyWidget ListEmptyComponent
onRefresh onRefresh
onEndReached onEndReached
`` refreshing
loading loading
numColumns numColumns
onEndReachedDelta onEndReachedThreshold
controller ``
inverted inverted

Basic setup

The complete example is available here.

FlatList requires you to provide data and buildItem:

FlatList(
  data: items.value,
  buildItem: (item, index) {
    var person = items.value[index];

    return ListItemView(person: person);
  },
)

Adding additional views

You can provide header and footer in [FlatList]. When providing listEmptyWidget, it will be rendered when the list of data is empty.

listHeaderWidget: const Header(), // Provider any header
listFooterWidget: const Footer(), // Provider any footer
listEmptyWidget: Container(
  alignment: Alignment.center,
  padding: const EdgeInsets.all(12),
  child: const Text('List is empty!'),
),

Refresh indicator

Providing onRefresh will add [RefreshIndicator]. Therefore you can refresh the data.

onRefresh: () async {
  await Future.delayed(const Duration(seconds: 2));

  if (context.mounted) {
    items.value = data;
  }
},

Infinite scroll

Infinite scrolling is possible using onEndReached. You should also provide loading to use this feature correctly.

loading: loading.value,
onEndReached: () async {
  loading.value = true;
  await Future.delayed(const Duration(seconds: 2));
  if (context.mounted) {
    items.value += getMoreData();
    loading.value = false;
  }
},

GridView

Just by giving numColumns value greater than 1, it will render [GridView].

numColums: 3, // Value greater than 1
),

One column

One column Multiple Columns

Demo

Examples are provided in /example folder.

TODO

  • Support optional horizontal mode

  • Separator support in ListView

  • Expose scroll controller: Ability to control similar functionalities listed below.

    - [scrollToEnd](https://reactnative.dev/docs/flatlist#scrolltoend)
    - [scrollToIndex](https://reactnative.dev/docs/flatlist#scrolltoindex)
    - [scrollToItem](https://reactnative.dev/docs/flatlist#scrolltoitem)
    - [scrollToOffset](https://reactnative.dev/docs/flatlist#scrolltooffset)
    
  • Support inverted

  • Enhance onEndReachedDelta with similar to onEndReachedThreshold

  • Test coverage

Additional information

Read our blog

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.