Creating a linear gradient roll over background on a Spark List in Flex 4

The following example shows how you can create a gradient roll over color on a Spark List control in Flex 4 by creating a custom item renderer setting the itemRenderer property.

Full code after the jump.

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/24/creating-a-linear-gradient-roll-over-background-on-a-spark-list-in-flex-gumbo/ -->
<s:Application name="Spark_List_itemRenderer_rollOverColor_LinearGradient_test"
        xmlns:fx="http://ns.adobe.com/mxml/2009"
        xmlns:s="library://ns.adobe.com/flex/spark"
        xmlns:mx="library://ns.adobe.com/flex/halo">
 
    <mx:ApplicationControlBar width="100%" cornerRadius="0">
        <mx:Form styleName="plain">
            <mx:FormItem label="rollOverColor:">
                <mx:ColorPicker id="colorPicker1" selectedColor="red" />
            </mx:FormItem>
            <mx:FormItem label="contentBackgroundColor:">
                <mx:ColorPicker id="colorPicker2" selectedColor="purple" />
            </mx:FormItem>
        </mx:Form>
    </mx:ApplicationControlBar>
 
    <s:List id="list"
            itemRenderer="skins.CustomDefaultItemRenderer"
            rollOverColor="{colorPicker1.selectedColor}"
            contentBackgroundColor="{colorPicker2.selectedColor}"
            horizontalCenter="0"
            verticalCenter="0">
        <s:dataProvider>
            <s:ArrayList source="[The,Quick,Brown,Fox,Jumps,Over,The,Lazy,Dog]" />
        </s:dataProvider>
    </s:List>
 
</s:Application>

The custom item renderer, skins/CustomDefaultItemRenderer.mxml, is as follows:

<?xml version="1.0" encoding="utf-8"?>
<!-- http://blog.flexexamples.com/2009/04/24/creating-a-linear-gradient-roll-over-background-on-a-spark-list-in-flex-gumbo/ -->
<s:ItemRenderer name="CustomDefaultItemRenderer"
        xmlns:fx="http://ns.adobe.com/mxml/2009"
        xmlns:s="library://ns.adobe.com/flex/spark"
        focusEnabled="false">
    <!-- states -->
    <s:states>
        <s:State name="normal" />
        <s:State name="hovered" />
        <s:State name="selected" />
        <s:State name="normalAndShowsCaret"/>
        <s:State name="hoveredAndShowsCaret"/>
        <s:State name="selectedAndShowsCaret"/>
    </s:states>
 
    <fx:Script>
        <![CDATA[
            override public function set label(value:String):void {
                super.label = value;
                labelDisplay.text = label; 
            }
        ]]>
    </fx:Script>
 
    <s:Rect left="0" right="0" top="0" bottom="0">
        <s:stroke.normalAndShowsCaret>
            <s:SolidColorStroke color="{selectionColor}"
                    weight="1"/>
        </s:stroke.normalAndShowsCaret>
        <s:stroke.hoveredAndShowsCaret>
            <s:SolidColorStroke color="{selectionColor}"
                    weight="1"/>
        </s:stroke.hoveredAndShowsCaret>
        <s:stroke.selectedAndShowsCaret>
            <s:SolidColorStroke color="{selectionColor}"
                    weight="1"/>
        </s:stroke.selectedAndShowsCaret>
        <s:fill>
            <s:SolidColor color.normal="{contentBackgroundColor}"
                    color.normalAndShowsCaret="{contentBackgroundColor}"
                    color.hoveredAndShowsCaret="{rollOverColor}"
                    color.selected="{selectionColor}"
                    color.selectedAndShowsCaret="{selectionColor}" />
        </s:fill>
        <s:fill.hovered>
            <s:LinearGradient>
                <s:GradientEntry color="{rollOverColor}" />
                <s:GradientEntry color="{contentBackgroundColor}" />
            </s:LinearGradient>
        </s:fill.hovered>
    </s:Rect>
 
    <s:SimpleText id="labelDisplay"
            verticalCenter="0"
            left="3" right="3" top="6" bottom="4" />
 
</s:ItemRenderer>

View source is enabled in the following example.

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.

3 thoughts on “Creating a linear gradient roll over background on a Spark List in Flex 4

  1. Thank you for your good information about flex4 :-)

    I’ve learned many things in here.

    Thank you so so so much.

    I have a question.

    There is not selectedColor in ItemRenederer.

    How can I change this attribute?

    :-)

Comments are closed.