|
| 1 | +import 'package:flutter/material.dart'; |
| 2 | +import 'package:flutter_text_decorator/src/modules/box/decorations/bubble_box_tip.dart'; |
| 3 | + |
| 4 | +/// A [CustomPainter] that draws a speech bubble-like box around a given [Text] widget. |
| 5 | +/// |
| 6 | +/// This painter creates a rounded rectangle with a "tip" or "tail" pointing outwards, |
| 7 | +/// commonly used to represent speech or thoughts in UI design. The appearance of |
| 8 | +/// the bubble, including its color, corner roundness, padding around the text, |
| 9 | +/// and the characteristics of the tip (its position and orientation), can be customized. |
| 10 | +/// |
| 11 | +/// The painter calculates the size of the provided text to determine the overall |
| 12 | +/// dimensions of the bubble. The tip's geometry is then calculated based on the |
| 13 | +/// bubble's width and the properties defined in the [BubbleBoxTip] object. |
| 14 | +/// |
| 15 | +/// Key properties: |
| 16 | +/// - `text`: The [Text] widget to be enclosed by the bubble. |
| 17 | +/// - `padding`: The space between the text and the bubble's edges. |
| 18 | +/// - `bubbleColor`: The color of the bubble's outline. |
| 19 | +/// - `borderRadius`: The radius for the rounded corners of the bubble. |
| 20 | +/// - `tip`: A [BubbleBoxTip] object defining the tip's position, orientation, and size. |
| 21 | +/// ``` |
| 22 | +class BubbleBoxPainter extends CustomPainter { |
| 23 | + final Text text; |
| 24 | + final double padding; //! TODO: fix text not being centered |
| 25 | + final Color bubbleColor; |
| 26 | + final double borderRadius; |
| 27 | + final BubbleBoxTip tip; |
| 28 | + |
| 29 | + BubbleBoxPainter({ |
| 30 | + required this.text, |
| 31 | + required this.padding, |
| 32 | + required this.bubbleColor, |
| 33 | + super.repaint, |
| 34 | + this.borderRadius = 8, |
| 35 | + this.tip = const BubbleBoxTip(), |
| 36 | + }); |
| 37 | + |
| 38 | + @override |
| 39 | + void paint(Canvas canvas, Size size) { |
| 40 | + final Paint paint = Paint() |
| 41 | + ..color = bubbleColor |
| 42 | + ..strokeWidth = 2 // TODO: add as param? |
| 43 | + ..style = PaintingStyle.stroke; |
| 44 | + |
| 45 | + // Calculate text size |
| 46 | + final textPainter = TextPainter( |
| 47 | + text: TextSpan(text: text.data, style: text.style), |
| 48 | + textDirection: TextDirection.ltr, |
| 49 | + )..layout(); |
| 50 | + |
| 51 | + final textWidth = textPainter.width; |
| 52 | + final textHeight = textPainter.height; |
| 53 | + |
| 54 | + // Calculate bubble size |
| 55 | + final bubbleWidth = textWidth + padding * 2; |
| 56 | + final bubbleHeight = textHeight + padding * 2; |
| 57 | + |
| 58 | + // Calculate tail size |
| 59 | + //! TODO: extract |
| 60 | + final tailHeight = bubbleHeight * 0.25; |
| 61 | + |
| 62 | + final path = Path() |
| 63 | + |
| 64 | + // Top left corner |
| 65 | + ..moveTo(0, borderRadius) |
| 66 | + ..quadraticBezierTo(0, 0, borderRadius, 0) |
| 67 | + |
| 68 | + // Top right corner |
| 69 | + ..lineTo(bubbleWidth - borderRadius, 0) |
| 70 | + ..quadraticBezierTo(bubbleWidth, 0, bubbleWidth, borderRadius) |
| 71 | + |
| 72 | + // Bottom right corner |
| 73 | + ..lineTo(bubbleWidth, bubbleHeight - borderRadius) |
| 74 | + ..quadraticBezierTo(bubbleWidth, bubbleHeight, bubbleWidth - borderRadius, bubbleHeight); |
| 75 | + |
| 76 | + final double tipOffset = bubbleWidth * 0.05; |
| 77 | + |
| 78 | + final double tipStart = bubbleWidth * tip.position.percentage; |
| 79 | + final double tipBaseWidth = bubbleWidth * 0.2; |
| 80 | + final double tipEnd = tipStart - tipBaseWidth; |
| 81 | + double tipPeak = tipEnd - tipOffset; |
| 82 | + if (tip.orientation == TipOrientation.right) { |
| 83 | + tipPeak = tipStart + tipOffset; |
| 84 | + } |
| 85 | + |
| 86 | + // Bottom left corner with tail |
| 87 | + path |
| 88 | + ..lineTo(tipStart, bubbleHeight) |
| 89 | + ..lineTo(tipPeak, bubbleHeight + tailHeight) |
| 90 | + ..lineTo(tipEnd, bubbleHeight) |
| 91 | + ..lineTo(borderRadius, bubbleHeight) |
| 92 | + ..quadraticBezierTo(0, bubbleHeight, 0, bubbleHeight - borderRadius) |
| 93 | + ..close(); |
| 94 | + |
| 95 | + // Draw the bubble |
| 96 | + canvas.drawPath(path, paint); |
| 97 | + } |
| 98 | + |
| 99 | + @override |
| 100 | + bool shouldRepaint(covariant CustomPainter oldDelegate) { |
| 101 | + return false; |
| 102 | + } |
| 103 | +} |
0 commit comments