extension<T extends Object> method
T
extension<T extends Object>()
Obtains a particular FScalableExtension.
Creating and passing a FScalableExtension to FTypeface
class BrandTypeface extends FScalableExtension<BrandTypeface> {
final TextStyle display;
const BrandTypeface({required this.display});
@override
BrandTypeface copyWith({TextStyle? display}) => BrandTypeface(display: display ?? this.display);
@override
BrandTypeface lerp(BrandTypeface? other, double t) {
if (other is! BrandTypeface) return this;
return BrandTypeface(display: TextStyle.lerp(display, other.display, t)!);
}
@override
BrandTypeface scale({double sizeScalar = 1.0}) =>
BrandTypeface(display: display.copyWith(fontSize: (display.fontSize ?? 14) * sizeScalar));
}
Passing it via constructor:
final body = FTypeface(
extensions: [BrandTypeface(display: TextStyle(fontSize: 32, fontWeight: .bold))],
... // other fields omitted for brevity
);
Passing it via copyWith:
body.copyWith(extensions: [
BrandTypeface(display: TextStyle(fontSize: 32, fontWeight: .bold)),
]);
Accessing the extension
final brand = context.theme.typography.body.extension<BrandTypeface>();
It is recommended to define a getter for your FScalableExtension:
extension FTypefaceBrandTypeface on FTypeface {
BrandTypeface get brand => extension<BrandTypeface>();
// Alternatively
TextStyle get display => extension<BrandTypeface>().display;
}
final brand = context.theme.typography.body.brand;
final display = context.theme.typography.body.display;
Implementation
T extension<T extends Object>() => _extensions[T]! as T;