Add nested picklist to modal example

This commit is contained in:
Cory Forsstrom 2023-02-18 13:49:11 -08:00 committed by Héctor Ramón Jiménez
parent 0a56ffb5d6
commit f608056c50
No known key found for this signature in database
GPG key ID: 140CC052C94F138E

View file

@ -3,11 +3,13 @@ use iced::keyboard;
use iced::subscription::{self, Subscription};
use iced::theme;
use iced::widget::{
self, button, column, container, horizontal_space, row, text, text_input,
self, button, column, container, horizontal_space, pick_list, row, text,
text_input,
};
use iced::{Alignment, Application, Command, Element, Event, Length, Settings};
use self::modal::Modal;
use modal::Modal;
use std::fmt;
pub fn main() -> iced::Result {
App::run(Settings::default())
@ -18,6 +20,7 @@ struct App {
show_modal: bool,
email: String,
password: String,
plan: Plan,
}
#[derive(Debug, Clone)]
@ -26,6 +29,7 @@ enum Message {
HideModal,
Email(String),
Password(String),
Plan(Plan),
Submit,
Event(Event),
}
@ -66,6 +70,10 @@ impl Application for App {
self.password = password;
Command::none()
}
Message::Plan(plan) => {
self.plan = plan;
Command::none()
}
Message::Submit => {
if !self.email.is_empty() && !self.password.is_empty() {
self.hide_modal();
@ -149,6 +157,16 @@ impl Application for App {
.padding(5),
]
.spacing(5),
column![
text("Plan").size(12),
pick_list(
Plan::ALL,
Some(self.plan),
Message::Plan
)
.padding(5),
]
.spacing(5),
button(text("Submit")).on_press(Message::HideModal),
]
.spacing(10)
@ -176,6 +194,29 @@ impl App {
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
enum Plan {
#[default]
Basic,
Pro,
Enterprise,
}
impl Plan {
pub const ALL: &[Self] = &[Self::Basic, Self::Pro, Self::Enterprise];
}
impl fmt::Display for Plan {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Plan::Basic => "Basic",
Plan::Pro => "Pro",
Plan::Enterprise => "Enterprise",
}
.fmt(f)
}
}
mod modal {
use iced::advanced::layout::{self, Layout};
use iced::advanced::overlay;
@ -469,6 +510,18 @@ mod modal {
renderer,
)
}
fn overlay<'c>(
&'c mut self,
layout: Layout<'_>,
renderer: &Renderer,
) -> Option<overlay::Element<'c, Message, Renderer>> {
self.content.as_widget_mut().overlay(
self.tree,
layout.children().next().unwrap(),
renderer,
)
}
}
impl<'a, Message, Renderer> From<Modal<'a, Message, Renderer>>