Cara Membuat Custom Bubble Chat di Flutter |
Berikut ini source code nya:
import 'package:flutter/material.dart';
import 'dart:math' as math;
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: false,
theme: ThemeData(
useMaterial3: false,
primarySwatch: Colors.green,
),
home: const HomePage(),
);
}
}
class HomePage extends StatefulWidget {
const HomePage({super.key});
@override
HomePageState createState() => HomePageState();
}
class HomePageState extends State {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('Exmaple Chat'),
),
body: Padding(
padding: const EdgeInsets.all(10),
child: ListView(
children: const [
InBubble(message: 'Oi bang'),
OutBubble(message: 'Oi, ada perlu apa ya lu?'),
OutBubble(message: 'Hari ini kemana? Kok lu gak masuk?'),
InBubble(message: 'Hari ini gue cuti.'),
OutBubble(message: 'Ah yang bener lu njir'),
InBubble(message: 'Beneran cukk'),
OutBubble(message: 'Interview kali lu hahah'),
],
),
)
);
}
}
class InBubble extends StatelessWidget {
final String message;
const InBubble({super.key, required this.message});
@override
Widget build(BuildContext context) {
return Row(
mainAxisAlignment: MainAxisAlignment.start,
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Transform(
alignment: Alignment.center,
transform: Matrix4.rotationY(math.pi),
child: CustomPaint(
painter: Triangle(Colors.grey.shade300),
),
),
Flexible(
child: Container(
padding: const EdgeInsets.all(15),
margin: const EdgeInsets.only(bottom: 5),
decoration: BoxDecoration(
color: Colors.grey.shade300,
borderRadius: const BorderRadius.only(
topRight: Radius.circular(19),
bottomLeft: Radius.circular(19),
bottomRight: Radius.circular(19),
),
),
child: Text(
message,
style: const TextStyle(
color: Colors.black,
fontSize: 15
),
),
),
),
],
);
}
}
class OutBubble extends StatelessWidget {
final String message;
const OutBubble({super.key, required this.message});
@override
Widget build(BuildContext context) {
return Row(
mainAxisAlignment: MainAxisAlignment.end,
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Flexible(
child: Container(
padding: const EdgeInsets.all(15),
margin: const EdgeInsets.only(bottom: 5),
decoration: BoxDecoration(
color: Colors.green.shade600,
borderRadius: const BorderRadius.only(
topLeft: Radius.circular(19),
bottomLeft: Radius.circular(19),
bottomRight: Radius.circular(19),
),
),
child: Text(
message,
style: const TextStyle(
color: Colors.white,
fontSize: 15
),
),
),
),
CustomPaint(
painter: Triangle(
Colors.green.shade600
)
),
],
);
}
}
class Triangle extends CustomPainter {
final Color backgroundColor;
Triangle(this.backgroundColor);
@override
void paint(Canvas canvas, Size size) {
var paint = Paint()..color = backgroundColor;
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;
}
}
Untuk Output-nya nanti seperti ini:Support Blog Rivaldi 48 agar terus bisa membagikan artikel-artikel yang bermanfaat dengan cara klik link Sociabuzz dibawah ini :https://sociabuzz.com/azharrvldi_/donate (bisa pakai GoPay, OVO, DANA, Bank Transfer)
Demikian informasi yang saya bagikan untuk kalian. Jangan lupa bagikan artikel ini ke teman-teman kalian agar ikut membaca Cara Menambahkan Border di Text Flutter ini. Subscribe juga blog Rivaldi 48 ini agar kalian mendapatkan notifikasi saat Admin update artikel terbaru. Semoga kalian lebih nyaman dan mudah dalam mengakses Blog Rivaldi 48 dimanapun kalian berada. Terima Kasih. Follow Instagram Admin @azhardvls_
0 Comments
Post a Comment
Silahkan tinggalkan komentar jika Anda punya saran, kritik, atau pertanyaan seputar topik pembahasan. Gunakan bahasa yang bijak dan santun. Terima Kasih.