|
I'm surprised there doesn't seem to be snippets for Components like there are for Flutter Widgets ( Because of this I keep having to copy existing Components when creating new ones. |
Answered by
schultek
Jul 4, 2025
Replies: 2 comments 5 replies
|
There is a quick action in jaspr_lints to create component boilerplates. But still good idea, what snippets would you like to see? |
2 replies
|
I did try something related to this so think will put this here if it helps anyone @schultek do let me know your thoughts on this kind of approach as well, import 'package:jaspr/jaspr.dart';
typedef Widget = Component;
abstract class StatelessWidget extends StatelessComponent {
const StatelessWidget({super.key});
@override
Iterable<Component> build(BuildContext context) sync* {
yield render(context);
}
Component render(BuildContext context);
}
abstract class StatefulWidget extends StatefulComponent {
const StatefulWidget({super.key});
@override
State<StatefulWidget> createState();
}
abstract class WidgetState<T extends StatefulWidget> extends State<T> {
@override
Iterable<Component> build(BuildContext context) sync* {
yield render(context);
}
/// Subclasses should implement this instead of `build`
Component render(BuildContext context);
}
class Group extends StatelessComponent {
final List<Component> children;
Group({required this.children});
@override
Iterable<Component> build(BuildContext context) {
return children;
}
}
class Div extends StatelessWidget {
final String? style;
final String? cls;
final Component? child;
const Div({super.key, this.style, this.cls, this.child});
@override
Component render(BuildContext context) {
return DomComponent(
tag: 'div', classes: '${style ?? ''} ${cls ?? ''}', child: child);
}
}
// .... more widgets/components
Then with this utility file, I can use it in more like a flutter way which makes it feel little bit more like home. I can then use the same import 'package:shipfast_clone/shared/utils.dart';
class LeaderboardButton extends StatelessWidget {
const LeaderboardButton({super.key});
@override
Widget render(BuildContext context) {
return Div(
style: '...', // use tailwind css
child: Group(
children: [
Text('Press'),
Span(
style: '...',
child: Text('L')
),
Text('to see the Leaderboards'),
]
)
);
}
}
|
3 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The extension now comes with snippets like
jstful,jstlessand more likejhtmlandjtext.