pub struct ListState { /* private fields */ }
Expand description
State of the List
widget
This state can be used to scroll through items and select one. When the list is rendered as a
stateful widget, the selected item will be highlighted and the list will be shifted to ensure
that the selected item is visible. This will modify the ListState
object passed to the
Frame::render_stateful_widget
method.
The state consists of two fields:
offset
: the index of the first item to be displayedselected
: the index of the selected item, which can beNone
if no item is selected
See the list in the Examples directory for a more in depth example of the various configuration options and for how to handle state.
§Example
use ratatui::{
layout::Rect,
widgets::{List, ListState},
Frame,
};
let items = ["Item 1"];
let list = List::new(items);
// This should be stored outside of the function in your application state.
let mut state = ListState::default();
*state.offset_mut() = 1; // display the second item and onwards
state.select(Some(3)); // select the forth item (0-indexed)
frame.render_stateful_widget(list, area, &mut state);
Implementations§
Source§impl ListState
impl ListState
Sourcepub const fn with_offset(self, offset: usize) -> Self
pub const fn with_offset(self, offset: usize) -> Self
Sets the index of the first item to be displayed
This is a fluent setter method which must be chained or used as it consumes self
§Examples
use ratatui::widgets::ListState;
let state = ListState::default().with_offset(1);
Sourcepub const fn with_selected(self, selected: Option<usize>) -> Self
pub const fn with_selected(self, selected: Option<usize>) -> Self
Sets the index of the selected item
This is a fluent setter method which must be chained or used as it consumes self
§Examples
use ratatui::widgets::ListState;
let state = ListState::default().with_selected(Some(1));
Sourcepub const fn offset(&self) -> usize
pub const fn offset(&self) -> usize
Index of the first item to be displayed
§Examples
use ratatui::widgets::ListState;
let state = ListState::default();
assert_eq!(state.offset(), 0);
Sourcepub fn offset_mut(&mut self) -> &mut usize
pub fn offset_mut(&mut self) -> &mut usize
Mutable reference to the index of the first item to be displayed
§Examples
use ratatui::widgets::ListState;
let mut state = ListState::default();
*state.offset_mut() = 1;
Sourcepub const fn selected(&self) -> Option<usize>
pub const fn selected(&self) -> Option<usize>
Index of the selected item
Returns None
if no item is selected
§Examples
use ratatui::widgets::ListState;
let state = ListState::default();
assert_eq!(state.selected(), None);
Sourcepub fn selected_mut(&mut self) -> &mut Option<usize>
pub fn selected_mut(&mut self) -> &mut Option<usize>
Mutable reference to the index of the selected item
Returns None
if no item is selected
§Examples
use ratatui::widgets::ListState;
let mut state = ListState::default();
*state.selected_mut() = Some(1);
Sourcepub fn select(&mut self, index: Option<usize>)
pub fn select(&mut self, index: Option<usize>)
Sets the index of the selected item
Set to None
if no item is selected. This will also reset the offset to 0
.
§Examples
use ratatui::widgets::ListState;
let mut state = ListState::default();
state.select(Some(1));
Sourcepub fn select_next(&mut self)
pub fn select_next(&mut self)
Selects the next item or the first one if no item is selected
Note: until the list is rendered, the number of items is not known, so the index is set to
0
and will be corrected when the list is rendered
§Examples
use ratatui::widgets::ListState;
let mut state = ListState::default();
state.select_next();
Sourcepub fn select_previous(&mut self)
pub fn select_previous(&mut self)
Selects the previous item or the last one if no item is selected
Note: until the list is rendered, the number of items is not known, so the index is set to
usize::MAX
and will be corrected when the list is rendered
§Examples
use ratatui::widgets::ListState;
let mut state = ListState::default();
state.select_previous();
Sourcepub fn select_first(&mut self)
pub fn select_first(&mut self)
Selects the first item
Note: until the list is rendered, the number of items is not known, so the index is set to
0
and will be corrected when the list is rendered
§Examples
use ratatui::widgets::ListState;
let mut state = ListState::default();
state.select_first();
Sourcepub fn select_last(&mut self)
pub fn select_last(&mut self)
Selects the last item
Note: until the list is rendered, the number of items is not known, so the index is set to
usize::MAX
and will be corrected when the list is rendered
§Examples
use ratatui::widgets::ListState;
let mut state = ListState::default();
state.select_last();
Sourcepub fn scroll_down_by(&mut self, amount: u16)
pub fn scroll_down_by(&mut self, amount: u16)
Scrolls down by a specified amount
in the list.
This method updates the selected index by moving it down by the given amount
.
If the amount
causes the index to go out of bounds (i.e., if the index is greater than
the length of the list), the last item in the list will be selected.
§Examples
use ratatui::widgets::ListState;
let mut state = ListState::default();
state.scroll_down_by(4);
Sourcepub fn scroll_up_by(&mut self, amount: u16)
pub fn scroll_up_by(&mut self, amount: u16)
Scrolls up by a specified amount
in the list.
This method updates the selected index by moving it up by the given amount
.
If the amount
causes the index to go out of bounds (i.e., less than zero),
the first item in the list will be selected.
§Examples
use ratatui::widgets::ListState;
let mut state = ListState::default();
state.scroll_up_by(4);
Trait Implementations§
impl Eq for ListState
impl StructuralPartialEq for ListState
Auto Trait Implementations§
impl Freeze for ListState
impl RefUnwindSafe for ListState
impl Send for ListState
impl Sync for ListState
impl Unpin for ListState
impl UnwindSafe for ListState
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
key
and return true
if they are equal.Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self
into a Left
variant of Either<Self, Self>
if into_left
is true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self
into a Left
variant of Either<Self, Self>
if into_left(&self)
returns true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read more