From d6f174627db491fdabe51fabf80569c16f1a12ea Mon Sep 17 00:00:00 2001 From: peng8350 Date: Sat, 20 Jun 2020 23:14:44 -0700 Subject: [PATCH] add one example:tap button refresh replace pull down refresh(#282) --- example/lib/ui/example/ExamplePage.dart | 8 ++ .../example/useStage/tapbutton_refresh.dart | 125 ++++++++++++++++++ 2 files changed, 133 insertions(+) create mode 100644 example/lib/ui/example/useStage/tapbutton_refresh.dart diff --git a/example/lib/ui/example/ExamplePage.dart b/example/lib/ui/example/ExamplePage.dart index 0d636974..214da3f4 100644 --- a/example/lib/ui/example/ExamplePage.dart +++ b/example/lib/ui/example/ExamplePage.dart @@ -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 @@ -107,6 +108,13 @@ class _ExamplePageState extends State ); })); }), + ExampleItem( + title: "点击按钮触发刷新", + onClick: () { + Navigator.of(context).push(MaterialPageRoute(builder: (context) { + return TapButtonRefreshExample(); + })); + }), ExampleItem( title: "NestedScrollView下刷新", onClick: () { diff --git a/example/lib/ui/example/useStage/tapbutton_refresh.dart b/example/lib/ui/example/useStage/tapbutton_refresh.dart new file mode 100644 index 00000000..8be67616 --- /dev/null +++ b/example/lib/ui/example/useStage/tapbutton_refresh.dart @@ -0,0 +1,125 @@ +/* + * Author: Jpeng + * Email: peng8350@gmail.com + * 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 createState() { + // TODO: implement createState + return _TapButtonRefreshExampleState(); + } +} + +class _TapButtonRefreshExampleState extends State { + List 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: [ + 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), + )], + ), + ); + } +}