Flex Snippet Series - States Part One

Skill

Flex Snippet Series - States Part One

Posted in:

One main feature in Flex that has many many uses is States. This tutorial is the first in a multi-part series on how to use States and what they are. States are defined as a collection of changes to a view. With the changes you can do multiple things including adding and removing child components, change component properties, and add effects. This first tutorial will give a quick rundown of changing the view by adding components depending on the state.

Below we have a small application that shows off the minimal basics of managing state in Flex. When the application starts we see a green textbox, this represents the first state, when the button is pressed we see our view change. The second state has an orange textbox. Basically what happens is that when the button is pressed the application is told to change to its second state. As we go through the code all this will become clearer.

Get Adobe Flash player

Well with that let's dive right into the code. Below you will see the entire code for this tutorial. Everything is standard up until we get to the point where <mx:states> is defined. At this point we start defining the states property of the application. Inside the states section is where we define what states the application has and what happens when the app is in that state.

So the first state in this application is "stateUno" which is defined using the obvious <mx:State> tag and the name property is used to give is a name. Inside the state we use a tag which you may not have seen before, <AddChild>, which tells Flex that when the application is in this state a child needs to be added. In this case we also give it the main panel name for the relativeTo property and a position of lastChild. That will tell Flex to add whatever is inside this AddChild tag to the last child of the main panel.

The same procedure is done for the second state, one important thing to know is that every child added from a state is removed when the state changes again. And on to the code.

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute"
   width="300" height="300" currentState="stateUno"
   viewSourceURL="/files/StatesTutorialOne.mxml">
  <mx:Panel id="pnlMain" x="0" y="0" width="300" height="300" layout="absolute"
     title="Basic States Tutorial">
    <mx:Button x="99" y="228" label="State Dos" click="{currentState = 'stateDos'}"/>
  </mx:Panel>
  <mx:states>
    <mx:State name="stateUno">
      <mx:AddChild relativeTo="{pnlMain}" position="lastChild">
        <mx:TextArea x="10" y="10" width="260" height="210" text="My First State"
           fontSize="20" textAlign="center" backgroundColor="#00ff40"/>
      </mx:AddChild>
    </mx:State>
    <mx:State name="stateDos">
      <mx:AddChild relativeTo="{pnlMain}" position="lastChild">
        <mx:TextArea x="10" y="10" width="260" height="210" text="My Second State"
           fontSize="20" textAlign="center" backgroundColor="#ff8000"/>
      </mx:AddChild>
    </mx:State>
  </mx:states>
</mx:Application>

Now, I have explained defining the states and using them to add components, but there are still two things you need to do to make sure this is all working nicely - the first is setting the initial state. Some of you might have noticed a property set on the application named currentState, which is where we define the starting state. We also use this property to change the state of the application. We do this with the click event of the button we have on our panel. As you can see in the click event we change the state from "stateUno" to "stateDos". We then have a way to change to the second state but in this example there isn't a way to change back to the first state. It could be done in a very similar fashion though.

That takes care of the basics of managing and using states in Flex. The next couple tutorials in this series will go much more into depth of what can be done visually with states and what can be changed using them. Further tutorials in the series will go into how to change the state using different methods. If you have any questions drop a comment.

Amar Shukla
06/09/2008 - 05:07

Like always it was also a gud tute... :)

reply

Mike
11/18/2008 - 13:42

Nice tutorial -but since it was named "Part One" -where to find the second part?

reply

Anonymous
02/17/2010 - 14:54

Jumping through all of the hide and show component discussions, using states is the absolute perfect answer for what I needed to do. Thank you for posting.

reply

Anonymous
03/09/2010 - 06:49

good job

reply

Anonymous
03/09/2010 - 06:51

View states let you vary the content and appearance of a component or application, typically in response to a user action. For example, the base, or default, view state of the application could be the home page and include a logo, a sidebar, and some welcome content. When the user clicks a button in the sidebar, the application dynamically changes its appearance, meaning its view state, by replacing the main content area with a purchase order form but leaving the logo and sidebar in place.

reply

Add Comment

Put code snippets inside language tags:
[language] [/language]

Examples:
[javascript] [/javascript]
[actionscript] [/actionscript]
[csharp] [/csharp]

See here for supported languages.

Javascript must be enabled to submit anonymous comments - or you can login.
CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.