Skip to main content

SimpleDialog

Purpose​

A modal list of choices, answered by picking one.

Signature​

pub struct SimpleDialog<Msg = crate::callback::Callback> { .. }

impl SimpleDialog {
pub fn new(open: bool) -> Self;
}

Example​

Rendered preview of the SimpleDialog demo

From frus-examples/widgets/src/features/feedback/widgets/simple_dialog.rs:

//! `SimpleDialog` and `SimpleDialogOption` — a modal list of choices, answered by
//! picking one row. No buttons: choosing **is** the answer.

use frus::{
Align, Button, Column, Icon, Icons, Row, SimpleDialog, SimpleDialogOption, State, StateContext,
StatefulWidget, Text, Widget,
};

use crate::common::shared::section;

pub fn example() -> Box<dyn Widget> {
Box::new(SimpleDialogDemo.into_widget())
}

const ACCOUNTS: [&str; 3] = ["[email protected]", "[email protected]", "[email protected]"];

struct SimpleDialogDemo;

struct SimpleDialogDemoState {
open: bool,
account: usize,
}

impl StatefulWidget for SimpleDialogDemo {
type State = SimpleDialogDemoState;

fn create_state(&self) -> SimpleDialogDemoState {
SimpleDialogDemoState {
open: false,
account: 0,
}
}
}

impl State for SimpleDialogDemoState {
type Widget = SimpleDialogDemo;

fn build(&self, cx: &StateContext<Self>) -> Box<dyn Widget> {
let body = section(
"Choose from a list",
Column::new()
.spacing(12.0)
.cross_axis_alignment(Align::Start)
.child(Text::new(format!(
"Signed in as {}",
ACCOUNTS[self.account]
)))
.child(
Button::new("Switch account")
.on_press(cx.callback(|s: &mut SimpleDialogDemoState| s.open = true)),
),
);

// Each option sends its own message; `build()` turns it into the full-width row.
let dialog = ACCOUNTS.iter().enumerate().fold(
SimpleDialog::new(self.open).title("Switch account"),
|dialog, (i, account)| {
dialog.option(
SimpleDialogOption::with_child(
Row::new()
.spacing(16.0)
.cross_axis_alignment(Align::Center)
.child(Icon::new(Icons::PERSON))
.child(Text::new(*account)),
)
.on_press(cx.callback(move |s: &mut SimpleDialogDemoState| {
s.account = i;
s.open = false;
}))
.build(),
)
},
);
// A plain text row works too.
let dialog = dialog
.option(
SimpleDialogOption::new(
"Cancel",
cx.callback(|s: &mut SimpleDialogDemoState| s.open = false),
)
.build(),
)
.on_dismiss(cx.callback(|s: &mut SimpleDialogDemoState| s.open = false))
.body(body);

Box::new(dialog)
}
}

Properties​

MethodArgumentEffect
.on_dismiss()message: impl Into<Msg>What a click on the scrim sends — see Dialog::on_dismiss.
.title()title: impl Into<String>The heading, as text.
.title_widget()title: impl Widget<Msg> + 'staticThe heading, as a widget.
.title_text_style()style: TextStyleThe heading's type. headlineSmall by default, as AlertDialog's is.
.title_padding()padding: InsetsThe space around the heading.
.content_padding()padding: InsetsThe space around the list of options. No side padding by default: an option is a full-width row that pads itself.
.content_text_style()style: TextStyleWhat the options are set in. bodyMedium by default.
.option()option: impl Widget<Msg> + 'staticAdds an option — usually a SimpleDialogOption.
.background()color: ColorThe surface's colour — see Dialog::background.
.elevation()elevation: f32How far off the page it sits — see Dialog::elevation.
.shape()shape: ShapeBorderWhat shape it is — see Dialog::shape.
.inset_padding()padding: InsetsHow far off the window's edges — see Dialog::inset_padding.

Behaviour​

A dialog that offers a choice: a title, and a list of options to pick from.

See also​

Source: crates/frus-widgets/src/dialog.rs @ e6fba33f5b42