|
| 1 | +import 'package:flutter/material.dart'; |
| 2 | +import 'package:flutter_picgo/components/manage_item.dart'; |
| 3 | +import 'package:flutter_picgo/model/lsky_content.dart'; |
| 4 | +import 'package:flutter_picgo/views/manage_page/base_loading_page_state.dart'; |
| 5 | +import 'package:flutter_picgo/views/manage_page/lsky_page/lsky_repo_page_presenter.dart'; |
| 6 | +import 'package:pull_to_refresh/pull_to_refresh.dart'; |
| 7 | +import 'package:url_launcher/url_launcher.dart'; |
| 8 | + |
| 9 | +class LskyRepoPage extends StatefulWidget { |
| 10 | + _LskyRepoPageState createState() => _LskyRepoPageState(); |
| 11 | +} |
| 12 | + |
| 13 | +class _LskyRepoPageState extends BaseLoadingPageState<LskyRepoPage> |
| 14 | + implements LskyRepoPageContract { |
| 15 | + String errorMsg; |
| 16 | + int currentPage = 1; |
| 17 | + RefreshController _refreshController = |
| 18 | + RefreshController(initialRefresh: false); |
| 19 | + List<LskyContent> contents = []; |
| 20 | + LskyRepoPagePresenter _presenter; |
| 21 | + |
| 22 | + _LskyRepoPageState() { |
| 23 | + _presenter = LskyRepoPagePresenter(this); |
| 24 | + } |
| 25 | + |
| 26 | + @override |
| 27 | + void initState() { |
| 28 | + super.initState(); |
| 29 | + _presenter.doLoadContents(currentPage); |
| 30 | + } |
| 31 | + |
| 32 | + @override |
| 33 | + AppBar get appBar => AppBar( |
| 34 | + title: Text('图床仓库'), |
| 35 | + centerTitle: true, |
| 36 | + ); |
| 37 | + |
| 38 | + Widget buildEmtpy() { |
| 39 | + return Center( |
| 40 | + child: Text('数据空空如也噢'), |
| 41 | + ); |
| 42 | + } |
| 43 | + |
| 44 | + @override |
| 45 | + Widget buildError() { |
| 46 | + return Center( |
| 47 | + child: Column( |
| 48 | + mainAxisAlignment: MainAxisAlignment.center, |
| 49 | + children: <Widget>[ |
| 50 | + RaisedButton( |
| 51 | + color: Theme.of(context).accentColor, |
| 52 | + textColor: Colors.white, |
| 53 | + child: Text('刷新'), |
| 54 | + onPressed: () { |
| 55 | + setState(() { |
| 56 | + this.state = LoadState.LOADING; |
| 57 | + // _presenter.doLoadContents(_path, _prePath); |
| 58 | + }); |
| 59 | + }, |
| 60 | + ), |
| 61 | + SizedBox(height: 5), |
| 62 | + Text(this.errorMsg ?? '未知错误', |
| 63 | + style: TextStyle(color: Colors.grey[400])), |
| 64 | + ], |
| 65 | + ), |
| 66 | + ); |
| 67 | + } |
| 68 | + |
| 69 | + @override |
| 70 | + Widget buildLoading() { |
| 71 | + return Center( |
| 72 | + child: SizedBox( |
| 73 | + width: 20, |
| 74 | + height: 20, |
| 75 | + child: CircularProgressIndicator( |
| 76 | + backgroundColor: Colors.transparent, |
| 77 | + valueColor: AlwaysStoppedAnimation(Colors.blue), |
| 78 | + ), |
| 79 | + ), |
| 80 | + ); |
| 81 | + } |
| 82 | + |
| 83 | + @override |
| 84 | + Widget buildSuccess() { |
| 85 | + return SmartRefresher( |
| 86 | + enablePullDown: false, |
| 87 | + enablePullUp: true, |
| 88 | + footer: CustomFooter( |
| 89 | + builder: (BuildContext context, LoadStatus mode) { |
| 90 | + Widget body; |
| 91 | + if (mode == LoadStatus.idle) { |
| 92 | + body = Text("上拉加载"); |
| 93 | + } else if (mode == LoadStatus.loading) { |
| 94 | + body = SizedBox( |
| 95 | + width: 15, |
| 96 | + height: 15, |
| 97 | + child: CircularProgressIndicator(), |
| 98 | + ); |
| 99 | + } else if (mode == LoadStatus.failed) { |
| 100 | + body = Text("加载失败!点击重试!"); |
| 101 | + } else if (mode == LoadStatus.canLoading) { |
| 102 | + body = Text("松手,加载更多!"); |
| 103 | + } else { |
| 104 | + body = Text("没有更多数据了!"); |
| 105 | + } |
| 106 | + return Container( |
| 107 | + height: 55.0, |
| 108 | + child: Center(child: body), |
| 109 | + ); |
| 110 | + }, |
| 111 | + ), |
| 112 | + onLoading: _onLoading, |
| 113 | + controller: _refreshController, |
| 114 | + child: ListView.builder( |
| 115 | + itemCount: contents.length, |
| 116 | + itemBuilder: (context, index) { |
| 117 | + return ManageItem( |
| 118 | + Key('$index'), |
| 119 | + contents[index].url, |
| 120 | + contents[index].name, |
| 121 | + '${contents[index].size}k', |
| 122 | + FileContentType.FILE, |
| 123 | + onTap: () { |
| 124 | + launch(contents[index].url); |
| 125 | + }, |
| 126 | + confirmDismiss: (direction) async { |
| 127 | + bool result = await showDialog( |
| 128 | + context: context, |
| 129 | + builder: (BuildContext context) { |
| 130 | + return AlertDialog( |
| 131 | + title: Text('确定删除吗'), |
| 132 | + content: Text('删除后无法恢复'), |
| 133 | + actions: <Widget>[ |
| 134 | + FlatButton( |
| 135 | + child: Text('确定'), |
| 136 | + onPressed: () { |
| 137 | + Navigator.pop(context, true); |
| 138 | + }), |
| 139 | + ], |
| 140 | + ); |
| 141 | + }); |
| 142 | + if (!(result ?? false)) { |
| 143 | + return false; |
| 144 | + } |
| 145 | + |
| 146 | + var del = await _presenter |
| 147 | + .doDeleteContents('${contents[index].id}'); |
| 148 | + return del; |
| 149 | + }, |
| 150 | + ); |
| 151 | + })); |
| 152 | + } |
| 153 | + |
| 154 | + _onLoading() async { |
| 155 | + _presenter.doLoadContents(currentPage); |
| 156 | + } |
| 157 | + |
| 158 | + @override |
| 159 | + void loadError(String msg) { |
| 160 | + setState(() { |
| 161 | + this.state = LoadState.ERROR; |
| 162 | + this.errorMsg = msg; |
| 163 | + }); |
| 164 | + } |
| 165 | + |
| 166 | + @override |
| 167 | + void loadSuccess(List<LskyContent> data, bool isEnd) { |
| 168 | + if (data != null && data.length > 0) { |
| 169 | + setState(() { |
| 170 | + if (this.state != LoadState.SUCCESS) { |
| 171 | + this.state = LoadState.SUCCESS; |
| 172 | + } |
| 173 | + this.contents.addAll(data); |
| 174 | + |
| 175 | + if (isEnd) { |
| 176 | + _refreshController.loadNoData(); |
| 177 | + } |
| 178 | + |
| 179 | + currentPage += 1; |
| 180 | + }); |
| 181 | + } else { |
| 182 | + if (currentPage == 1) { |
| 183 | + setState(() { |
| 184 | + this.state = LoadState.EMTPY; |
| 185 | + }); |
| 186 | + } |
| 187 | + } |
| 188 | + } |
| 189 | +} |
0 commit comments