Skip to content

Commit 27bf232

Browse files
committed
ImagePicker fix in settings and lazy load in chat
1 parent 663acaa commit 27bf232

File tree

2 files changed

+36
-7
lines changed

2 files changed

+36
-7
lines changed

lib/chat.dart

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,9 @@ class ChatScreenState extends State<ChatScreen> {
5757
String peerAvatar;
5858
String id;
5959

60-
List<QueryDocumentSnapshot> listMessage;
60+
List<QueryDocumentSnapshot> listMessage = new List.from([]);
61+
int _limit = 20;
62+
final int _limitIncrement = 20;
6163
String groupChatId;
6264
SharedPreferences prefs;
6365

@@ -70,10 +72,31 @@ class ChatScreenState extends State<ChatScreen> {
7072
final ScrollController listScrollController = ScrollController();
7173
final FocusNode focusNode = FocusNode();
7274

75+
_scrollListener() {
76+
if (listScrollController.offset >=
77+
listScrollController.position.maxScrollExtent &&
78+
!listScrollController.position.outOfRange) {
79+
print("reach the bottom");
80+
setState(() {
81+
print("reach the bottom");
82+
_limit += _limitIncrement;
83+
});
84+
}
85+
if (listScrollController.offset <=
86+
listScrollController.position.minScrollExtent &&
87+
!listScrollController.position.outOfRange) {
88+
print("reach the top");
89+
setState(() {
90+
print("reach the top");
91+
});
92+
}
93+
}
94+
7395
@override
7496
void initState() {
7597
super.initState();
7698
focusNode.addListener(onFocusChange);
99+
listScrollController.addListener(_scrollListener);
77100

78101
groupChatId = '';
79102

@@ -178,7 +201,10 @@ class ChatScreenState extends State<ChatScreen> {
178201
listScrollController.animateTo(0.0,
179202
duration: Duration(milliseconds: 300), curve: Curves.easeOut);
180203
} else {
181-
Fluttertoast.showToast(msg: 'Nothing to send');
204+
Fluttertoast.showToast(
205+
msg: 'Nothing to send',
206+
backgroundColor: Colors.black,
207+
textColor: Colors.red);
182208
}
183209
}
184210

@@ -667,7 +693,7 @@ class ChatScreenState extends State<ChatScreen> {
667693
.doc(groupChatId)
668694
.collection(groupChatId)
669695
.orderBy('timestamp', descending: true)
670-
.limit(20)
696+
.limit(_limit)
671697
.snapshots(),
672698
builder: (context, snapshot) {
673699
if (!snapshot.hasData) {
@@ -676,7 +702,7 @@ class ChatScreenState extends State<ChatScreen> {
676702
valueColor:
677703
AlwaysStoppedAnimation<Color>(themeColor)));
678704
} else {
679-
listMessage = snapshot.data.documents;
705+
listMessage.addAll(snapshot.data.documents);
680706
return ListView.builder(
681707
padding: EdgeInsets.all(10.0),
682708
itemBuilder: (context, index) =>

lib/settings.dart

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -69,9 +69,12 @@ class SettingsScreenState extends State<SettingsScreen> {
6969
}
7070

7171
Future getImage() async {
72-
ImagePicker imagePicker;
73-
File image =
74-
await imagePicker.getImage(source: ImageSource.gallery) as File;
72+
ImagePicker imagePicker = ImagePicker();
73+
PickedFile pickedFile;
74+
75+
pickedFile = await imagePicker.getImage(source: ImageSource.gallery);
76+
77+
File image = File(pickedFile.path);
7578

7679
if (image != null) {
7780
setState(() {

0 commit comments

Comments
 (0)