Last active
February 7, 2025 06:41
-
-
Save BarryDaBee/72fc7d7fb451b746d8732aa182ed9691 to your computer and use it in GitHub Desktop.
A robust approach to handling app typography using theme_extensions for large scale apps with SOLID principles. PS: Open to questions, suggestions and improvements.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'package:flutter/material.dart'; | |
abstract class AppTypography { | |
AppTypography({ | |
required this.name, | |
required this.regular, | |
required this.medium, | |
required this.semiBold, | |
required this.bold, | |
}); | |
final String name; | |
final TextStyle regular; | |
final TextStyle medium; | |
final TextStyle semiBold; | |
final TextStyle bold; | |
AppTypography copyWith({ | |
TextStyle? regular, | |
TextStyle? medium, | |
TextStyle? semiBold, | |
TextStyle? bold, | |
}); | |
AppTypography lerp(covariant AppTypography other, double t); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'package:flutter/material.dart'; | |
import 'app_typography.dart'; | |
class Header3 extends AppTypography { | |
Header3({ | |
TextStyle? regular, | |
TextStyle? medium, | |
TextStyle? semiBold, | |
TextStyle? bold, | |
}) : super( | |
name: 'Header3', | |
regular: regular ?? _base, | |
medium: medium ?? _base.copyWith(fontWeight: FontWeight.w500), | |
semiBold: semiBold ?? _base.copyWith(fontWeight: FontWeight.w600), | |
bold: bold ?? _base.copyWith(fontWeight: FontWeight.w700), | |
); | |
static const TextStyle _base = TextStyle(fontSize: 32); | |
@override | |
Header3 copyWith({ | |
TextStyle? regular, | |
TextStyle? medium, | |
TextStyle? semiBold, | |
TextStyle? bold, | |
}) { | |
return Header3( | |
regular: regular ?? this.regular, | |
medium: medium ?? this.medium, | |
semiBold: semiBold ?? this.semiBold, | |
bold: bold ?? this.bold, | |
); | |
} | |
@override | |
Header3 lerp(covariant AppTypography other, double t) { | |
if (other is! Header3) return this; | |
return Header3( | |
regular: TextStyle.lerp(regular, other.regular, t), | |
medium: TextStyle.lerp(medium, other.medium, t), | |
semiBold: TextStyle.lerp(semiBold, other.semiBold, t), | |
bold: TextStyle.lerp(bold, other.bold, t), | |
); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'package:flutter/material.dart'; | |
import 'app_typography'; | |
@immutable | |
class TypographyExtension extends ThemeExtension<TypographyExtension> { | |
const TypographyExtension({ | |
required this.heading3, | |
required this.heading4, | |
required this.heading5, | |
required this.heading6, | |
required this.subHeading, | |
required this.body, | |
required this.subText, | |
required this.caption, | |
}); | |
final AppTypography heading3; | |
final AppTypography heading4; | |
final AppTypography heading5; | |
final AppTypography heading6; | |
final AppTypography subHeading; | |
final AppTypography body; | |
final AppTypography subText; | |
final AppTypography caption; | |
@override | |
TypographyExtension copyWith({ | |
AppTypography? heading3, | |
AppTypography? heading4, | |
AppTypography? heading5, | |
AppTypography? heading6, | |
AppTypography? subHeading, | |
AppTypography? body, | |
AppTypography? subText, | |
AppTypography? caption, | |
}) { | |
return TypographyExtension( | |
heading3: heading3 ?? this.heading3, | |
heading4: heading4 ?? this.heading4, | |
heading5: heading5 ?? this.heading5, | |
heading6: heading6 ?? this.heading6, | |
subHeading: subHeading ?? this.subHeading, | |
body: body ?? this.body, | |
subText: subText ?? this.subText, | |
caption: caption ?? this.caption, | |
); | |
} | |
@override | |
TypographyExtension lerp( | |
ThemeExtension<TypographyExtension>? other, double t) { | |
if (other is! TypographyExtension) return this; | |
return TypographyExtension( | |
heading3: heading3.lerp(other.heading3, t), | |
heading4: heading4.lerp(other.heading4, t), | |
heading5: heading5.lerp(other.heading5, t), | |
heading6: heading6.lerp(other.heading6, t), | |
subHeading: subHeading.lerp(other.subHeading, t), | |
body: body.lerp(other.body, t), | |
subText: subText.lerp(other.subText, t), | |
caption: caption.lerp(other.caption, t), | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment