my_refresh_list.dart 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. import 'package:flutter/cupertino.dart';
  2. import 'package:flutter/material.dart';
  3. import 'package:liftmanager/res/resources.dart';
  4. import 'package:liftmanager/utils/theme_utils.dart';
  5. import 'package:liftmanager/widgets/state_layout.dart';
  6. /// 封装下拉刷新与加载更多
  7. class MyListView extends StatefulWidget {
  8. const MyListView({
  9. Key key,
  10. @required this.itemCount,
  11. @required this.itemBuilder,
  12. @required this.onRefresh,
  13. this.loadMore,
  14. this.normal:true,
  15. this.hasMore : false,
  16. this.stateType : StateType.empty,
  17. this.pageSize : 10,
  18. this.padding,
  19. this.itemExtent,
  20. }): super(key: key);
  21. final RefreshCallback onRefresh;
  22. final LoadMoreCallback loadMore;
  23. final int itemCount;
  24. final bool hasMore;
  25. final bool normal;
  26. final IndexedWidgetBuilder itemBuilder;
  27. final StateType stateType;
  28. /// 一页的数量,默认为10
  29. final int pageSize;
  30. final EdgeInsetsGeometry padding;
  31. final double itemExtent;
  32. @override
  33. _MyListViewState createState() => _MyListViewState();
  34. }
  35. typedef RefreshCallback = Future<void> Function();
  36. typedef LoadMoreCallback = Future<void> Function();
  37. class _MyListViewState extends State<MyListView> {
  38. /// 是否正在加载数据
  39. bool _isLoading = false;
  40. @override
  41. Widget build(BuildContext context) {
  42. return SafeArea(
  43. child: NotificationListener(
  44. onNotification: (ScrollNotification note){
  45. /// 确保是垂直方向滚动,且滑动至底部
  46. if (note.metrics.pixels == note.metrics.maxScrollExtent && note.metrics.axis == Axis.vertical){
  47. _loadMore();
  48. }
  49. return true;
  50. },
  51. child: RefreshIndicator(
  52. onRefresh: widget.onRefresh,
  53. child: widget.itemCount == 0 ? (widget.normal?StateLayout(type: widget.stateType):
  54. Container(child: null,)
  55. ) : ListView.builder(
  56. shrinkWrap: true,
  57. itemCount: widget.loadMore == null ? widget.itemCount : widget.itemCount + 1,
  58. padding: widget.padding,
  59. itemExtent: widget.itemExtent,
  60. itemBuilder: (BuildContext context, int index){
  61. /// 不需要加载更多则不需要添加FootView
  62. if (widget.loadMore == null){
  63. return widget.itemBuilder(context, index);
  64. }else{
  65. return index < widget.itemCount ? widget.itemBuilder(context, index) : MoreWidget(widget.itemCount, widget.hasMore, widget.pageSize);
  66. }
  67. }
  68. )
  69. ),
  70. ),
  71. );
  72. }
  73. Future _loadMore() async {
  74. if (widget.loadMore == null){
  75. return;
  76. }
  77. if (_isLoading) {
  78. return;
  79. }
  80. if (!widget.hasMore){
  81. return;
  82. }
  83. _isLoading = true;
  84. await widget.loadMore();
  85. _isLoading = false;
  86. }
  87. }
  88. class MoreWidget extends StatelessWidget {
  89. const MoreWidget(this.itemCount, this.hasMore, this.pageSize);
  90. final int itemCount;
  91. final bool hasMore;
  92. final int pageSize;
  93. @override
  94. Widget build(BuildContext context) {
  95. final style = const TextStyle(color: Color(0xff666666));
  96. return Container(
  97. color:ThemeUtils.getTabsBg(context),
  98. child: Padding(
  99. padding: EdgeInsets.only(top:10,bottom:30),
  100. child: Row(
  101. mainAxisAlignment: MainAxisAlignment.center,
  102. crossAxisAlignment: CrossAxisAlignment.center,
  103. children: <Widget>[
  104. hasMore ? const CupertinoActivityIndicator() : Gaps.empty,
  105. hasMore ? Gaps.hGap5 : Gaps.empty,
  106. /// 只有一页的时候,就不显示FooterView了
  107. Text(hasMore ? '正在加载中...' : (itemCount < pageSize ? '' : '没有了呦~'), style: style),
  108. ],
  109. ),
  110. ),
  111. );
  112. }
  113. }