Problem:
The current implementation defines a separate widget class for each headline level:
class LargeHeadlineText extends BaseText { ... }
class MediumHeadlineText extends BaseText { ... }
class SmallHeadlineText extends BaseText { ... }
Issues:
- Verbose and repetitive
- Hard to maintain or extend
- Violates DRY principle
- Risk of inconsistency between variants
Root Cause:
Lack of a semantic enum or abstraction to represent headline types centrally, resulting in hardcoded widget duplication.
✅ Recommendation: Use a Unified HeadlineText Widget with Enum
Step 1 – Define a semantic enum:
enum HeadlineSize {
large,
medium,
small,
}
Step 2 – Create a single reusable widget:
class HeadlineText extends BaseText {
final HeadlineSize size;
const HeadlineText(
String text, {
required this.size,
super.key,
super.textAlign,
super.overflow,
super.maxLines,
super.color,
super.style,
}) : super(text: text);
@override
TextStyle getBaseStyle(ThemeData theme) {
switch (size) {
case HeadlineSize.large:
return theme.textTheme.headlineLarge!;
case HeadlineSize.medium:
return theme.textTheme.headlineMedium!;
case HeadlineSize.small:
return theme.textTheme.headlineSmall!;
}
}
}
Step 3 – Usage:
HeadlineText(
'Section Title',
size: HeadlineSize.medium,
);
Impact:
- Reduces code duplication
- Improves maintainability and readability
- Maintains strong ties to
TextTheme while enforcing semantic use
- Prevents misuse of text styles by hiding raw
TextStyle injection
This approach also keeps layout consistent and easier to scale across the app.
Problem:
The current implementation defines a separate widget class for each headline level:
Issues:
Root Cause:
Lack of a semantic enum or abstraction to represent headline types centrally, resulting in hardcoded widget duplication.
✅ Recommendation: Use a Unified
HeadlineTextWidget with EnumStep 1 – Define a semantic enum:
Step 2 – Create a single reusable widget:
Step 3 – Usage:
Impact:
TextThemewhile enforcing semantic useTextStyleinjectionThis approach also keeps layout consistent and easier to scale across the app.