Skip to content

Instantly share code, notes, and snippets.

@loic-sharma
Last active October 17, 2024 23:17
Show Gist options
  • Save loic-sharma/93a3603c6f831f99ea353bdeefd266f9 to your computer and use it in GitHub Desktop.
Save loic-sharma/93a3603c6f831f99ea353bdeefd266f9 to your computer and use it in GitHub Desktop.
Flutter decorators using callables
class Key {}
class Size {}
class Widget {}
// Decorator uses Dart callable magic
// https://dart.dev/language/callable-objects
class SizedBoxDecorator {
SizedBoxDecorator(this.child);
final Widget child;
Widget call({
Key? key,
double? width,
double? height,
}) => Widget();
Widget expand({Key? key}) => Widget();
Widget fromSize({Key? key, Size? size}) => Widget();
Widget shrink({Key? key}) => Widget();
Widget square({Key? key, double? dimension}) => Widget();
}
extension BasicDecorators on Widget {
// Gross allocation :(
SizedBoxDecorator get sizedBox => SizedBoxDecorator(this);
}
void main() {
Widget()
.sizedBox.square(dimension: 50)
.sizedBox.shrink()
.sizedBox.fromSize(size: Size())
.sizedBox.expand()
.sizedBox(width: 100, height: 100);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment