pub struct Button<T, F> { /* private fields */ }
Expand description
A widget that performs an action when pressed/tapped.
Implementations§
Source§impl<F> Button<Text, F>
impl<F> Button<Text, F>
Sourcepub fn with_text(label: impl Into<String>, action: F) -> Button<Text, F>
pub fn with_text(label: impl Into<String>, action: F) -> Button<Text, F>
Examples found in repository?
More examples
examples/picker.rs (lines 22-26)
13 fn body(&self) -> Self::Body {
14 let selection = self.selection.clone();
15 VStack::new((
16 Picker::new("Favorite Color", selection.binding(), (
17 Text::new("Red"),
18 Text::new("Green"),
19 Text::new("Yellow"),
20 Text::new("Blue"),
21 )),
22 Button::with_text("Choose next", clone!(selection => move || {
23 if let Id::Index(i) = selection.get() {
24 selection.set((i + 1) % 4);
25 }
26 }))
27 ))
28 }
examples/fizz_buzz.rs (lines 24-26)
19 fn body(&self) -> Self::Body {
20 let count = self.count.clone();
21 let fizz = count.get() % 3 == 0;
22 let buzz = count.get() % 5 == 0;
23 VStack::new((
24 Button::with_text("Increment", clone!(count => move || {
25 count.set(count.get() + 1);
26 })),
27 If::new_or_else(fizz || buzz, || {
28 HStack::new((
29 If::new(fizz, || Text::new("Fizz")),
30 If::new(buzz, || Text::new("Buzz")),
31 ))
32 }, || {
33 Text::new(format!("{}", self.count.get()))
34 }),
35 ))
36 }
examples/scale_rotate.rs (lines 33-35)
23 fn body(&self) -> Self::Body {
24 let scale = self.scale.clone();
25 let rotation = self.rotation.clone();
26 VStack::with_spacing(100, (
27 Rectangle::new()
28 .overlay(Text::new("Hello world!").foreground_style(Color::GRAY))
29 .rotation_effect(rotation.get())
30 .scale_effect(scale.get())
31 .frame(100),
32 HStack::new((
33 Button::with_text("Rotate", move || {
34 rotation.set_with(Animation::LINEAR, rotation.get() + Angle::QUARTER);
35 }),
36 Button::with_text("Scale up", clone!(scale => move || {
37 scale.set_with(Animation::LINEAR, scale.get() + 0.5);
38 })),
39 Button::with_text("Scale down", move || {
40 scale.set_with(Animation::LINEAR, scale.get() - 0.5);
41 }),
42 ))
43 ))
44 }
examples/animations.rs (lines 47-51)
25 fn body(&self) -> Self::Body {
26 let animations = self.animations;
27 let flips = self.flips.clone();
28 let width = 200.0;
29 let radius = 10.0;
30 let inner_width = width - 2.0 * radius;
31
32 VStack::new((
33 VStack::new(
34 ForEach::with_index_id(animations, |i, animation| {
35 let factor = if flips.get()[i] { 1.0 } else { -1.0 };
36 HStack::new((
37 Text::new(format!("{}", animation))
38 .frame_with(Alignment::Trailing, Frame::with_width(100)),
39 ZStack::new((
40 Rectangle::new()
41 .frame(Frame::exact(inner_width, 2)),
42 Circle::new()
43 .frame(2.0 * radius)
44 .offset(Vec2::with_x(factor * inner_width / 2.0))
45 ))
46 .frame(Frame::with_width(width)),
47 Button::with_text("Flip", clone!(flips => move || {
48 let mut value = flips.get();
49 value[i] = !value[i];
50 flips.set_with(animation, value);
51 })),
52 ))
53 })
54 ),
55 ))
56 }
Trait Implementations§
impl<T, F> Eq for Button<T, F>
impl<T, F> StructuralPartialEq for Button<T, F>
Auto Trait Implementations§
impl<T, F> Freeze for Button<T, F>
impl<T, F> RefUnwindSafe for Button<T, F>where
T: RefUnwindSafe,
F: RefUnwindSafe,
impl<T, F> Send for Button<T, F>
impl<T, F> Sync for Button<T, F>
impl<T, F> Unpin for Button<T, F>
impl<T, F> UnwindSafe for Button<T, F>where
T: UnwindSafe,
F: UnwindSafe,
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more