pub fn state<S: 'static, V: View, D: Fn() -> S + 'static, F: Fn(StateHandle<S>, &Context) -> V + 'static>(
initial: D,
f: F,
) -> impl ViewExpand description
State allows you to associate some state with a view. This is what you’ll use for a data model, as well as per-view state. Your state should be efficiently clonable. Use Rc as necessary.
initial is the initial value for your state.
f callback which is passed a State<S>
Examples found in repository?
More examples
examples/gallery.rs (line 11)
10fn slider_example() -> impl View {
11 hstack((caption("slider"), state(|| 0.5, |s, _| hslider(s))))
12}
13
14fn caption(s: &'static str) -> impl View {
15 s.font_size(12).padding(Auto)
16}
17
18fn knob_example() -> impl View {
19 hstack((
20 caption("knob"),
21 state(|| 0.5, |s, _| knob(s).size([30.0, 30.0]).padding(Auto)),
22 ))
23}
24
25fn toggle_example() -> impl View {
26 hstack((
27 caption("toggle"),
28 state(|| false, |s, _| toggle(s).size([30.0, 30.0]).padding(Auto)),
29 ))
30}
31
32fn text_editor_example() -> impl View {
33 hstack((
34 caption("text_editor"),
35 state(
36 || "edit me".to_string(),
37 |txt, _| text_editor(txt).padding(Auto),
38 ),
39 ))
40}examples/font_size.rs (lines 4-14)
3fn main() {
4 rui(state(
5 || 0.0,
6 |size, cx| {
7 let s = (cx[size] * 100.0) as u32;
8 vstack((
9 "58".font_size(s),
10 format!("font size: {}", s),
11 hslider(size),
12 ))
13 },
14 ));
15}examples/counter.rs (lines 4-15)
3fn main() {
4 rui(state(
5 || 1,
6 |count, cx| {
7 vstack((
8 cx[count].padding(Auto),
9 button("increment", move |cx| {
10 cx[count] += 1;
11 })
12 .padding(Auto),
13 ))
14 },
15 ));
16}