Creating a linear gradient background fill on a Spark NumericStepper control in Flex 4

The following example shows how you can set a linear gradient background fill on a Spark NumericStepper control in Flex 4 by creating a LinearGradient object and setting the skinClass style.

The following example(s) require Flash Player 10 and the Adobe Flex 4 SDK. To download the Adobe Flash Builder 4 trial, see www.adobe.com/products/flex/. To download the latest nightly build of the Flex 4 SDK, see opensource.adobe.com/wiki/display/flexsdk/Download+Flex+4.

For more information on getting started with Flex 4 and Flash Builder 4, see the official Adobe Flex Team blog.

<?xml version="1.0" encoding="utf-8"?>
<!-- http://blog.flexexamples.com/2009/04/27/creating-a-linear-gradient-background-fill-on-a-spark-numericstepper-control-in-flex-gumbo/ -->
<s:Application name="Spark_NumericStepper_skinClass_test"
        xmlns:fx="http://ns.adobe.com/mxml/2009"
        xmlns:s="library://ns.adobe.com/flex/spark"
        xmlns:mx="library://ns.adobe.com/flex/mx">

    <s:NumericStepper id="numericStepper"
            skinClass="skins.CustomNumericStepperSkin"
            horizontalCenter="0" verticalCenter="0" />

</s:Application>

The custom Spark NumericStepper skin class, skins/CustomNumericStepperSkin.mxml, is as follows:

<?xml version="1.0" encoding="utf-8"?>
<!-- http://blog.flexexamples.com/2009/04/27/creating-a-linear-gradient-background-fill-on-a-spark-numericstepper-control-in-flex-gumbo/ -->
<s:SparkSkin name="CustomNumericStepperSkin"
        xmlns:fx="http://ns.adobe.com/mxml/2009"
        xmlns:s="library://ns.adobe.com/flex/spark"
        minHeight="24"
        alpha.disabled="0.5">
    <s:states>
        <s:State name="normal" />
        <s:State name="disabled" />
    </s:states>

    <fx:Metadata>
        <![CDATA[
            [HostComponent("spark.components.NumericStepper")]
        ]]>
    </fx:Metadata>

    <fx:Script>
        /* Define the skin elements that should not be colorized.
           For numeric stepper, the skin itself is colorized but the individual parts are not. */
        static private const exclusions:Array = ["textInput", "decrementButton", "incrementButton"];

        override public function get colorizeExclusions():Array {return exclusions;}
    </fx:Script>

    <s:Button id="incrementButton" right="0" top="0" height="50%"
              skinClass="spark.skins.default.SpinnerIncrButtonSkin" />
    <s:Button id="decrementButton" right="0" bottom="0" height="50%"
              skinClass="spark.skins.default.SpinnerDecrButtonSkin" />

    <s:TextInput id="textInput" left="0" top="0" right="18" bottom="0"
            skinClass="skins.CustomNumericStepperTextInputSkin" />

</s:SparkSkin>

And the custom Spark NumericStepper TextInput skin class, skins/CustomNumericStepperTextInputSkin.mxml, is as follows:

<?xml version="1.0" encoding="utf-8"?>
<!-- http://blog.flexexamples.com/2009/04/27/creating-a-linear-gradient-background-fill-on-a-spark-numericstepper-control-in-flex-gumbo/ -->
<s:SparkSkin xmlns:fx="http://ns.adobe.com/mxml/2009"
        xmlns:s="library://ns.adobe.com/flex/spark"
        alpha.disabled="0.5">
    <s:states>
        <s:State name="normal"/>
        <s:State name="disabled"/>
    </s:states>

    <fx:Metadata>
    <![CDATA[
        [HostComponent("spark.components.TextInput")]
    ]]>
    </fx:Metadata>

    <fx:Script>
        /* Define the skin elements that should not be colorized. */
        static private const exclusions:Array = ["background", "textView"];

        override public function get colorizeExclusions():Array {return exclusions;}

        /* Define the content fill items that should be colored by the "contentBackgroundColor" style. */
        static private const contentFill:Array = [];
        override public function get contentItems():Array {return contentFill};
    </fx:Script>

    <!-- border -->
    <s:Rect left="0" right="0" top="0" bottom="0">
        <s:stroke>
            <s:LinearGradientStroke rotation="90" weight="1">
                <s:GradientEntry color="0x000000" alpha="0.5525" />
                <s:GradientEntry color="0x000000" alpha="0.6375" />
            </s:LinearGradientStroke>
        </s:stroke>
    </s:Rect>

    <!-- fill -->
    <s:Rect id="background" left="1" right="1" top="1" bottom="1">
        <s:fill>
            <s:LinearGradient id="bgFill" rotation="90">
                <s:entries>
                    <s:GradientEntry color="haloBlue" />
                    <s:GradientEntry color="haloGreen" />
                </s:entries>
            </s:LinearGradient>
        </s:fill>
    </s:Rect>

    <!-- shadow -->
    <s:Rect left="1" top="1" right="1" height="1">
        <s:fill>
            <s:SolidColor color="0x000000" alpha="0.12" />
        </s:fill>
    </s:Rect>

    <!-- text -->
    <s:RichEditableText id="textView"
              left="1" right="1" top="1" bottom="1"
              paddingLeft="3" paddingTop="5"
              paddingRight="6" paddingBottom="3"/>

</s:SparkSkin>

[GoogleAdsWide]

View source is enabled in the following example.

You can also set the Spark NumericStepper control’s nested Spark TextInput skin using an external .CSS file or <Style/> block, as seen in the following example:

<?xml version="1.0" encoding="utf-8"?>
<!-- http://blog.flexexamples.com/2009/04/27/creating-a-linear-gradient-background-fill-on-a-spark-numericstepper-control-in-flex-gumbo/ -->
<s:Application name="Spark_NumericStepper_skinClass_test"
        xmlns:fx="http://ns.adobe.com/mxml/2009"
        xmlns:s="library://ns.adobe.com/flex/spark"
        xmlns:mx="library://ns.adobe.com/flex/mx">

    <fx:Script>
        <![CDATA[
            import skins.CustomNumericStepperTextInputSkin;

            private function init():void {
                numericStepper.textInput.setStyle("skinClass", CustomNumericStepperTextInputSkin);
            }
        ]]>
    </fx:Script>

    <s:NumericStepper id="numericStepper"
            horizontalCenter="0" verticalCenter="0"
            initialize="init();" />

</s:Application>

This entry is based on a beta version of the Flex 4 SDK and therefore is very likely to change as development of the Flex SDK continues. The API can (and will) change causing examples to possibly not compile in newer versions of the Flex 4 SDK.