Similar to a light switch, with on and off states that may be toggled. An alternative to a Check, especially on mobile.

The following example creates a toggle switch, programmatically selects it, and listens for when the selection changes:

var toggleSwitch = new ToggleSwitch();
toggleSwitch.selected = true;
toggleSwitch.addEventListener(Event.CHANGE, (event) -> {
	var toggleSwitch = cast(event.currentTarget, ToggleSwitch);
	trace("toggle switch changed: " + toggleSwitch.selected);
});
this.addChild(toggleSwitch);

Events:

openfl.events.Event.CHANGE

Dispatched when ToggleSwitch.selected changes.

Available since

1.0.0

.

See also:

Constructor

@:value({ selected : false })new(selected:Bool = false, ?changeListener:Event ‑> Void)

Creates a new ToggleSwitch object.

Available since

1.0.0

.

Variables

@:style@:flash.propertydisabledTextFormat:AbstractTextFormat

The font styles used to render the toggle switch's text when the toggle switch is disabled.

In the following example, the toggle switch's disabled text formatting is customized:

toggleSwitch.enabled = false;
toggleSwitch.disabledTextFormat = new TextFormat("Helvetica", 20, 0xee0000);
Available since

1.3.0

.

See also:

@:style@:flash.propertyembedFonts:Bool

Determines if an embedded font is used or not.

In the following example, the toggle switch uses embedded fonts:

toggleSwitch.embedFonts = true;
Available since

1.3.0

.

See also:

offText:String

The text displayed by the toggle switch when it is toggled off. If null, no text is displayed.

The following example sets the label's off text:

label.offText = "OFF";
Available since

1.3.0

.

See also:

onText:String

The text displayed by the toggle switch when it is toggled on. If null, no text is displayed.

The following example sets the label's on text:

label.onText = "ON";
Available since

1.3.0

.

See also:

@:style@:flash.propertypaddingLeft:Float

The minimum space, measured in pixels, between the toggle switch's left edge and the left edge of the thumb.

In the following example, the toggle switch's left padding is set to 20 pixels:

toggleSwitch.paddingLeft = 20.0;
Available since

1.0.0

.

@:style@:flash.propertypaddingRight:Float

The minimum space, measured in pixels, between the toggle switch's right edge and the right edge of the thumb.

In the following example, the toggle switch's right padding is set to 20 pixels:

toggleSwitch.paddingRight = 20.0;
Available since

1.0.0

.

@:style@:flash.propertysecondaryTrackSkin:DisplayObject

The skin to use for the toggle switch's optional secondary track. If a toggle switch has one track, it will fill the entire length of the toggle switch. If a toggle switch has a track and a secondary track, the primary track will stretch between the left edge of the toggle switch and the location of the slider's thumb, while the secondary track will stretch from the location of the toggle switch's thumb to the right edge of the toggle switch.

In the following example, a track skin and a secondary track skin are passed to the toggle switch:

var skin = new RectangleSkin();
skin.fill = SolidColor(0xaaaaaa);
toggleSwitch.trackSkin = skin;

var skin = new RectangleSkin();
skin.fill = SolidColor(0xcccccc);
toggleSwitch.secondaryTrackSkin = skin;
Available since

1.0.0

.

See also:

@:bindable("change")@:inspectable(defaultValue = "false")selected:Bool

Indicates if the toggle switch is selected or not.

The following example selects the toggle switch:

toggleSwitch.selected = true;

Note: When changing the selected property programatically, the position of the thumb is not animated. To change the selection with animation, call the setSelectionWithAnimation() method.

Available since

1.0.0

.

See also:

@:style@:flash.propertytextFormat:AbstractTextFormat

The font styles used to render the toggle switch's text.

In the following example, the toggle switch's text formatting is customized:

toggleSwitch.textFormat = new TextFormat("Helvetica", 20, 0xcc0000);
Available since

1.3.0

.

See also:

@:style@:flash.propertythumbSkin:DisplayObject

The skin to use for the toggle switch's thumb.

In the following example, a thumb skin is passed to the toggle switch:

var skin = new RectangleSkin();
skin.fill = SolidColor(0xcccccc);
toggleSwitch.thumbSkin = skin;
Available since

1.0.0

.

See also:

@:style@:flash.propertytoggleDuration:Float

The duration, measured in seconds, of the animation when the toggle switch is clicked or tap and the thumb slides to the other side.

In the following example, the duration of the animation that toggles the thumb is set to 500 milliseconds:

toggleSwitch.toggleDuration = 0.5;
Available since

1.0.0

.

@:style@:flash.propertytoggleEase:IEasing

The easing function used for the animation when the toggle switch is clicked or tap and the thumb slides to the other side.

In the following example, the ease of the animation that toggles the thumb is customized:

toggleSwitch.toggleEase = Elastic.easeOut;
Available since

1.0.0

.

@:style@:flash.propertytrackSkin:DisplayObject

The skin to use for the toggle switch's track.

In the following example, a track skin is passed to the toggle switch:

var skin = new RectangleSkin();
skin.fill = SolidColor(0xcccccc);
toggleSwitch.trackSkin = skin;
Available since

1.0.0

.

See also:

Methods

setPadding(value:Float):Void

Sets all padding properties to the same value.

Available since

1.0.0

.

See also:

setSelectionWithAnimation(selected:Bool):Bool

Changes the selected property and animates the position of the thumb.

Available since

1.0.0

.

See also:

Inherited Variables

Defined by FeathersControl

@:stylealwaysShowFocus:Bool

Indicates if the focusRectSkin should always be displayed when the component is focused, or only after keyboard focus changes.

Available since

1.3.0

.

@:bindable("creationComplete")read onlycreated:Bool

Determines if the component has been initialized and validated for the first time.

In the following example, we check if the component is created or not, and we listen for an event if it isn't:

if(!control.created)
{
	control.addEventListener(FeathersEventType.CREATION_COMPLETE, creationCompleteHandler);
}
Available since

1.0.0

.

See also:

@styledisabledAlpha:Null<Float>

When disabledAlpha is not null, sets the alpha property to this value when the the enabled property is set to false.

Available since

1.0.0

.

@:inspectable(defaultValue = "true")enabled:Bool

@stylefocusPaddingBottom:Float

Optional padding outside the bottom edge of this UI component when the focusRectSkin is visible.

Available since

1.0.0

.

@stylefocusPaddingLeft:Float

Optional padding outside the left edge of this UI component when the focusRectSkin is visible.

Available since

1.0.0

.

@stylefocusPaddingRight:Float

Optional padding outside the right edge of this UI component when the focusRectSkin is visible.

Available since

1.0.0

.

@stylefocusPaddingTop:Float

Optional padding outside the top edge of this UI component when the focusRectSkin is visible.

Available since

1.0.0

.

@stylefocusRectSkin:DisplayObject

An optional skin to display when an IFocusObject component receives focus.

Available since

1.0.0

.

@:bindable("layoutDataChange")includeInLayout:Bool

@:bindable("initialize")read onlyinitialized:Bool

Determines if the component has been initialized yet. The initialize() function is called one time only, when the Feathers UI control is added to the display list for the first time.

In the following example, we check if the component is initialized or not, and we listen for an event if it isn't initialized:

if(!control.initialized)
{
	control.addEventListener(FeathersEvent.INITIALIZE, initializeHandler);
}
Available since

1.0.0

.

See also:

@style@:bindable("layoutDataChange")layoutData:ILayoutData

read onlystyleContext:Class<IStyleObject>

The class used as the context for styling the component. If a subclass of a component should have different styles than its superclass, it should override the get_styleContext getter. However, if a subclass should continue using the same styles as its superclass, it happens automatically.

Available since

1.0.0

.

styleProvider:IStyleProvider

Typically used by the theme to provide styles to each component, but a custom style provider may be provided that will take precedence over the theme's style provider.

When a component initializes, its style provider sets properties that affect the component's visual appearance. If the style provider dispatches StyleProviderEvent.STYLES_CHANGE after the component has initialized, the original properties set by the style provider will be reset to their default values and before applying the new property values.

Setting the style provider or replacing an existing style provider before a component initializes will queue up the style changes until after initialization. Once a component initializes, the style provider may be changed, but the changes will be applied immediately. Similarly to when a style provider dispatcches StyleProviderEvent.STYLES_CHANGE, any properties that were set by the previous style provider will be reset to their default values before applying the new style provider.

If the themeEnabled property is false, the current theme's style provider will be ignored. However, if a custom style provider was provided from outside of the theme, it will still be used.

Available since

1.0.0

.

See also:

@:inspectablevariant:String

May be used to provide multiple different variations of the same UI component, each with a different appearance.

Available since

1.0.0

.

Defined by MeasureSprite

Defined by ValidatingSprite

read onlyvalidating:Bool

Indicates if the display object is currently validating.

Available since

1.0.0

.

Inherited Methods

Defined by FeathersControl

private@:dox(show)initialize():Void

Called the first time that the UI control is added to the stage, and you should override this function to customize the initialization process. Do things like create children and set up event listeners. After this function is called, `FeathersEvent.INITIALIZE is dispatched.

The following example overrides initialization:

override private function initialize():Void {
	super.initialize();

}
Available since

1.0.0

.

See also:

move(x:Float, y:Float):Void

Sets both the x and y positions of the control in a single function call.

Available since

1.0.0

.

See also:

  • DisplayObject.x

  • DisplayObject.y

setFocusPadding(value:Float):Void

setSize(width:Float, height:Float):Void

Sets both the width and height dimensions of the control in a single function call.

Available since

1.0.0

.

See also:

  • DisplayObject.width

  • DisplayObject.height

private@:dox(show)setStyle(styleName:String, ?state:EnumValue):Bool

Determines if a style may be changed, and restricts the style from being changed in the future, if necessary.

Available since

1.0.0

.

Defined by MeasureSprite

private@:value({ minHeight : 0.0, minWidth : 0.0 })@:dox(show)saveMeasurements(width:Float, height:Float, minWidth:Float = 0.0, minHeight:Float = 0.0, ?maxWidth:Float, ?maxHeight:Float):Bool

Saves the calculated dimensions for the component, replacing any values that haven't been set explicitly. Returns true if the reported values have changed and Event.RESIZE was dispatched.

Available since

1.0.0

.

Defined by ValidatingSprite

isInvalid(?flag:InvalidationFlag):Bool

Indicates whether the control is pending validation or not. By default, returns true if any invalidation flag has been set. If you pass in a specific flag, returns true only if that flag has been set (others may be set too, but it checks the specific flag only. If all flags have been marked as invalid, always returns true.

The following example invalidates a component:

component.setInvalid();
trace(component.isInvalid()); // true
Available since

1.0.0

.

runWithInvalidationFlagsOnly(callback:() ‑> Void):Void

Calls a function that temporarily limits setInvalid() calls to setting invalidation flags only, and the control will not be added to the validation queue. In other words, setInvalid() calls will work similarly to setInvalidationFlag() instead.

Typically, this method should be called only during validation. If called outside of update(), the component's validation may be delayed until a future call to setInvalid().

Available since

1.2.0

.

runWithoutInvalidation(callback:() ‑> Void):Void

Calls a function that temporarily disables invalidation. In other words, calls to setInvalid() will be ignored until the function returns.

Available since

1.0.0

.

setInvalid(?flag:InvalidationFlag):Void

Call this function to tell the UI control that a redraw is pending. The redraw will happen immediately before OpenFL renders the UI control to the screen. The validation system exists to ensure that multiple properties can be set together without redrawing multiple times in between each property change.

If you cannot wait until later for the validation to happen, you can call validate() to redraw immediately. As an example, you might want to validate immediately if you need to access the correct width or height values of the UI control, since these values are calculated during validation.

The following example invalidates a component:

component.setInvalid();
trace(component.isInvalid()); // true
Available since

1.0.0

.

private@:dox(show)setInvalidationFlag(flag:InvalidationFlag):Void

Sets an invalidation flag. This will not add the component to the validation queue. It only sets the flag. A subclass might use this function during draw() to manipulate the flags that its superclass sees.

Available since

1.0.0

.

See also:

private@:dox(show)update():Void

Override to customize layout and to adjust properties of children. Called when the component validates, if any flags have been marked to indicate that validation is pending.

The following example overrides updating after invalidation:

override private function update():Void {
	super.update();

}
Available since

1.0.0

.