Skip to content

Latest commit

 

History

History
41 lines (30 loc) · 1.32 KB

File metadata and controls

41 lines (30 loc) · 1.32 KB

Changing Displaying Content

To change the content in view dynamically, we can do the following:

  • Add some fields (e.g., counter) to the main struct MyApp.
  • Display the fields in view (e.g., text(self.counter)).
  • Produce some messages in view (e.g., button(...).on_press(MyAppMessage::ButtonPressed)).
  • Update the fields when messages are received in update (e.g., self.counter += 1).
use iced::widget::{button, column, text, Column};

pub fn main() -> iced::Result {
    iced::application("A counter", update, view).run()
}

#[derive(Debug, Clone)]

enum Message {
    Increment,
}

fn update(value: &mut u64, message: Message) {
    match message {
        Message::Increment => *value += 1,
    }
}

fn view(value: &u64) -> Column<Message> {
    column![
        text(value),
        button("Increase").on_press(Message::Increment),
    ]
}

Producing and receiving messages

➡️ Next: Text

📘 Back: Table of contents