Update counter example in README 🎉

This commit is contained in:
Héctor Ramón Jiménez 2022-07-27 06:56:26 +02:00
parent a1c5f8839d
commit 2dbcdba209
No known key found for this signature in database
GPG key ID: 140CC052C94F138E

View file

@ -98,15 +98,9 @@ that can be incremented and decremented using two buttons.
We start by modelling the __state__ of our application: We start by modelling the __state__ of our application:
```rust ```rust
use iced::button;
struct Counter { struct Counter {
// The counter value // The counter value
value: i32, value: i32,
// The local state of the two buttons
increment_button: button::State,
decrement_button: button::State,
} }
``` ```
@ -125,28 +119,23 @@ Now, let's show the actual counter by putting it all together in our
__view logic__: __view logic__:
```rust ```rust
use iced::{Button, Column, Text}; use iced::widget::{button, column, text, Column};
impl Counter { impl Counter {
pub fn view(&mut self) -> Column<Message> { pub fn view(&self) -> Column<Message> {
// We use a column: a simple vertical layout // We use a column: a simple vertical layout
Column::new() column![
.push( // The increment button. We tell it to produce an
// The increment button. We tell it to produce an // `IncrementPressed` message when pressed
// `IncrementPressed` message when pressed button("+").on_press(Message::IncrementPressed),
Button::new(&mut self.increment_button, Text::new("+"))
.on_press(Message::IncrementPressed), // We show the value of the counter here
) text(self.value).size(50),
.push(
// We show the value of the counter here // The decrement button. We tell it to produce a
Text::new(self.value.to_string()).size(50), // `DecrementPressed` message when pressed
) button("-").on_press(Message::DecrementPressed),
.push( ]
// The decrement button. We tell it to produce a
// `DecrementPressed` message when pressed
Button::new(&mut self.decrement_button, Text::new("-"))
.on_press(Message::DecrementPressed),
)
} }
} }
``` ```