Created
May 26, 2020 02:10
-
-
Save huxaiphaer/34f345c45a60df65bdffcf9c7e18e591 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'dart:async'; | |
import 'dart:developer'; | |
import 'package:flutter/material.dart'; | |
import 'package:flutter_svg/flutter_svg.dart'; | |
import 'package:newvisionapp/src/resources/services/database.dart'; | |
import 'package:newvisionapp/src/ui/comments/comments_tile.dart'; | |
import 'package:newvisionapp/src/utils/utils.dart'; | |
class CommentsPage extends StatefulWidget { | |
final String articleId; | |
final String articleEmail; | |
const CommentsPage({Key key, this.articleId, this.articleEmail}) | |
: super(key: key); | |
@override | |
_CommentsPageState createState() => _CommentsPageState(); | |
} | |
class _CommentsPageState extends State<CommentsPage> { | |
TextEditingController messageController = new TextEditingController(); | |
DatabaseMethods databaseMethods = new DatabaseMethods(); | |
LocalStorage localStorage = new LocalStorage(); | |
ScrollController _scrollController = new ScrollController(); | |
Stream commentsStream; | |
var userObject; | |
String _noOfComments = ''; | |
sendMessage() async { | |
if (isAuthorAvailable()) { | |
isAuthorSend(1); | |
} else { | |
isAuthorSend(0); | |
} | |
} | |
isAuthorSend(value) async { | |
var user = await localStorage.getUserDetails(); | |
if (messageController.text.isNotEmpty) { | |
var lastNameCheck = user.lastName.toString().trim() == "null" || | |
user.lastName.toString().trim() == "NULL" | |
? "" | |
: user.lastName; | |
Map<String, dynamic> messageMap = { | |
"message": messageController.text, | |
"username": '${user.firstName} $lastNameCheck', | |
"email": user.email, | |
"sendBy": user.email, | |
"time": DateTime.now().millisecondsSinceEpoch, | |
"isAuthor": value | |
}; | |
databaseMethods.addConversationMessages( | |
articleId: widget.articleId, messageMap: messageMap); | |
messageController.text = ""; | |
} | |
} | |
isAuthorAvailable() { | |
return userObject.email == widget.articleEmail; | |
} | |
int calculateDifference(DateTime date) { | |
DateTime now = DateTime.now(); | |
return DateTime(date.year, date.month, date.day) | |
.difference(DateTime(now.year, now.month, now.day)) | |
.inDays; | |
} | |
formatDate(d) { | |
DateTime date = new DateTime.fromMillisecondsSinceEpoch(d); | |
if (calculateDifference(date) == -1) { | |
return "YESTERDAY"; | |
} | |
if (calculateDifference(date) == 0) { | |
return "TODAY"; | |
} | |
return '${date.day} ${date.month.toString()}'; | |
} | |
Widget _numberOfComments() { | |
return Container( | |
child: StreamBuilder( | |
stream: commentsStream, | |
builder: (_context, _snapshot) { | |
if (!_snapshot.hasData) { | |
return Container(); | |
} | |
if (_snapshot.data.documents != null && _snapshot != null) { | |
_noOfComments = '${_snapshot.data.documents.length}'; | |
return Text( | |
'${_noOfComments != null ? _noOfComments : "0 Comments "} Comments', | |
style: TextStyle( | |
color: Color(0xFF9E9E9E), | |
fontWeight: FontWeight.normal, | |
fontSize: 15, | |
letterSpacing: 0.4, | |
), | |
); | |
} else { | |
Text('0 Comments', | |
style: TextStyle( | |
color: Color(0xFF9E9E9E), | |
fontWeight: FontWeight.normal, | |
fontSize: 15, | |
letterSpacing: 0.4, | |
)); | |
} | |
return Text('0 Comments', | |
style: TextStyle( | |
color: Color(0xFF9E9E9E), | |
fontWeight: FontWeight.normal, | |
fontSize: 15, | |
letterSpacing: 0.4, | |
)); | |
}, | |
), | |
); | |
} | |
Widget commentsList() { | |
if (userObject != null) { | |
Timer( | |
Duration(milliseconds: 800), | |
() => _scrollController | |
.jumpTo(_scrollController.position.maxScrollExtent)); | |
return StreamBuilder( | |
stream: commentsStream, | |
builder: (context, snapshot) { | |
if (!snapshot.hasData) { | |
return Container(); | |
} | |
return GestureDetector( | |
onTap: () { | |
FocusScope.of(context).unfocus(); | |
}, | |
child: snapshot.data.documents.length != 0 | |
? ListView.builder( | |
itemCount: snapshot.data.documents.length, | |
physics: AlwaysScrollableScrollPhysics(), | |
controller: _scrollController, | |
reverse: true, | |
itemBuilder: (context, index) { | |
return CommentsTile( | |
message: snapshot.data.documents[index].data["message"], | |
isAuthor: int.parse(snapshot | |
.data.documents[index].data["isAuthor"] | |
.toString() | |
.trim()) == | |
0 | |
? false | |
: true, | |
value: int.parse(snapshot | |
.data.documents[index].data["isAuthor"] | |
.toString() | |
.trim()), | |
name: snapshot.data.documents[index].data["username"], | |
date: formatDate( | |
snapshot.data.documents[index].data["time"]), | |
); | |
}) | |
: Center( | |
child: Text( | |
"No comments", | |
style: TextStyle( | |
color: Colors.black, | |
fontSize: 15, | |
fontStyle: FontStyle.italic), | |
), | |
), | |
); | |
}, | |
); | |
} | |
return Container(); | |
} | |
loadUser() async { | |
userObject = await localStorage.getUserDetails(); | |
} | |
onReloadNewChat() { | |
_scrollController.animateTo( | |
_scrollController.position.maxScrollExtent, | |
curve: Curves.easeOut, | |
duration: const Duration(milliseconds: 500), | |
); | |
} | |
@override | |
void initState() { | |
// TODO: implement initState | |
databaseMethods | |
.getCommentsPerArticle(articleId: widget.articleId) | |
.then((value) { | |
setState(() { | |
commentsStream = value; | |
}); | |
}); | |
loadUser(); | |
setState(() {}); | |
super.initState(); | |
} | |
@override | |
Widget build(BuildContext context) { | |
return Container( | |
child: Container( | |
margin: EdgeInsets.all(20), | |
child: Column( | |
children: <Widget>[ | |
Row( | |
mainAxisAlignment: MainAxisAlignment.spaceBetween, | |
children: <Widget>[ | |
Text( | |
'Comments', | |
style: TextStyle( | |
color: Colors.black, | |
letterSpacing: 0.1, | |
fontSize: 16, | |
fontWeight: FontWeight.w500), | |
), | |
_numberOfComments(), | |
], | |
), | |
SizedBox( | |
height: 20, | |
), | |
Divider(height: 20.0, color: Color.fromARGB(33, 33, 33, 6)), | |
SizedBox( | |
height: 20, | |
), | |
Container(height: 200, child: commentsList()), | |
SizedBox( | |
height: 20, | |
), | |
Row( | |
children: <Widget>[ | |
Expanded( | |
child: Container( | |
width: 50, | |
child: Material( | |
borderRadius: | |
const BorderRadius.all(const Radius.circular(30.0)), | |
elevation: 10.0, | |
child: TextField( | |
onTap: () { | |
_scrollController.jumpTo( | |
_scrollController.position.maxScrollExtent); | |
}, | |
keyboardType: TextInputType.multiline, | |
maxLines: null, | |
controller: messageController, | |
textInputAction: TextInputAction.send, | |
decoration: InputDecoration( | |
contentPadding: const EdgeInsets.all(15.0), | |
fillColor: Colors.white, | |
enabledBorder: const OutlineInputBorder( | |
// width: 0.0 produces a thin "hairline" border | |
borderSide: const BorderSide( | |
color: Colors.white70, width: 0.0), | |
), | |
focusedBorder: UnderlineInputBorder( | |
borderSide: BorderSide(color: Colors.transparent), | |
), | |
hintText: " Typing", | |
), | |
), | |
), | |
), | |
), | |
RawMaterialButton( | |
onPressed: () { | |
sendMessage(); | |
_scrollController.animateTo( | |
_scrollController.position.maxScrollExtent, | |
curve: Curves.easeOut, | |
duration: const Duration(milliseconds: 500), | |
); | |
setState(() {}); | |
}, | |
elevation: 10.0, | |
fillColor: Color(0xFFF7DA00), | |
child: SvgPicture.asset('assets/send_icon.svg'), | |
padding: EdgeInsets.all(10.0), | |
shape: CircleBorder(), | |
), | |
], | |
), | |
SizedBox( | |
height: 30.0, | |
) | |
], | |
), | |
), | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment