Skip to content

Commit 8045bd8

Browse files
committed
search page
1 parent 00db368 commit 8045bd8

6 files changed

Lines changed: 190 additions & 2 deletions

File tree

‎lib/common/dao/ReposDao.dart‎

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -348,4 +348,51 @@ class ReposDao {
348348
}
349349
return new DataResult(null, false);
350350
}
351+
352+
/**
353+
* 搜索仓库
354+
* @param q 搜索关键字
355+
* @param sort 分类排序,beat match、most star等
356+
* @param order 倒序或者正序
357+
* @param type 搜索类型,人或者仓库 null \ 'user',
358+
* @param page
359+
* @param pageSize
360+
*/
361+
static searchRepositoryDao(q, language, sort, order, type, page, pageSize) async {
362+
if (language != null) {
363+
q = q + "%2Blanguage%3A$language";
364+
}
365+
String url = Address.search(q, sort, order, type, page, pageSize);
366+
var res = await HttpManager.netFetch(url, null, null, null);
367+
if (type == null) {
368+
if (res != null && res.result && res.data["items"] != null) {
369+
List<ReposViewModel> list = new List();
370+
var dataList = res.data["items"];
371+
if (dataList == null || dataList.length == 0) {
372+
return new DataResult(null, false);
373+
}
374+
for (int i = 0; i < dataList.length; i++) {
375+
var data = dataList[i];
376+
list.add(ReposViewModel.fromMap(data));
377+
}
378+
return new DataResult(list, true);
379+
} else {
380+
return new DataResult(null, false);
381+
}
382+
} else {
383+
if (res != null && res.result && res.data["items"] != null) {
384+
List<UserItemViewModel> list = new List();
385+
var data = res.data["items"];
386+
if (data == null || data.length == 0) {
387+
return new DataResult(null, false);
388+
}
389+
for (int i = 0; i < data.length; i++) {
390+
list.add(new UserItemViewModel(data[i]['login'], data[i]["avatar_url"]));
391+
}
392+
return new DataResult(list, true);
393+
} else {
394+
return new DataResult(null, false);
395+
}
396+
}
397+
}
351398
}

‎lib/common/style/GSYStyle.dart‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -261,6 +261,10 @@ class GSYStrings {
261261
static const String notify_status = "状态";
262262
static const String notify_type = "类型";
263263

264+
static const String search_title = "搜索";
265+
static const String search_tab_repos = "仓库";
266+
static const String search_tab_user = "用户";
267+
264268
}
265269

266270
class GSYICons {

‎lib/common/utils/NavigatorUtils.dart‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import 'package:gsy_github_app_flutter/page/LoginPage.dart';
99
import 'package:gsy_github_app_flutter/page/NotifyPage.dart';
1010
import 'package:gsy_github_app_flutter/page/PersonPage.dart';
1111
import 'package:gsy_github_app_flutter/page/RepositoryDetailPage.dart';
12+
import 'package:gsy_github_app_flutter/page/SearchPage.dart';
1213

1314
/**
1415
* 导航栏
@@ -92,4 +93,11 @@ class NavigatorUtils {
9293
static Future<Null> goNotifyPage(BuildContext context) {
9394
return Navigator.push(context, new MaterialPageRoute(builder: (context) => new NotifyPage()));
9495
}
96+
97+
///搜索
98+
static Future<Null> goSearchPage(BuildContext context) {
99+
return Navigator.push(context, new MaterialPageRoute(builder: (context) => new SearchPage()));
100+
}
101+
102+
95103
}

‎lib/page/HomePage.dart‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import 'package:flutter/material.dart';
22
import 'package:gsy_github_app_flutter/common/style/GSYStyle.dart';
3+
import 'package:gsy_github_app_flutter/common/utils/NavigatorUtils.dart';
34
import 'package:gsy_github_app_flutter/page/DynamicPage.dart';
45
import 'package:gsy_github_app_flutter/page/MyPage.dart';
56
import 'package:gsy_github_app_flutter/page/TrendPage.dart';
@@ -52,7 +53,9 @@ class HomePage extends StatelessWidget {
5253
GSYStrings.app_name,
5354
iconData: GSYICons.MAIN_SEARCH,
5455
needRightIcon: true,
55-
onPressed: () {},
56+
onPressed: () {
57+
NavigatorUtils.goSearchPage(context);
58+
},
5659
));
5760
}
5861
}

‎lib/page/SearchPage.dart‎

Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
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+
}

‎lib/widget/GSYTitleBar.dart‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ class GSYTitleBar extends StatelessWidget {
1818

1919
@override
2020
Widget build(BuildContext context) {
21-
Widget right = (needRightIcon) ? new IconButton(icon: new Icon(iconData, size: 17.0,), onPressed: onPressed) : new Container();
21+
Widget right = (needRightIcon) ? new IconButton(icon: new Icon(iconData, size: 19.0,), onPressed: onPressed) : new Container();
2222
return Container(
2323
child: new Row(
2424
children: <Widget>[

0 commit comments

Comments
 (0)