Skip to content

[FTD #4] support circle for multi lines text, updated images #14

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file modified documentation/img/box.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified documentation/img/circle.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified documentation/img/highlight.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file removed documentation/img/overview.png
Binary file not shown.
Binary file modified documentation/img/underline.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion example/lib/screens/examples/box_example_screen.dart
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ class BoxExampleScreen extends StatelessWidget {
TextDecorator.boxed(
style: BoxStyle.curled,
text: const Text(
'Curcled Box',
'Curled Box',
style: TextStyle(fontSize: 16),
),
strokeWidth: 2,
Expand Down
26 changes: 21 additions & 5 deletions example/lib/screens/examples/circle_example_screen.dart
Original file line number Diff line number Diff line change
Expand Up @@ -23,18 +23,34 @@ class CircleExampleScreen extends StatelessWidget {
),
),
const SizedBox(height: 32),
Padding(
padding: const EdgeInsets.symmetric(horizontal: 30.0),
child: TextDecorator.circled(
style: CircleStyle.basic,
text: const Text(
'Franz jagt im komplett verwahrlosten Taxi quer durch Berlin',
style: TextStyle(fontSize: 16),
textAlign: TextAlign.center,
),
),
),
const SizedBox(height: 64),
TextDecorator.circled(
text: const Text(
'Circled Text',
style: TextStyle(fontSize: 32),
),
),
const SizedBox(height: 32),
TextDecorator.circled(
style: CircleStyle.basic,
text: const Text(
'Franz jagt im komplett verwahrlosten Taxi quer durch Berlin',
style: TextStyle(fontSize: 16),
Padding(
padding: const EdgeInsets.symmetric(horizontal: 30.0),
child: TextDecorator.circled(
style: CircleStyle.circled,
text: const Text(
'Franz jagt im komplett verwahrlosten Taxi quer durch Berlin',
style: TextStyle(fontSize: 16),
textAlign: TextAlign.center,
),
),
),
],
Expand Down
95 changes: 0 additions & 95 deletions lib/src/modules/box/painter/open_circle_painter.dart

This file was deleted.

47 changes: 47 additions & 0 deletions lib/src/modules/circle/classes/circle_angle_option.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
// ignore_for_file: public_member_api_docs, sort_constructors_first
import 'dart:math';

import 'package:flutter_text_decorator/src/modules/circle/painter/open_circle_painter.dart';

/// Defines a set of angles for rendering an "open circle" decoration.
///
/// This class encapsulates the start and sweep angles for two arcs
/// (typically one for the bottom part of the circle and one for the top part)
/// that together form an open or stylized circle.
///
/// It provides `const` factories for predefined angle configurations,
/// such as `bottomLeft()` or `topLeft()`, which can be used with
/// [OpenCirclePainter] to specify the orientation of the circle's opening.
/// Instances of this class are immutable.
class CircleAngleOption {
const CircleAngleOption({
required this.startAngleBottomCircle,
required this.sweepAngleBottomCircle,
required this.startAngleTopCircle,
required this.sweepAngleTopCircle,
});

const factory CircleAngleOption.bottomLeft() = _BottomLeftCircleAngleOption;

/// The starting angle for the bottom arc, in radians.
final double startAngleBottomCircle;

/// The sweep angle for the bottom arc, in radians.
final double sweepAngleBottomCircle;

/// The starting angle for the top arc, in radians.
final double startAngleTopCircle;

/// The sweep angle for the top arc, in radians.
final double sweepAngleTopCircle;
}

class _BottomLeftCircleAngleOption extends CircleAngleOption {
const _BottomLeftCircleAngleOption()
: super(
startAngleBottomCircle: -1.5 + pi,
sweepAngleBottomCircle: pi + 1.5,
startAngleTopCircle: (pi + 6.2) + pi,
sweepAngleTopCircle: pi - 1,
);
}
4 changes: 3 additions & 1 deletion lib/src/modules/circle/mixins/circle_mixin.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';
import 'package:flutter_text_decorator/src/modules/circle/classes/circle_size.dart';

/// A mixin that provides a utility method to calculate dimensions
Expand All @@ -19,12 +20,13 @@ mixin CircleConstraints {
CircleSize getCircleSizes({
required String text,
required TextStyle textStyle,
required Size size,
}) {
final textSpan = TextSpan(text: text, style: textStyle);
final textPainter = TextPainter(
text: textSpan,
textDirection: TextDirection.ltr,
)..layout();
)..layout(maxWidth: size.width);

const textHeightOffset = 2;
const textWidthScale = 1.8;
Expand Down
9 changes: 4 additions & 5 deletions lib/src/modules/circle/painter/closed_circle_painter.dart
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,10 @@ class ClosedCirclePainter extends TextDecoratorPainter with CircleConstraints {
..strokeWidth = decoration.strokeWidth
..style = PaintingStyle.stroke;

final circleSize = getCircleSizes(text: text, textStyle: textStyle);
final circleSize = getCircleSizes(text: text, textStyle: textStyle, size: size);

final scaledHorizontalRadius = circleSize.horizontalRadius * 2.1;
final scaledVerticalRadiusBottomCircle = circleSize.verticalRadius * 2.9;
const verticalOffset = 1.8;
final scaledVerticalRadiusBottomCircle = circleSize.verticalRadius * 3.5;
const verticalOffset = 2.5;
Comment on lines +48 to +49
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The centering looks off now and this makes the circle still overlap the circle. See:

Bildschirmfoto 2025-06-26 um 10 36 19


final centerOffset = Offset(
size.width / 2,
Expand All @@ -57,7 +56,7 @@ class ClosedCirclePainter extends TextDecoratorPainter with CircleConstraints {
canvas.drawOval(
Rect.fromCenter(
center: centerOffset,
width: scaledHorizontalRadius,
width: size.width * 1.1,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we implement this magic number as a param?

height: scaledVerticalRadiusBottomCircle,
),
paint,
Expand Down
34 changes: 21 additions & 13 deletions lib/src/modules/circle/painter/open_circle_painter.dart
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import 'dart:math';
import 'package:flutter/material.dart';
import 'package:flutter_text_decorator/src/modules/base/decoration_base.dart';
import 'package:flutter_text_decorator/src/modules/base/text_decoration_painter.dart';
import 'package:flutter_text_decorator/src/modules/circle/classes/circle_angle_option.dart';
import 'package:flutter_text_decorator/src/modules/circle/mixins/circle_mixin.dart';

/// A [CustomPainter] that draws an "open circle" decoration around text.
Expand Down Expand Up @@ -35,25 +35,27 @@ class OpenCirclePainter extends TextDecoratorPainter with CircleConstraints {
required super.text,
required super.textStyle,
required super.decoration,
this.circleAngleOption = const CircleAngleOption.bottomLeft(),
}) : assert(text != '' && decoration.strokeWidth > 0, 'text should not be empty and decoration.strokeWidth should be greater than 0');

/// Defines the start and sweep angles for the two arcs forming the open circle.
///
/// Defaults to [CircleAngleOption.bottomLeft] if not specified.
final CircleAngleOption circleAngleOption;

@override
void paint(Canvas canvas, Size size) {
final paint = Paint()
..color = decoration.color
..strokeWidth = decoration.strokeWidth
..style = PaintingStyle.stroke;

final circleSize = getCircleSizes(text: text, textStyle: textStyle);
final circleSize = getCircleSizes(text: text, textStyle: textStyle, size: size);

final scaledHorizontalRadius = circleSize.horizontalRadius * 2.1;
final scaledVerticalRadiusBottomCircle = circleSize.verticalRadius * 2.9;
final scaledHorizontalRadius = circleSize.horizontalRadius * 2;
final scaledVerticalRadiusBottomCircle = circleSize.verticalRadius * 3;
Comment on lines +55 to +56
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we make those magic numbers as params?

final scaledVerticalRadiusTopCircle = circleSize.verticalRadius * 3.5;
const verticalOffset = 1.8;
const startAngleBottomCircle = -1.5;
const sweepAngleBottomCircle = pi + 1.5;
const startAngleTopCircle = pi + 6.2;
const sweepAngleTopCircle = pi - 1;

final centerOffset = Offset(
size.width / 2,
Expand All @@ -67,8 +69,8 @@ class OpenCirclePainter extends TextDecoratorPainter with CircleConstraints {
width: scaledHorizontalRadius,
height: scaledVerticalRadiusBottomCircle,
),
startAngleBottomCircle,
sweepAngleBottomCircle,
circleAngleOption.startAngleBottomCircle,
circleAngleOption.sweepAngleBottomCircle,
false,
paint,
)
Expand All @@ -78,15 +80,21 @@ class OpenCirclePainter extends TextDecoratorPainter with CircleConstraints {
width: scaledHorizontalRadius,
height: scaledVerticalRadiusTopCircle,
),
startAngleTopCircle,
sweepAngleTopCircle,
circleAngleOption.startAngleTopCircle,
circleAngleOption.sweepAngleTopCircle,
false,
paint,
);
}

@override
bool shouldRepaint(covariant CustomPainter oldDelegate) {
return false;
if (oldDelegate is OpenCirclePainter) {
return oldDelegate.circleAngleOption.startAngleBottomCircle != circleAngleOption.startAngleBottomCircle ||
oldDelegate.circleAngleOption.sweepAngleBottomCircle != circleAngleOption.sweepAngleBottomCircle ||
oldDelegate.circleAngleOption.startAngleTopCircle != circleAngleOption.startAngleTopCircle ||
oldDelegate.circleAngleOption.sweepAngleTopCircle != circleAngleOption.sweepAngleTopCircle;
}
return true; // Repaint if the delegate type changes or for other unknown reasons
}
}