update pagination listener
This commit is contained in:
@@ -1,4 +1,3 @@
|
||||
import 'package:flutter/cupertino.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter/rendering.dart';
|
||||
|
||||
@@ -6,20 +5,25 @@ import 'paginator_listener.dart';
|
||||
|
||||
typedef RowBuilder = Widget Function(dynamic);
|
||||
typedef OnScroll = void Function(bool down);
|
||||
typedef OnRemove = Function(dynamic t);
|
||||
|
||||
class PaginatorListView<T> extends StatelessWidget {
|
||||
class PaginatorListView<T> extends StatefulWidget {
|
||||
final PaginatorListener<T> paginatorListener;
|
||||
final RowBuilder rowBuilder;
|
||||
final OnScroll? onScroll;
|
||||
final ScrollController _scrollController;
|
||||
final Color color;
|
||||
final OnRemove? onRemove;
|
||||
final Widget? noMoreData;
|
||||
|
||||
PaginatorListView(
|
||||
{Key? key,
|
||||
required this.paginatorListener,
|
||||
required this.rowBuilder,
|
||||
this.onScroll,
|
||||
this.color = Colors.blueAccent})
|
||||
this.color = Colors.blueAccent,
|
||||
this.noMoreData,
|
||||
this.onRemove})
|
||||
: _scrollController = ScrollController(),
|
||||
super(key: key) {
|
||||
_scrollController.addListener(() async {
|
||||
@@ -35,10 +39,37 @@ class PaginatorListView<T> extends StatelessWidget {
|
||||
});
|
||||
}
|
||||
|
||||
@override
|
||||
State<PaginatorListView<T>> createState() => _PaginatorListViewState<T>();
|
||||
}
|
||||
|
||||
class _PaginatorListViewState<T> extends State<PaginatorListView<T>> {
|
||||
late PaginatorListener<T> _paginatorListener;
|
||||
@override
|
||||
initState() {
|
||||
super.initState();
|
||||
initListener();
|
||||
}
|
||||
|
||||
@override
|
||||
void didUpdateWidget(covariant PaginatorListView<T> oldWidget) {
|
||||
super.didUpdateWidget(oldWidget);
|
||||
initListener();
|
||||
}
|
||||
|
||||
initListener() {
|
||||
_paginatorListener = widget.paginatorListener;
|
||||
widget.paginatorListener.addListener(() {
|
||||
if (mounted) {
|
||||
setState(() {});
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
bool ended = paginatorListener.ended;
|
||||
int count = paginatorListener.data.length;
|
||||
bool ended = _paginatorListener.ended;
|
||||
int count = _paginatorListener.data.length;
|
||||
if (ended) count++;
|
||||
|
||||
return Column(
|
||||
@@ -46,12 +77,12 @@ class PaginatorListView<T> extends StatelessWidget {
|
||||
Expanded(
|
||||
child: RefreshIndicator(
|
||||
onRefresh: () {
|
||||
return paginatorListener.refresh();
|
||||
return _paginatorListener.refresh();
|
||||
},
|
||||
child: ListView.separated(
|
||||
separatorBuilder: (context, index) =>
|
||||
Divider(height: 1, color: Colors.black),
|
||||
controller: _scrollController,
|
||||
controller: widget._scrollController,
|
||||
scrollDirection: Axis.vertical,
|
||||
physics: const AlwaysScrollableScrollPhysics(),
|
||||
shrinkWrap: true,
|
||||
@@ -60,22 +91,24 @@ class PaginatorListView<T> extends StatelessWidget {
|
||||
if (ended && index == count - 1) {
|
||||
return Padding(
|
||||
padding: const EdgeInsets.all(8.0),
|
||||
child: Center(child: Text("No more data")),
|
||||
child: Center(
|
||||
child:
|
||||
widget.noMoreData ?? const Text("No more data")),
|
||||
);
|
||||
}
|
||||
T t = paginatorListener.data[index];
|
||||
return rowBuilder(t);
|
||||
T t = _paginatorListener.data[index];
|
||||
return widget.rowBuilder(t);
|
||||
}),
|
||||
),
|
||||
),
|
||||
paginatorListener.isLoading ? _loadingRow() : Container()
|
||||
_paginatorListener.isLoading ? _loadingRow() : Container()
|
||||
],
|
||||
);
|
||||
}
|
||||
|
||||
Widget _loadingRow() {
|
||||
return Container(
|
||||
padding: EdgeInsets.all(8),
|
||||
padding: const EdgeInsets.all(8),
|
||||
color: Colors.transparent,
|
||||
child: Row(
|
||||
mainAxisAlignment: MainAxisAlignment.center,
|
||||
@@ -83,10 +116,10 @@ class PaginatorListView<T> extends StatelessWidget {
|
||||
Column(
|
||||
children: [
|
||||
CircularProgressIndicator(
|
||||
valueColor: new AlwaysStoppedAnimation<Color>(color)),
|
||||
valueColor: AlwaysStoppedAnimation<Color>(widget.color)),
|
||||
Text(
|
||||
"Loading...",
|
||||
style: TextStyle(color: color),
|
||||
style: TextStyle(color: widget.color),
|
||||
)
|
||||
],
|
||||
),
|
||||
|
||||
Reference in New Issue
Block a user