extension<T extends Object> method

T extension<T extends Object>()

Obtains a particular ThemeExtension.

Creating and passing a FTypographyExtension to FTypography

class BrandTypography extends FTypographyExtension<BrandTypography> {
  final TextStyle display;

  const BrandTypography({required this.display});

  @override
  BrandTypography copyWith({TextStyle? display}) => BrandTypography(display: display ?? this.display);

  @override
  BrandTypography lerp(BrandTypography? other, double t) {
    if (other is! BrandTypography) return this;
    return BrandTypography(display: TextStyle.lerp(display, other.display, t)!);
  }

  @override
  BrandTypography scale({double sizeScalar = 1.0}) =>
      BrandTypography(display: display.copyWith(fontSize: (display.fontSize ?? 14) * sizeScalar));
}

Passing it via constructor:

final typography = FTypography(
  extensions: [BrandTypography(display: TextStyle(fontSize: 32, fontWeight: .bold))],
  ... // other fields omitted for brevity
);

Passing it via copyWith:

typography.copyWith(extensions: [
  BrandTypography(display: TextStyle(fontSize: 32, fontWeight: .bold)),
]);

Accessing the extension

final brand = context.theme.typography.extension<BrandTypography>();

It is recommended to define a getter for your FTypographyExtension:

extension FTypographyBrandTypography on FTypography {
  BrandTypography get brand => extension<BrandTypography>();

  // Alternatively
  TextStyle get display => extension<BrandTypography>().display;
}

final brand = context.theme.typography.brand;

final display = context.theme.typography.display;

Implementation

T extension<T extends Object>() => _extensions[T]! as T;