-
Notifications
You must be signed in to change notification settings - Fork 33
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Nested Copy with for Styles #360
Comments
I agree that the current way of composing nested styles is extremely verbose. However, I don’t think we should commit to dart_mappable or any package for the time being as dart macros are on the horizon. We’ll probably wait until that comes out before making a decision. |
@Pante Remeber me? Well, macros aint coming. |
Yup, just saw the news as well, that's a massive shame. |
I've been tinkering around with some ideas. And I wonder if introducing a To me, it seems like the most annoying issue is not having a reference to a style immediately after it has been created. This disrupts the "natural nesting" of styles by forcing us to declare variables out of order. It also automatically excludes the usage inside arrow functions/expressions. See My idea is to introduce a Given: // Simulates a parent style/FThemeData.
void OtherStyle({
required AStyle style,
}) {}
class AStyle {
final int foo;
final int bar;
final BStyle b;
AStyle({required this.foo, required this.bar, required this.b});
factory AStyle.inherit() => AStyle(foo: 1, bar: 2, b: BStyle(foo: 3, bar: 4));
AStyle map(AStyle Function(AStyle) builder) => builder(this);
AStyle copyWith({
int? foo,
int? bar,
BStyle? b,
}) =>
AStyle(
foo: foo ?? this.foo,
bar: bar ?? this.bar,
b: b ?? this.b,
);
}
class BStyle {
final int foo;
final int bar;
BStyle({required this.foo, required this.bar});
factory BStyle.inherit() => BStyle(foo: 1, bar: 2);
BStyle map(BStyle Function(BStyle) builder) => builder(this);
BStyle copyWith({
int? foo,
int? bar,
}) =>
BStyle(
foo: foo ?? this.foo,
bar: bar ?? this.bar,
);
} The current approach will yield: void current() {
// Requires a block.
final a = AStyle.inherit();
final b = a.b.copyWith(foo: 6);
OtherStyle(style: AStyle(foo: 5, bar: a.bar, b: b));
} The proposed changes will yield: void proposed() {
// Does not require a block (usable in arrow functions & expressions)
OtherStyle(
style: AStyle.inherit().map(
(style) => style.copyWith(
foo: 5,
b: style.b.copyWith(foo: 6),
),
),
);
} This allows a style to be reused multiple times in a context. It also allows the modification of deeply nested (3+ levels) properties. |
Is your feature request related to a problem? Please describe.
I really like how forui handles styles. Instead of tens of arguments for widgets there is a single style object which you copyWith.
However copyWith for nested classes is verbose and ugly
Describe the solution you'd like
Support for nested copyWith using dart_mappable
Describe alternatives you've considered
N/A
Additional context
N/A
The text was updated successfully, but these errors were encountered: