Advertisement
Guest User

Rust Iced Grocery List

a guest
Oct 2nd, 2024
153
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Rust 2.72 KB | Source Code | 0 0
  1. use iced::widget::{button, column, container, row, scrollable, text, text_input, Column};
  2. use iced::{Alignment, Element, Length, Padding, Theme};
  3.  
  4. fn main() -> iced::Result {
  5.     iced::application("Grocery List App", GroceryList::update, GroceryList::view)
  6.         .theme(GroceryList::theme)
  7.         .centered()
  8.         .run()
  9. }
  10.  
  11. struct GroceryList {
  12.     grocery_items: Vec<String>,
  13.     input_value: String,
  14. }
  15.  
  16. impl Default for GroceryList {
  17.     fn default() -> Self {
  18.         Self {
  19.             grocery_items: vec!["Eggs".to_owned(), "Milk".to_owned(), "Flour".to_owned()],
  20.             input_value: String::default(),
  21.         }
  22.     }
  23. }
  24.  
  25. #[derive(Debug, Clone)]
  26. enum Message {
  27.     InputValue(String),
  28.     Submitted,
  29.     DeleteItem(usize),
  30. }
  31.  
  32. impl GroceryList {
  33.     fn view(&self) -> Element<Message> {
  34.         container(
  35.             column!(
  36.                 Self::items_list_view(&self.grocery_items),
  37.                 row!(
  38.                     text_input("Input grocery item", &self.input_value)
  39.                         .on_input(Message::InputValue)
  40.                         .on_submit(Message::Submitted),
  41.                     button("Submit").on_press(Message::Submitted),
  42.                 )
  43.                 .spacing(30)
  44.                 .padding(Padding::from(30)),
  45.             )
  46.             .align_x(Alignment::Center),
  47.         )
  48.         .height(Length::Fill)
  49.         .width(Length::Fill)
  50.         .align_x(Alignment::Center)
  51.         .align_y(Alignment::Center)
  52.         .into()
  53.     }
  54.  
  55.     fn update(&mut self, message: Message) {
  56.         match message {
  57.             Message::InputValue(value) => self.input_value = value,
  58.             Message::Submitted => {
  59.                 self.grocery_items.push(self.input_value.clone());
  60.                 self.input_value = String::default();
  61.             }
  62.             Message::DeleteItem(item) => {
  63.                 self.grocery_items.remove(item);
  64.             }
  65.         }
  66.     }
  67.  
  68.     fn theme(&self) -> Theme {
  69.         Theme::Dark
  70.     }
  71.  
  72.     fn items_list_view(items: &[String]) -> Element<Message> {
  73.         let mut column = Column::new()
  74.             .spacing(20)
  75.             .align_x(Alignment::Center)
  76.             .width(Length::Fill);
  77.  
  78.         for (index, item) in items.iter().enumerate() {
  79.             column = column.push(Self::grocery_item(index, item));
  80.         }
  81.  
  82.         scrollable(container(column))
  83.             .width(250.0)
  84.             .height(300)
  85.             .into()
  86.     }
  87.  
  88.     fn grocery_item(index: usize, value: &str) -> Element<Message> {
  89.         row![
  90.             text(value),
  91.             button("Delete").on_press(Message::DeleteItem(index))
  92.         ]
  93.         .align_y(Alignment::Center)
  94.         .spacing(30)
  95.         .into()
  96.     }
  97. }
Tags: Rust Iced
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement