Advertisement
sourav8256

Flutter chat interface

Jul 29th, 2023
1,105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Dart 4.03 KB | None | 0 0
  1. import 'package:flutter/material.dart';
  2.  
  3. // Model for a chat message
  4. class Message {
  5.   final String senderName;
  6.   final String content;
  7.   final DateTime timestamp;
  8.  
  9.   Message({required this.senderName, required this.content, required this.timestamp});
  10. }
  11.  
  12. void main() => runApp(ChatApp());
  13.  
  14. class ChatApp extends StatelessWidget {
  15.   @override
  16.   Widget build(BuildContext context) {
  17.     return MaterialApp(
  18.       home: ChatScreen(),
  19.     );
  20.   }
  21. }
  22.  
  23. class ChatScreen extends StatefulWidget {
  24.   @override
  25.   _ChatScreenState createState() => _ChatScreenState();
  26. }
  27.  
  28. class _ChatScreenState extends State<ChatScreen> {
  29.   List<Message> _messages = []; // List to store chat messages
  30.   TextEditingController _textController = TextEditingController();
  31.  
  32.   @override
  33.   Widget build(BuildContext context) {
  34.     return Scaffold(
  35.       appBar: AppBar(
  36.         title: Text('Group Chat'),
  37.       ),
  38.       body: Column(
  39.         children: [
  40.           Expanded(
  41.             child: ListView.builder(
  42.               reverse: true,
  43.               itemCount: _messages.length,
  44.               itemBuilder: (context, index) {
  45.                 Message message = _messages[index];
  46.                 bool isMe = message.senderName == 'Me'; // Replace 'Me' with the user's name
  47.                 return ChatBubble(
  48.                   message: message.content,
  49.                   senderName: message.senderName,
  50.                   isMe: isMe,
  51.                   time: message.timestamp,
  52.                 );
  53.               },
  54.             ),
  55.           ),
  56.           Container(
  57.             padding: EdgeInsets.symmetric(horizontal: 8.0),
  58.             child: Row(
  59.               children: [
  60.                 Expanded(
  61.                   child: TextField(
  62.                     controller: _textController,
  63.                     decoration: InputDecoration(hintText: 'Type a message...'),
  64.                   ),
  65.                 ),
  66.                 IconButton(
  67.                   icon: Icon(Icons.send),
  68.                   onPressed: () {
  69.                     _sendMessage();
  70.                   },
  71.                 ),
  72.               ],
  73.             ),
  74.           ),
  75.         ],
  76.       ),
  77.     );
  78.   }
  79.  
  80.   // Function to send a new message
  81.   void _sendMessage() {
  82.     String text = _textController.text;
  83.     if (text.trim().isNotEmpty) {
  84.       // Replace 'Me' with the user's name
  85.       Message newMessage = Message(senderName: 'Me', content: text, timestamp: DateTime.now());
  86.       setState(() {
  87.         _messages.insert(0, newMessage);
  88.       });
  89.       _textController.clear();
  90.     }
  91.   }
  92. }
  93.  
  94. class ChatBubble extends StatelessWidget {
  95.   final String message;
  96.   final String senderName;
  97.   final bool isMe;
  98.   final DateTime time;
  99.  
  100.   ChatBubble({required this.message, required this.senderName, required this.isMe, required this.time});
  101.  
  102.   @override
  103.   Widget build(BuildContext context) {
  104.     return Padding(
  105.       padding: const EdgeInsets.all(8.0),
  106.       child: Column(
  107.         crossAxisAlignment: isMe ? CrossAxisAlignment.end : CrossAxisAlignment.start,
  108.         children: [
  109.           if (!isMe)
  110.             Text(
  111.               senderName,
  112.               style: TextStyle(fontWeight: FontWeight.bold, color: Colors.grey[600]),
  113.             ),
  114.           Container(
  115.             padding: EdgeInsets.symmetric(horizontal: 12.0, vertical: 8.0),
  116.             decoration: BoxDecoration(
  117.               color: isMe ? Colors.blue : Colors.grey[300],
  118.               borderRadius: BorderRadius.circular(20.0),
  119.             ),
  120.             child: Column(
  121.               crossAxisAlignment: CrossAxisAlignment.start,
  122.               children: [
  123.                 Text(
  124.                   message,
  125.                   style: TextStyle(color: isMe ? Colors.white : Colors.black),
  126.                 ),
  127.                 SizedBox(height: 4.0),
  128.                 Text(
  129.                   time.toString(), // Format the timestamp as needed
  130.                   style: TextStyle(fontSize: 12.0, color: Colors.grey),
  131.                 ),
  132.               ],
  133.             ),
  134.           ),
  135.         ],
  136.       ),
  137.     );
  138.   }
  139. }
  140.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement