How to extend existing utility? #321
-
In my application, all gradients are top-down, whereas in Flutter they go left to right. Therefore, I want to create an API like extension MixGradientUtilityX on GradientUtility {
LinearGradientUtility get linearVertical => LinearGradientUtility(
(p0) => builder(
const LinearGradientDto(begin: Alignment.topCenter, end: Alignment.bottomCenter).merge(p0),
),
);
} However, the |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 2 replies
-
@Maksimka101, you bring up a valid point. Right now, we wanted to keep the builder method protected because we want to avoid the generation of custom build methods outside of the main signatures. Here is an example: extension LinearGradientUtilityX<T extends Attribute> on LinearGradientUtility<T> {
LinearGradientUtility<T> get vertical {
return LinearGradientUtility(
(v) => only(begin: Alignment.topCenter, end: Alignment.bottomCenter));
}
} With this, you can have the When doing Another option, which I prefer, is to create a function method to have that as the only composable piece, and you can compose the vertical as part of the style. extension LinearGradientUtilityX<T extends Attribute> on LinearGradientUtility<T> {
T vertical() {
return only(
begin: Alignment.topCenter,
end: Alignment.bottomCenter,
);
}
} The main difference with this approach is that once you do FYI: I would not do the merge within the utility, as this is the same as using: |
Beta Was this translation helpful? Give feedback.
@Maksimka101, you bring up a valid point. Right now, we wanted to keep the builder method protected because we want to avoid the generation of custom build methods outside of the main signatures.
Here is an example:
With this, you can have the
vertical
whenever the LinearGradientUtility is used. That would be$box.linearGradient.vertical
or$box.decoration.gradient.linear.vertical
.When doing
linearGradient.vertical.colors()
, the begin and end will…