Skip to content

Commit 927100b

Browse files
committed
refactor(mix_tailwinds): simplify code with Dart 3 patterns
- Extract common H1-H6 build logic into _buildHeading helper - Simplify _BorderAccum setters with compound assignment - Refactor _extractMargin to use pattern matching with switch - Apply Dart 3 case patterns in type checks throughout tw_parser - Convert shadow apply functions to switch expressions
1 parent 8903549 commit 927100b

File tree

2 files changed

+85
-153
lines changed

2 files changed

+85
-153
lines changed

packages/mix_tailwinds/lib/src/tw_parser.dart

Lines changed: 28 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -150,22 +150,12 @@ class _BorderAccum {
150150
leftWidth != null ||
151151
rightWidth != null;
152152

153-
void setAll(double width) {
154-
topWidth = width;
155-
bottomWidth = width;
156-
leftWidth = width;
157-
rightWidth = width;
158-
}
153+
void setAll(double width) =>
154+
topWidth = bottomWidth = leftWidth = rightWidth = width;
159155

160-
void setHorizontal(double width) {
161-
leftWidth = width;
162-
rightWidth = width;
163-
}
156+
void setHorizontal(double width) => leftWidth = rightWidth = width;
164157

165-
void setVertical(double width) {
166-
topWidth = width;
167-
bottomWidth = width;
168-
}
158+
void setVertical(double width) => topWidth = bottomWidth = width;
169159
}
170160

171161
// =============================================================================
@@ -933,21 +923,16 @@ FlexBoxStyler _applyFlexDisplay(FlexBoxStyler styler, TwValue value) {
933923
}
934924

935925
FlexBoxStyler _applyFlexDirection(FlexBoxStyler styler, TwValue value) {
936-
if (value is TwEnumValue<Axis>) {
937-
final result = value.value == Axis.horizontal
938-
? styler.row()
939-
: styler.column();
940-
return result;
926+
if (value case TwEnumValue<Axis>(:final value)) {
927+
return value == Axis.horizontal ? styler.row() : styler.column();
941928
}
942929
return styler;
943930
}
944931

945932
FlexBoxStyler _applyAlignItems(FlexBoxStyler styler, TwValue value) {
946-
if (value is TwEnumValue<CrossAxisAlignment>) {
947-
final alignment = value.value;
948-
var result = styler.crossAxisAlignment(alignment);
949-
// CrossAxisAlignment.baseline requires textBaseline to be set
950-
if (alignment == CrossAxisAlignment.baseline) {
933+
if (value case TwEnumValue<CrossAxisAlignment>(:final value)) {
934+
var result = styler.crossAxisAlignment(value);
935+
if (value == CrossAxisAlignment.baseline) {
951936
result = result.textBaseline(TextBaseline.alphabetic);
952937
}
953938
return result;
@@ -956,16 +941,13 @@ FlexBoxStyler _applyAlignItems(FlexBoxStyler styler, TwValue value) {
956941
}
957942

958943
FlexBoxStyler _applyFlexShadow(FlexBoxStyler styler, TwValue value) {
959-
if (value is TwEnumValue) {
960-
final shadowValue = value.value;
961-
if (shadowValue is ElevationShadow?) {
962-
return shadowValue == null
963-
? styler.boxShadows(const <BoxShadowMix>[])
964-
: styler.elevation(shadowValue);
965-
}
966-
if (shadowValue is List<BoxShadowMix>?) {
967-
return styler.boxShadows(shadowValue ?? const <BoxShadowMix>[]);
968-
}
944+
if (value case TwEnumValue(:final value)) {
945+
return switch (value) {
946+
ElevationShadow shadow => styler.elevation(shadow),
947+
null => styler.boxShadows(const <BoxShadowMix>[]),
948+
List<BoxShadowMix> shadows => styler.boxShadows(shadows),
949+
_ => styler,
950+
};
969951
}
970952
return styler;
971953
}
@@ -1110,27 +1092,21 @@ BoxStyler _applyPropertyToBox(
11101092
}
11111093

11121094
BoxStyler _applyBoxShadow(BoxStyler styler, TwValue value) {
1113-
if (value is TwEnumValue) {
1114-
final shadowValue = value.value;
1115-
if (shadowValue is ElevationShadow?) {
1116-
return shadowValue == null
1117-
? styler.boxShadows(const <BoxShadowMix>[])
1118-
: styler.elevation(shadowValue);
1119-
}
1120-
if (shadowValue is List<BoxShadowMix>?) {
1121-
return styler.boxShadows(shadowValue ?? const <BoxShadowMix>[]);
1122-
}
1095+
if (value case TwEnumValue(:final value)) {
1096+
return switch (value) {
1097+
ElevationShadow shadow => styler.elevation(shadow),
1098+
null => styler.boxShadows(const <BoxShadowMix>[]),
1099+
List<BoxShadowMix> shadows => styler.boxShadows(shadows),
1100+
_ => styler,
1101+
};
11231102
}
11241103
return styler;
11251104
}
11261105

11271106
List<ShadowMix>? _resolveTextShadowMixes(TwValue value) {
1128-
if (value is TwEnumValue<TextShadowPreset?>) {
1129-
final preset = value.value;
1130-
if (preset == null) {
1131-
return const <ShadowMix>[];
1132-
}
1133-
final shadows = kTextShadowPresets[preset]!
1107+
if (value case TwEnumValue<TextShadowPreset?>(:final value)) {
1108+
if (value == null) return const <ShadowMix>[];
1109+
return kTextShadowPresets[value]!
11341110
.map(
11351111
(s) => ShadowMix(
11361112
color: s.color,
@@ -1139,7 +1115,6 @@ List<ShadowMix>? _resolveTextShadowMixes(TwValue value) {
11391115
),
11401116
)
11411117
.toList();
1142-
return shadows;
11431118
}
11441119
return null;
11451120
}
@@ -1187,8 +1162,8 @@ TextStyler _applyPropertyToText(
11871162
}
11881163

11891164
TextStyler _applyTextTransform(TextStyler styler, TwValue value) {
1190-
if (value is TwEnumValue<String>) {
1191-
return switch (value.value) {
1165+
if (value case TwEnumValue<String>(:final value)) {
1166+
return switch (value) {
11921167
'uppercase' => styler.uppercase(),
11931168
'lowercase' => styler.lowercase(),
11941169
'capitalize' => styler.capitalize(),

packages/mix_tailwinds/lib/src/tw_widget.dart

Lines changed: 57 additions & 100 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,19 @@ bool _hasBoxUtilities(String classNames) {
3939
return false;
4040
}
4141

42+
(String?, String) _splitMarginToken(String base) {
43+
const prefixes = ['mx-', 'my-', 'mt-', 'mr-', 'mb-', 'ml-', 'm-'];
44+
for (final prefix in prefixes) {
45+
if (base.startsWith(prefix)) {
46+
return (
47+
prefix.substring(0, prefix.length - 1),
48+
base.substring(prefix.length),
49+
);
50+
}
51+
}
52+
return (null, base);
53+
}
54+
4255
/// Extract margin value from tokens for a given prefix (e.g., 'mb-').
4356
/// Returns null if not found.
4457
EdgeInsets? _extractMargin(String classNames, TwConfig cfg) {
@@ -49,41 +62,27 @@ EdgeInsets? _extractMargin(String classNames, TwConfig cfg) {
4962
final colonIdx = findLastColonOutsideBrackets(token);
5063
final base = colonIdx >= 0 ? token.substring(colonIdx + 1) : token;
5164

52-
if (base.startsWith('m-')) {
53-
final value = cfg.spaceOf(base.substring(2), fallback: double.nan);
54-
if (!value.isNaN) {
65+
final (prefix, suffix) = _splitMarginToken(base);
66+
if (prefix == null) continue;
67+
68+
final value = cfg.spaceOf(suffix, fallback: double.nan);
69+
if (value.isNaN) continue;
70+
71+
switch (prefix) {
72+
case 'm':
5573
top = right = bottom = left = value;
56-
}
57-
} else if (base.startsWith('mx-')) {
58-
final value = cfg.spaceOf(base.substring(3), fallback: double.nan);
59-
if (!value.isNaN) {
74+
case 'mx':
6075
left = right = value;
61-
}
62-
} else if (base.startsWith('my-')) {
63-
final value = cfg.spaceOf(base.substring(3), fallback: double.nan);
64-
if (!value.isNaN) {
76+
case 'my':
6577
top = bottom = value;
66-
}
67-
} else if (base.startsWith('mt-')) {
68-
final value = cfg.spaceOf(base.substring(3), fallback: double.nan);
69-
if (!value.isNaN) {
78+
case 'mt':
7079
top = value;
71-
}
72-
} else if (base.startsWith('mr-')) {
73-
final value = cfg.spaceOf(base.substring(3), fallback: double.nan);
74-
if (!value.isNaN) {
80+
case 'mr':
7581
right = value;
76-
}
77-
} else if (base.startsWith('mb-')) {
78-
final value = cfg.spaceOf(base.substring(3), fallback: double.nan);
79-
if (!value.isNaN) {
82+
case 'mb':
8083
bottom = value;
81-
}
82-
} else if (base.startsWith('ml-')) {
83-
final value = cfg.spaceOf(base.substring(3), fallback: double.nan);
84-
if (!value.isNaN) {
84+
case 'ml':
8585
left = value;
86-
}
8786
}
8887
}
8988

@@ -392,6 +391,24 @@ class Span extends StatelessWidget {
392391
}
393392
}
394393

394+
Widget _buildHeading(
395+
BuildContext context,
396+
String text,
397+
String classNames,
398+
TwConfig? config,
399+
) {
400+
final cfg = config ?? TwConfigProvider.of(context);
401+
final style = TwParser(config: cfg).parseText(classNames);
402+
Widget result = StyledText(text, style: style);
403+
404+
final margin = _extractMargin(classNames, cfg);
405+
if (margin != null) {
406+
result = Padding(padding: margin, child: result);
407+
}
408+
409+
return result;
410+
}
411+
395412
/// Heading level 1 element with Tailwind styling.
396413
///
397414
/// Equivalent to HTML `<h1>`. Note: Like Tailwind's Preflight, headings have
@@ -405,18 +422,8 @@ class H1 extends StatelessWidget {
405422
final TwConfig? config;
406423

407424
@override
408-
Widget build(BuildContext context) {
409-
final cfg = config ?? TwConfigProvider.of(context);
410-
final style = TwParser(config: cfg).parseText(classNames);
411-
Widget result = StyledText(text, style: style);
412-
413-
final margin = _extractMargin(classNames, cfg);
414-
if (margin != null) {
415-
result = Padding(padding: margin, child: result);
416-
}
417-
418-
return result;
419-
}
425+
Widget build(BuildContext context) =>
426+
_buildHeading(context, text, classNames, config);
420427
}
421428

422429
/// Heading level 2 element with Tailwind styling.
@@ -432,18 +439,8 @@ class H2 extends StatelessWidget {
432439
final TwConfig? config;
433440

434441
@override
435-
Widget build(BuildContext context) {
436-
final cfg = config ?? TwConfigProvider.of(context);
437-
final style = TwParser(config: cfg).parseText(classNames);
438-
Widget result = StyledText(text, style: style);
439-
440-
final margin = _extractMargin(classNames, cfg);
441-
if (margin != null) {
442-
result = Padding(padding: margin, child: result);
443-
}
444-
445-
return result;
446-
}
442+
Widget build(BuildContext context) =>
443+
_buildHeading(context, text, classNames, config);
447444
}
448445

449446
/// Heading level 3 element with Tailwind styling.
@@ -459,18 +456,8 @@ class H3 extends StatelessWidget {
459456
final TwConfig? config;
460457

461458
@override
462-
Widget build(BuildContext context) {
463-
final cfg = config ?? TwConfigProvider.of(context);
464-
final style = TwParser(config: cfg).parseText(classNames);
465-
Widget result = StyledText(text, style: style);
466-
467-
final margin = _extractMargin(classNames, cfg);
468-
if (margin != null) {
469-
result = Padding(padding: margin, child: result);
470-
}
471-
472-
return result;
473-
}
459+
Widget build(BuildContext context) =>
460+
_buildHeading(context, text, classNames, config);
474461
}
475462

476463
/// Heading level 4 element with Tailwind styling.
@@ -486,18 +473,8 @@ class H4 extends StatelessWidget {
486473
final TwConfig? config;
487474

488475
@override
489-
Widget build(BuildContext context) {
490-
final cfg = config ?? TwConfigProvider.of(context);
491-
final style = TwParser(config: cfg).parseText(classNames);
492-
Widget result = StyledText(text, style: style);
493-
494-
final margin = _extractMargin(classNames, cfg);
495-
if (margin != null) {
496-
result = Padding(padding: margin, child: result);
497-
}
498-
499-
return result;
500-
}
476+
Widget build(BuildContext context) =>
477+
_buildHeading(context, text, classNames, config);
501478
}
502479

503480
/// Heading level 5 element with Tailwind styling.
@@ -513,18 +490,8 @@ class H5 extends StatelessWidget {
513490
final TwConfig? config;
514491

515492
@override
516-
Widget build(BuildContext context) {
517-
final cfg = config ?? TwConfigProvider.of(context);
518-
final style = TwParser(config: cfg).parseText(classNames);
519-
Widget result = StyledText(text, style: style);
520-
521-
final margin = _extractMargin(classNames, cfg);
522-
if (margin != null) {
523-
result = Padding(padding: margin, child: result);
524-
}
525-
526-
return result;
527-
}
493+
Widget build(BuildContext context) =>
494+
_buildHeading(context, text, classNames, config);
528495
}
529496

530497
/// Heading level 6 element with Tailwind styling.
@@ -540,18 +507,8 @@ class H6 extends StatelessWidget {
540507
final TwConfig? config;
541508

542509
@override
543-
Widget build(BuildContext context) {
544-
final cfg = config ?? TwConfigProvider.of(context);
545-
final style = TwParser(config: cfg).parseText(classNames);
546-
Widget result = StyledText(text, style: style);
547-
548-
final margin = _extractMargin(classNames, cfg);
549-
if (margin != null) {
550-
result = Padding(padding: margin, child: result);
551-
}
552-
553-
return result;
554-
}
510+
Widget build(BuildContext context) =>
511+
_buildHeading(context, text, classNames, config);
555512
}
556513

557514
/// Convenience wrapper for truncated text in flex containers.

0 commit comments

Comments
 (0)