Skip to content

Commit e6fa3f3

Browse files
committed
增加注释
1 parent ab6d92c commit e6fa3f3

File tree

1 file changed

+50
-14
lines changed

1 file changed

+50
-14
lines changed

lib/widget/GSYPullLoadWidget.dart

Lines changed: 50 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,9 @@ class _GSYPullLoadWidgetState extends State<GSYPullLoadWidget> {
4141

4242
@override
4343
void initState() {
44+
///增加滑动监听
4445
_scrollController.addListener(() {
46+
///判断当前滑动位置是不是到达底部,触发加载更多回调
4547
if (_scrollController.position.pixels == _scrollController.position.maxScrollExtent) {
4648
if (this.onLoadMore != null && this.control.needLoadMore) {
4749
this.onLoadMore();
@@ -51,41 +53,66 @@ class _GSYPullLoadWidgetState extends State<GSYPullLoadWidget> {
5153
super.initState();
5254
}
5355

56+
///根据配置状态返回实际列表数量
57+
///实际上这里可以根据你的需要做更多的处理
58+
///比如多个头部,是否需要空页面,是否需要显示加载更多。
5459
_getListCount() {
60+
///是否需要头部
5561
if (control.needHeader) {
62+
///如果需要头部,用Item 0 的 Widget 作为ListView的头部
63+
///列表数量大于0时,因为头部和底部加载更多选项,需要对列表数据总数+2
5664
return (control.dataList.length > 0) ? control.dataList.length + 2 : control.dataList.length + 1;
5765
} else {
58-
if(control.dataList.length == 0) {
66+
///如果不需要头部,在没有数据时,固定返回数量1用于空页面呈现
67+
if (control.dataList.length == 0) {
5968
return 1;
6069
}
70+
71+
///如果有数据,因为部加载更多选项,需要对列表数据总数+1
6172
return (control.dataList.length > 0) ? control.dataList.length + 1 : control.dataList.length;
6273
}
6374
}
6475

76+
///根据配置状态返回实际列表渲染Item
77+
_getItem(int index) {
78+
if (!control.needHeader && index == control.dataList.length && control.dataList.length != 0) {
79+
///如果不需要头部,并且数据不为0,当index等于数据长度时,渲染加载更多Item(因为index是从0开始)
80+
return _buildProgressIndicator();
81+
} else if (control.needHeader && index == _getListCount() - 1 && control.dataList.length != 0) {
82+
///如果需要头部,并且数据不为0,当index等于实际渲染长度 - 1时,渲染加载更多Item(因为index是从0开始)
83+
return _buildProgressIndicator();
84+
} else if (!control.needHeader && control.dataList.length == 0) {
85+
///如果不需要头部,并且数据为0,渲染空页面
86+
return _buildEmpty();
87+
} else {
88+
///回调外部正常渲染Item,如果这里有需要,可以直接返回相对位置的index
89+
return itemBuilder(context, index);
90+
}
91+
}
92+
6593
@override
6694
Widget build(BuildContext context) {
6795
return new RefreshIndicator(
96+
///GlobalKey,用户外部获取RefreshIndicator的State,做显示刷新
6897
key: refreshKey,
98+
///下拉刷新触发,返回的是一个Future
6999
onRefresh: onRefresh,
70100
child: new ListView.builder(
101+
///保持ListView任何情况都能滚动,解决在RefreshIndicator的兼容问题。
71102
physics: const AlwaysScrollableScrollPhysics(),
103+
///根据状态返回子孔健
72104
itemBuilder: (context, index) {
73-
if (!control.needHeader && index == control.dataList.length && control.dataList.length != 0) {
74-
return _buildProgressIndicator();
75-
} else if (control.needHeader && index == _getListCount() - 1 && control.dataList.length != 0) {
76-
return _buildProgressIndicator();
77-
} else if (!control.needHeader && control.dataList.length == 0) {
78-
return _buildEmpty();
79-
} else {
80-
return itemBuilder(context, index);
81-
}
105+
return _getItem(index);
82106
},
107+
///根据状态返回数量
83108
itemCount: _getListCount(),
109+
///滑动监听
84110
controller: _scrollController,
85111
),
86112
);
87113
}
88114

115+
///空页面
89116
Widget _buildEmpty() {
90117
return new Container(
91118
height: MediaQuery.of(context).size.height - 100,
@@ -104,19 +131,28 @@ class _GSYPullLoadWidgetState extends State<GSYPullLoadWidget> {
104131
);
105132
}
106133

134+
///上拉加载更多
107135
Widget _buildProgressIndicator() {
136+
///是否需要显示上拉加载更多的loading
108137
Widget bottomWidget = (control.needLoadMore)
109138
? new Row(mainAxisAlignment: MainAxisAlignment.center, children: <Widget>[
110-
new SpinKitRotatingCircle(color: Color(GSYColors.primaryValue)),
139+
///loading框
140+
new SpinKitRotatingCircle(color: Color(0xFF24292E)),
111141
new Container(
112142
width: 5.0,
113143
),
144+
///加载中文本
114145
new Text(
115-
GSYStrings.load_more_text,
116-
style: GSYConstant.smallTextBold,
146+
"加载中···",
147+
style: TextStyle(
148+
color: Color(0xFF121917),
149+
fontSize: 14.0,
150+
fontWeight: FontWeight.bold,
151+
),
117152
)
118153
])
119-
: new Text(GSYStrings.load_more_not, style: GSYConstant.smallTextBold);
154+
/// 不需要加载
155+
: new Container();
120156
return new Padding(
121157
padding: const EdgeInsets.all(20.0),
122158
child: new Center(

0 commit comments

Comments
 (0)