Overview
StatelessComponent is a component that does not require mutable state. It describes part of the user interface by building a constellation of other components that describe the user interface more concretely.
Stateless components are useful when the part of the user interface you are describing does not depend on anything other than the configuration information in the object itself and the BuildContext in which the component is inflated.
Constructor
An optional key to use for controlling how components are replaced in the tree.
Methods
build
BuildContext and when the dependencies of this component change (e.g., an InheritedComponent referenced by this component changes).
The location in the tree where this component is being built. Provides access to inherited components and other contextual information.
The component that describes this part of the user interface.
- Must only depend on the fields of the component (which must not change over time)
- Must only depend on ambient state obtained from
contextusingBuildContext.dependOnInheritedComponentOfExactType - Should not have any side effects beyond building a component
shouldRebuild
The new component configuration.
true if the component should rebuild (default), false to skip the rebuild.This method exists only as a performance optimization and gives no guarantees about when the component is rebuilt. Keep the implementation efficient and avoid deep comparisons or performance-heavy checks.
createElement
StatelessElement to manage this component’s location in the tree.
The element that will manage this component.
It is uncommon for subclasses to override this method.
Performance Considerations
Thebuild method is typically only called in three situations:
- The first time the component is inserted in the tree
- When the component’s parent changes its configuration
- When an
InheritedComponentit depends on changes
Optimization Techniques
Minimize nodes created
Minimize nodes created
Minimize the number of nodes transitively created by the build method and any components it creates.
Use const components
Use const components
Use
const components where possible, and provide a const constructor for the component so that users can also do so.Prefer components over helper methods
Prefer components over helper methods
When creating a reusable piece of UI, prefer using a component rather than a helper method. This allows the framework to efficiently re-render only parts that need to be updated.
Push state to the leaves
Push state to the leaves
If the component depends on
InheritedComponents that frequently change, consider refactoring into multiple components with the parts that change being pushed to the leaves.Example Usage
Basic Stateless Component
With Const Constructor
With shouldRebuild Optimization
Related Components
- StatefulComponent - For components that can change dynamically
- InheritedComponent - For components that provide ambient state
- AsyncStatelessComponent - For async components on the server