Skip to main content

conditional

Function conditional 

Source
pub fn conditional(flag: bool, if_true: View, if_false: View) -> View
Expand description

A ViewBuilder if/else, preserving branch identity.

Examples found in repository?
examples/sugar.rs (line 23)
4fn main() {
5    let show_detail = true;
6    let items = vec![String::from("alpha"), String::from("beta")];
7
8    let sugared = swiftui! {
9        VStack(spacing: 8.0) {
10            Text("Title").text_style(.large_title)
11            if show_detail {
12                Text("the details")
13            }
14            ForEach(items) { item in
15                Text(item)
16            }
17        }
18    };
19
20    let by_hand = vstack_spacing(
21        &[
22            text("Title").text_style(views::TextStyle::LargeTitle),
23            conditional(show_detail, text("the details"), empty()),
24            for_each_items(&items, |item| text(item)),
25        ],
26        8.0,
27    );
28
29    let a = views::dump_value_to_depth(&sugared.into_handle(), 6);
30    let b = views::dump_value_to_depth(&by_hand.into_handle(), 6);
31    eprintln!("macro == hand-written? {}", a == b);
32    if a != b {
33        for (x, y) in a.lines().zip(b.lines()) {
34            if x != y {
35                eprintln!("  macro: {}\n  hand : {}", x.trim(), y.trim());
36                break;
37            }
38        }
39    }
40}