Creating a chat bubble in Flutter involves building a custom widget that visually represents a single message, adjusting its appearance and position based on who sent it.
A common approach is to create a StatelessWidget
specifically for the chat bubble. This widget will typically accept the message text and a flag indicating if the message was sent by the current user.
Core Steps to Build a Flutter Chat Bubble
Based on the provided references, here's how you can structure a ChatBubble
widget:
1. Define the Chat Bubble Widget
You start by creating a custom StatelessWidget
. This widget will hold the necessary data to display a single message bubble.
class ChatBubble extends StatelessWidget {
final String message;
final bool isCurrentUser;
const ChatBubble({
Key? key,
required this.message,
required this.isCurrentUser,
}) : super(key: key);
// ... build method will go here
}
- Reference 1:
class ChatBubble extends StatelessWidget { const ChatBubble({ Key? key, required this. ...
confirms the structure of defining the widget and its constructor with required parameters like the message content and user information.
2. Build the Widget Layout
Inside the build
method, you'll arrange the elements that make up the bubble. This typically involves using containers, padding, and alignment.
@override
Widget build(BuildContext context) {
// ... layout goes here
}
- Reference 3:
@override Widget build(BuildContext context) { return Padding( padding: EdgeInsets. fromLTRB( isCurrentUser ?
shows the start of thebuild
method returning aPadding
widget, with padding defined conditionally based onisCurrentUser
.
3. Implement Dynamic Alignment
The bubble's alignment (left for others, right for the current user) is crucial for a chat interface.
@override
Widget build(BuildContext context) {
return Align( // Use Align to position the bubble
alignment: isCurrentUser ? Alignment.centerRight : Alignment.centerLeft, // Reference 2
child: Padding( // Reference 3 starts with Padding
padding: EdgeInsets.fromLTRB(
isCurrentUser ? 8.0 : 64.0, // Left padding (more on left for others)
4.0, // Top padding
isCurrentUser ? 64.0 : 8.0, // Right padding (more on right for current user)
4.0, // Bottom padding
),
// ... the bubble content goes here
),
);
}
- Reference 2:
alignment: isCurrentUser ? Alignment. ...
directly indicates using thealignment
property and a conditional check (isCurrentUser
) to determine positioning.Alignment.centerRight
is typically used for the current user, andAlignment.centerLeft
for others. - Reference 3:
@override Widget build(BuildContext context) { return Padding( padding: EdgeInsets. fromLTRB( isCurrentUser ?
shows starting with aPadding
widget and conditionally setting its properties based onisCurrentUser
, likely usingEdgeInsets.fromLTRB
to control space around the bubble. The padding values should be adjusted to create appropriate spacing and potentially limit the bubble's width.
4. Add Bubble Styling and Content
Wrap the message text in a container that provides the visual styling (background color, rounded corners).
@override
Widget build(BuildContext context) {
return Align(
alignment: isCurrentUser ? Alignment.centerRight : Alignment.centerLeft,
child: Padding(
padding: EdgeInsets.fromLTRB(
isCurrentUser ? 8.0 : 64.0,
4.0,
isCurrentUser ? 64.0 : 8.0,
4.0,
),
child: Container(
decoration: BoxDecoration(
color: isCurrentUser ? Colors.blue[300] : Colors.grey[300], // Dynamic color
borderRadius: BorderRadius.circular(12.0), // Rounded corners
),
padding: const EdgeInsets.all(10.0), // Inner padding for text
child: Text(
message,
style: TextStyle(color: isCurrentUser ? Colors.white : Colors.black87), // Dynamic text color
),
),
),
);
}
This structure creates a basic chat bubble that aligns correctly and has a distinct appearance based on the sender. You can further enhance it by adding features like timestamps, read receipts, or different shapes.