Created
February 22, 2021 22:43
-
-
Save baileyn/8d70ed67b91f36beaddcbac8736ff90a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use iced::{Checkbox, Column, Container, Element, Length, Sandbox, Settings}; | |
fn main() -> iced::Result { | |
Example::run(Settings::default()) | |
} | |
#[derive(Debug)] | |
struct Item { | |
name: String, | |
checked: bool, | |
} | |
#[derive(Debug, Default)] | |
struct Example { | |
items: Vec<Item>, | |
} | |
#[derive(Debug, Clone)] | |
enum Message { | |
ItemToggled(bool), | |
} | |
impl Sandbox for Example { | |
type Message = Message; | |
fn new() -> Self { | |
let items = vec![ | |
Item { | |
name: "example".into(), | |
checked: false, | |
}, | |
Item { | |
name: "another example".into(), | |
checked: false, | |
}, | |
Item { | |
name: "last example".into(), | |
checked: false, | |
}, | |
]; | |
Self { items } | |
} | |
fn title(&self) -> String { | |
"Checked Example".into() | |
} | |
fn update(&mut self, message: Self::Message) { | |
match message { | |
Message::ItemToggled(selected) => { | |
println!("Item was toggled to {}", selected); | |
} | |
} | |
} | |
fn view(&mut self) -> Element<'_, Self::Message> { | |
let col = self | |
.items | |
.iter() | |
.fold(Column::new().spacing(10), |column, item| { | |
column.push(Checkbox::new( | |
item.checked, | |
&item.name, | |
Message::ItemToggled, | |
)) | |
}); | |
Container::new(col) | |
.width(Length::Fill) | |
.height(Length::Fill) | |
.center_x() | |
.into() | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment