Skip to content

Instantly share code, notes, and snippets.

@yjbanov
Last active June 6, 2018 05:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yjbanov/9a3f1bf6dd45b18d82a0b75ef294ca1b to your computer and use it in GitHub Desktop.
Save yjbanov/9a3f1bf6dd45b18d82a0b75ef294ca1b to your computer and use it in GitHub Desktop.
A Darty widget declaration syntax
// Stateless widget:
//
// - no `extends StatelessWidget`
// - fields are `final` by default
// - `@override`, `Widget` return type and `BuildContext context` parameter are implied
widget MyApp {
String title;
build {
}
}
// Stateful widget:
//
// - same as a stateless widget, but with a nested `state` block
// - fields outside the `state` block are `final`
// - fields inside the `state` block are mutable
// - ideally one wouldn't need to call `setState`; instead any assignment
// to one of the `state` fields automatically results in state change
widget MyApp {
String title;
state {
String counter;
}
build {
}
}
@yjbanov
Copy link
Author

yjbanov commented Jun 6, 2018

Two important properties of this syntax are:

  • low boilerplate encourages breaking down your UI into reusable (and independently updatable) widgets
  • conversion between stateless and stateful widget is a matter of adding/removing the state block.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment