Last active
October 17, 2024 23:17
-
-
Save loic-sharma/93a3603c6f831f99ea353bdeefd266f9 to your computer and use it in GitHub Desktop.
Flutter decorators using callables
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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