Update qr_code example with theme selector
This commit is contained in:
parent
c2d82833a0
commit
4c6ea3cfe2
1 changed files with 28 additions and 6 deletions
|
|
@ -1,6 +1,6 @@
|
|||
use iced::widget::qr_code::{self, QRCode};
|
||||
use iced::widget::{column, container, text, text_input};
|
||||
use iced::{Alignment, Color, Element, Length, Sandbox, Settings};
|
||||
use iced::widget::{column, container, pick_list, row, text, text_input};
|
||||
use iced::{Alignment, Element, Length, Sandbox, Settings, Theme};
|
||||
|
||||
pub fn main() -> iced::Result {
|
||||
QRGenerator::run(Settings::default())
|
||||
|
|
@ -10,11 +10,13 @@ pub fn main() -> iced::Result {
|
|||
struct QRGenerator {
|
||||
data: String,
|
||||
qr_code: Option<qr_code::State>,
|
||||
theme: Theme,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
enum Message {
|
||||
DataChanged(String),
|
||||
ThemeChanged(Theme),
|
||||
}
|
||||
|
||||
impl Sandbox for QRGenerator {
|
||||
|
|
@ -41,13 +43,18 @@ impl Sandbox for QRGenerator {
|
|||
|
||||
self.data = data;
|
||||
}
|
||||
Message::ThemeChanged(theme) => {
|
||||
self.theme = theme;
|
||||
|
||||
if self.qr_code.is_some() {
|
||||
self.qr_code = qr_code::State::new(&self.data).ok();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn view(&self) -> Element<Message> {
|
||||
let title = text("QR Code Generator")
|
||||
.size(70)
|
||||
.style(Color::from([0.5, 0.5, 0.5]));
|
||||
let title = text("QR Code Generator").size(70);
|
||||
|
||||
let input =
|
||||
text_input("Type the data of your QR code here...", &self.data)
|
||||
|
|
@ -55,7 +62,18 @@ impl Sandbox for QRGenerator {
|
|||
.size(30)
|
||||
.padding(15);
|
||||
|
||||
let mut content = column![title, input]
|
||||
let choose_theme = row![
|
||||
text("Theme:"),
|
||||
pick_list(
|
||||
Theme::ALL,
|
||||
Some(self.theme.clone()),
|
||||
Message::ThemeChanged,
|
||||
)
|
||||
]
|
||||
.spacing(10)
|
||||
.align_items(Alignment::Center);
|
||||
|
||||
let mut content = column![title, input, choose_theme]
|
||||
.width(700)
|
||||
.spacing(20)
|
||||
.align_items(Alignment::Center);
|
||||
|
|
@ -72,4 +90,8 @@ impl Sandbox for QRGenerator {
|
|||
.center_y()
|
||||
.into()
|
||||
}
|
||||
|
||||
fn theme(&self) -> Theme {
|
||||
self.theme.clone()
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue