Widget

Abstract Class
Represents the base class for all widgets. Provides a foundation for defining user interface components, with support for diagnostics, modifiers, state reconciliation, and defining widget-specific behavior.

Fields

constants: object[]
An array of constant objects associated with the widget.

The constants property is used to prevent reconciliation (rebuilding) when updating the widget tree. If the array of constants in the current widget matches the array in the previous widget during a reconciliation check, the system assumes the widgets are equivalent, and no further updates are applied.

The exact same widget with the same reference is always considered equivalent and therefore treated as constant, using a constants array here is not beneficial.

If the constants provided in two widgets do not match, the reconciliation system performs a normal rebuild of the corresponding widget subtree.

key: Key
A unique identifier for the widget instance.

The key property is used to determine whether a widget can be reused or must be replaced during the reconciliation process. Keys also play a critical role in preserving widget states when the widget tree is updated.

If two widgets with the same parent have keys that are equal, those widgets are considered positionally equivalent and their states can be reused if their types match and reconciliation checks are successful.

If no key is specified, parent widgets will attempt to match children based primarily on their actual position. Insertions and removal in the middle of the widget list may lead to unintended rebuilds and state recreation as possibly valid widgets are being discarded.

Keys may also be used to for clarity or persistent tracking of a widget's identity using a GlobalKey which is guaranteed to be unique across multiple parents and holds a reference to the underlying IWidgetElement


Key
modifiers: ModifierSet
An effectively immutable collection of modifications applied to a widget's underlying VisualElement. They can manipulate the element's style, layout, properties, and behavior generically.

Modifiers may be added to a widget using the Widget.modifiers constructor parameter, the Widget.AddModifier or Widget.AddModifiers methods, or by using the ModifierExtensions.

Constructors

Widget(key: Key, constants: object[], modifiers: IReadOnlyCollection<Modifier>)
Base constructor for a widget. See Widget.key and Widget.constants and Widget.modifiers for more information on the constructor parameters.

Methods

AddModifier(modifier: Modifier): void
Adds a modifier to the widget.
AddModifiers(additions: IEnumerable<Modifier>): void
Adds a sequence of modifiers to the widget.
DefaultModifiers(defaults: ModifierSet, user: IReadOnlyCollection<Modifier>): void
Sets the default modifiers for the widget.
RemarksWhen using this method, do not pass the modifiers into the base constructor and instead call this method in the constructor body of the derived class.

Parameters

defaults:ModifierSet - The default modifiers specified by the widget implementation.
user:IReadOnlyCollection<Modifier> - The modifiers specified by the user.
GetModifiers(): ModifierSet
Returns the ModifierSet that is applied to the widget.
ReconcileInto(element: IWidgetElement): IWidgetElement
Reconciles the widget into the given element by registering a onetime callback for AttachToPanelEvent that triggers the initial reconciliation once the element is attached to the panel.

Parameters

element:IWidgetElement - The newly created uninitialized element that is not yet attached to a panel.

Returns

IWidgetElement-The same element that was passed in, for chaining purposes.
DebugFillProperties(properties: DiagnosticPropertiesBuilder): void
ToStringShort(): string
GetWidgetName(): string
CreateElement(): IWidgetElement
Creates a new IWidgetElement for the given widget configuration.
RemarksUsually, this will create an instance of an IWidgetElement and then call Widget.ReconcileInto to perform the initial reconciliation after that element gets attached to its parent and the panel.

Operators

BuildFunction(widget: Widget): BuildFunction
Converts the widget into a constant BuildFunction.
BuildFunction<WidgetState>(widget: Widget): BuildFunction<WidgetState>
Converts the widget into a constant BuildFunction that always returns the same widget for all widget states.

Inherited Members

Extension Methods

ToDiagnosticsNodeSafeIfClipConstDisplayExpandFillFlexibleMarginOpacityPaddingPositionedShrinkSizeStretchTightStretchTightVisibilityWithModifierToBuildableToFactoryDescribeIdentityShortHashToStringNullable

On this page