Feed is a Flutter package that enables developers to create dynamic feeds of content that lazy load when they reach the bottom. This package simplifies the process of managing and displaying large sets of data in a Flutter app. With Feed, you can efficiently load and display data as the user scrolls through a feed, providing a smooth and responsive user experience.
Installation: Add feed to your pubspec.yaml file under dependencies:
dependencies:
feed: ^1.0.0 # Replace with the latest versionThe FeedController is a fundamental component of the Feed package, responsible for managing your feed's data and controlling its behavior. It provides methods and properties to interact with the feed, including refreshing the feed, loading more items, and managing the feed's content.
To get started, you'll need to create an instance of FeedController and configure it with the necessary parameters, such as the data fetching function and feed settings.
import 'package:flutter/material.dart';
import 'package:feed/feed.dart';
class MyFeed extends StatefulWidget {
@override
_MyFeedState createState() => _MyFeedState();
}
class _MyFeedState extends State<MyFeed> {
// Create a FeedController for your data type (e.g., String)
final FeedController<String> _feedController = FeedController<String>(
fetch: (offset, limit) async {
// Implement your data fetching logic here
// Return a List of data to be added to the feed
return [];
},
limit: 20, // Set the limit for each query
);
@override
void initState() {
super.initState();
// Initialize the feed controller
_feedController.init();
}
@override
void dispose() {
// Dispose of the feed controller when it's no longer needed
_feedController.dispose();
super.dispose();
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Feed Example'),
),
body: FeedListView<String>(
controller: _feedController,
builder: (context, item) {
// Build your feed item widget here
return ListTile(
title: Text(item),
);
},
),
);
}
}The FeedController is a powerful tool for managing feeds in your Flutter app. It provides various parameters that allow you to customize and control the behavior of your feed.
-
Type:
List<Future<List<T>> Function(int offset, int limit)>The
fetchparameter is a required parameter that defines the data fetching function for your feed. It should be a list of functions that return a list of data when invoked. These functions are responsible for querying and fetching data based on theoffsetandlimitprovided.
-
Type:
int -
Default:
20The
limitparameter specifies the maximum number of items to fetch and display per query. Adjust this value to control how many items are loaded at a time. The default limit is set to 20.
-
Type:
List<T>? -
Default:
nullThe
startsWithparameter allows you to provide an initial list of items to populate the feed. This is useful when you want to start the feed with some predefined content.
-
Type:
int Function(T a, T b)? -
Default:
nullThe
sortparameter is a function that defines how the data should be sorted once it's fetched. You can provide a custom sorting function to order the feed items as needed.
-
Type:
FeedFilter<T>? -
Default:
nullThe
filterparameter allows you to apply a filter to incoming elements based on a provided filter function. If specified, the filter function decides whether to include or exclude an element in the feed.
-
Type:
bool -
Default:
falseThe
initializedparameter determines whether the feed controller is initialized. It is set totrueafter callinginit()on the controller.
-
Type:
ScrollController? -
Default:
nullThe
scrollControllerparameter allows you to attach a customScrollControllerto your feed. You can use this controller to interact with the scroll behavior of the feed.
-
Type:
bool -
Default:
trueThe
startsWithLoadingparameter specifies whether the feed should start with a loading state. When set totrue, the feed will display a loading indicator while the initial data is being fetched.
-
Type:
Widget Function(BuildContext)? -
Default:
nullThe
loadingItemBuilderparameter allows you to customize the loading indicator widget displayed in the feed. Provide a function that returns the loading widget.
-
Type:
bool -
Default:
trueThe
pagingEnabledparameter determines whether the feed supports paging. When set totrue, the feed will automatically load more data as the user scrolls to the bottom. Disable this to implement custom paging behavior.
-
Type:
bool -
Default:
falseThe
refreshOnAppResumedparameter controls whether the feed should refresh its content when the app is resumed after being in the background. Set it totrueto enable this behavior.
-
Type:
bool -
Default:
trueThe
initialLoadparameter specifies whether the feed should load data immediately after initialization. When set totrue, the feed will initiate the initial data load.
-
Type:
bool -
Default:
trueThe
refreshOnPullDownparameter determines whether the feed should refresh its content when the user pulls down on the feed view. Set it totrueto enable pull-to-refresh functionality.
-
Type:
bool -
Default:
trueThe
loadOnRefreshparameter controls whether the feed should automatically load more data when the user performs a refresh action. Set it totrueto enable loading on refresh.
-
Type:
ScrollController? -
Default:
nullThe
scrollControllerparameter allows you to attach a customScrollControllerto your feed. You can use this controller to interact with the scroll behavior of the feed.
-
Type:
void Function(dynamic)? -
Default:
nullThe
onErrorparameter is a callback function that is triggered when an error occurs during data fetching. You can use it to handle and log errors.
These parameters provide fine-grained control over how your feed behaves and how it interacts with your app's user interface.
For more advanced usage and customization, refer to the package documentation and code comments.