To change the content in view dynamically, we can do the following:
- Add some fields (e.g.,
counter) to the main structMyApp. - 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),
]
}➡️ Next: Text
📘 Back: Table of contents
