Skip to content

Commit 874f2e9

Browse files
authored
Introduce getSize String extension (#73)
* chore: Introduce StringPresentationExtensions + getSize * chore: Add PresentationExtensions export library * chore: Move isWithinMaxLines to presentation extensions
1 parent ecd780a commit 874f2e9

File tree

3 files changed

+66
-23
lines changed

3 files changed

+66
-23
lines changed

lib/extensions/string_extensions.dart

Lines changed: 0 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
import 'package:flutter/widgets.dart';
2-
31
extension StringExNullable on String? {
42
bool get isNullOrEmpty => this == null || this!.isEmpty;
53

@@ -27,27 +25,6 @@ extension StringEx on String {
2725
}
2826
}
2927

30-
bool isWithinMaxLines(
31-
{required int maxLines,
32-
required double maxWidth,
33-
double minWidth = 0.0,
34-
TextStyle style = const TextStyle()}) {
35-
if (isNullOrBlank) {
36-
return false;
37-
}
38-
39-
final text = TextSpan(text: this, style: style);
40-
final textPainter = TextPainter(
41-
maxLines: maxLines,
42-
textAlign: TextAlign.left,
43-
textDirection: TextDirection.ltr,
44-
text: text,
45-
);
46-
47-
textPainter.layout(minWidth: minWidth, maxWidth: maxWidth);
48-
return textPainter.didExceedMaxLines;
49-
}
50-
5128
String withDate(String date) => replaceAll('[DATE]', date);
5229

5330
String withNumber(num number) => replaceAll('[NUMBER]', number.toString());
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
// A set of extensions that can be used on the presentation layer.
2+
library presentation_extensions;
3+
4+
export 'string_presentation_extensions.dart';
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
import 'package:flutter/widgets.dart';
2+
import 'package:flutter_template/extensions/string_extensions.dart';
3+
4+
extension StringPresentationExtensions on String {
5+
TextPainter _getTextPainter({
6+
TextStyle style = const TextStyle(),
7+
int maxLines = 1,
8+
double minWidth = .0,
9+
double maxWidth = double.infinity,
10+
}) {
11+
final text = TextSpan(text: this, style: style);
12+
final textPainter = TextPainter(
13+
maxLines: maxLines,
14+
textAlign: TextAlign.left,
15+
textDirection: TextDirection.ltr,
16+
text: text,
17+
);
18+
19+
textPainter.layout(minWidth: minWidth, maxWidth: maxWidth);
20+
return textPainter;
21+
}
22+
23+
Size getSize(
24+
TextStyle style, {
25+
int maxLines = 1,
26+
double minWidth = .0,
27+
double maxWidth = double.infinity,
28+
}) {
29+
if (isNullOrBlank) {
30+
return Size.zero;
31+
}
32+
33+
final textPainter = _getTextPainter(
34+
style: style,
35+
maxLines: maxLines,
36+
maxWidth: maxWidth,
37+
minWidth: minWidth,
38+
);
39+
40+
return textPainter.size;
41+
}
42+
43+
bool isWithinMaxLines({
44+
required int maxLines,
45+
required double maxWidth,
46+
double minWidth = 0.0,
47+
TextStyle style = const TextStyle(),
48+
}) {
49+
if (isNullOrBlank) {
50+
return false;
51+
}
52+
53+
final textPainter = _getTextPainter(
54+
style: style,
55+
maxLines: maxLines,
56+
maxWidth: maxWidth,
57+
minWidth: minWidth,
58+
);
59+
60+
return textPainter.didExceedMaxLines;
61+
}
62+
}

0 commit comments

Comments
 (0)