Implementing Syde An online Forum in Flutter (Update 3 : Add Comments to a story)
utopian-io·@ideba·
0.000 HBDImplementing Syde An online Forum in Flutter (Update 3 : Add Comments to a story)
### Repository https://github.com/enyason/Syde <center></center> ### History - ##### [Implementing Syde An online Forum in Flutter (Update 2 : Post A Story, Retrieve All Stories)](https://steemit.com/utopian-io/@ideba/implementing-syde-an-online-forum-in-flutter-update-2--post-a-story-retrieve-all-stories-) - [Implementing Syde An online Forum in Flutter (Update : Authenticate With Google)](https://steemit.com/utopian-io/@ideba/implementing-syde-an-online-forum-in-flutter-update--authenticate-with-google) - [Implementing Syde(An online Forum) in Flutter](https://steemit.com/utopian-io/@ideba/implementing-sydean-online-forum-in-flutter) ### New Features - **Add comments to a story** - **UI Update** ### Add comments to a story Beyond creating a story on the platform, users can also comment on a specific story. This feature which is part of the back-end implementation is Handled by Fire-base. ***Implementation*** - **Create a Comment Object** ``` class Comment { String commentBody, commentId, userId, timeStamp; Comment({this.commentBody, this.commentId, this.userId, this.timeStamp}); factory Comment.fromMap(Map comment) { return Comment( commentBody: comment["comment_body"], commentId: comment["comment_id"], timeStamp: comment["time_stamp"], userId: comment["user_id"], ); } Map<String, dynamic> toMap() { return { "comment_body": commentBody, "comment_id": commentId, "time_stamp": timeStamp, "user_id": userId }; } } ``` - This class shows a basic structure of the comment object - **Add a Comment** ``` addComment(){ Map<String, dynamic> data = Comment( commentBody: _controller.value.text, commentId: "na", userId: _user.uid, timeStamp: DateTime.now() .millisecondsSinceEpoch .toString()) .toMap(); _controller.clear(); _commentRef.add(data).then((docRef) { _commentRef .document(docRef.documentID) .updateData({"comment_id": docRef.documentID}); incrementComment(widget.story.storyId); } ); } ``` - This method takes the users message from the text-field and adds it to the lists of comments - **Increment Comment Count** ``` incrementComment(String docRef) async { //use transaction on the fire store instance to prevent race condition Firestore.instance.runTransaction((transaction) async { //get a document snapshot at the current position DocumentSnapshot snapshot = await transaction.get(Firestore.instance.collection("all_post").document(docRef)); //increment comment count for the story await transaction.update(snapshot.reference, {"comment_count": snapshot.data["comment_count"] + 1}); }); } ``` - To Keep track of the number of comments, I have a field called "comment_count" - This method gets the document reference and increments the number by 1 - **Listen to Comment Count** ``` StreamBuilder( stream: Firestore.instance .collection("all_post") .document(document.data.documents[index] ["post_id"]).snapshots(), builder: (BuildContext context, AsyncSnapshot snapshot) { return Padding( padding: const EdgeInsets.only(right: 20.0), child: Row( children: <Widget>[ Icon(Icons.chat_bubble_outline, color: Colors.grey, size: 18.0), Text( snapshot.data == null ? "0" : snapshot .data["comment_count"] .toString(), style: TextStyle( color: Colors.grey, fontSize: 10.0), ), ], ), ); }, )), ``` - With the stream builder widget, I listen to the node that carries the "comment_count" filed. With this, there is a real-time update of the number of comments made on a particular story <center> <p float="left"> <img src="/https://steemitimages.com/p/2bP4pJr4wVimqCWjYimXJe2cnCgn9eVrBqRBsERBjX4?format=match&mode=fit&width=640&height=600.png" /> <img src="/https://steemitimages.com/p/4W3i2hyrJTVfeKM2gnkYJZVrfqSuqn15iebdkSwKmQKMDjDNH96?format=match&mode=fit&width=640&height=600" /> </p> </center> ### More UI Layouts <center> <p float="left"> <img src="/https://steemitimages.com/p/2bP4pJr4wVimqCWjYimXJe2cnCgnLqBAjKPbziY55LS?format=match&mode=fit&width=640&height=500.png" width="100" /> <img src="/https://steemitimages.com/p/2bP4pJr4wVimqCWjYimXJe2cnCgnKZrVqgFdDdpfZ3L?format=match&mode=fit&width=640&height=500.png" width="100" /> <img src="/https://steemitimages.com/p/2ADPRBseKViToZTQ1BqZsv9HK2WaGBarbJx3Xi6yVy74uarn7oJeYLfuccytGk3WE6rPvXHwN6KRg4tKPWyZAPS7m6TVphoSXEg5WLhT8EYYEm2gW6oLRaUn6AVVe1wcL1QfzFfhVQJX3jMPftezDtB7wTZPS29cu8ia2XkrCVJ86kFqs5ecys6PMwfHQmN1PVWBpmuYU3R6xk3ekayQaAmLmzYefjzfTLKwVFeXzcUL5gaaTGUja6z5oK6GM2KWk6GAxaKJj29KupzRJTGf1JtifkgmjgAGPk8RGZYgxYezSA3hkLrddKkSz3H93QNCVMLKKfNyk6yowSXrgmMjp3xM869qbSH43KCqT9KLVqHQHtwTtsMrnChjSptxUwsmgzHWy7bWR3ZsdhSoMEmSikkFR4M23LToes7m?format=match&mode=fit&width=640&height=500" width="100" /> </p> </center> Minor modifications have been made, as specified by my designer. Link to complete UI design https://www.figma.com/file/Rb5CIpYQoOtFI52awQrC6Yo7/SydeTeam **N/B** Other functionalities stated in the GitHub commits are still in a basic structure. As these functionalities are updated to work fully, I will duly write about them. ### IMPORTANT RESOURCES *Github *: https://github.com/enyason/Syde ### Road Map - ~~Authenticate User~~ - ~~Persisting user post~~ - ~~Displaying all post~~ - ~~Detailed screen implementation~~ - ~~Story Reaction Implementation (like and unlike a story)~~ - ~~Adding Comments to a story~~ - Searching all posts - ~~Providing different layouts~~ - ToDo for users to track their daily progress - Push Notifications - Posts sharing - Users dashboard - Searching all users - Direct Messaging with Users - ChatGroup - Bookmarking post ### How to contribute? You can reach me by commenting on this post or send a message via enyasonjnr@gmail.com. If you want to make this application better, you can make a Pull Request. [Github](https://github.com/enyason/Syde)
👍 cheneats, votes4minnows, imisstheoldkanye, marina007, primeradue, ideba, bogdasha, ezravandi, jadabug, hamsa.quality, hdu, delabo, bayba, digraigs, ombisput, irolyem, indis, unend, editoredi, ronced, unsarted, ishac, itranedi, odondel, erara, odended, rarso, eshofedsa, enstanlossma, taylor5d1cfhall, tairenrephe, simpwendoubtli, zoe69, grace0c3, okalbucar, jasmine4p, phelrenew, kornhafvakim, dyegadese, gonanmaco, chloexdrmnmiller, cianobogen, gorraudechan, distcountevi, elizabethg4, oliviahnl, mosuffcurge, jennifer07908, nasdaq.analitizz, madhardmax, get.kayl, oncidrow, ucelorsal, inganda, omaryomuc, esisc, amosbastian, tobias-g, rufans, helo, jaff8, ismailkah, fego, bukiland, yassinebad, sweetone, leyt, ahmedess, codingdefined, espoem, mops2e, ascorphat, mcfarhat, ulockblock, greenorange, bachuslib, properfraction, phage93, steem-plus, lostmine27, ajayyy, utopian.trail, techslut, minersean, erikaflynn, jakipatryk, suesa, che-shyr, didic, daan, abh12345, tykee, silviu93, dakeshi, vishalsingh4997, elear, zoneboy, mcyusuf, gentleshaid, aussieninja, bflanagin, dssdsds, jayplayco, stem-espanol, mrsbozz, jk6276.mons, jaxson2011, sargoon, darewealth, dongentle2, jubreal, rollthedice, jk6276, rewarding, tsoldovieri, iamphysical, felixrodriguez, azulear, carloserp-2000, vjap55, vanarchist, miguelangel2801, emiliomoron, ulisesfl17, tomastonyperez, elvigia, luiscd8a, elpdl, josedelacruz, joseangelvs, viannis, erickyoussif, yusvelasquez, ubaldonet, lupafilotaxia, fran.frey, aleestra, giulyfarci52, lorenzor, ivymalifred, eliaschess333, ydavgonzalez, arac, andrick, joelsegovia, jesusfl17, mary11, amart29, jrevilla, wilmer14molina, alaiza, lapp, steemtpistia, crassipes, agrovision, flores39, buckydurddle, mirkosche, eastmael, dalz, alexs1320, cpufronz, scienceangel, yu-stem, amestyj, eniolw, geadriana, alex-hm, ennyta, anaestrada12, carlos84, douglimarbalzan, gaming.yer, steem-familia, evangelista.yova, endopediatria, ingmarvin, elimao, antunez25, chavas, uzcateguiazambra, asmeira, rubenp, jeferc, hirally, eugenialobo, jcmontilva, marbely20, moyam, darys, sibaja, balcej, lmanjarres, anaka, benhurg, judisa, juddarivv, kimmorales, loraine25, steem-ua, newsrx, bejust, feronio, progressing, tdre, jjay, ryuna.siege, utopian-io, tombstone, jga, cryptouno, dicetime, kaczynski,