Component System
An overview of Jasprs component system.
Jaspr comes with a component system that is very similar to Flutters widgets. This is a core design decision of jaspr, in order to look and feel very familiar to developers coming from Flutter. You might think of it as just replacing the word 'Widget' with 'Component' (which actually was a small part of jasprs development process ๐), while keeping the same structure and behavior.
The following page and other documentation assumes, that you have already a basic understanding of Flutters widget system, especially the widget tree and the three base widgets: StatelessWidget, StatefulWidget and InheritedWidget.
When building an app or website with jaspr you will mostly use these three basic components:
StatelessComponent
A custom component that does not require any mutable state and looks like this:
class MyComponent extends StatelessComponent {
const MyComponent({Key? key}): super(key: key);
@override
Component build(BuildContext context) {
return p([text('Hello World')]);
}
}
Similar to Flutter, this component:
- extends the abstract
StatelessComponent
class, - has a constructor receiving a
Key
and optionally some custom parameters, - has a
build()
method receiving aBuildContext
.
StatefulComponent
A custom component that has mutable state and looks like this:
class MyComponent extends StatefulComponent {
const MyComponent({Key? key}): super(key: key);
@override
State createState() => MyComponentState();
}
class MyComponentState extends State<MyComponent> {
@override
Component build(BuildContext context) {
return p([text('Hello World')]);
}
}
Similar to Flutter, this component:
- extends the abstract
StatefulComponent
class, - has a constructor receiving a
Key
and optionally some custom parameters, - has a
createState()
method returning an instance of its custom state class
and has an associated state class that:
- extends the abstract
State<T>
class, - has a
build()
method inside the state class receiving aBuildContext
, - can have optional
initState()
,didChangeDependencies()
, and other lifecycle methods.
InheritedComponent
A base class for components that efficiently propagate information down the tree and looks like this:
class MyInheritedComponent extends InheritedComponent {
const MyInheritedComponent({required super.child, super.key}) ;
static MyInheritedComponent of(BuildContext context) {
final MyInheritedComponent? result = context.dependOnInheritedComponentOfExactType<MyInheritedComponent>();
assert(result != null, 'No MyInheritedComponent found in context');
return result!;
}
@override
bool updateShouldNotify(covariant MyInheritedComponent oldComponent) {
return false;
}
}
In every aspect, this component behaves the same as Flutters InheritedWidget
.