extension<T extends Object> method
T
extension<T extends Object>()
Obtains a particular ThemeExtension.
Creating and passing a ThemeExtension to FThemeData
class BrandColor extends ThemeExtension<BrandColor> {
final Color color;
BrandColor(this.color);
@override
BrandColor copyWith({Color? color}) => BrandColor(color ?? this.color);
@override
BrandColor lerp(BrandColor? other, double t) {
if (other is! BrandColor) return this;
return BrandColor(Color.lerp(color, other.color, t)!);
}
}
Passing it via constructor:
final theme = FThemeData(
extensions: [BrandColor(Colors.blue)],
... // other fields omitted for brevity
);
Passing it via copyWith:
theme.copyWith(extensions: [BrandColor(Colors.blue)]);
Accessing the extension
final brandColor = context.theme.extension<BrandColor>();
It is recommended to define a getter for your ThemeExtension:
BrandColor get brandColor => extension<BrandColor>();
}
Implementation
T extension<T extends Object>() => _extensions[T]! as T;