Skip to content

Commit 8a2bf93

Browse files
committed
⚡ (performance) Create userTile widget.
1. User tile widget is created for userlistpage
1 parent 75e14be commit 8a2bf93

File tree

3 files changed

+85
-74
lines changed

3 files changed

+85
-74
lines changed

lib/page/common/usersListPage.dart

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@ class UsersListPage extends StatelessWidget {
3333
body: Consumer<AuthState>(
3434
builder: (context, state, child) {
3535
return UserListWidget(
36-
fetchingListbool: state.isbusy ?? false,
3736
list: userList,
3837
emptyScreenText: emptyScreenText,
3938
emptyScreenSubTileText: emptyScreenSubTileText,

lib/page/common/widget/userListWidget.dart

Lines changed: 84 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -13,19 +13,98 @@ import 'package:provider/provider.dart';
1313

1414
class UserListWidget extends StatelessWidget {
1515
final List<String> list;
16-
final fetchingListbool;
1716
final String emptyScreenText;
1817
final String emptyScreenSubTileText;
19-
String myId;
2018
UserListWidget(
2119
{Key key,
2220
this.list,
2321
this.emptyScreenText,
24-
this.fetchingListbool,
2522
this.emptyScreenSubTileText})
2623
: super(key: key);
27-
Widget _userTile(BuildContext context, User user) {
28-
bool isFollow = isFollowing(user);
24+
25+
@override
26+
Widget build(BuildContext context) {
27+
var state = Provider.of<AuthState>(context, listen: false);
28+
String myId = state.userModel.key;
29+
return list != null && list.isNotEmpty
30+
? ListView.separated(
31+
itemBuilder: (context, index) {
32+
return FutureBuilder(
33+
future: state.getuserDetail(list[index]),
34+
builder: (context, AsyncSnapshot<User> snapshot) {
35+
if (snapshot.hasData) {
36+
return UserTile(
37+
user: snapshot.data,
38+
myId: myId,
39+
);
40+
} else if (index == 0) {
41+
return Container(
42+
child: SizedBox(
43+
height: 3,
44+
child: LinearProgressIndicator(),
45+
));
46+
} else {
47+
return SizedBox.shrink();
48+
}
49+
},
50+
);
51+
},
52+
separatorBuilder: (context, index) {
53+
return Divider(
54+
height: 0,
55+
);
56+
},
57+
itemCount: list.length,
58+
)
59+
: state.isbusy
60+
? SizedBox(
61+
height: 3,
62+
child: LinearProgressIndicator(),
63+
)
64+
: Container(
65+
width: fullWidth(context),
66+
padding: EdgeInsets.only(top: 0, left: 30, right: 30),
67+
child: NotifyText(
68+
title: emptyScreenText,
69+
subTitle: emptyScreenSubTileText,
70+
),
71+
);
72+
}
73+
}
74+
75+
class UserTile extends StatelessWidget {
76+
const UserTile({Key key, this.user, this.myId}) : super(key: key);
77+
final User user;
78+
final String myId;
79+
80+
/// Return empty string for default bio
81+
/// Max length of bio is 100
82+
String getBio(String bio) {
83+
if (bio != null && bio.isNotEmpty && bio != "Edit profile to update bio") {
84+
if (bio.length > 100) {
85+
bio = bio.substring(0, 100) + '...';
86+
return bio;
87+
} else {
88+
return bio;
89+
}
90+
}
91+
return null;
92+
}
93+
94+
/// Check if user followerlist contain your or not
95+
/// If your id exist in follower list it mean you are following him
96+
bool isFollowing() {
97+
if (user.followersList != null &&
98+
user.followersList.any((x) => x == myId)) {
99+
return true;
100+
} else {
101+
return false;
102+
}
103+
}
104+
105+
@override
106+
Widget build(BuildContext context) {
107+
bool isFollow = isFollowing();
29108
return Container(
30109
padding: EdgeInsets.symmetric(vertical: 10),
31110
color: TwitterColor.white,
@@ -103,71 +182,4 @@ class UserListWidget extends StatelessWidget {
103182
),
104183
);
105184
}
106-
107-
String getBio(String bio) {
108-
if (bio != null && bio.isNotEmpty && bio != "Edit profile to update bio") {
109-
if (bio.length > 100) {
110-
bio = bio.substring(0, 100) + '...';
111-
return bio;
112-
} else {
113-
return bio;
114-
}
115-
}
116-
return null;
117-
}
118-
119-
bool isFollowing(User model) {
120-
if (model.followersList != null &&
121-
model.followersList.any((x) => x == myId)) {
122-
return true;
123-
} else {
124-
return false;
125-
}
126-
}
127-
128-
@override
129-
Widget build(BuildContext context) {
130-
var state = Provider.of<AuthState>(context, listen: false);
131-
myId = state.userModel.key;
132-
return list != null && list.isNotEmpty
133-
? ListView.separated(
134-
itemBuilder: (context, index) {
135-
return FutureBuilder(
136-
future: state.getuserDetail(list[index]),
137-
builder: (context, AsyncSnapshot<User> snapshot) {
138-
if (snapshot.hasData) {
139-
return _userTile(context, snapshot.data);
140-
} else if (index == 0) {
141-
return Container(
142-
child: SizedBox(
143-
height: 3,
144-
child: LinearProgressIndicator(),
145-
));
146-
} else {
147-
return SizedBox.shrink();
148-
}
149-
},
150-
);
151-
},
152-
separatorBuilder: (context, index) {
153-
return Divider(
154-
height: 0,
155-
);
156-
},
157-
itemCount: list.length,
158-
)
159-
: fetchingListbool
160-
? SizedBox(
161-
height: 3,
162-
child: LinearProgressIndicator(),
163-
)
164-
: Container(
165-
width: fullWidth(context),
166-
padding: EdgeInsets.only(top: 0, left: 30, right: 30),
167-
child: NotifyText(
168-
title: emptyScreenText,
169-
subTitle: emptyScreenSubTileText,
170-
),
171-
);
172-
}
173185
}

lib/state/authState.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ class AuthState extends AppState {
6363
databaseInit() {
6464
try {
6565
if (_profileQuery == null) {
66-
_profileQuery = kDatabase.child("profile").child(userModel.userId);
66+
_profileQuery = kDatabase.child("profile").child(userId);
6767
_profileQuery.onValue.listen(_onProfileChanged);
6868
}
6969
} catch (error) {

0 commit comments

Comments
 (0)