|
| 1 | +import 'package:flutter/material.dart'; |
| 2 | +import 'package:gsy_github_app_flutter/common/config/Config.dart'; |
| 3 | +import 'package:gsy_github_app_flutter/common/dao/ReposDao.dart'; |
| 4 | +import 'package:gsy_github_app_flutter/common/style/GSYStyle.dart'; |
| 5 | +import 'package:gsy_github_app_flutter/common/utils/NavigatorUtils.dart'; |
| 6 | +import 'package:gsy_github_app_flutter/widget/GSYListState.dart'; |
| 7 | +import 'package:gsy_github_app_flutter/widget/GSYPullLoadWidget.dart'; |
| 8 | +import 'package:gsy_github_app_flutter/widget/GSYSearchInputWidget.dart'; |
| 9 | +import 'package:gsy_github_app_flutter/widget/GSYSelectItemWidget.dart'; |
| 10 | +import 'package:gsy_github_app_flutter/widget/ReposItem.dart'; |
| 11 | +import 'package:gsy_github_app_flutter/widget/UserItem.dart'; |
| 12 | + |
| 13 | +/** |
| 14 | + * Created by guoshuyu |
| 15 | + * on 2018/7/24. |
| 16 | + */ |
| 17 | +class SearchPage extends StatefulWidget { |
| 18 | + @override |
| 19 | + _SearchPageState createState() => _SearchPageState(); |
| 20 | +} |
| 21 | + |
| 22 | +class _SearchPageState extends GSYListState<SearchPage> { |
| 23 | + int selectIndex = 0; |
| 24 | + |
| 25 | + String searchText; |
| 26 | + |
| 27 | + _renderEventItem(index) { |
| 28 | + var data = pullLoadWidgetControl.dataList[index]; |
| 29 | + if (selectIndex == 0) { |
| 30 | + return new ReposItem(data, onPressed: () { |
| 31 | + NavigatorUtils.goReposDetail(context, data.ownerName, data.repositoryName); |
| 32 | + }); |
| 33 | + } else if (selectIndex == 1) { |
| 34 | + return new UserItem(data, onPressed: () { |
| 35 | + NavigatorUtils.goPerson(context, data.userName); |
| 36 | + }); |
| 37 | + } |
| 38 | + } |
| 39 | + |
| 40 | + _resolveSelectIndex() { |
| 41 | + clearData(); |
| 42 | + showRefreshLoading(); |
| 43 | + } |
| 44 | + |
| 45 | + _getDataLogic() async { |
| 46 | + return await ReposDao.searchRepositoryDao(searchText, null, null, null, selectIndex == 0 ? null : 'user', page, Config.PAGE_SIZE); |
| 47 | + } |
| 48 | + |
| 49 | + @override |
| 50 | + bool get wantKeepAlive => true; |
| 51 | + |
| 52 | + @override |
| 53 | + bool get needHeader => false; |
| 54 | + |
| 55 | + @override |
| 56 | + bool get isRefreshFirst => false; |
| 57 | + |
| 58 | + @override |
| 59 | + requestLoadMore() async { |
| 60 | + return await _getDataLogic(); |
| 61 | + } |
| 62 | + |
| 63 | + @override |
| 64 | + requestRefresh() async { |
| 65 | + return await _getDataLogic(); |
| 66 | + } |
| 67 | + |
| 68 | + @override |
| 69 | + Widget build(BuildContext context) { |
| 70 | + super.build(context); // See AutomaticKeepAliveClientMixin. |
| 71 | + return new Scaffold( |
| 72 | + backgroundColor: Color(GSYColors.mainBackgroundColor), |
| 73 | + appBar: new AppBar( |
| 74 | + title: new Text(GSYStrings.search_title), |
| 75 | + bottom: new SearchBottom((value) {}, (value) { |
| 76 | + searchText = value; |
| 77 | + if (searchText == null || searchText.trim().length == 0) { |
| 78 | + return; |
| 79 | + } |
| 80 | + _resolveSelectIndex(); |
| 81 | + }, (selectIndex) { |
| 82 | + this.selectIndex = selectIndex; |
| 83 | + _resolveSelectIndex(); |
| 84 | + })), |
| 85 | + body: GSYPullLoadWidget( |
| 86 | + pullLoadWidgetControl, |
| 87 | + (BuildContext context, int index) => _renderEventItem(index), |
| 88 | + handleRefresh, |
| 89 | + onLoadMore, |
| 90 | + refreshKey: refreshIndicatorKey, |
| 91 | + ), |
| 92 | + ); |
| 93 | + } |
| 94 | +} |
| 95 | + |
| 96 | +class SearchBottom extends StatelessWidget implements PreferredSizeWidget { |
| 97 | + final ValueChanged<String> onChanged; |
| 98 | + |
| 99 | + final ValueChanged<String> onSubmitted; |
| 100 | + |
| 101 | + final SelectItemChanged selectItemChanged; |
| 102 | + |
| 103 | + SearchBottom(this.onChanged, this.onSubmitted, this.selectItemChanged); |
| 104 | + |
| 105 | + @override |
| 106 | + Widget build(BuildContext context) { |
| 107 | + return Column( |
| 108 | + children: <Widget>[ |
| 109 | + GSYSearchInputWidget(onChanged, onSubmitted), |
| 110 | + new GSYSelectItemWidget( |
| 111 | + [ |
| 112 | + GSYStrings.search_tab_repos, |
| 113 | + GSYStrings.search_tab_user, |
| 114 | + ], |
| 115 | + selectItemChanged, |
| 116 | + margin: const EdgeInsets.only(top: 10.0), |
| 117 | + ) |
| 118 | + ], |
| 119 | + ); |
| 120 | + } |
| 121 | + |
| 122 | + @override |
| 123 | + Size get preferredSize { |
| 124 | + return new Size.fromHeight(100.0); |
| 125 | + } |
| 126 | +} |
0 commit comments