重构列表请求

see more search BeeListView
hmxc
小赖 4 years ago
parent 7db743624b
commit c8eb1d7346

@ -2,6 +2,7 @@ import 'package:akuCommunity/base/base_style.dart';
import 'package:akuCommunity/constants/api.dart';
import 'package:akuCommunity/model/user/committee_item_model.dart';
import 'package:akuCommunity/pages/industry_committee/committee_mailbox/committee_mailbox_page.dart';
import 'package:akuCommunity/pages/things_page/widget/bee_list_view.dart';
import 'package:akuCommunity/utils/network/base_list_model.dart';
import 'package:akuCommunity/utils/network/net_util.dart';
import 'package:akuCommunity/widget/bee_scaffold.dart';
@ -158,20 +159,39 @@ class _IndustryCommitteePageState extends State<IndustryCommitteePage> {
Widget build(BuildContext context) {
return BeeScaffold(
title: '业委会',
body: EasyRefresh(
firstRefresh: true,
header: MaterialHeader(),
body: BeeListView(
path: API.manager.commiteeStaff,
convert: (model) {
return model.tableList
.map((e) => CommitteeItemModel.fromJson(e))
.toList();
},
controller: _refreshController,
onRefresh: refresh,
child: ListView.separated(
padding: EdgeInsets.symmetric(horizontal: 32.w, vertical: 20.w),
itemBuilder: (context, index) {
return _buildCard(_committeeModels[index]);
},
separatorBuilder: (context, index) => 20.hb,
itemCount: _committeeModels.length,
),
builder: (items) {
return ListView.separated(
padding: EdgeInsets.symmetric(horizontal: 32.w, vertical: 20.w),
itemBuilder: (context, index) {
return _buildCard(items[index]);
},
separatorBuilder: (context, index) => 20.hb,
itemCount: items.length,
);
},
),
// body: EasyRefresh(
// firstRefresh: true,
// header: MaterialHeader(),
// controller: _refreshController,
// onRefresh: refresh,
// child: ListView.separated(
// padding: EdgeInsets.symmetric(horizontal: 32.w, vertical: 20.w),
// itemBuilder: (context, index) {
// return _buildCard(_committeeModels[index]);
// },
// separatorBuilder: (context, index) => 20.hb,
// itemCount: _committeeModels.length,
// ),
// ),
bottomNavi: _buildBottomNavi(),
);
}

@ -0,0 +1,115 @@
import 'package:akuCommunity/utils/network/base_list_model.dart';
import 'package:akuCommunity/utils/network/net_util.dart';
import 'package:flutter/material.dart';
import 'package:flutter_easyrefresh/easy_refresh.dart';
/// ## BeeListView
///```dart
///BeeListView(
/// path: API.someAPI,
/// convert: (model) {
/// return model.tableList
/// .map((e) => SomeModel.fromJson(e))
/// .toList();
/// },
/// controller: _refreshController,
/// builder: (items) {
/// return ListView.builder(
/// itemBuilder: (context, index) {
/// return _buildSomeItem(items[index]);
/// },
/// itemCount: items.length,
/// );
/// },
///)
///```
class BeeListView<T> extends StatefulWidget {
///API path
final String path;
///same as EasyRefreshController
final EasyRefreshController controller;
///
///
///BaseListModel to T
///
///T is a tableList item.
///
///```dart
///...
///convert: (model) {
/// return model.tableList
/// .map((e) => SomeModel.fromJson(e))
/// .toList();
///},
///...
///```
final List<T> Function(BaseListModel model) convert;
///
final Widget Function(List<T> items) builder;
///
final int size;
///
final Map<String, dynamic> extraParams;
BeeListView({
Key key,
this.path,
@required this.controller,
this.convert,
this.builder,
this.size = 10,
this.extraParams,
}) : super(key: key);
@override
_BeeListViewState<T> createState() => _BeeListViewState<T>();
}
class _BeeListViewState<T> extends State<BeeListView> {
int _pageNum = 1;
BaseListModel _model = BaseListModel.zero();
List<T> _models = [];
Map<String, dynamic> get _params {
Map<String, dynamic> tempMap = {
'pageNum': _pageNum,
'size': widget.size,
};
tempMap.addAll(widget.extraParams ?? {});
return tempMap;
}
@override
Widget build(BuildContext context) {
return EasyRefresh(
controller: widget.controller,
header: MaterialHeader(),
footer: MaterialFooter(),
onRefresh: () async {
_pageNum = 1;
_model = await NetUtil().getList(
widget.path,
params: _params,
);
_models = widget.convert(_model);
setState(() {});
},
firstRefresh: true,
onLoad: () async {
_pageNum++;
_model = await NetUtil().getList(
widget.path,
params: _params,
);
_models.addAll(widget.convert(_model) as List<T>);
if (_pageNum >= _model.pageCount)
widget.controller.finishLoad(noMore: true);
setState(() {});
},
child: widget.builder(_models),
);
}
}
Loading…
Cancel
Save