Encrypted messaging app
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

197 lines
9.1 KiB

  1. import 'package:flutter/material.dart';
  2. import 'package:shared_preferences/shared_preferences.dart';
  3. import '/models/conversations.dart';
  4. import '/models/messages.dart';
  5. String convertToAgo(String input){
  6. DateTime time = DateTime.parse(input);
  7. Duration diff = DateTime.now().difference(time);
  8. if(diff.inDays >= 1){
  9. return '${diff.inDays} day${diff.inDays == 1 ? "" : "s"} ago';
  10. }
  11. if(diff.inHours >= 1){
  12. return '${diff.inHours} hour${diff.inHours == 1 ? "" : "s"} ago';
  13. }
  14. if(diff.inMinutes >= 1){
  15. return '${diff.inMinutes} minute${diff.inMinutes == 1 ? "" : "s"} ago';
  16. }
  17. if (diff.inSeconds >= 1){
  18. return '${diff.inSeconds} second${diff.inSeconds == 1 ? "" : "s"} ago';
  19. }
  20. return 'just now';
  21. }
  22. class ConversationDetail extends StatefulWidget{
  23. final Conversation conversation;
  24. const ConversationDetail({
  25. Key? key,
  26. required this.conversation,
  27. }) : super(key: key);
  28. @override
  29. _ConversationDetailState createState() => _ConversationDetailState();
  30. }
  31. class _ConversationDetailState extends State<ConversationDetail> {
  32. List<Message> messages = [];
  33. String userId = '';
  34. @override
  35. void initState() {
  36. super.initState();
  37. fetchMessages();
  38. }
  39. void fetchMessages() async {
  40. final preferences = await SharedPreferences.getInstance();
  41. userId = preferences.getString('userId')!;
  42. messages = await getMessagesForThread(widget.conversation.messageThreadKey);
  43. setState(() {});
  44. }
  45. @override
  46. Widget build(BuildContext context) {
  47. return Scaffold(
  48. appBar: AppBar(
  49. elevation: 0,
  50. automaticallyImplyLeading: false,
  51. backgroundColor: Colors.white,
  52. flexibleSpace: SafeArea(
  53. child: Container(
  54. padding: const EdgeInsets.only(right: 16),
  55. child: Row(
  56. children: <Widget>[
  57. IconButton(
  58. onPressed: (){
  59. Navigator.pop(context);
  60. },
  61. icon: const Icon(Icons.arrow_back,color: Colors.black,),
  62. ),
  63. const SizedBox(width: 2,),
  64. Expanded(
  65. child: Column(
  66. crossAxisAlignment: CrossAxisAlignment.start,
  67. mainAxisAlignment: MainAxisAlignment.center,
  68. children: <Widget>[
  69. Text(
  70. widget.conversation.name,
  71. style: const TextStyle(
  72. fontSize: 16,
  73. fontWeight: FontWeight.w600),
  74. ),
  75. ],
  76. ),
  77. ),
  78. const Icon(Icons.settings,color: Colors.black54,),
  79. ],
  80. ),
  81. ),
  82. ),
  83. ),
  84. body: Stack(
  85. children: <Widget>[
  86. ListView.builder(
  87. itemCount: messages.length,
  88. shrinkWrap: true,
  89. padding: const EdgeInsets.only(top: 10,bottom: 10),
  90. reverse: true,
  91. itemBuilder: (context, index) {
  92. return Container(
  93. padding: const EdgeInsets.only(left: 14,right: 14,top: 0,bottom: 0),
  94. child: Align(
  95. alignment: (
  96. messages[index].senderId == userId ?
  97. Alignment.topLeft:
  98. Alignment.topRight
  99. ),
  100. child: Column(
  101. crossAxisAlignment: messages[index].senderId == userId ?
  102. CrossAxisAlignment.start :
  103. CrossAxisAlignment.end,
  104. children: <Widget>[
  105. Container(
  106. decoration: BoxDecoration(
  107. borderRadius: BorderRadius.circular(20),
  108. color: (
  109. messages[index].senderId == userId ?
  110. Colors.grey.shade200 :
  111. Colors.blue[200]
  112. ),
  113. ),
  114. padding: const EdgeInsets.all(12),
  115. child: Text(messages[index].data, style: const TextStyle(fontSize: 15)),
  116. ),
  117. messages[index].senderId != userId ?
  118. Text(messages[index].senderUsername) :
  119. const SizedBox.shrink(),
  120. Text(
  121. convertToAgo(messages[index].createdAt),
  122. textAlign: TextAlign.left,
  123. style: TextStyle(
  124. fontSize: 12,
  125. color: Colors.grey[500],
  126. ),
  127. ),
  128. ]
  129. )
  130. ),
  131. );
  132. },
  133. ),
  134. Align(
  135. alignment: Alignment.bottomLeft,
  136. child: ConstrainedBox(
  137. constraints: const BoxConstraints(
  138. maxHeight: 200.0,
  139. ),
  140. child: Container(
  141. padding: const EdgeInsets.only(left: 10,bottom: 10,top: 10),
  142. // height: 60,
  143. width: double.infinity,
  144. color: Colors.white,
  145. child: Row(
  146. children: <Widget>[
  147. GestureDetector(
  148. onTap: (){
  149. },
  150. child: Container(
  151. height: 30,
  152. width: 30,
  153. decoration: BoxDecoration(
  154. color: Colors.lightBlue,
  155. borderRadius: BorderRadius.circular(30),
  156. ),
  157. child: const Icon(Icons.add, color: Colors.white, size: 20, ),
  158. ),
  159. ),
  160. const SizedBox(width: 15,),
  161. const Expanded(
  162. child: TextField(
  163. decoration: InputDecoration(
  164. hintText: "Write message...",
  165. hintStyle: TextStyle(color: Colors.black54),
  166. border: InputBorder.none,
  167. ),
  168. maxLines: null,
  169. ),
  170. ),
  171. const SizedBox(width: 15,),
  172. FloatingActionButton(
  173. onPressed: () {
  174. },
  175. child: const Icon(Icons.send,color: Colors.white,size: 18,),
  176. backgroundColor: Colors.blue,
  177. elevation: 0,
  178. ),
  179. ],
  180. ),
  181. ),
  182. ),
  183. ),
  184. ],
  185. ),
  186. );
  187. }
  188. }