Scrollable widget creates a scrollable area that can display content larger than the available space. It provides both vertical and horizontal scrolling with customizable scrollbars.
Basic Usage
Builder Methods
new(content: impl Into<Element>)
Creates a new vertical scrollable with the given content.
with_direction(content: impl Into<Element>, direction: Direction)
Creates a scrollable with specified direction.
horizontal()
Makes the scrollable scroll horizontally.
direction(direction: Direction)
Sets the scroll direction.
id(id: widget::Id)
Sets the widget ID for programmatic control.
width(width: impl Into<Length>)
Sets the width.
height(height: impl Into<Length>)
Sets the height.
on_scroll(callback: impl Fn(Viewport) -> Message)
Sets a callback for scroll events.
style(style_fn: impl Fn(&Theme, Status) -> Style)
Applies custom styling.
Scroll Direction
TheDirection enum controls scroll behavior:
Scrollbar Configuration
Customize scrollbar appearance:Programmatic Scrolling
Control scroll position using commands:Viewport Information
TheViewport type provides scroll position information:
Examples
Long List
Horizontal Gallery
Chat Window
Content with Header
Bidirectional Scroll
Infinite Scroll
Tips and Best Practices
- Set explicit height - Scrollables need a constrained height to work
- Use
Length::Fill- Let scrollable take available space - ID for programmatic control - Set an ID if you need to scroll programmatically
- Optimize content - Avoid rendering too many items at once
- Smooth scrolling - Use
on_scrollsparingly for performance - Consider direction - Choose appropriate scroll direction for your content
Related Widgets
- Column - Often used as scrollable content
- Container - For content styling
- List widgets - For structured data display
