Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import 'package:flutter/material.dart';
- // Model for a chat message
- class Message {
- final String senderName;
- final String content;
- final DateTime timestamp;
- Message({required this.senderName, required this.content, required this.timestamp});
- }
- void main() => runApp(ChatApp());
- class ChatApp extends StatelessWidget {
- @override
- Widget build(BuildContext context) {
- return MaterialApp(
- home: ChatScreen(),
- );
- }
- }
- class ChatScreen extends StatefulWidget {
- @override
- _ChatScreenState createState() => _ChatScreenState();
- }
- class _ChatScreenState extends State<ChatScreen> {
- List<Message> _messages = []; // List to store chat messages
- TextEditingController _textController = TextEditingController();
- @override
- Widget build(BuildContext context) {
- return Scaffold(
- appBar: AppBar(
- title: Text('Group Chat'),
- ),
- body: Column(
- children: [
- Expanded(
- child: ListView.builder(
- reverse: true,
- itemCount: _messages.length,
- itemBuilder: (context, index) {
- Message message = _messages[index];
- bool isMe = message.senderName == 'Me'; // Replace 'Me' with the user's name
- return ChatBubble(
- message: message.content,
- senderName: message.senderName,
- isMe: isMe,
- time: message.timestamp,
- );
- },
- ),
- ),
- Container(
- padding: EdgeInsets.symmetric(horizontal: 8.0),
- child: Row(
- children: [
- Expanded(
- child: TextField(
- controller: _textController,
- decoration: InputDecoration(hintText: 'Type a message...'),
- ),
- ),
- IconButton(
- icon: Icon(Icons.send),
- onPressed: () {
- _sendMessage();
- },
- ),
- ],
- ),
- ),
- ],
- ),
- );
- }
- // Function to send a new message
- void _sendMessage() {
- String text = _textController.text;
- if (text.trim().isNotEmpty) {
- // Replace 'Me' with the user's name
- Message newMessage = Message(senderName: 'Me', content: text, timestamp: DateTime.now());
- setState(() {
- _messages.insert(0, newMessage);
- });
- _textController.clear();
- }
- }
- }
- class ChatBubble extends StatelessWidget {
- final String message;
- final String senderName;
- final bool isMe;
- final DateTime time;
- ChatBubble({required this.message, required this.senderName, required this.isMe, required this.time});
- @override
- Widget build(BuildContext context) {
- return Padding(
- padding: const EdgeInsets.all(8.0),
- child: Column(
- crossAxisAlignment: isMe ? CrossAxisAlignment.end : CrossAxisAlignment.start,
- children: [
- if (!isMe)
- Text(
- senderName,
- style: TextStyle(fontWeight: FontWeight.bold, color: Colors.grey[600]),
- ),
- Container(
- padding: EdgeInsets.symmetric(horizontal: 12.0, vertical: 8.0),
- decoration: BoxDecoration(
- color: isMe ? Colors.blue : Colors.grey[300],
- borderRadius: BorderRadius.circular(20.0),
- ),
- child: Column(
- crossAxisAlignment: CrossAxisAlignment.start,
- children: [
- Text(
- message,
- style: TextStyle(color: isMe ? Colors.white : Colors.black),
- ),
- SizedBox(height: 4.0),
- Text(
- time.toString(), // Format the timestamp as needed
- style: TextStyle(fontSize: 12.0, color: Colors.grey),
- ),
- ],
- ),
- ),
- ],
- ),
- );
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement