RxPaginatedBuilder<B extends RxBlocTypeBase, T> class
RxPaginatedBuilder provides the flexibility and simplicity of presentation of paginated data with the use of RxBlocs inside the RxBloc ecosystem. It was created with the intention of presenting a list of data that can be loaded once that list has reached its end, or can be refreshed using a pull-to-refresh feature (see RxPaginatedBuilder.withRefreshIndicator).
In order to make use of the RxPaginatedBuilder, you first need to specify the following required parameters:
- state is the state of the BLoC that will be listened for changes. The
state is a
Stream<PaginatedList<T>>
where T is the type of the data that is being paginated. - buildSuccess is a callback which is invoked when the state stream produces PaginatedList with non empty PaginatedList.list or all further data loads, such as next page loads.
- buildError Callback which is invoked when the state stream produces PaginatedList where PaginatedList.error is not null.
- buildLoading is a callback which is invoked when the state stream produces PaginatedList where PaginatedList.isInitialLoading is true.
- onBottomScrolled is a callback that is executed once the end of the list is reached and there is next page. This can be, for instance, used for fetching the next page of data.
RxPaginatedBuilder also comes with additional optional parameters that can be adjusted to you needs.
The wrapperBuilder method is a builder method with the intention of
creating a wrapper widget around the child widget built using the main
buildSuccess method. The wrapperBuilder method gives you access to the
BuildContext
, BLoC
containing the state that is listened and the
Widget
that is build with the builder
method. This method can be used
for adding additional functionality or help in cases when the built child
widget is needed beforehand.
You can manage the execution of the onBottomScrolled parameter by enabling or disabling it via the enableOnBottomScrolledCallback.
Additionally, you can define the minimum scroll threshold which will execute the onBottomScrolled callback by changing the value of scrollThreshold. The default value of the scroll threshold is 100 pixels.
The RxPaginatedBuilder also provides the ability to react to scrolling via the onScrolled callback, with a parameter telling whether the user is or has stopped scrolling.
There may be cases where you have a reference to the BLoC that is used by the RxPaginatedBuilder. By specifying the bloc parameter you remove the need to perform a lookup for that BLoC in the widget tree, improving the performance by a small bit.
Here is an example of what a RxPaginatedBloc using a UserBloc looks like:
RxPaginatedBuilder<UserBlocType, User>(
state: (bloc) => bloc.states.paginatedUsers,
onBottomScrolled: (bloc) => bloc.events.loadNextPage(),
buildSuccess: (context, list, bloc) => ListView.builder(
itemBuilder: (context, index) {
final user = list.getItem(index);
if (user == null) {
return const YourProgressIndicator();
}
return YourListTile(user: user);
},
itemCount: list.itemCount,
),
buildLoading: (context, list, bloc) =>
const YourProgressIndicator(),
buildError: (context, list, bloc) =>
YourErrorWidget(error: list.error!),
);
Sometimes, you may want to have a working pagination and pull-to-refresh without spending too much time on it. Using the RxPaginatedBuilder.withRefreshIndicator gives you access to a Refresh Indicator straight out of the box.
Along with the required parameters of the default implementation,
RxPaginatedBuilder.withRefreshIndicator gets rid of the wrapperBuilder
but introduces a new required parameter onRefresh
. The onRefresh
callback is triggered once a pull-to-refresh has been performed. The
callback, containing the BLoC as a parameter, should return a future, which
once complete will make the refresh indicator disappear.
Here is an example of what a RxPaginatedBloc using a UserBloc looks like using the withRefreshIndicator constructor :
RxPaginatedBuilder<UserBlocType, User>.withRefreshIndicator(
state: (bloc) => bloc.states.paginatedList,
onBottomScrolled: (bloc) => bloc.events.loadPage(),
onRefresh: (bloc) async {
bloc.events.loadPage(reset: true);
return bloc.states.refreshDone;
},
buildSuccess: (context, list, bloc) => ListView.builder(
itemBuilder: (context, index) {
final user = list.getItem(index);
if (user == null) {
return const YourProgressIndicator();
}
return YourListTile(user: user);
},
itemCount: list.itemCount,
),
buildLoading: (context, list, bloc) =>
const YourProgressIndicator(),
buildError: (context, list, bloc) =>
YourErrorWidget(error: list.error!),
)
- Inheritance
-
- Object
- DiagnosticableTree
- Widget
- StatefulWidget
- RxPaginatedBuilder
Constructors
-
RxPaginatedBuilder({required Stream<
PaginatedList< state(B), required Widget buildSuccess(BuildContext, PaginatedList<T> >T> , B), required Widget buildError(BuildContext, PaginatedList<T> , B), required Widget buildLoading(BuildContext, PaginatedList<T> ?, B), required void onBottomScrolled(B), Widget wrapperBuilder(BuildContext, B, Widget)?, dynamic onScrolled(bool)?, double scrollThreshold = 100.0, bool enableOnBottomScrolledCallback = true, B? bloc, Key? key}) -
RxPaginatedBuilder default constructor
const
-
RxPaginatedBuilder.withRefreshIndicator({required Stream<
PaginatedList< state(B), required Widget buildSuccess(BuildContext, PaginatedList<T> >T> , B), required Widget buildError(BuildContext, PaginatedList<T> , B), required Widget buildLoading(BuildContext, PaginatedList<T> ?, B), required Future<void> onRefresh(B), required void onBottomScrolled(B), dynamic onScrolled(bool)?, B? bloc, double scrollThreshold = 100, bool enableOnBottomScrolledCallback = true}) -
RxPaginatedBuilder constructor with refresh indicator.
An addition to the default constructor is the requirement for the
onRefresh
callback which will be executed once the refreshed using the pull-down to refresh feature.factory
Properties
- bloc → B?
-
The bloc (Business Logic of Component) that is used. If none specified
a lookup is performed in the widget tree to find the nearest ancestor.
final
-
buildError
→ Widget Function(BuildContext, PaginatedList<
T> , B) -
Callback which is invoked when the state stream produces PaginatedList
where PaginatedList.error is not null.
final
-
buildLoading
→ Widget Function(BuildContext, PaginatedList<
T> ?, B) -
Callback which is invoked when the state stream produces PaginatedList
where PaginatedList.isInitialLoading is true.
final
-
buildSuccess
→ Widget Function(BuildContext, PaginatedList<
T> , B) -
Callback which is invoked when the state stream produces PaginatedList
with non empty PaginatedList.list or all further data loads, such as
next page loads.
final
- enableOnBottomScrolledCallback → bool
-
When set to true onBottomScrolled is executed once the user scrolls at
the bottom of the list.
final
- hashCode → int
-
The hash code for this object.
no setterinherited
- key → Key?
-
Controls how one widget replaces another widget in the tree.
finalinherited
- onBottomScrolled → void Function(B)
-
Callback triggered once the user gets to the bottom of the list while
scrolling when a threshold has been passed. This callback is triggered
only if the enableOnBottomScrolledCallback is set to true and the
fetched items are less than the total count.
final
- onScrolled → (dynamic Function(bool)?)
-
Optional callback triggered while the user is scrolling the list.
final
- runtimeType → Type
-
A representation of the runtime type of the object.
no setterinherited
- scrollThreshold → double
-
Minimum threshold (defaults to 100 pixels) that needs to be passed in
order for the onBottomScrolled to be triggered.
final
-
state
→ Stream<
PaginatedList< Function(B)T> > -
The state of the BLoC to listen to for changes in data. The state is
represented as a Stream of PaginatedList, which contains individual
items.
final
- wrapperBuilder → (Widget Function(BuildContext, B, Widget)?)
-
Optional builder method that is intended for creation of a wrapper widget
on top of the widget built using the buildSuccess method.
This can be handy when implementing onRefresh or any other features
requiring the widget beforehand (for example for building a
RefreshIndicator around the widget).
final
Methods
-
createElement(
) → StatefulElement -
Creates a StatefulElement to manage this widget's location in the tree.
inherited
-
createState(
) → RxPaginatedBuilderState< B, T> -
Creates the mutable state for this widget at a given location in the tree.
override
-
debugDescribeChildren(
) → List< DiagnosticsNode> -
Returns a list of
DiagnosticsNode
objects describing this node's children.inherited -
debugFillProperties(
DiagnosticPropertiesBuilder properties) → void -
Add additional properties associated with the node.
inherited
-
noSuchMethod(
Invocation invocation) → dynamic -
Invoked when a nonexistent method or property is accessed.
inherited
-
toDiagnosticsNode(
{String? name, DiagnosticsTreeStyle? style}) → DiagnosticsNode -
Returns a debug representation of the object that is used by debugging
tools and by DiagnosticsNode.toStringDeep.
inherited
-
toString(
{DiagnosticLevel minLevel = DiagnosticLevel.info}) → String -
A string representation of this object.
inherited
-
toStringDeep(
{String prefixLineOne = '', String? prefixOtherLines, DiagnosticLevel minLevel = DiagnosticLevel.debug}) → String -
Returns a string representation of this node and its descendants.
inherited
-
toStringShallow(
{String joiner = ', ', DiagnosticLevel minLevel = DiagnosticLevel.debug}) → String -
Returns a one-line detailed description of the object.
inherited
-
toStringShort(
) → String -
A short, textual description of this widget.
inherited
Operators
-
operator ==(
Object other) → bool -
The equality operator.
inherited