Flex Snippet Tutorial - View Stack Component

Skill

Flex Snippet Tutorial - View Stack Component

Posted in:

The Flex framework has a few different components to modify the layout on the page. This small tutorial is going to go over one of those options, the ViewStack component. This component is a fairly simple component that lets the programmer have multiple ui layers on one panel, but flip between which one is visible cleanly and easily. Basically Flex stacks multiple containers on top of each other and when one of them is selected the others are invisible. It should be noted that when one container is selected the others still exist - they are just not visible.

Also the ViewStack component does not offer any visual navigation for the user. It is the job of the programmer to provide this navigation. So lets look at the working example below. This is a very simple way to use a viewStack. We initially show the user a login page, but if they don't have a username they can click the "Need to Register?" LinkButton. This switches the view to the register page. And on the register page if they already have a username they can click the LinkButton "Already Registered?" to switch the view back.

Get Adobe Flash player

Now lets see how easy it is to actually do this type of swap.

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute"
   width="285" height="221">
  <mx:Panel x="0" y="0" width="285" height="221"
     layout="absolute" title="ViewStack Basic ">
    <mx:ViewStack x="0" y="0" id="vsMain" width="100%" height="100%">
      <mx:Canvas id="cnvLogin" label="Login" width="100%" height="100%">
        <mx:Label x="10" y="10" text="Login Page" fontWeight="bold"/>
        <mx:Label x="10" y="36" text="Username:"/>
        <mx:TextInput x="85" y="34" id="txtLoginUser"/>
        <mx:Label x="10" y="62" text="Password:"/>
        <mx:TextInput x="85" y="60" id="txtLoginPass"/>
        <mx:Button x="10" y="100" label="Login" id="butLogin"/>
        <mx:LinkButton x="126" y="100" label="Need to Register?" enabled="true"
           click="{vsMain.selectedChild=cnvRegister}"/>
      </mx:Canvas>
      <mx:Canvas id="cnvRegister" label="Register" width="100%" height="100%">
        <mx:Label x="10" y="10" text="Register Page" fontWeight="bold"/>
        <mx:Label x="10" y="36" text="Username:"/>
        <mx:TextInput x="107" y="34" id="txtRegUser" width="138"/>
        <mx:Label x="10" y="62" text="Password:"/>
        <mx:TextInput x="107" y="60" id="txtRegPass" width="138"/>
        <mx:Label x="10" y="90" text="Password Again:"/>
        <mx:TextInput x="107" y="88" id="txtRegPassAgain" width="138"/>
        <mx:Button x="10" y="141" label="Register" id="butRegister"/>
        <mx:LinkButton x="107" y="141" label="Already Registered?" enabled="true"
           click="{vsMain.selectedChild=cnvLogin}"/>
      </mx:Canvas>
    </mx:ViewStack>
  </mx:Panel>
</mx:Application>

So you will notice that we have an application and a panel, just like almost any normal Flex application. Then we add the <mx:ViewStack> tag and we specify an id, size, and initial position. Now that we have this tag we can start adding client containers inside the ViewStack. Each client container we have (in this case cnvLogin and cnvRegister) we can treat like a normal container and add UI components to it.

The only other major thing we need to do in this simple example is provide the navigation, which we do by using a couple LinkButtons. You can see that in both the cnvRegister and cnvLogin canvases we have a LinkButton, and on the On the click event for these buttons, we have the code to swap the view. All this code consists of is setting the vsMain.selectedChild to the child container that we now want to show.

The second example in this tutorial shows off using a navigation component supplied in flex to do the navigation. As you can see in the example below we have three different links at the top of the panel. Each one of these links to a different UI view in our ViewStack. Instead of having to do any fancy actionscript calls in click events on our LinkButtons we can use the dataProvider of the LinkBar. This technique works for the LinkBar, TabBar and ToggleButtonBar control components.

Get Adobe Flash player

As we look at the code below we will notice a similar setup to the first example. This time, though, we have added a <mx:LinkBar> component to our panel, which is our navigation control.

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute"
   width="397" height="365">
  <mx:Panel x="0" y="0" width="397" height="365"
     layout="absolute" title="ViewStack LinkBar Example">
    <mx:LinkBar dataProvider="{vsMain}" fontWeight="Bold" x="51" y="3"/>
    <mx:ViewStack horizontalCenter="0" y="30" id="vsMain"
       width="95%" height="87%" borderStyle="solid">
      <mx:Canvas label="Search Items" width="100%" height="100%"
         backgroundColor="#12AE63">
        <mx:Label horizontalCenter="0" verticalCenter="0" text="Search Items"
           fontSize="17"/>
      </mx:Canvas>
      <mx:Canvas label="Browse Items" width="100%" height="100%"
         backgroundColor="#F22131">
        <mx:Label horizontalCenter="0" verticalCenter="0" text="Browse Items"
           fontSize="17"/>
      </mx:Canvas>
      <mx:Canvas label="Add Items" width="100%" height="100%"
         backgroundColor="#1231FF">
        <mx:Label horizontalCenter="0" verticalCenter="0" text="Add Items"
           fontSize="17"/>
      </mx:Canvas>
    </mx:ViewStack>
  </mx:Panel>
</mx:Application>

Here, the link bar automatically lists the available child canvases in the ViewStack because we have set the dataProvider property of the link bar to the ViewStack. Because Flex understands how to handle this binding it will show links to all our main child containers of our ViewStack and handle changing the UI view on the stack appropriately when one is clicked. The text of each of the links is taken from the label field of the appropriate child canvas. If you would like to know more about how to use the link bar, the Adobe Flex documentation is a good place to look.

Hope you enjoyed this quick look at the ViewStack and LinkBar components. If you have any questions/concerns please just drop us a comment.

vika_1987
08/18/2007 - 18:40

Hello, very nice, good Luck!

reply

Training Guy
11/09/2007 - 16:32

Very cool, how can you make the panel expand to the size of the content? Is there an autosize height, width?
Thanks

reply

Marcus
12/17/2008 - 21:16

How many viewstack is allowed in a flex application?

reply

Anonymous
11/24/2009 - 12:42

how to put 3components displayed in fixed local by switching with visibility=true each from one comboboxl..
by default all there lets say grids are visible,i want to select one gride1 from a combobox then datagride1 should disply,next if i select second grid in combobox,then second datagrid2 must be displyed in same locaiton exactly..how can i accomplish this....am new to flex...any one can help me

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.

Sponsors