Center contents with Container in exit example

... also add some `padding` to buttons!
This commit is contained in:
Héctor Ramón Jiménez 2022-01-03 12:09:07 +07:00
parent 8a70d10401
commit ecd0997576
No known key found for this signature in database
GPG key ID: 140CC052C94F138E

View file

@ -1,5 +1,6 @@
use iced::{ use iced::{
button, Alignment, Button, Column, Element, Sandbox, Settings, Text, button, Alignment, Button, Column, Container, Element, Length, Sandbox,
Settings, Text,
}; };
pub fn main() -> iced::Result { pub fn main() -> iced::Result {
@ -47,9 +48,9 @@ impl Sandbox for Exit {
} }
fn view(&mut self) -> Element<Message> { fn view(&mut self) -> Element<Message> {
if self.show_confirm { let content = if self.show_confirm {
Column::new() Column::new()
.padding(20) .spacing(10)
.align_items(Alignment::Center) .align_items(Alignment::Center)
.push(Text::new("Are you sure you want to exit?")) .push(Text::new("Are you sure you want to exit?"))
.push( .push(
@ -57,19 +58,27 @@ impl Sandbox for Exit {
&mut self.confirm_button, &mut self.confirm_button,
Text::new("Yes, exit now"), Text::new("Yes, exit now"),
) )
.padding([10, 20])
.on_press(Message::Confirm), .on_press(Message::Confirm),
) )
.into()
} else { } else {
Column::new() Column::new()
.padding(20) .spacing(10)
.align_items(Alignment::Center) .align_items(Alignment::Center)
.push(Text::new("Click the button to exit")) .push(Text::new("Click the button to exit"))
.push( .push(
Button::new(&mut self.exit_button, Text::new("Exit")) Button::new(&mut self.exit_button, Text::new("Exit"))
.padding([10, 20])
.on_press(Message::Exit), .on_press(Message::Exit),
) )
};
Container::new(content)
.width(Length::Fill)
.height(Length::Fill)
.padding(20)
.center_x()
.center_y()
.into() .into()
} }
}
} }