-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathIndexPage.dart
96 lines (86 loc) · 2.64 KB
/
IndexPage.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
import 'dart:convert';
import 'dart:io';
import 'package:flutter/material.dart';
import 'package:flutter_app/index/IndexPageBannerBean.dart';
import 'package:flutter_app/utils/BannerView.dart';
class IndexPage extends StatefulWidget {
@override
createState() => new IndexPageState();
}
class IndexPageState extends State<IndexPage>
with AutomaticKeepAliveClientMixin {
List<BannerChildBean> mResult = [];
String bannerTitle = "ssss";
@override
bool get wantKeepAlive => true;
_getIPAddress() async {
var url = 'http://www.wanandroid.com/banner/json';
var httpClient = new HttpClient();
List<BannerChildBean> result;
try {
var request = await httpClient.getUrl(Uri.parse(url));
var response = await request.close();
if (response.statusCode == HttpStatus.OK) {
var json = await response.transform(UTF8.decoder).join();
result = new IndexPageBannerBean.fromJson(JSON.decode(json)).data;
} else {
result = null;
}
request.close();
} catch (exception) {
result = null;
}
// If the widget was removed from the tree while the message was in flight,
// we want to discard the reply rather than calling setState to update our
// non-existent appearance.
if (!mounted) return;
setState(() {
mResult = result;
bannerTitle = result[0].title;
});
}
@override
void initState() {
super.initState();
_getIPAddress();
}
@override
Widget build(BuildContext context) {
return SingleChildScrollView(
child: Column(
children: <Widget>[
Stack(
alignment: Alignment.bottomCenter,
children: <Widget>[
BannerView(
data: mResult == null ? [] : mResult,
onPageChanged: onPageChanged,
buildShowView: (index, itemData) {
return new FadeInImage(
placeholder: ExactAssetImage("images/image.png"),
image: NetworkImage(mResult[index].imagePath));
},
),
Container(
alignment: Alignment.topLeft,
padding: const EdgeInsets.fromLTRB(12.0, 8.0, 12.0, 8.0),
decoration: BoxDecoration(color: Colors.black38),
child: Text(
bannerTitle,
style: TextStyle(color: Colors.white),
),
)
],
)
],
),
);
}
void onPageChanged(int index) {
setState(() {
if (mResult != null && mResult.length > index) {
this.bannerTitle = mResult[index].title;
}
});
}
}