Toggling a Flex container’s visibility using states
In a previous post, “Toggling a Flex container’s visibility”, we looked at toggling a VBox container’s visibility by setting both the visible property and includeInLayout property. While the approach felt a little crude, we can build the same thing using the much more powerful view states. What are view states and how do we use them? Well, if you’re new to states, this should clear everything up: “Adobe – Flex Quick Start Basics: Creating States”.
Now, with that out of the way, lets look at some code and a basic example.
Full code after the jump.
<?xml version="1.0" encoding="utf-8"?>
<!-- http://blog.flexexamples.com/2007/08/24/toggling-a-flex-containers-visibility-using-states/ -->
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
layout="vertical"
verticalAlign="middle"
backgroundColor="white">
<mx:transitions>
<mx:Transition fromState="*" toState="*">
<mx:Parallel targets="{[vbox2]}">
<mx:WipeDown />
<mx:Fade />
</mx:Parallel>
</mx:Transition>
</mx:transitions>
<mx:states>
<mx:State name="expanded">
<mx:AddChild relativeTo="{vbox1}" position="after">
<mx:VBox id="vbox2"
width="120"
height="100%"
backgroundColor="haloGreen">
<mx:Label text="VBox 2" />
</mx:VBox>
</mx:AddChild>
</mx:State>
</mx:states>
<mx:Style>
VBox {
paddingLeft: 10;
paddingRight: 10;
paddingTop: 10;
paddingBottom: 10;
}
</mx:Style>
<mx:Script>
<![CDATA[
import mx.effects.easing.*;
[Bindable]
[Embed(source="assets/help.png")]
private var helpIcon:Class;
private function toggleExpanded():void {
switch (currentState) {
case "expanded":
currentState = "";
break;
default:
currentState = "expanded";
break;
}
}
]]>
</mx:Script>
<mx:HBox width="100%" height="100%">
<mx:VBox id="vbox1"
width="120"
height="100%"
backgroundColor="haloOrange">
<mx:Label text="VBox 1" />
<mx:Button label="Help"
icon="{helpIcon}"
click="toggleExpanded()" />
</mx:VBox>
<mx:VBox id="vbox3"
width="100%"
height="100%"
backgroundColor="haloBlue">
<mx:Label text="VBox 3" />
</mx:VBox>
</mx:HBox>
</mx:Application>
View source is enabled in the following example.
Peter deHaan
Peter deHaan currently works for Adobe on the Flex SDK QA team. While not working on Flex, Flash, and ColdFusion applications, Peter enjoys making up bios and writing in 3rd person. Peter's rarely updated blog can be found at blogs.adobe.com/pdehaan/, actionscriptexamples.com, airexamples.com, and coldfusionexamples.com.
-
Add Widgets (Content Sidebar)
This is your Content Sidebar. Edit this content that appears here in the widgets panel by adding or removing widgets in the Content Sidebar area.
11 Responses to Toggling a Flex container’s visibility using states
Leave a Reply Cancel reply
-
Categories
- Accordion
- AccordionHeader
- ActionScript
- AddChild
- AdvancedDataGrid
- Alert
- alpha
- Animate
- AnimateProperties
- Application
- Application (Spark)
- ArrayCollection
- BarChart
- baseColor
- beta
- beta1
- beta2
- Bitmap
- Bitmap/BitmapData
- BitmapData
- BitmapFill
- BitmapFill (Spark)
- BitmapGraphic
- BitmapImage
- BitmapImage (Spark)
- BitmapImageResizeMode
- Border (Spark)
- BorderContainer (Spark)
- Box
- BuildInfo
- Button
- Button (Spark)
- ButtonBar
- ButtonBar (Spark)
- ByteArray
- Camera
- Charting
- CheckBox
- CheckBox (Spark)
- ClassFactory
- CollectionEvent
- Color
- ColorPicker
- ColorUtil
- ComboBox
- ComboBoxArrowSkin
- Compiler
- Component
- Component (Spark)
- Configuration
- Container
- ContextMenu
- ContextMenuEvent
- ContextMenuItem
- CSSCondition
- CSSSelector
- CSSStyleDeclaration
- CurrencyFormatter
- CursorManager
- Data Binding
- DataGrid
- DataGrid (Spark)
- DataGridColumn
- Date
- DateBase
- DateChooser
- DateField
- DateFormatter
- Debugging
- DefaultComplexItemRenderer
- DefaultTileListEffect
- DropDownList
- DropDownList (Spark)
- DropDownListButtonSkin
- DropDownListSkin
- DropShadowFilter
- E4X
- Effects
- Ellipse
- EmailValidator
- Embed
- Event
- Fade
- FileFilter
- FileReference
- fill
- Filters
- Flash
- Flash Integration
- FlashVars
- Flex 3 SDK
- Flex Builder
- Flex Builder 3
- Flex SDK
- Flex4
- FLVPlayback
- FocusManager
- FontLookup
- Fonts
- Form
- Form (Spark)
- FormHeading (Spark)
- FormItem
- FormItem (Spark)
- Forms
- FTETextField (Spark)
- FullScreen
- FullScreenEvent
- FxAnimateColor
- FxButtonBar
- FxCheckBox
- FXG
- FxHScrollBar
- FxHSlider
- FxList
- FxNumericStepper
- FxRadioButton
- FxRotate3D
- FxScroller
- FxTextArea
- FxTextInput
- FxToggleButton
- FxVScrollBar
- FxVSlider
- getStyleDeclaration()
- GradientEntry
- Graphic (Spark)
- HBox
- HDividedBox
- HGroup (Spark)
- HorizontalLayout
- HorizontalList
- HSBColor (Spark)
- HScrollBar (Spark)
- HSlider
- HSlider (Spark)
- HTML template
- ID3Info
- Image
- Image (Spark)
- ImageSnapshot
- itemRenderer
- JointStyle
- Label
- Label (Spark)
- Legend
- LegendItem
- LigatureLevel
- Line
- LinearGradientStroke
- LineScaleMode
- LinkBar
- LinkButton
- List
- List (Spark)
- Menu
- MenuBar
- Metadata
- MetadataEvent
- Model
- Mouse
- MouseCursor
- MouseEvent
- Move
- Namespace
- NavigatorContent (Spark)
- needsSWF
- NetConnection
- NetStream
- Nightly Builds
- NumberBaseRoundType
- NumberFormatter
- NumberValidator
- NumericCompare
- NumericStepper
- NumericStepper (Spark)
- ObjectProxy
- ObjectUtil
- paddingLeft
- paddingRight
- Panel
- Panel (Spark)
- Parallel
- Path
- PieChart
- PieSeries
- PieSeriesItem
- PopUpAnchor (Spark)
- PopUpButton
- PopUpManager
- ProgrammaticSkin
- ProgressBar
- PropertyChangeEvent
- QName
- RadialGradient
- RadioButton
- RadioButton (Spark)
- RadioButtonGroup
- RadioButtonGroup (Spark)
- Rect
- RegExp
- Regular Expressions
- Repeater
- RichEditableText
- RichText
- RichText (Spark)
- RichTextEditor
- Rotate
- Rotate3D (Spark)
- Scroller (Spark)
- Sequence
- setStyle()
- SimpleText
- SimpleText (Spark)
- skinClass
- Slider
- SliderEvent
- SolidColor
- SolidColorStroke
- Sort
- SortField
- Sound
- SoundEffect
- Spinner (Spark)
- SpriteVisualElement (Spark)
- StageDisplayState
- States
- StringUtil
- StringValidator
- StyleManager
- Styles
- SWFLoader
- SWFObject
- System
- SystemManager
- TabBar
- TabBar (Spark)
- TabNavigator
- TabStopFormat
- Text
- Text Layout Framework (TLF)
- TextArea
- TextArea (Spark)
- TextBox
- TextConverter
- TextEvent
- TextFlow
- TextFlowUtil
- TextFormat
- TextGraphic
- TextInput
- TextInput (Spark)
- TextLayoutFormat
- TextView
- Themes
- TileLayout
- TileList
- TileOrientation
- Timer
- TitleWindow
- TitleWindow (Spark)
- TLF
- ToggleButton (Spark)
- ToggleButtonBar
- ToolTip
- Transition
- Tree
- TruncationOptions
- UIComponent
- UIFTETextField
- Updater
- URLLoader
- URLRequest
- URLUtil
- URLVariables
- ValidationResultEvent
- Validator
- Validators
- VBox
- VDividedBox
- Vector
- VerticalLayout
- VerticalLayout (Spark)
- VGroup (Spark)
- Video
- VideoDisplay
- VideoElement
- VideoElement (Spark)
- VideoEvent
- VideoPlayer (Spark)
- VideoPlayerScrubBar
- ViewStack
- VScrollBar (Spark)
- VSlider
- VSlider (Spark)
- XML
- XMLList
- XMLListCollection
- ZipCodeValidator
- ZipCodeValidatorDomainType
- Zoom
-
Articles
- December 2010
- November 2010
- October 2010
- September 2010
- August 2010
- July 2010
- June 2010
- May 2010
- April 2010
- March 2010
- February 2010
- January 2010
- December 2009
- November 2009
- October 2009
- September 2009
- August 2009
- July 2009
- June 2009
- May 2009
- April 2009
- March 2009
- February 2009
- January 2009
- December 2008
- November 2008
- October 2008
- September 2008
- August 2008
- July 2008
- June 2008
- May 2008
- April 2008
- March 2008
- February 2008
- January 2008
- December 2007
- November 2007
- October 2007
- September 2007
- August 2007
- July 2007
-
Meta


Hey Peter, I just wanted to give you some kudos. This is one of the most high quality, fresh, and content rich Flex blogs out there! Actually I’d give it the #1 rating.
Thanks!
Me, too ;-)
This is great for starters, we have added your blog.
This should be the most usefull blog for flex developers after adobe’s flex website.You were added to favourites. Thanks and waiting for more :)
Exactly that i’m looking for. Thank for this example. Is it possible in a new example to add a VBox 4 witch appear between the VBox 2 and VBox 3 when you click on a new button put in the VBox 2. Thanks
Started working with Flex. got stuck in the beginning itself..
What I want is When you click on “Application Admin” Tab I want to show the “_tool_admin_que” State. And when I am in “_tool_admin_que” ( that is “Application Admin” tab) state and click on “Login” tab, I want to show “_login” state.
How do I do it? Any help please? click event does not work on canvas. want to make the header work like a URL link.
Great article, helped me a lot. I am stuck with a very odd problem though:
Inside a component, I got a VBox that removes all its childs when you click (anwhere) on the VBox and adds some other childs. It basically removes some text and adds a button. When you click on that button which is located inside the state, it should switch back to the base state, but that does not work. It fires the event, but it does not jump into the base state via
currentState=''. When the button is outside of the state, it works.Any help? Drives me mad… here is an extract of the code:
<mx:State name="changeContent"> <mx:RemoveChild target="{child1}"/> <mx:RemoveChild target="{child2}"/> ... <mx:AddChild relativeTo="{myVBox}"> <mx:Button id="changeAgain" label="close" click="currentState='';" /> ... </mx:AddChild> <mx:SetEventHandler target="{myVBox}" name="click" handler="{null}" /> <mx:SetEventHandler target="{changeAgain}" name="click" handler="currentState='';" /> </mxState> ... ... <mx:VBox id="myVBox" click="currentState='changeContent'" > ...</VBox>use the tag initially mentioning as .After that continue with ur original program as u have proceeded earlier.its work definitely.k
@Jules:
Have you tried using currentState=null? I know that’s supposed to be the Base State.
good aricle . Here i found another good article explaining use of states for creating a website using states in flex .
here is link :
http://askmeflash.com/tutorial_m.php?p=tutorial&tid=0
Very nice blog about view state.
I have also gone through following links which are also helpful in case of View State
http://jksnu.blogspot.com/2011/06/view-state-difference-between-flex3-and.html
http://jksnu.blogspot.com/2011/06/view-state-difference-between-flex3-and_24.html