Skip to content

Commit 553dbad

Browse files
committed
Add:二期开发-重构首页布局
1 parent 1e91ef2 commit 553dbad

19 files changed

+1192
-51
lines changed

assets/images/ali_connors.png

19 KB
Loading

flutter_01.png

40.8 KB
Loading

lib/blocs/industry_api.dart

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
/**
2+
* Created with Android Studio.
3+
* User: 一晟
4+
* Date: 2019/4/28
5+
* Time: 3:20 PM
6+
7+
* tartget: FlatButton 的示例
8+
*/
9+
import 'dart:async';
10+
import 'package:dio/dio.dart';
11+
import 'dart:convert';
12+
import 'package:html/parser.dart' show parse;
13+
import './industry_model.dart';
14+
import './search_result.dart';
15+
16+
var dio = new Dio();
17+
class Api2 {
18+
/// 关键字提示(起点)
19+
Future<List<String>> suggestion(String query) async {
20+
// http.Response response = await http.get(
21+
// "https://www.qidian.com/ajax/Search/AutoComplete?siteid=1&query=$query");
22+
var response = await dio.get("https://www.qidian.com/ajax/Search/AutoComplete?siteid=1&query=$query", data: {});
23+
//var response = await dio.get("https://www.so.com/s?ie=utf-8&q=$query");
24+
print('1=====>${query}');
25+
print('2=====>${response.data}');
26+
//var data = Suggestion.fromJson(json.decode(response.body));
27+
//var data = Suggestion.fromJson(json.decode(response.data));
28+
var data = Suggestion.fromJson(json.decode(response.data));
29+
List<String> suggestion = [];
30+
data.suggestions.forEach((k) {
31+
//print('=====>${k.value}');
32+
suggestion.add(k.value);
33+
});
34+
35+
return Future.delayed(Duration(seconds:2), () {
36+
return suggestion;
37+
});
38+
//return suggestion;
39+
}
40+
}
41+
class Api {
42+
/// 关键字提示(起点)
43+
Future<List<SearchResult>> suggestion(String query) async {
44+
// http.Response response = await http.get(
45+
// "https://www.qidian.com/ajax/Search/AutoComplete?siteid=1&query=$query");
46+
/// var response = await dio.get("https://www.qidian.com/ajax/Search/AutoComplete?siteid=1&query=$query", data: {});
47+
var response = await dio.get("https://www.so.com/s?ie=utf-8&q=$query");
48+
var document = parse(response.data);
49+
var app = document.querySelectorAll('.res-title a');
50+
print('1=====>${query}');
51+
print('2=====>${response.data}');
52+
print('3=====>${app}');
53+
//var data = Suggestion.fromJson(json.decode(response.body));
54+
//var data = Suggestion.fromJson(json.decode(response.data));
55+
List<SearchResult> res = [];
56+
app.forEach((f) {
57+
print('f==>${f}');
58+
res.add(
59+
SearchResult(
60+
title: f.text,
61+
source: f.attributes["data-url"] ?? f.attributes["href"],
62+
),
63+
);
64+
});
65+
66+
return Future.delayed(Duration(seconds:2), () {
67+
return res;
68+
});
69+
//return suggestion;
70+
}
71+
}
72+
73+
Api api = Api();

lib/blocs/industry_bloc.dart

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/**
2+
* Created with Android Studio.
3+
* User: 一晟
4+
* Date: 2019/4/28
5+
* Time: 5:19 PM
6+
7+
* tartget:
8+
*/
9+
import 'dart:async';
10+
import 'package:bloc/bloc.dart';
11+
import './industry_api.dart';
12+
import './industry_event.dart';
13+
import './industry_state.dart';
14+
15+
class SuggestionBloc extends Bloc<SuggestionEvent, SuggestionState> {
16+
@override
17+
SuggestionState get initialState => SuggestionUninitialized();
18+
19+
@override
20+
Stream<SuggestionState> mapEventToState(SuggestionEvent event)async* {
21+
//Stream<SuggestionState> mapEventToState(SuggestionState currentState, SuggestionEvent event,) async* {
22+
if (event is SuggestionFetch) {
23+
print('event==>${event.query}');
24+
try {
25+
yield SuggestionLoading();
26+
final res = await api.suggestion(event.query);
27+
yield SuggestionLoaded(res: res);
28+
} catch (_) {
29+
yield SuggestionError();
30+
}
31+
}
32+
}
33+
}

lib/blocs/industry_event.dart

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
/**
2+
* Created with Android Studio.
3+
* User: 一晟
4+
* Date: 2019/4/28
5+
* Time: 3:35 PM
6+
7+
*/
8+
abstract class SuggestionEvent {}
9+
10+
class SuggestionFetch extends SuggestionEvent {
11+
final String query;
12+
13+
SuggestionFetch({this.query});
14+
15+
@override
16+
String toString() => 'SuggestionFetch:获取关键字提示事件';
17+
}

lib/blocs/industry_main.dart

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
/**
2+
* Created with Android Studio.
3+
* User: 一晟
4+
* Date: 2019/4/28
5+
* Time: 3:52 PM
6+
7+
*/
8+
import 'package:flutter/material.dart';
9+
//import 'package:bloc/bloc.dart';
10+
import 'package:flutter_bloc/flutter_bloc.dart';
11+
import './industry_bloc.dart';
12+
import './industry_event.dart';
13+
import './industry_state.dart';
14+
15+
//class Industry extends StatelessWidget {
16+
// @override
17+
// Widget build(BuildContext context) {
18+
// return MaterialApp(
19+
// title: '关键字提示',
20+
// home: Scaffold(
21+
// appBar: AppBar(
22+
// title: Text('关键字提示'),
23+
// ),
24+
// body: AppHome(),
25+
// ),
26+
// );
27+
// }
28+
//}
29+
30+
class IndustryPage extends StatefulWidget {
31+
@override
32+
_IndustryState createState() => _IndustryState();
33+
}
34+
35+
class _IndustryState extends State<IndustryPage> {
36+
final SuggestionBloc _suggestion = SuggestionBloc();
37+
38+
@override
39+
Widget build(BuildContext context) {
40+
return Material(
41+
child: Column(
42+
children: [
43+
TextField(
44+
autofocus: true,
45+
textAlign: TextAlign.center,
46+
onSubmitted: (text) {
47+
_suggestion.dispatch(SuggestionFetch(query: text));
48+
},
49+
),
50+
Expanded(
51+
child: BlocBuilder(
52+
bloc: _suggestion,
53+
builder: (BuildContext context, SuggestionState state) {
54+
print('-------${state}');
55+
if (state is SuggestionUninitialized) {
56+
return Center(
57+
child: Text('暂无内容'),
58+
);
59+
} else if (state is SuggestionLoading) {
60+
return Center(
61+
child: CircularProgressIndicator(),
62+
);
63+
} else if (state is SuggestionError) {
64+
return Center(
65+
child: Text('出现错误'),
66+
);
67+
} else if (state is SuggestionLoaded) {
68+
// return ListView.builder(
69+
// itemBuilder: (BuildContext context, int index) {
70+
// //return ListTile(title: Text(state.res[index]));
71+
// return null;
72+
// },
73+
// itemCount: state.res.length,
74+
// );
75+
return ListView.builder(
76+
itemBuilder: (BuildContext context, int index) {
77+
return ListTile(
78+
dense: true,
79+
leading: Icon(
80+
Icons.bookmark_border,
81+
size: 32,
82+
),
83+
title: Text(
84+
state.res[index].title,
85+
overflow: TextOverflow.ellipsis,
86+
),
87+
subtitle: Text(state.res[index].source),
88+
onTap: () {
89+
// 在这里对选中的结果进行解析,因为我目前是用golang实现的,所以就没贴代码了。
90+
print(state.res[index].source);
91+
},
92+
);
93+
},
94+
itemCount: state.res.length,
95+
);
96+
}
97+
},
98+
),
99+
),
100+
],
101+
),
102+
);
103+
}
104+
105+
@override
106+
void dispose() {
107+
_suggestion.dispose();
108+
super.dispose();
109+
}
110+
}

lib/blocs/industry_model.dart

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
/**
2+
* Created with Android Studio.
3+
* User: 一晟
4+
* Date: 2019/4/28
5+
* Time: 3:19 PM
6+
7+
*/
8+
class Suggestion {
9+
String query;
10+
List<Suggestions> suggestions;
11+
int code;
12+
13+
Suggestion({this.query, this.suggestions, this.code});
14+
15+
Suggestion.fromJson(Map<String, dynamic> json) {
16+
query = json['query'];
17+
if (json['suggestions'] != null) {
18+
suggestions = new List<Suggestions>();
19+
json['suggestions'].forEach((v) {
20+
suggestions.add(new Suggestions.fromJson(v));
21+
});
22+
}
23+
code = json['code'];
24+
}
25+
26+
Map<String, dynamic> toJson() {
27+
final Map<String, dynamic> data = new Map<String, dynamic>();
28+
data['query'] = this.query;
29+
if (this.suggestions != null) {
30+
data['suggestions'] = this.suggestions.map((v) => v.toJson()).toList();
31+
}
32+
data['code'] = this.code;
33+
return data;
34+
}
35+
}
36+
37+
class Suggestions {
38+
Data data;
39+
String value;
40+
41+
Suggestions({this.data, this.value});
42+
43+
Suggestions.fromJson(Map<String, dynamic> json) {
44+
data = json['data'] != null ? new Data.fromJson(json['data']) : null;
45+
value = json['value'];
46+
}
47+
48+
Map<String, dynamic> toJson() {
49+
final Map<String, dynamic> data = new Map<String, dynamic>();
50+
if (this.data != null) {
51+
data['data'] = this.data.toJson();
52+
}
53+
data['value'] = this.value;
54+
return data;
55+
}
56+
}
57+
58+
class Data {
59+
String category;
60+
61+
Data({this.category});
62+
63+
Data.fromJson(Map<String, dynamic> json) {
64+
category = json['category'];
65+
}
66+
67+
Map<String, dynamic> toJson() {
68+
final Map<String, dynamic> data = new Map<String, dynamic>();
69+
data['category'] = this.category;
70+
return data;
71+
}
72+
}

lib/blocs/industry_state.dart

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/**
2+
* Created with Android Studio.
3+
* User: 一晟
4+
* Date: 2019/4/28
5+
* Time: 3:37 PM
6+
7+
*/
8+
abstract class SuggestionState {}
9+
10+
class SuggestionError extends SuggestionState {
11+
@override
12+
String toString() => 'SuggestionError:获取失败';
13+
}
14+
15+
class SuggestionUninitialized extends SuggestionState {
16+
@override
17+
String toString() => 'SuggestionUninitialized:未初始化';
18+
}
19+
20+
class SuggestionLoading extends SuggestionState {
21+
@override
22+
String toString() => 'SuggestionLoading :正在加载';
23+
}
24+
25+
class SuggestionLoaded extends SuggestionState {
26+
final List res;
27+
28+
SuggestionLoaded({
29+
this.res,
30+
});
31+
32+
@override
33+
String toString() => 'SuggestionLoaded:加载完毕';
34+
}

lib/blocs/search_api.dart

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/**
2+
* Created with Android Studio.
3+
* User: 一晟
4+
* Date: 2019/4/28
5+
* Time: 3:20 PM
6+
7+
* tartget: FlatButton 的示例
8+
*/
9+
import 'dart:async';
10+
import 'package:dio/dio.dart';
11+
import 'dart:convert';
12+
import './search_result.dart';
13+
import 'package:html/parser.dart' show parse;
14+
15+
var dio = new Dio();
16+
class Api {
17+
Future<List<SearchResult>> search(name) async {
18+
print('=========>>>');
19+
var response = await dio.get("https://www.so.com/s?ie=utf-8&q=$name");
20+
// var document = parse(response.data);
21+
// var app = document.querySelectorAll('.res-title a');
22+
List<SearchResult> res = [];
23+
// app.forEach((f) {
24+
// res.add(
25+
// SearchResult(
26+
// title: f.text,
27+
// source: f.attributes["data-url"] ?? f.attributes["href"],
28+
// ),
29+
// );
30+
// });
31+
return res;
32+
}
33+
}
34+
35+
Api api = Api();

0 commit comments

Comments
 (0)