Skip to content

Commit 72cd176

Browse files
committed
fix bubble box painter
1 parent 73758f7 commit 72cd176

File tree

3 files changed

+22
-18
lines changed

3 files changed

+22
-18
lines changed

example/lib/screens/examples/box_example_screen.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,8 @@ class BoxExampleScreen extends StatelessWidget {
4646
TextDecorator.boxed(
4747
style: BoxStyle.bubble,
4848
text: const Text(
49-
'Bubble Text 2',
50-
style: TextStyle(fontSize: 32),
49+
'Franz jagt im komplett verwahrlosten Taxi quer durch Berlin',
50+
style: TextStyle(fontSize: 16),
5151
),
5252
strokeWidth: 2,
5353
borderRadius: 16,

lib/src/modules/box/enums/box_style.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ enum BoxStyle {
2828
case BoxStyle.bubble:
2929
return BubbleBoxPainter(
3030
text: text,
31-
padding: 4,
31+
// padding: 4,
3232
bubbleColor: Colors.orange,
3333
tip: const BubbleBoxTip(
3434
position: TipPosition.left,

lib/src/modules/box/painter/bubble_box_painter.dart

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import 'dart:math';
2+
13
import 'package:flutter/material.dart';
24
import 'package:flutter_text_decorator/src/modules/box/decorations/bubble_box_tip.dart';
35

@@ -22,14 +24,14 @@ import 'package:flutter_text_decorator/src/modules/box/decorations/bubble_box_ti
2224
class BubbleBoxPainter extends CustomPainter {
2325
BubbleBoxPainter({
2426
required this.text,
25-
required this.padding,
2627
required this.bubbleColor,
28+
this.padding = const EdgeInsets.all(8),
2729
super.repaint,
2830
this.borderRadius = 8,
2931
this.tip = const BubbleBoxTip(),
3032
});
3133
final Text text;
32-
final double padding; //! TODO: fix text not being centered
34+
final EdgeInsets padding; //! TODO: fix text not being centered
3335
final Color bubbleColor;
3436
final double borderRadius;
3537
final BubbleBoxTip tip;
@@ -48,11 +50,13 @@ class BubbleBoxPainter extends CustomPainter {
4850
)..layout();
4951

5052
final textWidth = textPainter.width;
53+
final availableWidth = min(textWidth, size.width);
5154
final textHeight = textPainter.height;
55+
final availableHeight = max(textHeight, size.height);
5256

5357
// Calculate bubble size
54-
final bubbleWidth = textWidth + padding * 2;
55-
final bubbleHeight = textHeight + padding * 2;
58+
final bubbleWidth = availableWidth + padding.horizontal;
59+
final bubbleHeight = availableHeight + padding.vertical;
5660

5761
// Calculate tail size
5862
//! TODO: extract
@@ -61,16 +65,16 @@ class BubbleBoxPainter extends CustomPainter {
6165
final path = Path()
6266

6367
// Top left corner
64-
..moveTo(0, borderRadius)
65-
..quadraticBezierTo(0, 0, borderRadius, 0)
68+
..moveTo(0 - padding.left, borderRadius - padding.top)
69+
..quadraticBezierTo(0 - padding.left, 0 - padding.top, borderRadius - padding.left, 0 - padding.top)
6670

6771
// Top right corner
68-
..lineTo(bubbleWidth - borderRadius, 0)
69-
..quadraticBezierTo(bubbleWidth, 0, bubbleWidth, borderRadius)
72+
..lineTo(bubbleWidth - borderRadius - padding.left, 0 - padding.top)
73+
..quadraticBezierTo(bubbleWidth - padding.left, 0 - padding.top, bubbleWidth - padding.left, borderRadius - padding.top)
7074

7175
// Bottom right corner
72-
..lineTo(bubbleWidth, bubbleHeight - borderRadius)
73-
..quadraticBezierTo(bubbleWidth, bubbleHeight, bubbleWidth - borderRadius, bubbleHeight);
76+
..lineTo(bubbleWidth - padding.left, bubbleHeight - borderRadius - padding.top)
77+
..quadraticBezierTo(bubbleWidth - padding.left, bubbleHeight - padding.top, bubbleWidth - borderRadius - padding.left, bubbleHeight - padding.top);
7478

7579
final tipOffset = bubbleWidth * 0.05;
7680

@@ -84,11 +88,11 @@ class BubbleBoxPainter extends CustomPainter {
8488

8589
// Bottom left corner with tail
8690
path
87-
..lineTo(tipStart, bubbleHeight)
88-
..lineTo(tipPeak, bubbleHeight + tailHeight)
89-
..lineTo(tipEnd, bubbleHeight)
90-
..lineTo(borderRadius, bubbleHeight)
91-
..quadraticBezierTo(0, bubbleHeight, 0, bubbleHeight - borderRadius)
91+
..lineTo(tipStart, bubbleHeight - padding.top)
92+
..lineTo(tipPeak, bubbleHeight + tailHeight - padding.top)
93+
..lineTo(tipEnd, bubbleHeight - padding.top)
94+
..lineTo(borderRadius - padding.left, bubbleHeight - padding.top)
95+
..quadraticBezierTo(0 - padding.left, bubbleHeight - padding.top, 0 - padding.left, bubbleHeight - borderRadius - padding.top)
9296
..close();
9397

9498
// Draw the bubble

0 commit comments

Comments
 (0)