Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import 'package:flutte/PageChatView/user_model.dart';
- import 'package:flutter/material.dart';
- import 'package:socket_io_client/socket_io_client.dart' as IO;
- import '../main.dart';
- import 'MessageModel.dart';
- class ChatScreen extends StatefulWidget {
- final User user;
- final int id;
- ChatScreen({this.user, this.id,});
- @override
- _ChatScreenState createState() => _ChatScreenState(id);
- }
- class _ChatScreenState extends State<ChatScreen> {
- User user;
- getmsg()async{
- socket.emit('getMessages',1);
- socket.on('chatMessages', (data1)=>{
- // user = data.getUser(32),
- for( var i = 0 ; i < data1.length; i++ ) {
- // user = User.fromJson(data1[i]),
- debugPrint("omg"),
- // if(data1[i]['user_id'])
- data.contacts.add(
- Message(
- sender: user,
- time: " ",
- text: data1[i]['message'].toString(),
- unread: true,
- ),
- ),
- },
- debugPrint(data.toString())
- });
- }
- final int id;
- TextEditingController textEditingController = new TextEditingController();
- _ChatScreenState(this.id);
- _chatBubble(Message message, bool isMe, bool isSameUser) {
- data.connection('connected', null);
- if (isMe) {
- return Column(
- children: <Widget>[
- Container(
- alignment: Alignment.topRight,
- child: Container(
- constraints: BoxConstraints(
- maxWidth: MediaQuery.of(context).size.width * 0.80,
- ),
- padding: EdgeInsets.all(10),
- margin: EdgeInsets.symmetric(vertical: 10),
- decoration: BoxDecoration(
- color: Theme.of(context).primaryColor,
- borderRadius: BorderRadius.circular(15),
- boxShadow: [
- BoxShadow(
- color: Colors.grey.withOpacity(0.5),
- spreadRadius: 2,
- blurRadius: 5,
- ),
- ],
- ),
- child: Text(
- message.text,
- style: TextStyle(
- color: Colors.white,
- ),
- ),
- ),
- ),
- !isSameUser
- ? Row(
- mainAxisAlignment: MainAxisAlignment.end,
- children: <Widget>[
- Text(
- message.time,
- style: TextStyle(
- fontSize: 12,
- color: Colors.black45,
- ),
- ),
- SizedBox(
- width: 10,
- ),
- Container(
- decoration: BoxDecoration(
- shape: BoxShape.circle,
- boxShadow: [
- BoxShadow(
- color: Colors.grey.withOpacity(0.5),
- spreadRadius: 2,
- blurRadius: 5,
- ),
- ],
- ),
- ),
- ],
- )
- : Container(
- child: null,
- ),
- ],
- );
- } else {
- return Column(
- children: <Widget>[
- Container(
- alignment: Alignment.topLeft,
- child: Container(
- constraints: BoxConstraints(
- maxWidth: MediaQuery.of(context).size.width * 0.80,
- ),
- padding: EdgeInsets.all(10),
- margin: EdgeInsets.symmetric(vertical: 10),
- decoration: BoxDecoration(
- color: Colors.white,
- borderRadius: BorderRadius.circular(15),
- boxShadow: [
- BoxShadow(
- color: Colors.grey.withOpacity(0.5),
- spreadRadius: 2,
- blurRadius: 5,
- ),
- ],
- ),
- child: Text(
- message.text,
- style: TextStyle(
- color: Colors.black54,
- ),
- ),
- ),
- ),
- !isSameUser
- ? Row(
- children: <Widget>[
- Container(
- decoration: BoxDecoration(
- shape: BoxShape.circle,
- boxShadow: [
- BoxShadow(
- color: Colors.grey.withOpacity(0.5),
- spreadRadius: 2,
- blurRadius: 5,
- ),
- ],
- ),
- ),
- SizedBox(
- width: 10,
- ),
- Text(
- message.time,
- style: TextStyle(
- fontSize: 12,
- color: Colors.black45,
- ),
- ),
- ],
- )
- : Container(
- child: null,
- ),
- ],
- );
- }
- }
- _sendMessageArea() {
- // IO.Socket socket = IO.io('https://laravel.init.kz:3404', <String, dynamic>{
- // 'transports': ['websocket'],
- // 'autoConnect': false,
- // });
- // socket.on('event', (data)=>{
- // debugPrint('connected')
- // });
- return Container(
- padding: EdgeInsets.symmetric(horizontal: 8),
- height: 70,
- color: Colors.white,
- child: Row(
- children: <Widget>[
- IconButton(
- icon: Icon(Icons.photo),
- iconSize: 25,
- color: Theme.of(context).primaryColor,
- onPressed: () {},
- ),
- Expanded(
- child: TextField(
- controller: textEditingController,
- decoration: InputDecoration.collapsed(
- hintText: 'Send a message..',
- ),
- textCapitalization: TextCapitalization.sentences,
- ),
- ),
- IconButton(
- icon: Icon(Icons.send),
- iconSize: 25,
- color: Theme.of(context).primaryColor,
- onPressed: () {
- socket.emit('message',textEditingController.text );
- // ()=>{}()
- },
- ),
- ],
- ),
- );
- }
- @override
- Widget build(BuildContext context) {
- getmsg();
- debugPrint(textEditingController.text);
- int prevUserId;
- return Scaffold(
- backgroundColor: Color(0xFFF6F6F6),
- appBar: AppBar(
- brightness: Brightness.dark,
- centerTitle: true,
- title: RichText(
- textAlign: TextAlign.center,
- text: TextSpan(
- children: [
- TextSpan(
- text: widget.user.display_name,
- style: TextStyle(
- fontSize: 16,
- fontWeight: FontWeight.w400,
- )),
- TextSpan(text: '\n'),
- widget.user.isOnline ?
- TextSpan(
- text: 'Online',
- style: TextStyle(
- fontSize: 11,
- fontWeight: FontWeight.w400,
- ),
- )
- :
- TextSpan(
- text: 'Offline',
- style: TextStyle(
- fontSize: 11,
- fontWeight: FontWeight.w400,
- ),
- )
- ],
- ),
- ),
- leading: IconButton(
- icon: Icon(Icons.arrow_back_ios),
- color: Colors.white,
- onPressed: () {
- Navigator.pop(context);
- }),
- ),
- body: Column(
- children: <Widget>[
- Expanded(
- child: ListView.builder(
- reverse: true,
- padding: EdgeInsets.all(20),
- itemCount: data.chats.length,
- itemBuilder: (BuildContext context, int index) {
- final Message message = data.messages[index];
- final bool isMe = message.sender.id == currentUser.id;
- final bool isSameUser = prevUserId == message.sender.id;
- prevUserId = message.sender.id;
- return _chatBubble(message, isMe, isSameUser);
- },
- ),
- ),
- _sendMessageArea(),
- ],
- ),
- );
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement