Skip to content

Commit

Permalink
fixed "textSizeOf" (#393)
Browse files Browse the repository at this point in the history
  • Loading branch information
frankmer authored May 27, 2024
1 parent 61fd185 commit 8a0b4c7
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 9 deletions.
6 changes: 4 additions & 2 deletions lib/utils/utils.dart
Original file line number Diff line number Diff line change
Expand Up @@ -100,8 +100,10 @@ String? getErrorMessageFromResponse(Response response) {
return errorMessage;
}

Size textSizeOf(String text, TextStyle style, {int? maxLines = 1, double minWidth = 0, double maxWidth = double.infinity}) {
final TextPainter textPainter = TextPainter(text: TextSpan(text: text, style: style), maxLines: maxLines, textDirection: TextDirection.ltr)
Size textSizeOf(
{required String text, required TextStyle style, required TextScaler? textScaler, int? maxLines, double minWidth = 0, double maxWidth = double.infinity}) {
final TextPainter textPainter = TextPainter(
text: TextSpan(text: text, style: style), maxLines: maxLines, textDirection: TextDirection.ltr, textScaler: textScaler ?? TextScaler.noScaling)
..layout(minWidth: minWidth, maxWidth: maxWidth);
return textPainter.size;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,12 @@ class TokenFolderWidget extends ConsumerWidget {
? LongPressDraggable(
maxSimultaneousDrags: 1,
dragAnchorStrategy: (draggable, context, position) {
final textSize = textSizeOf(folder.label, Theme.of(context).textTheme.titleLarge!);
final textSize = textSizeOf(
text: folder.label,
style: Theme.of(context).textTheme.titleLarge!,
textScaler: MediaQuery.of(context).textScaler,
maxLines: 1,
);
return Offset(max(textSize.width / 2, 30), textSize.height / 2 + 30);
},
onDragStarted: () => draggingSortableNotifier.state = folder,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,12 @@ class TokenWidgetBase extends ConsumerWidget {
},
onDraggableCanceled: (velocity, offset) => globalRef?.read(draggingSortableProvider.notifier).state = null,
dragAnchorStrategy: (Draggable<Object> d, BuildContext context, Offset point) {
final textSize = textSizeOf(token.label, Theme.of(context).textTheme.titleLarge!);
final textSize = textSizeOf(
text: token.label,
style: Theme.of(context).textTheme.titleLarge!,
textScaler: MediaQuery.of(context).textScaler,
maxLines: 1,
);
return Offset(max(textSize.width / 2, 30), textSize.height / 2 + 30);
},
feedback: Column(
Expand Down
6 changes: 3 additions & 3 deletions lib/widgets/focused_item_as_overlay.dart
Original file line number Diff line number Diff line change
Expand Up @@ -135,11 +135,11 @@ class _FocusedItemOverlayState extends State<_FocusedItemOverlay> {
_disposeOverlay();
if (widget.tooltipWhenFocused != null) {
final textSize = textSizeOf(
widget.tooltipWhenFocused!,
Theme.of(context).textTheme.bodyLarge!,
text: widget.tooltipWhenFocused!,
style: Theme.of(context).textTheme.bodyLarge!,
maxWidth: MediaQuery.of(context).size.width / 3 * 2 -
(tooltipPadding.left + tooltipPadding.right + tooltipMargin.left + tooltipMargin.right + tooltipBorderWidth * 2),
maxLines: null,
textScaler: MediaQuery.of(context).textScaler,
);

final overlaySize = Size(
Expand Down
18 changes: 16 additions & 2 deletions lib/widgets/status_bar.dart
Original file line number Diff line number Diff line change
Expand Up @@ -142,8 +142,22 @@ class _StatusBarOverlayEntryState extends State<StatusBarOverlayEntry> with Sing
final maxWidth = MediaQuery.of(context).size.width - margin * 2 - padding * 2;
final statusTextStyle = Theme.of(context).textTheme.bodyLarge?.copyWith(color: Colors.white) ?? const TextStyle();
final statusSubTextStyle = Theme.of(context).textTheme.bodySmall?.copyWith(color: Colors.white) ?? const TextStyle();
final statusTextHeight = textSizeOf(widget.statusText, statusTextStyle, maxWidth: maxWidth).height;
final statusSubTextHeight = widget.statusSubText != null ? textSizeOf(widget.statusSubText!, statusSubTextStyle, maxWidth: maxWidth).height : 0;
final statusTextHeight = textSizeOf(
text: widget.statusText,
style: statusTextStyle,
maxWidth: maxWidth,
textScaler: MediaQuery.of(context).textScaler,
maxLines: 1,
).height;
final statusSubTextHeight = widget.statusSubText != null
? textSizeOf(
text: widget.statusSubText!,
style: statusSubTextStyle,
maxWidth: maxWidth,
textScaler: MediaQuery.of(context).textScaler,
maxLines: 1,
).height
: 0;
return AnimatedPositioned(
onEnd: () {
if (mounted) {
Expand Down

0 comments on commit 8a0b4c7

Please sign in to comment.