void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return App();
}
}
class App extends StatelessWidget {
@override
Widget build(BuildContext context) {
// TODO: implement build
return MaterialApp(
home: MovieScreen(),
);
}
}
import 'package:dio/dio.dart';
import 'dart:convert';
class RequestManager {
Dio dio;
static RequestManager _requestManager;
static RequestManager getInstance() {
if (_requestManager == null) {
_requestManager = RequestManager();
}
return _requestManager;
}
RequestManager() {
dio = Dio();
dio.options.connectTimeout = 5000; // 5s
dio.options.receiveTimeout = 3000;
}
get(String url, Map params,
{Function success, Function failure}) async {
try {
Response response;
response = await dio.get(url, queryParameters: params);
Map data = json.decode(response.toString());
success(data);
} catch (e) {
print("RequestManager - e ${e}");
failure(e);
}
}
}
class Movie {
String title; // 名称
String imageUrl; // 图片链接
int commentCount; // 评论数
double mark; // 评分
String type; // 类型
Movie.fromMap(Map json) {
this.title = json["title"];
this.imageUrl = json["images"]["small"];
this.commentCount = json["collect_count"];
this.mark = json["rating"]["average"];
List genres = json["genres"];
this.type = genres.join(" / ");
}
}
class MovieScreen extends StatelessWidget {
@override
Widget build(BuildContext context) {
// TODO: implement build
return Scaffold(
appBar: AppBar(
title: Text("豆瓣影评"),
),
body: MovieList(),
);
}
}
class MovieList extends StatefulWidget {
@override
State createState() {
return MovieListState();
}
}
class MovieListState extends State {
List movies = [];
// 电影列表
void getMoiveListData() {
var url = "https://douban.uieee.com/v2/movie/top250";
var params = {"start": "0", "count": "10"};
RequestManager.getInstance().get(url, params,success: (value){
setState(() {
List subjects = value["subjects"];
subjects.forEach((value) {
movies.add(Movie.fromMap(value));
});
});
},failure: (value){
print("exceptaion- ${value}");
print("failure");
});
}
@override
void initState() {
super.initState();
getMoiveListData();
}
@override
Widget build(BuildContext context) {
return ListView.builder(
itemCount: movies.length,
itemBuilder: (context,index) {
return MoiveRow(movies[index]);
},
);
}
}
class MoiveRow extends StatelessWidget {
final Movie movie;
MoiveRow(this.movie);
// 电影排名
Widget MovieRankWidget() {
return Container(
padding: EdgeInsets.fromLTRB(10.0, 5.0, 10.0, 5.0),
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(3.0),
color: Colors.orange,
),
child: Text("No.1",style: TextStyle(fontSize: 18.0,color: Colors.white),),
);
}
// 具体内容
Widget MovieContent() {
return Container(
height: 150,
child: Row(
children: [
MovieIcon(),
MovieDescribe(),
],
),
);
}
// 电影图片
Widget MovieIcon() {
return ClipRRect(
borderRadius: BorderRadius.circular(5.0),
child: Image.network(movie.imageUrl),
);
}
// 描述
Widget MovieDescribe() {
return Expanded(
child: Container(
padding: EdgeInsets.only(left: 15),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
MovieTitle(),
Padding(padding: EdgeInsets.only(top: 5.0),),
MovieMark(),
Padding(padding: EdgeInsets.only(top: 5.0),),
MovieDetail(),
],
),
),
);
}
// 电影名称
Widget MovieTitle() {
return Text(
"${movie.title}",
style: TextStyle(fontSize: 20.0,color: Colors.black),
);
}
// 评分
Widget MovieMark() {
return Text("豆瓣评分: ${movie.mark}",style: TextStyle(fontSize: 13.0,color: Colors.grey),);
}
// 具体详情
Widget MovieDetail() {
return Text(
"${movie.type}",
maxLines: 5,
softWrap: true,
style: TextStyle(fontSize: 16.0,color: Colors.grey),
);
}
// 评价
Widget MovieComment() {
return Container(
color: Colors.black12,
height: 30.0,
padding: EdgeInsets.all(5.0),
child: Row(
crossAxisAlignment: CrossAxisAlignment.center,
children: [
Text("${movie.commentCount}评价",style: TextStyle(fontSize: 15.0,color: Colors.blueGrey),)
],
),
);
}
@override
Widget build(BuildContext context) {
return Container(
padding: EdgeInsets.all(10.0),
decoration: BoxDecoration( // 分割线
border: Border(bottom: BorderSide(width: 10, color: Colors.black12))
),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
// 排名
MovieRankWidget(),
Padding(padding: EdgeInsets.only(top: 10.0),),
// 具体内容
MovieContent(),
Padding(padding: EdgeInsets.only(top: 10.0),),
// 评价
MovieComment(),
],
),
);
}
}