Skip to main content

ScrollPosition

Trait ScrollPosition 

Source
pub trait ScrollPosition: ViewportOffset {
Show 53 methods // Required methods fn scroll_position_data( self: Handle<Self>, app: &App, ) -> &ScrollPositionData; fn scroll_position_data_mut( self: Handle<Self>, app: &mut App, ) -> &mut ScrollPositionData; fn axis_direction(self: Handle<Self>, app: &App) -> AxisDirection; fn pointer_scroll(self: Handle<Self>, app: &mut App, delta: f64); fn jump_to_without_settling(self: Handle<Self>, app: &mut App, value: f64); fn hold( self: Handle<Self>, app: &mut App, hold_cancel_callback: Listener, ) -> Rc<dyn ScrollHoldController>; fn drag( self: Handle<Self>, app: &mut App, details: DragStartDetails, drag_cancel_callback: Listener, ) -> Rc<dyn Drag>; // Provided methods fn physics(self: Handle<Self>, app: &App) -> ScrollPhysicsRef { ... } fn context(self: Handle<Self>, app: &App) -> Rc<dyn ScrollContext> { ... } fn keep_scroll_offset(self: Handle<Self>, app: &App) -> bool { ... } fn debug_label(self: Handle<Self>, app: &App) -> Option<&str> { ... } fn min_scroll_extent(self: Handle<Self>, app: &App) -> f64 { ... } fn max_scroll_extent(self: Handle<Self>, app: &App) -> f64 { ... } fn has_content_dimensions(self: Handle<Self>, app: &App) -> bool { ... } fn viewport_dimension(self: Handle<Self>, app: &App) -> f64 { ... } fn has_viewport_dimension(self: Handle<Self>, app: &App) -> bool { ... } fn axis(self: Handle<Self>, app: &App) -> Axis { ... } fn device_pixel_ratio(self: Handle<Self>, app: &App) -> f64 { ... } fn out_of_range(self: Handle<Self>, app: &App) -> bool { ... } fn at_edge(self: Handle<Self>, app: &App) -> bool { ... } fn extent_before(self: Handle<Self>, app: &App) -> f64 { ... } fn extent_inside(self: Handle<Self>, app: &App) -> f64 { ... } fn extent_after(self: Handle<Self>, app: &App) -> f64 { ... } fn extent_total(self: Handle<Self>, app: &App) -> f64 { ... } fn copy_with(self: Handle<Self>, app: &App) -> Rc<dyn ScrollMetrics> { ... } fn have_dimensions(self: Handle<Self>, app: &App) -> bool { ... } fn should_ignore_pointer(self: Handle<Self>, app: &App) -> bool { ... } fn absorb(self: Handle<Self>, app: &mut App, other: AnyScrollPosition) { ... } fn set_pixels(self: Handle<Self>, app: &mut App, new_pixels: f64) -> f64 { ... } fn correct_pixels(self: Handle<Self>, app: &mut App, value: f64) { ... } fn force_pixels(self: Handle<Self>, app: &mut App, value: f64) { ... } fn save_scroll_offset(self: Handle<Self>, app: &mut App) { ... } fn restore_scroll_offset(self: Handle<Self>, app: &mut App) { ... } fn restore_offset( self: Handle<Self>, app: &mut App, offset: f64, initial_restore: bool, ) { ... } fn save_offset(self: Handle<Self>, app: &mut App) { ... } fn apply_boundary_conditions( self: Handle<Self>, app: &mut App, value: f64, ) -> f64 { ... } fn correct_for_new_dimensions( self: Handle<Self>, app: &mut App, old_position: &dyn ScrollMetrics, new_position: &dyn ScrollMetrics, ) -> bool { ... } fn apply_new_dimensions(self: Handle<Self>, app: &mut App) { ... } fn ensure_visible( self: Handle<Self>, app: &mut App, object: AnyRenderObject, alignment: f64, duration: Duration, curve: Rc<dyn Curve>, alignment_policy: ScrollPositionAlignmentPolicy, target_render_object: Option<AnyRenderObject>, ) -> CompleterFuture<()> ⓘ { ... } fn is_scrolling_notifier( self: Handle<Self>, app: &App, ) -> Handle<ValueNotifier<bool>> { ... } fn activity(self: Handle<Self>, app: &App) -> Option<AnyScrollActivity> { ... } fn begin_activity( self: Handle<Self>, app: &mut App, new_activity: Option<ScrollActivityRef>, ) { ... } fn did_start_scroll(self: Handle<Self>, app: &mut App) { ... } fn did_update_scroll_position_by( self: Handle<Self>, app: &mut App, delta: f64, ) { ... } fn did_end_scroll(self: Handle<Self>, app: &mut App) { ... } fn did_overscroll_by(self: Handle<Self>, app: &mut App, value: f64) { ... } fn did_update_scroll_direction( self: Handle<Self>, app: &mut App, direction: ScrollDirection, ) { ... } fn did_update_scroll_metrics(self: Handle<Self>, app: &mut App) { ... } fn recommend_deferred_loading( self: Handle<Self>, app: &mut App, context: BuildContext, ) -> bool { ... } fn dispose(self: Handle<Self>, app: &mut App) { ... } fn debug_fill_description( self: Handle<Self>, app: &App, description: &mut Vec<String>, ) { ... } fn to_string(self: Handle<Self>, app: &App) -> String { ... } fn as_scroll_position(self: Handle<Self>) -> AnyScrollPosition { ... }
}
Expand description

Determines which portion of the content is visible in a scroll view.

The pixels value determines the scroll offset that the scroll view uses to select which part of its content to display. As the user scrolls the viewport, this value changes, which changes the content that is displayed.

The ScrollPosition applies physics to scrolling, and stores the min_scroll_extent and max_scroll_extent.

Scrolling is controlled by the current activity, which is set by begin_activity. ScrollPosition itself does not start any activities. Instead, implementors, such as ScrollPositionWithSingleContext, typically start activities in response to user input or instructions from a ScrollController.

This object is a Listenable that notifies its listeners when pixels changes.

Dart’s ScrollMetrics mixin members are re-declared here with the arena receiver; copy_with is the ScrollMetrics snapshot to hand to anything that reads the position’s metrics.

§Implementing ScrollPosition

Over time, a Scrollable might have many different ScrollPosition objects. For example, if Scrollable.physics changes type, Scrollable creates a new ScrollPosition with the new physics. To transfer state from the old instance to the new instance, implementors override absorb.

Implementors also need to call did_update_scroll_direction whenever user_scroll_direction changes values.

See also:

  • Scrollable, which uses a ScrollPosition to determine which portion of its content to display.
  • ScrollController, which can be used with ListView, GridView and other scrollable widgets to control a ScrollPosition.
  • ScrollPositionWithSingleContext, which is the most commonly used concrete implementor.
  • ScrollNotification and NotificationListener, which can be used to watch the scroll position without using a ScrollController.

Required Methods§

Source

fn scroll_position_data(self: Handle<Self>, app: &App) -> &ScrollPositionData

Dart’s ScrollPosition fields, held under the field scroll_position (scroll_position_accessors!).

Source

fn scroll_position_data_mut( self: Handle<Self>, app: &mut App, ) -> &mut ScrollPositionData

Source

fn axis_direction(self: Handle<Self>, app: &App) -> AxisDirection

The direction in which the scroll view scrolls; Dart’s ScrollMetrics.axisDirection.

Source

fn pointer_scroll(self: Handle<Self>, app: &mut App, delta: f64)

Changes the scrolling position based on a pointer signal from current value to delta without animation and without checking if new value is in range, taking min/max scroll extent into account.

Any active animation is canceled. If the user is currently scrolling, that action is canceled.

This method dispatches the start/update/end sequence of scrolling notifications.

This method is very similar to ViewportOffset::jump_to, but pointer_scroll will update the ScrollDirection.

Source

fn jump_to_without_settling(self: Handle<Self>, app: &mut App, value: f64)

👎Deprecated:

This method bypasses scroll activity management and can cause inconsistent layouts or scrolling behavior. Use jump_to or a custom ScrollPosition instead.

Deprecated. Use ViewportOffset::jump_to or a custom ScrollPosition instead.

Source

fn hold( self: Handle<Self>, app: &mut App, hold_cancel_callback: Listener, ) -> Rc<dyn ScrollHoldController>

Stop the current activity and start a crate::HoldScrollActivity.

Source

fn drag( self: Handle<Self>, app: &mut App, details: DragStartDetails, drag_cancel_callback: Listener, ) -> Rc<dyn Drag>

Start a drag activity corresponding to the given DragStartDetails.

The drag_cancel_callback argument will be invoked if the drag is ended prematurely (e.g. from another activity taking over). See crate::ScrollDragController::on_drag_canceled for details.

Provided Methods§

Source

fn physics(self: Handle<Self>, app: &App) -> ScrollPhysicsRef

How the scroll position should respond to user input.

For example, determines how the widget continues to animate after the user stops dragging the scroll view.

Source

fn context(self: Handle<Self>, app: &App) -> Rc<dyn ScrollContext>

Where the scrolling is taking place.

Typically implemented by ScrollableState.

Source

fn keep_scroll_offset(self: Handle<Self>, app: &App) -> bool

Save the current scroll offset with PageStorage and restore it if this scroll position’s scrollable is recreated.

See also:

  • ScrollController::keep_scroll_offset, which creates scroll positions and initializes this property.
Source

fn debug_label(self: Handle<Self>, app: &App) -> Option<&str>

A label that is used in the to_string output.

Intended to aid with identifying scroll position instances in debug output.

Source

fn min_scroll_extent(self: Handle<Self>, app: &App) -> f64

Source

fn max_scroll_extent(self: Handle<Self>, app: &App) -> f64

Source

fn has_content_dimensions(self: Handle<Self>, app: &App) -> bool

Source

fn viewport_dimension(self: Handle<Self>, app: &App) -> f64

Source

fn has_viewport_dimension(self: Handle<Self>, app: &App) -> bool

Source

fn axis(self: Handle<Self>, app: &App) -> Axis

Source

fn device_pixel_ratio(self: Handle<Self>, app: &App) -> f64

Source

fn out_of_range(self: Handle<Self>, app: &App) -> bool

Source

fn at_edge(self: Handle<Self>, app: &App) -> bool

Source

fn extent_before(self: Handle<Self>, app: &App) -> f64

Source

fn extent_inside(self: Handle<Self>, app: &App) -> f64

Source

fn extent_after(self: Handle<Self>, app: &App) -> f64

Source

fn extent_total(self: Handle<Self>, app: &App) -> f64

Source

fn copy_with(self: Handle<Self>, app: &App) -> Rc<dyn ScrollMetrics>

A snapshot of this position’s ScrollMetrics; Dart’s ScrollMetrics.copyWith.

This is what to hand to anything that reads the metrics — the physics, a ScrollNotification, ScrollIncrementDetails.

Source

fn have_dimensions(self: Handle<Self>, app: &App) -> bool

Whether viewport_dimension, min_scroll_extent, max_scroll_extent, out_of_range, and at_edge are available.

Set to true just before the first time apply_new_dimensions is called.

Source

fn should_ignore_pointer(self: Handle<Self>, app: &App) -> bool

Whether scrollables should absorb pointer events at this position.

This value relates to the current activity, which determines if additional touch input should be received by the scroll view or its children. If the position is overscrolled, as is allowed by crate::BouncingScrollPhysics, children of the scroll view will receive pointer events as the scroll view settles back from the overscrolled state.

Source

fn absorb(self: Handle<Self>, app: &mut App, other: AnyScrollPosition)

Take any current applicable state from the given ScrollPosition.

This method is called by ScrollPositionBase::init if it is given an old_position. The other argument might not have the same runtime type as this object.

This method can be destructive to the other ScrollPosition. The other object must be disposed immediately after this call.

If the old ScrollPosition object has a different runtime type than this one, the AnyScrollActivity::reset_activity method is invoked on the newly adopted activity.

§Overriding

Overrides of this method must call ScrollPositionBase::absorb after setting any metrics-related or activity-related state, since this method may restart the activity and scroll activities tend to use those metrics when being restarted.

Overrides of this method might need to start an crate::IdleScrollActivity if they are unable to absorb the activity from the other ScrollPosition.

Overrides of this method might also need to update the delegates of absorbed scroll activities if they use themselves as a crate::ScrollActivityDelegate.

Source

fn set_pixels(self: Handle<Self>, app: &mut App, new_pixels: f64) -> f64

Update the scroll position (pixels) to a given pixel value.

This should only be called by the current activity, either during the transient callback phase or in response to user input.

Returns the overscroll, if any. If the return value is 0.0, that means that pixels now returns the given new_pixels. If the return value is positive, then pixels is less than the requested value by the given amount (overscroll past the max extent), and if it is negative, it is greater than the requested value by the given amount (underscroll past the min extent).

The amount of overscroll is computed by apply_boundary_conditions.

The amount of the change that is applied is reported using did_update_scroll_position_by. If there is any overscroll, it is reported using did_overscroll_by.

Source

fn correct_pixels(self: Handle<Self>, app: &mut App, value: f64)

Change the value of pixels to the new value, without notifying any customers.

This is used to adjust the position while doing layout. In particular, this is typically called as a response to ViewportOffset::apply_viewport_dimension or ViewportOffset::apply_content_dimensions (in both cases, if this method is called, those methods should then return false to indicate that the position has been adjusted).

Calling this is rarely correct in other contexts. It will not immediately cause the rendering to change, since it does not notify the widgets or render objects that might be listening to this object.

To cause the position to jump or animate to a new value, consider ViewportOffset::jump_to or ViewportOffset::animate_to, which will honor the normal conventions for changing the scroll offset.

To force the pixels to a particular value without honoring the normal conventions for changing the scroll offset, consider force_pixels.

Source

fn force_pixels(self: Handle<Self>, app: &mut App, value: f64)

Change the value of pixels to the new value, and notify any customers, but without honoring normal conventions for changing the scroll offset.

This is used to implement ViewportOffset::jump_to. It can also be used to adjust the position when the dimensions of the viewport change. It should only be used when manually implementing the logic for honoring the relevant conventions of the class.

This should not be called during layout (e.g. when setting the initial scroll offset). Consider correct_pixels if you find you need to adjust the position during layout.

Source

fn save_scroll_offset(self: Handle<Self>, app: &mut App)

Called whenever scrolling ends, to store the current scroll offset in a storage mechanism with a lifetime that matches the app’s lifetime.

The stored value will be used by restore_scroll_offset when the ScrollPosition is recreated, in the case of the Scrollable being disposed then recreated in the same session.

The default implementation writes the pixels using the nearest PageStorage found from the context’s ScrollContext::storage_context property.

Source

fn restore_scroll_offset(self: Handle<Self>, app: &mut App)

Called whenever the ScrollPosition is created, to restore the scroll offset if possible.

The value is stored by save_scroll_offset when the scroll position changes, so that it can be restored in the case of the Scrollable being disposed then recreated in the same session.

The default implementation reads the value from the nearest PageStorage found from the context’s ScrollContext::storage_context property, and sets it using correct_pixels, if pixels is still unset.

This method is called from ScrollPositionBase::init, so layout has not yet occurred, and the viewport dimensions aren’t yet known when it is called.

Source

fn restore_offset( self: Handle<Self>, app: &mut App, offset: f64, initial_restore: bool, )

Called by context to restore the scroll offset to the provided value.

The provided value has previously been provided to the context by calling ScrollContext::save_offset, e.g. from save_offset.

This method may be called right after the scroll position is created before layout has occurred. In that case, initial_restore is set to true and the viewport dimensions will not be known yet. If the context doesn’t have any information to restore the scroll offset this method is not called.

The method may be called multiple times in the lifecycle of a ScrollPosition to restore it to different scroll offsets.

Source

fn save_offset(self: Handle<Self>, app: &mut App)

Called whenever scrolling ends, to persist the current scroll offset for state restoration purposes.

The default implementation stores the current value of pixels on the context by calling ScrollContext::save_offset. At a later point in time or after the application restarts, the context may restore the scroll position to the persisted offset by calling restore_offset.

Source

fn apply_boundary_conditions( self: Handle<Self>, app: &mut App, value: f64, ) -> f64

Returns the overscroll by applying the boundary conditions.

If the given value is in bounds, returns 0.0. Otherwise, returns the amount of value that cannot be applied to pixels as a result of the boundary conditions. If the physics allow out-of-bounds scrolling, this method always returns 0.0.

The default implementation defers to the physics object’s crate::ScrollPhysics::apply_boundary_conditions.

Source

fn correct_for_new_dimensions( self: Handle<Self>, app: &mut App, old_position: &dyn ScrollMetrics, new_position: &dyn ScrollMetrics, ) -> bool

Verifies that the new content and viewport dimensions are acceptable.

Called by ViewportOffset::apply_content_dimensions to determine its return value.

Should return true if the current scroll offset is correct given the new content and viewport dimensions.

Otherwise, should call correct_pixels to correct the scroll offset given the new dimensions, and then return false.

This is only called when have_dimensions is true.

The default implementation defers to crate::ScrollPhysics::adjust_position_for_new_dimensions.

Source

fn apply_new_dimensions(self: Handle<Self>, app: &mut App)

Notifies the activity that the dimensions of the underlying viewport or contents have changed.

Called after ViewportOffset::apply_viewport_dimension or ViewportOffset::apply_content_dimensions have changed the min_scroll_extent, the max_scroll_extent, or the viewport_dimension. When this method is called, it should be called after any corrections are applied to pixels using correct_pixels, not before.

The default implementation informs the activity of the new dimensions by calling its AnyScrollActivity::apply_new_dimensions method.

An override calls ScrollPositionBase::apply_new_dimensions where Dart writes super.applyNewDimensions().

Source

fn ensure_visible( self: Handle<Self>, app: &mut App, object: AnyRenderObject, alignment: f64, duration: Duration, curve: Rc<dyn Curve>, alignment_policy: ScrollPositionAlignmentPolicy, target_render_object: Option<AnyRenderObject>, ) -> CompleterFuture<()> ⓘ

Animates the position such that the given object is as visible as possible by just scrolling this position.

The optional target_render_object parameter is used to determine which area of that object should be as visible as possible. If it is None, the entire render object (as defined by its paint bounds) will be as visible as possible. If it is provided, it must be a descendant of the object.

See also:

Source

fn is_scrolling_notifier( self: Handle<Self>, app: &App, ) -> Handle<ValueNotifier<bool>>

This notifier’s value is true if a scroll is underway and false if the scroll position is idle.

Listeners added by stateful widgets should be removed in the widget’s State::dispose method.

Source

fn activity(self: Handle<Self>, app: &App) -> Option<AnyScrollActivity>

The currently operative AnyScrollActivity.

If the scroll position is not performing any more specific activity, the activity will be an crate::IdleScrollActivity. To determine whether the scroll position is idle, check the is_scrolling_notifier.

Call begin_activity to change the current activity.

Source

fn begin_activity( self: Handle<Self>, app: &mut App, new_activity: Option<ScrollActivityRef>, )

Change the current activity, disposing of the old one and sending scroll notifications as necessary.

If the argument is None, this method has no effect. This is convenient for cases where the new activity is obtained from another method, and that method might return None.

Source

fn did_start_scroll(self: Handle<Self>, app: &mut App)

Called by begin_activity to report when an activity has started.

Source

fn did_update_scroll_position_by(self: Handle<Self>, app: &mut App, delta: f64)

Called by set_pixels to report a change to the pixels position.

Source

fn did_end_scroll(self: Handle<Self>, app: &mut App)

Called by begin_activity to report when an activity has ended.

This also saves the scroll offset using save_scroll_offset.

Source

fn did_overscroll_by(self: Handle<Self>, app: &mut App, value: f64)

Called by set_pixels to report overscroll when an attempt is made to change the pixels position. Overscroll is the amount of change that was not applied to the pixels value.

Source

fn did_update_scroll_direction( self: Handle<Self>, app: &mut App, direction: ScrollDirection, )

Dispatches a notification that the ViewportOffset::user_scroll_direction has changed.

Implementors should call this function when they change ViewportOffset::user_scroll_direction.

Source

fn did_update_scroll_metrics(self: Handle<Self>, app: &mut App)

Dispatches a notification that the ScrollMetrics have changed.

Source

fn recommend_deferred_loading( self: Handle<Self>, app: &mut App, context: BuildContext, ) -> bool

Provides a heuristic to determine if expensive frame-bound tasks should be deferred.

The actual work of this is delegated to the physics via crate::ScrollPhysics::recommend_deferred_loading called with the current activity’s AnyScrollActivity::velocity.

Returning true from this method indicates that the crate::ScrollPhysics evaluate the current scroll velocity to be great enough that expensive operations impacting the UI should be deferred.

Source

fn dispose(self: Handle<Self>, app: &mut App)

Discards any resources used by the object.

Source

fn debug_fill_description( self: Handle<Self>, app: &App, description: &mut Vec<String>, )

Add additional information to the given description for use by to_string.

An override calls ScrollPositionBase::debug_fill_description where Dart writes super.debugFillDescription(description).

Source

fn to_string(self: Handle<Self>, app: &App) -> String

Dart’s toString, which reads the arena and so cannot be Debug.

Source

fn as_scroll_position(self: Handle<Self>) -> AnyScrollPosition

This position as the erased AnyScrollPosition — what to pass where a Dart API takes a ScrollPosition.

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementors§