-
Notifications
You must be signed in to change notification settings - Fork 7
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
Creating multiple classes from one structure #17
Comments
In #9 (comment) we started converging on what I think might be an interesting approach here. Consider the following: class ExampleState extends State<Example> {
int count = 0;
void increment() => setState(() => count++);
@statefulWidget
Widget _build(BuildContext context, String title) {
return Scaffold(
body: Text('$title ${count}'),
floatingButton: FloatingButton(
onPressed: () => increment())
),
);
}
} Which would generate the widget class, something like the following: class Example extends StatefulWidget {
final String title;
Example(this.title) : super();
createState() => ExampleState();
} And then also generates the class ExampleState extends State<Example> {
Widget build(BuildContext context) => _build(context, widget.title);
} I believe you could similarly handle all of the other lifecycle methods as mentioned in your example. |
What is |
It doesn't exist in the original code - the user only writes the state class (so there is only one "structure", The The actual build method on the state class is also code generated, and it calls your annotated build method with the requested parameters by grabbing them off the widget (in this case it would be |
I think creating the |
I agree with that statement - I just don't see how it would work without ripping that class apart which I think comes with worse downsides (imo), and is overall more confusing/magic. The widget classes aren't interesting, the state classes are really where are all the logic lives. So this is the best solution I have come up with, given the trade-offs (and capabilities currently available). |
Since this is only a prototype anyway, maybe it's worth adding support for these capabilities and try it out. That would let us get a better feel for the user experience. I'm worried about things being too magical too, but at the same time, it would make the stateful widget macro a lot nicer to use in practice. Maybe we should at least try it out and see without committing to supporting those capabilities? |
There would be a bunch to hash out here, we could maybe use a bit of our upcoming meeting time to discuss some of the details? I am not sure that the current prototype is good for identifying any usability concerns around either approach because ultimately the user in this case sees the fully generated file so it won't look any different. Although we could evaluate the author side some. |
Consider a widget that receives a Listenable object, and wants to subscribe to it and rebuild each time it triggers. Currently this requires a lot of boilerplate:
The current API makes this difficult because of the way this impacts two classes simultaneously. Ideally you'd only write something like:
...and it would build everything else around you. However, in the current prototype, you have to hook everything to a class that will eventually be created, since we're just annotating to add code, we can't really change any of the code.
The text was updated successfully, but these errors were encountered: