Advertisement
Guest User

Untitled

a guest
May 21st, 2021
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.28 KB | None | 0 0
  1. import 'package:flutte/PageChatView/user_model.dart';
  2. import 'package:flutter/material.dart';
  3. import 'package:socket_io_client/socket_io_client.dart' as IO;
  4. import '../main.dart';
  5. import 'MessageModel.dart';
  6.  
  7.  
  8. class ChatScreen extends StatefulWidget {
  9. final User user;
  10. final int id;
  11. ChatScreen({this.user, this.id,});
  12.  
  13. @override
  14. _ChatScreenState createState() => _ChatScreenState(id);
  15. }
  16.  
  17. class _ChatScreenState extends State<ChatScreen> {
  18. User user;
  19.  
  20. getmsg()async{
  21. socket.emit('getMessages',1);
  22. socket.on('chatMessages', (data1)=>{
  23. // user = data.getUser(32),
  24. for( var i = 0 ; i < data1.length; i++ ) {
  25. // user = User.fromJson(data1[i]),
  26. debugPrint("omg"),
  27. // if(data1[i]['user_id'])
  28. data.contacts.add(
  29. Message(
  30. sender: user,
  31. time: " ",
  32. text: data1[i]['message'].toString(),
  33. unread: true,
  34. ),
  35. ),
  36. },
  37. debugPrint(data.toString())
  38. });
  39. }
  40.  
  41. final int id;
  42. TextEditingController textEditingController = new TextEditingController();
  43.  
  44.  
  45. _ChatScreenState(this.id);
  46.  
  47. _chatBubble(Message message, bool isMe, bool isSameUser) {
  48. data.connection('connected', null);
  49. if (isMe) {
  50. return Column(
  51. children: <Widget>[
  52. Container(
  53. alignment: Alignment.topRight,
  54. child: Container(
  55. constraints: BoxConstraints(
  56. maxWidth: MediaQuery.of(context).size.width * 0.80,
  57. ),
  58. padding: EdgeInsets.all(10),
  59. margin: EdgeInsets.symmetric(vertical: 10),
  60. decoration: BoxDecoration(
  61. color: Theme.of(context).primaryColor,
  62. borderRadius: BorderRadius.circular(15),
  63. boxShadow: [
  64. BoxShadow(
  65. color: Colors.grey.withOpacity(0.5),
  66. spreadRadius: 2,
  67. blurRadius: 5,
  68. ),
  69. ],
  70. ),
  71. child: Text(
  72. message.text,
  73. style: TextStyle(
  74. color: Colors.white,
  75. ),
  76. ),
  77. ),
  78. ),
  79. !isSameUser
  80. ? Row(
  81. mainAxisAlignment: MainAxisAlignment.end,
  82. children: <Widget>[
  83. Text(
  84. message.time,
  85. style: TextStyle(
  86. fontSize: 12,
  87. color: Colors.black45,
  88. ),
  89. ),
  90. SizedBox(
  91. width: 10,
  92. ),
  93. Container(
  94. decoration: BoxDecoration(
  95. shape: BoxShape.circle,
  96. boxShadow: [
  97. BoxShadow(
  98. color: Colors.grey.withOpacity(0.5),
  99. spreadRadius: 2,
  100. blurRadius: 5,
  101. ),
  102. ],
  103. ),
  104. ),
  105. ],
  106. )
  107. : Container(
  108. child: null,
  109. ),
  110. ],
  111. );
  112. } else {
  113. return Column(
  114. children: <Widget>[
  115. Container(
  116. alignment: Alignment.topLeft,
  117. child: Container(
  118. constraints: BoxConstraints(
  119. maxWidth: MediaQuery.of(context).size.width * 0.80,
  120. ),
  121. padding: EdgeInsets.all(10),
  122. margin: EdgeInsets.symmetric(vertical: 10),
  123. decoration: BoxDecoration(
  124. color: Colors.white,
  125. borderRadius: BorderRadius.circular(15),
  126. boxShadow: [
  127. BoxShadow(
  128. color: Colors.grey.withOpacity(0.5),
  129. spreadRadius: 2,
  130. blurRadius: 5,
  131. ),
  132. ],
  133. ),
  134. child: Text(
  135. message.text,
  136. style: TextStyle(
  137. color: Colors.black54,
  138. ),
  139. ),
  140. ),
  141. ),
  142. !isSameUser
  143. ? Row(
  144. children: <Widget>[
  145. Container(
  146. decoration: BoxDecoration(
  147. shape: BoxShape.circle,
  148. boxShadow: [
  149. BoxShadow(
  150. color: Colors.grey.withOpacity(0.5),
  151. spreadRadius: 2,
  152. blurRadius: 5,
  153. ),
  154. ],
  155. ),
  156. ),
  157. SizedBox(
  158. width: 10,
  159. ),
  160. Text(
  161. message.time,
  162. style: TextStyle(
  163. fontSize: 12,
  164. color: Colors.black45,
  165. ),
  166. ),
  167. ],
  168. )
  169. : Container(
  170. child: null,
  171. ),
  172. ],
  173. );
  174. }
  175. }
  176.  
  177. _sendMessageArea() {
  178. // IO.Socket socket = IO.io('https://laravel.init.kz:3404', <String, dynamic>{
  179. // 'transports': ['websocket'],
  180. // 'autoConnect': false,
  181. // });
  182. // socket.on('event', (data)=>{
  183. // debugPrint('connected')
  184. // });
  185. return Container(
  186. padding: EdgeInsets.symmetric(horizontal: 8),
  187. height: 70,
  188. color: Colors.white,
  189. child: Row(
  190. children: <Widget>[
  191. IconButton(
  192. icon: Icon(Icons.photo),
  193. iconSize: 25,
  194. color: Theme.of(context).primaryColor,
  195. onPressed: () {},
  196. ),
  197. Expanded(
  198. child: TextField(
  199. controller: textEditingController,
  200. decoration: InputDecoration.collapsed(
  201. hintText: 'Send a message..',
  202. ),
  203. textCapitalization: TextCapitalization.sentences,
  204. ),
  205. ),
  206. IconButton(
  207. icon: Icon(Icons.send),
  208. iconSize: 25,
  209. color: Theme.of(context).primaryColor,
  210. onPressed: () {
  211. socket.emit('message',textEditingController.text );
  212.  
  213.  
  214. // ()=>{}()
  215. },
  216. ),
  217. ],
  218. ),
  219. );
  220. }
  221.  
  222. @override
  223. Widget build(BuildContext context) {
  224.  
  225. getmsg();
  226. debugPrint(textEditingController.text);
  227. int prevUserId;
  228. return Scaffold(
  229. backgroundColor: Color(0xFFF6F6F6),
  230. appBar: AppBar(
  231. brightness: Brightness.dark,
  232. centerTitle: true,
  233. title: RichText(
  234. textAlign: TextAlign.center,
  235. text: TextSpan(
  236. children: [
  237. TextSpan(
  238. text: widget.user.display_name,
  239. style: TextStyle(
  240. fontSize: 16,
  241. fontWeight: FontWeight.w400,
  242. )),
  243. TextSpan(text: '\n'),
  244. widget.user.isOnline ?
  245. TextSpan(
  246. text: 'Online',
  247. style: TextStyle(
  248. fontSize: 11,
  249. fontWeight: FontWeight.w400,
  250. ),
  251. )
  252. :
  253. TextSpan(
  254. text: 'Offline',
  255. style: TextStyle(
  256. fontSize: 11,
  257. fontWeight: FontWeight.w400,
  258. ),
  259. )
  260. ],
  261. ),
  262. ),
  263. leading: IconButton(
  264. icon: Icon(Icons.arrow_back_ios),
  265. color: Colors.white,
  266. onPressed: () {
  267. Navigator.pop(context);
  268. }),
  269. ),
  270. body: Column(
  271. children: <Widget>[
  272. Expanded(
  273. child: ListView.builder(
  274. reverse: true,
  275. padding: EdgeInsets.all(20),
  276. itemCount: data.chats.length,
  277. itemBuilder: (BuildContext context, int index) {
  278. final Message message = data.messages[index];
  279. final bool isMe = message.sender.id == currentUser.id;
  280. final bool isSameUser = prevUserId == message.sender.id;
  281. prevUserId = message.sender.id;
  282. return _chatBubble(message, isMe, isSameUser);
  283.  
  284. },
  285. ),
  286. ),
  287. _sendMessageArea(),
  288. ],
  289. ),
  290. );
  291.  
  292. }
  293.  
  294. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement