Skip to content

Commit

Permalink
add one example:tap button refresh replace pull down refresh(#282)
Browse files Browse the repository at this point in the history
  • Loading branch information
peng8350 committed Jun 21, 2020
1 parent ee8897a commit d6f1746
Show file tree
Hide file tree
Showing 2 changed files with 133 additions and 0 deletions.
8 changes: 8 additions & 0 deletions example/lib/ui/example/ExamplePage.dart
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import 'package:example/ui/example/useStage/twolevel_refresh.dart';
import 'useStage/qq_chat_list.dart';
import 'otherwidget/refresh_recordable_listview_example.dart';
import 'otherwidget/draggable_bottomsheet_loadmore.dart';
import 'useStage/tapbutton_refresh.dart';

class ExamplePage extends StatefulWidget {
@override
Expand Down Expand Up @@ -107,6 +108,13 @@ class _ExamplePageState extends State<ExamplePage>
);
}));
}),
ExampleItem(
title: "点击按钮触发刷新",
onClick: () {
Navigator.of(context).push(MaterialPageRoute(builder: (context) {
return TapButtonRefreshExample();
}));
}),
ExampleItem(
title: "NestedScrollView下刷新",
onClick: () {
Expand Down
125 changes: 125 additions & 0 deletions example/lib/ui/example/useStage/tapbutton_refresh.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
/*
* Author: Jpeng
* Email: [email protected]
* Time: 2020-06-21 13:43
*/

import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';
import 'package:pull_to_refresh/pull_to_refresh.dart';

/*
achieve requirement
tap button to trigger refresh insteal of pull down refresh
*/
class TapButtonRefreshExample extends StatefulWidget {
TapButtonRefreshExample();

@override
State<StatefulWidget> createState() {
// TODO: implement createState
return _TapButtonRefreshExampleState();
}
}

class _TapButtonRefreshExampleState extends State<TapButtonRefreshExample> {
List<String> data = [];
RefreshController _refreshController =
RefreshController(initialRefresh: false);
bool _enablePullDown = false;


Widget buildEmpty() {
// there are two ways
// this way is more converient,but it doesn't reference ListView some attribute
// If you don't need some attribute like physics,cacheExtent,just default
// you can return emptyWidget directly,else return ListView
// from 1.5.2,you needn't compute the height by LayoutBuilder,If your boxConstaints is double.infite,
// SmartRefresher can convert the height to the viewport mainExtent
return Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Image.asset(
"images/empty1.png",
fit: BoxFit.cover,
),
Text("没数据,请点击按钮刷新")
],
);
// second way
return ListView(
children: [
Image.asset(
"images/empty.png",
fit: BoxFit.cover,
)
],
physics: BouncingScrollPhysics(),
cacheExtent: 100.0,
);
}

@override
void initState() {
// TODO: implement initState
super.initState();
_refreshController.headerMode.addListener(() {
if(_refreshController.headerMode.value == RefreshStatus.idle){

Future.delayed(const Duration(milliseconds: 20)).then((value) {
_enablePullDown = false;
setState(() {

});
});
}
});
}


@override
Widget build(BuildContext context) {
// TODO: implement build
return Scaffold(
body: SmartRefresher(
controller: _refreshController,
enablePullUp: data.length != 0,
enablePullDown: _enablePullDown,
header: ClassicHeader(),
onRefresh: () async {
await Future.delayed(const Duration(milliseconds: 2000));
if (mounted)
setState(() {
data.add("new");
data.add("new");
data.add("new");
data.add("new");
data.add("new");
data.add("new");
});
_refreshController.refreshCompleted();
},
child: data.length == 0
? buildEmpty()
: ListView.builder(
itemBuilder: (c, i) => Text(data[i]),
itemCount: data.length,
itemExtent: 100.0,
),
),
appBar: AppBar(
title: Text("点击按钮刷新"),
actions: [GestureDetector(
onTap: (){
_enablePullDown = true;
setState(() {

});
WidgetsBinding.instance.addPostFrameCallback((timeStamp) { _refreshController.requestRefresh();});
},
child: Icon(Icons.refresh),
)],
),
);
}
}

0 comments on commit d6f1746

Please sign in to comment.