如何使用自定义画家在 Flutter 中为容器绘制尾端
How to draw tail end for container in Flutter using Custom painter
我有一个聊天屏幕,其中的 chatItem 框尾端是这样的
需要在接收方和发送方以及
我目前正在为此使用自定义画家。并且无法弄清楚这一点。
`class CustomChatBubble extends CustomPainter {CustomChatBubble({this.color, @required this.isOwn});
final Color color;
final bool isOwn;
@override
void paint(Canvas canvas, Size size) {
final paint = Paint()..color = color ?? Colors.blue;
Path paintBubbleTail() {
Path path;
if (!isOwn) {
path = Path()
..moveTo(0, size.height)
..relativeMoveTo(0,5)
..quadraticBezierTo(10, size.height , 0, size.height - 5);
}
if (isOwn) {
path = Path()
..moveTo(0, size.height)
..relativeMoveTo(0,5)
..quadraticBezierTo(10, size.height , 0, size.height - 5);
}
return path;
}
final RRect bubbleBody = RRect.fromRectAndRadius(
Rect.fromLTWH(0, 0, size.width, size.height), Radius.circular(16));
final Path bubbleTail = paintBubbleTail();
canvas.drawRRect(bubbleBody, paint);
canvas.drawPath(bubbleTail, paint);
}
@override
bool shouldRepaint(CustomPainter oldDelegate) {
// TODO: implement shouldRepaint
return true;
}
}`
这是我以前做的class
isOwn 用于在发送者和接收者之间切换
你用这个包怎么样?
该包中有很多聊天气泡小部件。
flutter_chat_bubble
这是您案例的路径:
class CustomChatBubble extends CustomPainter {
CustomChatBubble({this.color, @required this.isOwn});
final Color color;
final bool isOwn;
@override
void paint(Canvas canvas, Size size) {
final paint = Paint()
..color = color ?? Colors.blue
..style = PaintingStyle.fill;
Path path;
double triangleHeight = 10.0;
double triangleWidth = 10.0;
double roundedBorder = 5.0;
Path drawBubbleBody() {
if (!isOwn) {
path = Path()
..lineTo(0, size.height)
..lineTo(triangleWidth, size.height - triangleHeight)
..lineTo(size.width - roundedBorder, size.height - triangleHeight)
..quadraticBezierTo(size.width, size.height - triangleHeight,
size.width, size.height - triangleHeight - roundedBorder)
..lineTo(size.width, roundedBorder)
..quadraticBezierTo(size.width, 0, size.width - roundedBorder, 0);
path.close();
}
if (isOwn) {
path = Path()
..moveTo(size.width - roundedBorder, 0)
..lineTo(roundedBorder, 0)
..quadraticBezierTo(0, 0, 0, roundedBorder)
..lineTo(0, size.height - triangleHeight - roundedBorder)
..quadraticBezierTo(0, size.height - triangleHeight, roundedBorder,
size.height - triangleHeight)
..lineTo(size.width - triangleWidth, size.height - triangleHeight)
..lineTo(size.width, size.height)
..lineTo(size.width, roundedBorder)
..quadraticBezierTo(size.width, 0, size.width - roundedBorder, 0);
}
return path;
}
canvas.drawPath(drawBubbleBody(), paint);
}
@override
bool shouldRepaint(CustomPainter oldDelegate) {
// TODO: implement shouldRepaint
return true;
}
}
如果您想复制:
import 'package:flutter/material.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Material App',
home: Scaffold(
appBar: AppBar(
title: Text('Material App Bar'),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
CustomPaint(
painter: CustomChatBubble(isOwn: true, color: Colors.grey),
child: Container(
width: 200,
height: 50,
),
),
CustomPaint(
painter: CustomChatBubble(isOwn: false, color: Colors.orange),
child: Container(
width: 200,
height: 50,
),
)
],
),
),
),
);
}
}
结果:
我有一个聊天屏幕,其中的 chatItem 框尾端是这样的
需要在接收方和发送方以及 我目前正在为此使用自定义画家。并且无法弄清楚这一点。
`class CustomChatBubble extends CustomPainter {CustomChatBubble({this.color, @required this.isOwn});
final Color color;
final bool isOwn;
@override
void paint(Canvas canvas, Size size) {
final paint = Paint()..color = color ?? Colors.blue;
Path paintBubbleTail() {
Path path;
if (!isOwn) {
path = Path()
..moveTo(0, size.height)
..relativeMoveTo(0,5)
..quadraticBezierTo(10, size.height , 0, size.height - 5);
}
if (isOwn) {
path = Path()
..moveTo(0, size.height)
..relativeMoveTo(0,5)
..quadraticBezierTo(10, size.height , 0, size.height - 5);
}
return path;
}
final RRect bubbleBody = RRect.fromRectAndRadius(
Rect.fromLTWH(0, 0, size.width, size.height), Radius.circular(16));
final Path bubbleTail = paintBubbleTail();
canvas.drawRRect(bubbleBody, paint);
canvas.drawPath(bubbleTail, paint);
}
@override
bool shouldRepaint(CustomPainter oldDelegate) {
// TODO: implement shouldRepaint
return true;
}
}`
这是我以前做的class isOwn 用于在发送者和接收者之间切换
你用这个包怎么样?
该包中有很多聊天气泡小部件。
flutter_chat_bubble
这是您案例的路径:
class CustomChatBubble extends CustomPainter {
CustomChatBubble({this.color, @required this.isOwn});
final Color color;
final bool isOwn;
@override
void paint(Canvas canvas, Size size) {
final paint = Paint()
..color = color ?? Colors.blue
..style = PaintingStyle.fill;
Path path;
double triangleHeight = 10.0;
double triangleWidth = 10.0;
double roundedBorder = 5.0;
Path drawBubbleBody() {
if (!isOwn) {
path = Path()
..lineTo(0, size.height)
..lineTo(triangleWidth, size.height - triangleHeight)
..lineTo(size.width - roundedBorder, size.height - triangleHeight)
..quadraticBezierTo(size.width, size.height - triangleHeight,
size.width, size.height - triangleHeight - roundedBorder)
..lineTo(size.width, roundedBorder)
..quadraticBezierTo(size.width, 0, size.width - roundedBorder, 0);
path.close();
}
if (isOwn) {
path = Path()
..moveTo(size.width - roundedBorder, 0)
..lineTo(roundedBorder, 0)
..quadraticBezierTo(0, 0, 0, roundedBorder)
..lineTo(0, size.height - triangleHeight - roundedBorder)
..quadraticBezierTo(0, size.height - triangleHeight, roundedBorder,
size.height - triangleHeight)
..lineTo(size.width - triangleWidth, size.height - triangleHeight)
..lineTo(size.width, size.height)
..lineTo(size.width, roundedBorder)
..quadraticBezierTo(size.width, 0, size.width - roundedBorder, 0);
}
return path;
}
canvas.drawPath(drawBubbleBody(), paint);
}
@override
bool shouldRepaint(CustomPainter oldDelegate) {
// TODO: implement shouldRepaint
return true;
}
}
如果您想复制:
import 'package:flutter/material.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Material App',
home: Scaffold(
appBar: AppBar(
title: Text('Material App Bar'),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
CustomPaint(
painter: CustomChatBubble(isOwn: true, color: Colors.grey),
child: Container(
width: 200,
height: 50,
),
),
CustomPaint(
painter: CustomChatBubble(isOwn: false, color: Colors.orange),
child: Container(
width: 200,
height: 50,
),
)
],
),
),
),
);
}
}
结果: