页面传递参数

第一个页面:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
class Page extends StatelessWidget {
final String title;

Page({this.title});

@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(title),
elevation: 0.0,
),
body: BlogSheet(title: title),
);
}
}

第二个页面

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
class BlogSheet extends StatefulWidget {
BlogSheet({Key key, this.title}) : super(key: key);
final String title;

@override
_BlogSheetState createState() => _BlogSheetState();
}

class _BlogSheetState extends State<BlogSheet> {
List bloglists;

@override
void initState() {
super.initState();

getData(type: 'api', category: widget.title);
}

Future getData({String type = 'api', String category = 'PHP'}) async {
final String url = "http://localhost:8081/$type?category=$category";
final response = await http.get(url);

if (response.statusCode == 200) {
List top = json.decode(response.body);
setState(() {
bloglists = top.map((json) => Blog.fromJson(json)).toList();
});
} else {
print("err code $response.statusCode");
}
}

@override
Widget build(BuildContext context) {
return Container(
child: bloglists == null
? Center(child: CircularProgressIndicator())
: Padding(
padding: EdgeInsets.symmetric(horizontal: 6.0, vertical: 10.0),
child: ListView.builder(
itemCount: bloglists.length,
itemBuilder: (BuildContext context, int index) {
return BlogCard(bloglists[index]);
},
),
),
);
}
}