swift_chat 1.1.0
swift_chat: ^1.1.0 copied to clipboard
A highly customizable and easy-to-use chat UI package for Flutter. It provides a complete chat screen designed to be powered by a real-time backend like WebSockets.
SwiftChat A highly customizable and easy-to-use chat UI package for Flutter. SwiftChat provides a complete, responsive chat screen designed to be powered by a real-time backend like WebSockets.
Features Easy Integration: Drop the SwiftChat widget into your app.
WebSocket Ready: Designed to be driven by a Stream of messages, making WebSocket integration trivial.
Highly Customizable: Use the SwiftChatTheme to change colors, text styles, and more.
Responsive UI: The chat interface is designed to work on various screen sizes.
Clean Architecture: The package provides the UI, leaving you in full control of the business logic and backend communication.
Installation Add this to your package's pubspec.yaml file:
dependencies: swift_chat: ^1.1.0 # Replace with the latest version
Install it by running the following command in your project's root directory:
flutter pub get
How to Use Here's an example of how to use the SwiftChat widget with a live WebSocket connection.
import 'package:flutter/material.dart'; import 'package:swift_chat/swift_chat.dart'; import 'package:web_socket_channel/web_socket_channel.dart'; // You need to add this dependency import 'dart:convert'; import 'dart:math';
// Your main app setup...
class ChatScreen extends StatefulWidget { const ChatScreen({Key? key}) : super(key: key);
@override _ChatScreenState createState() => _ChatScreenState(); }
class _ChatScreenState extends State
@override void initState() { super.initState(); _channel.stream.listen((data) { final decoded = jsonDecode(data); final user = ChatUser(id: decoded['userId'], name: decoded['userName']); final incomingMessage = ChatMessage( id: decoded['id'], text: decoded['text'], user: user, createdAt: DateTime.parse(decoded['createdAt']), );
if (mounted) {
setState(() {
_messages.add(incomingMessage);
});
}
});
}
void handleSend(String text) { final message = ChatMessage( id: 'msg${Random().nextInt(9999)}', text: text, user: _currentUser, createdAt: DateTime.now(), );
setState(() {
_messages.add(message);
});
final messageData = {
'id': message.id,
'text': message.text,
'userId': message.user.id,
'userName': message.user.name,
'createdAt': message.createdAt.toIso8601String(),
};
_channel.sink.add(jsonEncode(messageData));
}
@override void dispose() { _channel.sink.close(); super.dispose(); }
@override Widget build(BuildContext context) { return Scaffold( appBar: AppBar(title: const Text('Global Chat')), body: SwiftChat( currentUser: _currentUser, messages: _messages, onSend: _handleSend, ), ); } }
Customization You can customize the look and feel of the chat UI by providing a SwiftChatTheme object to the SwiftChat widget. See the source code for all available properties.
Contributing Contributions are welcome! Please feel free to open an issue or a pull request on GitHub.