Opacity Widget
The Opacity widget makes its child partially or fully transparent. It’s useful for creating visual effects, transitions, and indicating widget states.
Properties
opacity
: number between 0.0 (fully transparent) and 1.0 (fully opaque)child
: The widget to apply opacity to
Common Use Cases
-
Disabled State:
Opacity({ opacity: isEnabled ? 1.0 : 0.5, child: Button() })
-
Fade Effects:
Opacity({ opacity: isVisible ? 1.0 : 0.0, child: Content() })
-
Visual Hierarchy:
Column({ children: [ Opacity({ opacity: 1.0, child: PrimaryContent() }), Opacity({ opacity: 0.7, child: SecondaryContent() }) ] })
Best Practices
-
Performance:
- Use opacity sparingly
- Consider using alpha color values for static transparency
- Be aware of compositing costs
-
Accessibility:
- Ensure sufficient contrast
- Consider disabled states
- Hide non-interactive transparent elements
-
Animation:
- Use smooth transitions
- Consider performance impact
- Test on different devices
Common Patterns
-
Loading States:
Stack({ children: [ Content(), Opacity({ opacity: isLoading ? 0.5 : 0.0, child: LoadingOverlay() }) ] })
-
Modal Overlays:
Stack({ children: [ MainContent(), Opacity({ opacity: 0.5, child: ModalBackground() }), ModalContent() ] })
-
Fade Transitions:
Opacity({ opacity: isVisible ? 1.0 : 0.0, child: TransitioningContent() })
Tips and Tricks
-
Visual Effects:
- Layer multiple opacities
- Combine with other effects
- Consider background contrast
-
Interactive Elements:
- Maintain visibility of important UI
- Use appropriate opacity levels
- Consider hover states
-
Debugging:
- Test different opacity values
- Verify touch/click areas
- Check contrast ratios