-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuser_list.dart
68 lines (63 loc) · 2.1 KB
/
user_list.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
import 'dart:developer';
import 'package:demo_flutter/models/user.dart';
import 'package:demo_flutter/models/user_list_response.dart';
import 'package:demo_flutter/services/user_service.dart';
import 'package:demo_flutter/utils/error_handaling.dart';
import 'package:flutter/material.dart';
import 'package:cached_network_image/cached_network_image.dart';
class UserList extends StatefulWidget {
@override
_UserListState createState() => _UserListState();
}
class _UserListState extends State<UserList> {
Future<UserListResponse> futureUsers;
@override
void initState() {
super.initState();
futureUsers = UserService.userList();
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: Text("User List")),
body: Center(
child: FutureBuilder<UserListResponse>(
future: futureUsers,
builder: (context, snapshot) {
if (snapshot.hasData) {
if (snapshot.data.isSuccess) {
return getUserListView(snapshot.data.users);
} else {
return Text(snapshot.data.message);
}
} else if (snapshot.hasError) {
return Text(ErrorHandling.SOMETHINF_IS_WRONG);
}
return CircularProgressIndicator();
},
),
),
);
}
Widget getUserListView(List<User> userList) {
return ListView.separated(
itemCount: userList.length,
separatorBuilder: (BuildContext context, int index) => Divider(),
itemBuilder: (BuildContext context, int index) {
User user = userList[index];
return ListTile(
key: Key(user.id.toString()),
title: Text(user.name),
subtitle: Text("Description comes here..."),
trailing: Icon(Icons.arrow_forward_ios),
leading: CachedNetworkImage(
placeholder: (context, url) => CircularProgressIndicator(),
imageUrl:
'https://picsum.photos/250?image=' + (index + 170).toString(),
),
onTap: () => {log(user.name + " clicked")},
);
},
);
}
}