Skip to content

Commit 4868e11

Browse files
committed
完成网络请求封装和动态数据获取
1 parent 9697edf commit 4868e11

File tree

8 files changed

+105
-17
lines changed

8 files changed

+105
-17
lines changed

.vscode/launch.json

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{
2+
// Use IntelliSense to learn about possible attributes.
3+
// Hover to view descriptions of existing attributes.
4+
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
5+
"version": "0.2.0",
6+
"configurations": [
7+
{
8+
"name": "blog_flutter",
9+
"request": "launch",
10+
"type": "dart"
11+
}
12+
]
13+
}

android/app/src/main/AndroidManifest.xml

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,10 @@
22
package="com.example.blog_flutter">
33
<application
44
android:label="blog_flutter"
5-
android:icon="@mipmap/ic_launcher">
5+
android:icon="@mipmap/ic_launcher"
6+
android:usesCleartextTraffic="true"
7+
android:networkSecurityConfig="@xml/network_security_config"
8+
>
69
<activity
710
android:name=".MainActivity"
811
android:launchMode="singleTop"
@@ -38,4 +41,7 @@
3841
android:name="flutterEmbedding"
3942
android:value="2" />
4043
</application>
44+
<uses-permission android:name="android.permission.INTERNET"/>
45+
46+
4147
</manifest>
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<network-security-config>
3+
<base-config cleartextTrafficPermitted="true">
4+
<trust-anchors>
5+
<certificates src="system" />
6+
</trust-anchors>
7+
</base-config>
8+
</network-security-config>

lib/config.dart

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,5 @@ const primaryColor = const MaterialColor(
1717
900: Color(0xFF1B5E20),
1818
},
1919
);
20+
21+
const baseUrl = 'http://192.168.1.135:3001';

lib/http.dart

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import 'package:blog_flutter/config.dart';
2+
import 'package:dio/dio.dart';
3+
import 'package:flutter/cupertino.dart';
4+
import 'package:flutter/material.dart';
5+
6+
class Http {
7+
factory Http() => _getInstance();
8+
static Http _instance;
9+
10+
Dio get http => _http;
11+
Dio _http;
12+
13+
static _getInstance() {
14+
if (_instance == null) {
15+
_instance = Http._();
16+
}
17+
return _instance;
18+
}
19+
20+
Http._() {
21+
BaseOptions options =
22+
new BaseOptions(baseUrl: baseUrl);
23+
InterceptorsWrapper interceptorsWrapper = new InterceptorsWrapper(
24+
onResponse: (res, handler) {
25+
// if (res.data.code != 0) {
26+
// return handler.reject(DioError(requestOptions: res.requestOptions));
27+
// }
28+
return handler.next(res);
29+
},
30+
onError: (e, handler) {
31+
AlertDialog(
32+
title: Text('网络出错'),
33+
content: Text(e.toString()),
34+
);
35+
return handler.next(e);
36+
},
37+
);
38+
39+
_http = Dio(options);
40+
_http.interceptors.add(interceptorsWrapper);
41+
}
42+
}
43+
44+
final http = new Http().http;

lib/widgets/home.dart

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import 'package:blog_flutter/config.dart';
2-
import 'package:english_words/english_words.dart';
2+
import 'package:blog_flutter/http.dart';
33
import 'package:flutter/material.dart';
44

55
class HomeWidget extends StatefulWidget {
@@ -32,7 +32,8 @@ class HomeWidgetState extends State<HomeWidget> {
3232
800: Color(0xFF2E7D32),
3333
900: Color(0xFF1B5E20),
3434
});
35-
var _articles = <String>[];
35+
var _articles = <dynamic>[];
36+
var _start = 0, _limit = 10, _total = 0;
3637

3738
@override
3839
void initState() {
@@ -41,18 +42,16 @@ class HomeWidgetState extends State<HomeWidget> {
4142
}
4243

4344
void _onTapArticle(index) {
44-
print(_articles[index]);
45+
print(index);
4546
}
4647

47-
void _getArticles() {
48-
Future.delayed(Duration(seconds: 1)).then((value) {
49-
setState(() {
50-
_articles.addAll(generateWordPairs()
51-
.take(20)
52-
.map((wordPair) => wordPair.asPascalCase)
53-
.toList());
54-
});
55-
print(_articles);
48+
void _getArticles() async {
49+
print('$_start ~ $_limit');
50+
var response = await http
51+
.get('/articles', queryParameters: {'start': _start, 'limit': _limit});
52+
setState(() {
53+
_articles.addAll(response.data['data']);
54+
_total = response.data['total'];
5655
});
5756
}
5857

@@ -72,7 +71,8 @@ class HomeWidgetState extends State<HomeWidget> {
7271
final backgroundColor = index % 2 == 0 ? lightGreen : lightGrey;
7372

7473
if (_articles.length == 0 || index == _articles.length - 1) {
75-
if (_articles.length < 51) {
74+
if (_articles.length < _total - 1) {
75+
_start += _limit;
7676
_getArticles();
7777
return Container(
7878
padding: EdgeInsets.all(16),
@@ -98,12 +98,12 @@ class HomeWidgetState extends State<HomeWidget> {
9898
return Container(
9999
decoration: BoxDecoration(color: backgroundColor),
100100
child: ListTile(
101-
title: Text(_articles[index],
101+
title: Text(_articles[index]['title'],
102102
overflow: TextOverflow.ellipsis,
103103
style: TextStyle(
104104
fontSize: 22, height: 1.5, color: Colors.green)),
105105
subtitle: Text(
106-
_articles[index] * 15,
106+
_articles[index]['content'],
107107
overflow: TextOverflow.ellipsis,
108108
maxLines: 2,
109109
style: TextStyle(fontSize: 16, height: 1.5),

pubspec.lock

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,13 @@ packages:
5050
url: "https://pub.flutter-io.cn"
5151
source: hosted
5252
version: "1.0.2"
53+
dio:
54+
dependency: "direct main"
55+
description:
56+
name: dio
57+
url: "https://pub.flutter-io.cn"
58+
source: hosted
59+
version: "4.0.0"
5360
english_words:
5461
dependency: "direct main"
5562
description:
@@ -74,6 +81,13 @@ packages:
7481
description: flutter
7582
source: sdk
7683
version: "0.0.0"
84+
http_parser:
85+
dependency: transitive
86+
description:
87+
name: http_parser
88+
url: "https://pub.flutter-io.cn"
89+
source: hosted
90+
version: "4.0.0"
7791
matcher:
7892
dependency: transitive
7993
description:
@@ -157,4 +171,4 @@ packages:
157171
source: hosted
158172
version: "2.1.0"
159173
sdks:
160-
dart: ">=2.12.0-0.0 <3.0.0"
174+
dart: ">=2.12.0 <3.0.0"

pubspec.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ dependencies:
3030
cupertino_icons: ^1.0.2
3131

3232
english_words: ^3.1.0
33+
dio: ^4.0.0
3334

3435
dev_dependencies:
3536
flutter_test:

0 commit comments

Comments
 (0)