Custom Chat Bubble In Flutter
Conversation chat applications show messages in chat rises with strong shading backgrounds. Modern chat applications show chat bubbles with slopes that depend on the bubbles’ situation on the screen. There are times when we need to utilize a chat bubble in our flutter application. Yet, utilizing a library for a particularly inconsequential errand isn’t great.
In this blog, we will explore the Custom Chat Bubble In Flutter. We will see how to implement a demo program of the custom chat bubble and how to make a custom chat bubble most simply without using any third-party libraries in your flutter applications.
Table Of Contents::
Flutter:
“ Flutter is Google’s UI toolkit that helps you build beautiful and natively combined applications for mobile, web, and desktop in a single codebase in record time ”
It is free and open-source. It was at first evolved from Google and presently overseen by an ECMA standard. Flutter applications utilize the Dart programming language for making an application. The dart programming shares a few same highlights as other programming dialects, like Kotlin and Swift, and can be trans-arranged into JavaScript code.
If you want to explore more about Flutter, please visit Flutter’s official website to get more information.
Flutter Is Proudly Entrusted By These Organizations For their Products : — Flutter Showcase
Implementation:
Step 1: Add the assets
Add assets to pubspec — yaml file.
assets:
- assets/images/
Step 2: Run flutter packages get in the root directory of your app.
How to implement code in dart file :
You need to implement it in your code respectively:
Create a new dart file called custom_shape.dart
inside the lib
folder.
First, create the custom shape Custom Painter class. This will be used to draw the custom shape at the end of the chat bubble. Users can add any color in custom shape.
import 'package:flutter/material.dart';
class CustomShape extends CustomPainter {
final Color bgColor;
CustomShape(this.bgColor);
@override
void paint(Canvas canvas, Size size) {
var paint = Paint()..color = bgColor;
var path = Path();
path.lineTo(-5, 0);
path.lineTo(0, 10);
path.lineTo(5, 0);
canvas.drawPath(path, paint);
}
@override
bool shouldRepaint(CustomPainter oldDelegate) {
return false;
}
}
Create a new dart file called send_message_screen.dart
inside the lib
folder.
First, we will create a constructer final string message.
final String message;
const SentMessageScreen({
Key key,
@required this.message,
}) : super(key: key);
In the build method, we will return Padding(). Inside, we will add the Row() widget. In this widget, we will add the mainAxisAlignment was the end and add the messageTextGroup. We will define the below code.
return Padding(
padding: EdgeInsets.only(right: 18.0, left: 50, top: 15, bottom: 5),
child: Row(
mainAxisAlignment: MainAxisAlignment.end,
children: <Widget>[
SizedBox(height: 30),
messageTextGroup,
],
),
);
We will deeply define messageTextGroup:
We will create a final messageTextGroup is equal to the Flexible() widget. In this widget, we will add the Row() widget. Inside, add mainAxisAlignment was the end and crossAxisAlignment was started. Inside children, we will add Conatiner with decoration box and add color, borderRadius. It’s child property, we will add a variable message text. We will add CustomPaint(), we will use the above painter class was CustomShape with color.
final messageTextGroup = Flexible(
child: Row(
mainAxisAlignment: MainAxisAlignment.end,
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Flexible(
child: Container(
padding: EdgeInsets.all(14),
decoration: BoxDecoration(
color: Colors.cyan[900],
borderRadius: BorderRadius.only(
topLeft: Radius.circular(18),
bottomLeft: Radius.circular(18),
bottomRight: Radius.circular(18),
),
),
child: Text(
message,
style: TextStyle(color: Colors.white, fontSize: 14),
),
),
),
CustomPaint(painter: CustomShape(Colors.cyan[900])),
],
));
Create a new dart file called received_message_screen.dart
inside the lib
folder.
Similarly, We can now create a received message screen. We just need to flip the custom shape and put it in the start instead of the end. We will use the transform widget to flip the custom shape widget. In the transform widget, we will add alignment was center and transform was Matrix4.rotationY(math. pi).
final messageTextGroup = Flexible(
child: Row(
mainAxisAlignment: MainAxisAlignment.start,
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Transform(
alignment: Alignment.center,
transform: Matrix4.rotationY(math.pi),
child: CustomPaint(
painter: CustomShape(Colors.grey[300]),
),
),
Flexible(
child: Container(
padding: EdgeInsets.all(14),
decoration: BoxDecoration(
color: Colors.grey[300],
borderRadius: BorderRadius.only(
topRight: Radius.circular(18),
bottomLeft: Radius.circular(18),
bottomRight: Radius.circular(18),
),
),
child: Text(
message,
style: TextStyle(color: Colors.black, fontSize: 14),
),
),
),
],
));
Create a new dart file called home_page.dart
inside the lib
folder.
In the body, we will add a Container() widget. Inside, add decoration box and add image. It’s child property, we can add both send and received message screens in our ListView().
Container(
decoration: BoxDecoration(
image: DecorationImage(
image: AssetImage("assets/bg_chat.jpg"),
fit: BoxFit.cover)),
child: ListView(
children: [
SentMessageScreen(message: "Hello"),
ReceivedMessageScreen(message: "Hi, how are you"),
SentMessageScreen(message: "I am great how are you doing"),
ReceivedMessageScreen(message: "I am also fine"),
SentMessageScreen(message: "Can we meet tomorrow?"),
ReceivedMessageScreen(message: "Yes, of course we will meet tomorrow"),
],
),
),
When we run the application, we ought to get the screen’s output like the underneath screen capture.
Code File:
import 'package:flutter/material.dart';
import 'package:flutter_custom_chat_bubble/received_message_screen.dart';
import 'package:flutter_custom_chat_bubble/send_messsage_screen.dart';
class HomePage extends StatefulWidget {
HomePage({Key key, this.title}) : super(key: key);
final String title;
@override
_HomePageState createState() => _HomePageState();
}
class _HomePageState extends State<HomePage> {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
backgroundColor: Colors.cyan[900],
automaticallyImplyLeading: false,
title: Text(widget.title),
),
body: Container(
decoration: BoxDecoration(
image: DecorationImage(
image: AssetImage("assets/bg_chat.jpg"),
fit: BoxFit.cover)),
child: ListView(
children: [
SentMessageScreen(message: "Hello"),
ReceivedMessageScreen(message: "Hi, how are you"),
SentMessageScreen(message: "I am great how are you doing"),
ReceivedMessageScreen(message: "I am also fine"),
SentMessageScreen(message: "Can we meet tomorrow?"),
ReceivedMessageScreen(message: "Yes, of course we will meet tomorrow"),
],
),
),
);
}
}
Conclusion:
In the article, I have explained the basic structure of the Custom Chat Bubble in a flutter; you can modify this code according to your choice. This was a small introduction to Custom Chat BubbleOn User Interaction from my side, and it’s working using Flutter.
I hope this blog will provide you with sufficient information on Trying up the Custom Chat Bubble in your flutter projects. We will make a demo program for working Custom Chat Bubble using any third-party libraries in your flutter applications. So please try it.
❤ ❤ Thanks for reading this article ❤❤
If I got something wrong? Let me know in the comments. I would love to improve.
Clap 👏 If this article helps you.
find the source code of the Flutter Custom Chat Bubble:
flutter-devs/flutter_custom_chat_bubble
A new Flutter application. This project is a starting point for a Flutter application. A few resources to get you…github.com
Feel free to connect with us:
And read more articles from FlutterDevs.com.
FlutterDevs team of Flutter developers to build high-quality and functionally-rich apps. Hire a flutter developer for your cross-platform Flutter mobile app project on an hourly or full-time basis as per your requirement! You can connect with us on Facebook, GitHub, Twitter, and LinkedIn for any flutter-related queries.
We welcome feedback and hope that you share what you’re working on using #FlutterDevs. We truly enjoy seeing how you use Flutter to build beautiful, interactive web experiences.