|
| 1 | +/* |
| 2 | + * Author: Jpeng |
| 3 | + |
| 4 | + * Time: 2020-06-21 13:43 |
| 5 | + */ |
| 6 | + |
| 7 | +import 'package:flutter/material.dart'; |
| 8 | +import 'package:flutter/widgets.dart'; |
| 9 | +import 'package:pull_to_refresh/pull_to_refresh.dart'; |
| 10 | + |
| 11 | +/* |
| 12 | + achieve requirement |
| 13 | + tap button to trigger refresh insteal of pull down refresh |
| 14 | + */ |
| 15 | +class TapButtonRefreshExample extends StatefulWidget { |
| 16 | + TapButtonRefreshExample(); |
| 17 | + |
| 18 | + @override |
| 19 | + State<StatefulWidget> createState() { |
| 20 | + // TODO: implement createState |
| 21 | + return _TapButtonRefreshExampleState(); |
| 22 | + } |
| 23 | +} |
| 24 | + |
| 25 | +class _TapButtonRefreshExampleState extends State<TapButtonRefreshExample> { |
| 26 | + List<String> data = []; |
| 27 | + RefreshController _refreshController = |
| 28 | + RefreshController(initialRefresh: false); |
| 29 | + bool _enablePullDown = false; |
| 30 | + |
| 31 | + |
| 32 | + Widget buildEmpty() { |
| 33 | + // there are two ways |
| 34 | + // this way is more converient,but it doesn't reference ListView some attribute |
| 35 | + // If you don't need some attribute like physics,cacheExtent,just default |
| 36 | + // you can return emptyWidget directly,else return ListView |
| 37 | + // from 1.5.2,you needn't compute the height by LayoutBuilder,If your boxConstaints is double.infite, |
| 38 | + // SmartRefresher can convert the height to the viewport mainExtent |
| 39 | + return Column( |
| 40 | + mainAxisAlignment: MainAxisAlignment.center, |
| 41 | + children: <Widget>[ |
| 42 | + Image.asset( |
| 43 | + "images/empty1.png", |
| 44 | + fit: BoxFit.cover, |
| 45 | + ), |
| 46 | + Text("没数据,请点击按钮刷新") |
| 47 | + ], |
| 48 | + ); |
| 49 | + // second way |
| 50 | + return ListView( |
| 51 | + children: [ |
| 52 | + Image.asset( |
| 53 | + "images/empty.png", |
| 54 | + fit: BoxFit.cover, |
| 55 | + ) |
| 56 | + ], |
| 57 | + physics: BouncingScrollPhysics(), |
| 58 | + cacheExtent: 100.0, |
| 59 | + ); |
| 60 | + } |
| 61 | + |
| 62 | + @override |
| 63 | + void initState() { |
| 64 | + // TODO: implement initState |
| 65 | + super.initState(); |
| 66 | + _refreshController.headerMode.addListener(() { |
| 67 | + if(_refreshController.headerMode.value == RefreshStatus.idle){ |
| 68 | + |
| 69 | + Future.delayed(const Duration(milliseconds: 20)).then((value) { |
| 70 | + _enablePullDown = false; |
| 71 | + setState(() { |
| 72 | + |
| 73 | + }); |
| 74 | + }); |
| 75 | + } |
| 76 | + }); |
| 77 | + } |
| 78 | + |
| 79 | + |
| 80 | + @override |
| 81 | + Widget build(BuildContext context) { |
| 82 | + // TODO: implement build |
| 83 | + return Scaffold( |
| 84 | + body: SmartRefresher( |
| 85 | + controller: _refreshController, |
| 86 | + enablePullUp: data.length != 0, |
| 87 | + enablePullDown: _enablePullDown, |
| 88 | + header: ClassicHeader(), |
| 89 | + onRefresh: () async { |
| 90 | + await Future.delayed(const Duration(milliseconds: 2000)); |
| 91 | + if (mounted) |
| 92 | + setState(() { |
| 93 | + data.add("new"); |
| 94 | + data.add("new"); |
| 95 | + data.add("new"); |
| 96 | + data.add("new"); |
| 97 | + data.add("new"); |
| 98 | + data.add("new"); |
| 99 | + }); |
| 100 | + _refreshController.refreshCompleted(); |
| 101 | + }, |
| 102 | + child: data.length == 0 |
| 103 | + ? buildEmpty() |
| 104 | + : ListView.builder( |
| 105 | + itemBuilder: (c, i) => Text(data[i]), |
| 106 | + itemCount: data.length, |
| 107 | + itemExtent: 100.0, |
| 108 | + ), |
| 109 | + ), |
| 110 | + appBar: AppBar( |
| 111 | + title: Text("点击按钮刷新"), |
| 112 | + actions: [GestureDetector( |
| 113 | + onTap: (){ |
| 114 | + _enablePullDown = true; |
| 115 | + setState(() { |
| 116 | + |
| 117 | + }); |
| 118 | + WidgetsBinding.instance.addPostFrameCallback((timeStamp) { _refreshController.requestRefresh();}); |
| 119 | + }, |
| 120 | + child: Icon(Icons.refresh), |
| 121 | + )], |
| 122 | + ), |
| 123 | + ); |
| 124 | + } |
| 125 | +} |
0 commit comments