Skip to main content

waft_material/
toggle_buttons_theme.rs

1//! Flutter counterpart: material_ui's `toggle_buttons_theme.dart`.
2
3use waft_embedder::{Color, lerp_double};
4use waft_foundation::{App, DiagnosticsProperty, DoubleProperty};
5use waft_painting::{AnyColor, BorderRadius, ColorProperty, TextStyle};
6use waft_rendering::BoxConstraints;
7use waft_widgets::{BuildContext, InheritedTheme, InheritedWidget, IntoWidget, KeyRef, WidgetRef};
8
9use crate::theme::Theme;
10
11/// Defines default property values for descendant `ToggleButtons` widgets.
12///
13/// Descendant widgets obtain the current [`ToggleButtonsThemeData`] object using
14/// [`ToggleButtonsTheme::of`]. Instances of [`ToggleButtonsThemeData`] can be customized
15/// with [`ToggleButtonsThemeData::copy_with`].
16///
17/// Typically a [`ToggleButtonsThemeData`] is specified as part of the overall `Theme`
18/// with `ThemeData::toggle_buttons_theme`.
19///
20/// All [`ToggleButtonsThemeData`] properties are `None` by default. When null, the
21/// `ToggleButtons` will use the values from `ThemeData` if they exist, otherwise it will
22/// provide its own defaults based on the overall `Theme`'s `ThemeData::text_theme` and
23/// `ThemeData::color_scheme`. See the individual `ToggleButtons` properties for details.
24///
25/// See also:
26///
27///  * [`ToggleButtonsTheme`], which describes the actual configuration of a
28///    toggle buttons theme.
29#[derive(Clone, Debug, Default, PartialEq)]
30pub struct ToggleButtonsThemeData {
31    /// The default text style for `ToggleButtons::children`.
32    ///
33    /// `TextStyle::color` will be ignored and substituted by [`color`](Self::color),
34    /// [`selected_color`](Self::selected_color) or [`disabled_color`](Self::disabled_color)
35    /// depending on whether the buttons are active, selected, or disabled.
36    pub text_style: Option<TextStyle>,
37
38    /// Defines the button's size.
39    ///
40    /// Typically used to constrain the button's minimum size.
41    pub constraints: Option<BoxConstraints>,
42
43    /// The color for descendant `Text` and `Icon` widgets if the button is enabled and not selected.
44    pub color: Option<Color>,
45
46    /// The color for descendant `Text` and `Icon` widgets if the button is selected.
47    pub selected_color: Option<Color>,
48
49    /// The color for descendant `Text` and `Icon` widgets if the button is disabled.
50    pub disabled_color: Option<Color>,
51
52    /// The fill color for selected toggle buttons.
53    ///
54    /// It can be a `WidgetStateColor`, which then resolves in the `WidgetState::Selected` and
55    /// `WidgetState::Disabled` states.
56    pub fill_color: Option<AnyColor>,
57
58    /// The color to use for filling the button when the button has input focus.
59    pub focus_color: Option<Color>,
60
61    /// The highlight color for the button's `InkWell`.
62    pub highlight_color: Option<Color>,
63
64    /// The color to use for filling the button when the button has a pointer hovering over it.
65    pub hover_color: Option<Color>,
66
67    /// The splash color for the button's `InkWell`.
68    pub splash_color: Option<Color>,
69
70    /// The border color to display when the toggle button is enabled.
71    pub border_color: Option<Color>,
72
73    /// The border color to display when the toggle button is selected.
74    pub selected_border_color: Option<Color>,
75
76    /// The border color to display when the toggle button is disabled.
77    pub disabled_border_color: Option<Color>,
78
79    /// The radii of the border's corners.
80    pub border_radius: Option<BorderRadius>,
81
82    /// The width of the border surrounding each toggle button.
83    ///
84    /// This applies to both the greater surrounding border, as well as the
85    /// borders dividing each toggle button.
86    ///
87    /// To render a hairline border (one physical pixel), set border_width to 0.0.
88    /// See [`BorderSide::width`] for more details on hairline borders.
89    pub border_width: Option<f64>,
90}
91
92impl ToggleButtonsThemeData {
93    /// Creates the set of color and border properties used to configure `ToggleButtons`.
94    pub fn new() -> ToggleButtonsThemeData {
95        ToggleButtonsThemeData::default()
96    }
97
98    /// Dart `ToggleButtonsThemeData(textStyle:)`.
99    pub fn text_style(mut self, text_style: TextStyle) -> ToggleButtonsThemeData {
100        self.text_style = Some(text_style);
101        self
102    }
103
104    /// Dart `ToggleButtonsThemeData(constraints:)`.
105    pub fn constraints(mut self, constraints: BoxConstraints) -> ToggleButtonsThemeData {
106        self.constraints = Some(constraints);
107        self
108    }
109
110    /// Dart `ToggleButtonsThemeData(color:)`.
111    pub fn color(mut self, color: Color) -> ToggleButtonsThemeData {
112        self.color = Some(color);
113        self
114    }
115
116    /// Dart `ToggleButtonsThemeData(selectedColor:)`.
117    pub fn selected_color(mut self, selected_color: Color) -> ToggleButtonsThemeData {
118        self.selected_color = Some(selected_color);
119        self
120    }
121
122    /// Dart `ToggleButtonsThemeData(disabledColor:)`.
123    pub fn disabled_color(mut self, disabled_color: Color) -> ToggleButtonsThemeData {
124        self.disabled_color = Some(disabled_color);
125        self
126    }
127
128    /// Dart `ToggleButtonsThemeData(fillColor:)`.
129    pub fn fill_color(mut self, fill_color: impl Into<AnyColor>) -> ToggleButtonsThemeData {
130        self.fill_color = Some(fill_color.into());
131        self
132    }
133
134    /// Dart `ToggleButtonsThemeData(focusColor:)`.
135    pub fn focus_color(mut self, focus_color: Color) -> ToggleButtonsThemeData {
136        self.focus_color = Some(focus_color);
137        self
138    }
139
140    /// Dart `ToggleButtonsThemeData(highlightColor:)`.
141    pub fn highlight_color(mut self, highlight_color: Color) -> ToggleButtonsThemeData {
142        self.highlight_color = Some(highlight_color);
143        self
144    }
145
146    /// Dart `ToggleButtonsThemeData(hoverColor:)`.
147    pub fn hover_color(mut self, hover_color: Color) -> ToggleButtonsThemeData {
148        self.hover_color = Some(hover_color);
149        self
150    }
151
152    /// Dart `ToggleButtonsThemeData(splashColor:)`.
153    pub fn splash_color(mut self, splash_color: Color) -> ToggleButtonsThemeData {
154        self.splash_color = Some(splash_color);
155        self
156    }
157
158    /// Dart `ToggleButtonsThemeData(borderColor:)`.
159    pub fn border_color(mut self, border_color: Color) -> ToggleButtonsThemeData {
160        self.border_color = Some(border_color);
161        self
162    }
163
164    /// Dart `ToggleButtonsThemeData(selectedBorderColor:)`.
165    pub fn selected_border_color(mut self, selected_border_color: Color) -> ToggleButtonsThemeData {
166        self.selected_border_color = Some(selected_border_color);
167        self
168    }
169
170    /// Dart `ToggleButtonsThemeData(disabledBorderColor:)`.
171    pub fn disabled_border_color(mut self, disabled_border_color: Color) -> ToggleButtonsThemeData {
172        self.disabled_border_color = Some(disabled_border_color);
173        self
174    }
175
176    /// Dart `ToggleButtonsThemeData(borderRadius:)`.
177    pub fn border_radius(mut self, border_radius: BorderRadius) -> ToggleButtonsThemeData {
178        self.border_radius = Some(border_radius);
179        self
180    }
181
182    /// Dart `ToggleButtonsThemeData(borderWidth:)`.
183    pub fn border_width(mut self, border_width: f64) -> ToggleButtonsThemeData {
184        self.border_width = Some(border_width);
185        self
186    }
187
188    /// Creates a copy of this object but with the given fields replaced with the
189    /// new values.
190    ///
191    /// Dart's named arguments are the fluent setters on the copy
192    /// (`data.copy_with().color(color)`).
193    pub fn copy_with(&self) -> ToggleButtonsThemeData {
194        self.clone()
195    }
196
197    /// Linearly interpolate between two toggle buttons themes.
198    pub fn lerp(
199        a: Option<&ToggleButtonsThemeData>,
200        b: Option<&ToggleButtonsThemeData>,
201        t: f64,
202    ) -> Option<ToggleButtonsThemeData> {
203        match (a, b) {
204            (None, None) => return None,
205            (Some(a), Some(b)) if std::ptr::eq(a, b) => return Some(a.clone()),
206            _ => {}
207        }
208        Some(ToggleButtonsThemeData {
209            text_style: TextStyle::lerp(
210                a.and_then(|a| a.text_style.as_ref()),
211                b.and_then(|b| b.text_style.as_ref()),
212                t,
213            ),
214            constraints: BoxConstraints::lerp(
215                a.and_then(|a| a.constraints),
216                b.and_then(|b| b.constraints),
217                t,
218            ),
219            color: Color::lerp(a.and_then(|a| a.color), b.and_then(|b| b.color), t),
220            selected_color: Color::lerp(
221                a.and_then(|a| a.selected_color),
222                b.and_then(|b| b.selected_color),
223                t,
224            ),
225            disabled_color: Color::lerp(
226                a.and_then(|a| a.disabled_color),
227                b.and_then(|b| b.disabled_color),
228                t,
229            ),
230            fill_color: AnyColor::lerp(
231                a.and_then(|a| a.fill_color.as_ref()),
232                b.and_then(|b| b.fill_color.as_ref()),
233                t,
234            )
235            .map(AnyColor::from),
236            focus_color: Color::lerp(
237                a.and_then(|a| a.focus_color),
238                b.and_then(|b| b.focus_color),
239                t,
240            ),
241            highlight_color: Color::lerp(
242                a.and_then(|a| a.highlight_color),
243                b.and_then(|b| b.highlight_color),
244                t,
245            ),
246            hover_color: Color::lerp(
247                a.and_then(|a| a.hover_color),
248                b.and_then(|b| b.hover_color),
249                t,
250            ),
251            splash_color: Color::lerp(
252                a.and_then(|a| a.splash_color),
253                b.and_then(|b| b.splash_color),
254                t,
255            ),
256            border_color: Color::lerp(
257                a.and_then(|a| a.border_color),
258                b.and_then(|b| b.border_color),
259                t,
260            ),
261            selected_border_color: Color::lerp(
262                a.and_then(|a| a.selected_border_color),
263                b.and_then(|b| b.selected_border_color),
264                t,
265            ),
266            disabled_border_color: Color::lerp(
267                a.and_then(|a| a.disabled_border_color),
268                b.and_then(|b| b.disabled_border_color),
269                t,
270            ),
271            border_radius: BorderRadius::lerp(
272                a.and_then(|a| a.border_radius),
273                b.and_then(|b| b.border_radius),
274                t,
275            ),
276            border_width: lerp_double(
277                a.and_then(|a| a.border_width),
278                b.and_then(|b| b.border_width),
279                t,
280            ),
281        })
282    }
283}
284
285/// An inherited widget that defines color and border parameters for `ToggleButtons` in this
286/// widget's subtree.
287///
288/// Values specified here are used for `ToggleButtons` properties that are not given an
289/// explicit non-null value.
290#[derive(Debug)]
291pub struct ToggleButtonsTheme {
292    pub key: Option<KeyRef>,
293    /// Defines the color and border properties for descendant `ToggleButtons` widgets.
294    pub data: ToggleButtonsThemeData,
295    pub child: WidgetRef,
296}
297
298impl ToggleButtonsTheme {
299    /// Creates a toggle buttons theme that controls the color and border parameters for
300    /// `ToggleButtons`.
301    #[cfg_attr(debug_assertions, track_caller)]
302    pub fn new<K>(data: ToggleButtonsThemeData, child: impl IntoWidget<K>) -> ToggleButtonsTheme {
303        ToggleButtonsTheme {
304            key: None,
305            data,
306            child: child.into_widget(),
307        }
308    }
309
310    /// Dart `ToggleButtonsTheme(key:)`.
311    pub fn key(mut self, key: KeyRef) -> ToggleButtonsTheme {
312        self.key = Some(key);
313        self
314    }
315
316    /// Retrieves the [`ToggleButtonsThemeData`] from the closest ancestor
317    /// [`ToggleButtonsTheme`] widget, or `ThemeData::toggle_buttons_theme` when there is none.
318    ///
319    /// Typical usage is as follows:
320    ///
321    /// ```text
322    /// let theme = ToggleButtonsTheme::of(app, context);
323    /// ```
324    pub fn of(app: &mut App, context: BuildContext) -> ToggleButtonsThemeData {
325        match context.depend_on_inherited_widget_of_exact_type::<ToggleButtonsTheme>(app) {
326            Some(toggle_buttons_theme) => toggle_buttons_theme.data.clone(),
327            None => Theme::of(app, context).toggle_buttons_theme.clone(),
328        }
329    }
330}
331
332impl InheritedTheme for ToggleButtonsTheme {
333    fn wrap(&self, _app: &mut App, _context: BuildContext, child: WidgetRef) -> WidgetRef {
334        ToggleButtonsTheme::new(self.data.clone(), child).into_widget()
335    }
336}
337
338impl InheritedWidget for ToggleButtonsTheme {
339    fn key(&self) -> Option<&KeyRef> {
340        self.key.as_ref()
341    }
342
343    fn child(&self) -> &WidgetRef {
344        &self.child
345    }
346
347    fn update_should_notify(&self, old_widget: &ToggleButtonsTheme) -> bool {
348        self.data != old_widget.data
349    }
350
351    fn as_inherited_theme(&self) -> Option<&dyn InheritedTheme> {
352        Some(self)
353    }
354}
355
356impl waft_foundation::Diagnosticable for ToggleButtonsThemeData {
357    fn debug_fill_properties(&self, properties: &mut waft_foundation::DiagnosticPropertiesBuilder) {
358        if let Some(text_style) = &self.text_style {
359            text_style.debug_fill_properties_with_prefix(properties, "textStyle.");
360        }
361        properties.add(
362            DiagnosticsProperty::<BoxConstraints>::new("constraints", self.constraints)
363                .default_value(None),
364        );
365        properties.add(ColorProperty::new("color", self.color).default_value(None));
366        properties
367            .add(ColorProperty::new("selectedColor", self.selected_color).default_value(None));
368        properties
369            .add(ColorProperty::new("disabledColor", self.disabled_color).default_value(None));
370        properties.add(
371            DiagnosticsProperty::<AnyColor>::new("fillColor", self.fill_color.clone())
372                .default_value(None),
373        );
374        properties.add(ColorProperty::new("focusColor", self.focus_color).default_value(None));
375        properties
376            .add(ColorProperty::new("highlightColor", self.highlight_color).default_value(None));
377        properties.add(ColorProperty::new("hoverColor", self.hover_color).default_value(None));
378        properties.add(ColorProperty::new("splashColor", self.splash_color).default_value(None));
379        properties.add(ColorProperty::new("borderColor", self.border_color).default_value(None));
380        properties.add(
381            ColorProperty::new("selectedBorderColor", self.selected_border_color)
382                .default_value(None),
383        );
384        properties.add(
385            ColorProperty::new("disabledBorderColor", self.disabled_border_color)
386                .default_value(None),
387        );
388        properties.add(
389            DiagnosticsProperty::<BorderRadius>::new("borderRadius", self.border_radius)
390                .default_value(None),
391        );
392        properties.add(DoubleProperty::new("borderWidth", self.border_width).default_value(None));
393    }
394}
395
396impl std::fmt::Display for ToggleButtonsThemeData {
397    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
398        f.write_str(&waft_foundation::Diagnosticable::to_string(self))
399    }
400}
401
402#[cfg(test)]
403mod tests {
404    //! Ported from `test/toggle_buttons_theme_test.dart`; the `debugFillProperties` cases wait
405    //! for diagnostics, and `hashCode` for `Hash`.
406
407    use waft_embedder::TextBaseline;
408    use waft_painting::TextDirection;
409    use waft_rendering::RenderBox;
410    use waft_widgets::{Center, Directionality, Icon, Row, Text, WidgetState, WidgetStateColor};
411    use waft_widgets::{SizedBox, WidgetStates};
412
413    use waft_widgets::testing::test_widgets;
414
415    use super::*;
416    use crate::colors::Colors;
417    use crate::icons::Icons;
418    use crate::material::{Material, MaterialType};
419    use crate::theme_data::MaterialTapTargetSize;
420    use crate::toggle_buttons::ToggleButtons;
421    use crate::toggle_buttons::tests::{
422        button_material, button_rect, button_text_style, check_ink_colors, default_theme,
423        icon_theme_color, material_color, paints_physical_model_then_strokes,
424        select_toggle_button_objects, style_color, texts, three_icons,
425    };
426
427    /// `Material(child: boilerplate(child: ToggleButtonsTheme(data:, child:)))`.
428    fn themed(data: ToggleButtonsThemeData, buttons: ToggleButtons) -> WidgetRef {
429        Material::new()
430            .child(Directionality::new(
431                TextDirection::Ltr,
432                Center::new().child(ToggleButtonsTheme::new(data, buttons)),
433            ))
434            .into_widget()
435    }
436
437    fn row_with_check() -> Vec<WidgetRef> {
438        vec![
439            // This Row is used like this to test for both TextStyle
440            // and IconTheme for Text and Icon widgets respectively.
441            Row::new()
442                .children([
443                    Text::new("First child").into_widget(),
444                    Icon::new(Some(Icons::check())).into_widget(),
445                ])
446                .into_widget(),
447        ]
448    }
449
450    // 'ToggleButtonsThemeData copyWith, ==, hashCode basics'
451    #[test]
452    fn copy_with_and_equality_basics() {
453        assert_eq!(
454            ToggleButtonsThemeData::new(),
455            ToggleButtonsThemeData::new().copy_with()
456        );
457    }
458
459    // 'ToggleButtonsThemeData lerp special cases'
460    #[test]
461    fn lerp_special_cases() {
462        assert!(ToggleButtonsThemeData::lerp(None, None, 0.0).is_none());
463        let data = ToggleButtonsThemeData::new();
464        assert_eq!(
465            ToggleButtonsThemeData::lerp(Some(&data), Some(&data), 0.5),
466            Some(data)
467        );
468    }
469
470    // 'ToggleButtonsThemeData defaults'
471    #[test]
472    fn theme_data_has_no_values_by_default() {
473        let theme_data = ToggleButtonsThemeData::new();
474        assert_eq!(theme_data.text_style, None);
475        assert_eq!(theme_data.constraints, None);
476        assert_eq!(theme_data.color, None);
477        assert_eq!(theme_data.selected_color, None);
478        assert_eq!(theme_data.disabled_color, None);
479        assert_eq!(theme_data.fill_color, None);
480        assert_eq!(theme_data.focus_color, None);
481        assert_eq!(theme_data.highlight_color, None);
482        assert_eq!(theme_data.hover_color, None);
483        assert_eq!(theme_data.splash_color, None);
484        assert_eq!(theme_data.border_color, None);
485        assert_eq!(theme_data.selected_border_color, None);
486        assert_eq!(theme_data.disabled_border_color, None);
487        assert_eq!(theme_data.border_radius, None);
488        assert_eq!(theme_data.border_width, None);
489
490        let theme = ToggleButtonsTheme::new(ToggleButtonsThemeData::new(), SizedBox::new());
491        assert_eq!(theme.data, ToggleButtonsThemeData::new());
492    }
493
494    // 'Theme text style, except color, is applied'
495    #[test]
496    fn the_theme_text_style_is_applied_except_its_color() {
497        test_widgets(|tester| {
498            tester.pump_widget(themed(
499                ToggleButtonsThemeData::new().text_style(
500                    TextStyle::new()
501                        .color(Colors::ORANGE.primary())
502                        .text_baseline(TextBaseline::Ideographic)
503                        .font_size(20.0),
504                ),
505                ToggleButtons::new(texts(&["First child", "Second child"]), vec![false, true])
506                    .on_pressed(|_app, _index| {}),
507            ));
508
509            for text in ["First child", "Second child"] {
510                let text_style = button_text_style(tester, text);
511                assert_eq!(text_style.text_baseline, Some(TextBaseline::Ideographic));
512                assert_eq!(text_style.font_size, Some(20.0));
513                assert_ne!(style_color(&text_style), Some(Colors::ORANGE.primary()));
514            }
515        });
516    }
517
518    // 'Custom BoxConstraints'
519    #[test]
520    fn the_theme_box_constraints_size_the_buttons() {
521        test_widgets(|tester| {
522            // Test for minimum constraints
523            tester.pump_widget(themed(
524                ToggleButtonsThemeData::new()
525                    .constraints(BoxConstraints::new().min_width(50.0).min_height(60.0)),
526                ToggleButtons::new(three_icons(), vec![false; 3])
527                    .tap_target_size(MaterialTapTargetSize::ShrinkWrap)
528                    .on_pressed(|_app, _index| {}),
529            ));
530            for index in 0..3 {
531                let rect = button_rect(tester, index);
532                assert_eq!(rect.width(), 50.0);
533                assert_eq!(rect.height(), 60.0);
534            }
535
536            // Test for maximum constraints
537            tester.pump_widget(themed(
538                ToggleButtonsThemeData::new()
539                    .constraints(BoxConstraints::new().max_width(20.0).max_height(10.0)),
540                ToggleButtons::new(three_icons(), vec![false; 3])
541                    .tap_target_size(MaterialTapTargetSize::ShrinkWrap)
542                    .on_pressed(|_app, _index| {}),
543            ));
544            for index in 0..3 {
545                let rect = button_rect(tester, index);
546                assert_eq!(rect.width(), 20.0);
547                assert_eq!(rect.height(), 10.0);
548            }
549        });
550    }
551
552    // 'Theme text/icon colors for enabled, selected and disabled states'
553    #[test]
554    fn the_theme_text_and_icon_colors_follow_the_state() {
555        test_widgets(|tester| {
556            let theme = default_theme(tester);
557            let check = Icons::check();
558            let enabled_color = Colors::LIME.primary();
559            let selected_color = Colors::GREEN.primary();
560            let disabled_color = Colors::YELLOW.primary();
561
562            tester.pump_widget(themed(
563                ToggleButtonsThemeData::new(),
564                ToggleButtons::new(row_with_check(), vec![false])
565                    .color(enabled_color)
566                    .on_pressed(|_app, _index| {}),
567            ));
568            // Custom theme enabled color
569            assert_ne!(theme.color_scheme.on_surface(), enabled_color);
570            assert_eq!(
571                style_color(&button_text_style(tester, "First child")),
572                Some(enabled_color)
573            );
574            assert_eq!(icon_theme_color(tester, &check), Some(enabled_color));
575
576            tester.pump_widget(themed(
577                ToggleButtonsThemeData::new().selected_color(selected_color),
578                ToggleButtons::new(row_with_check(), vec![true])
579                    .color(enabled_color)
580                    .on_pressed(|_app, _index| {}),
581            ));
582            tester.pump_and_settle();
583            // Custom theme selected color
584            assert_ne!(theme.color_scheme.primary(), selected_color);
585            assert_eq!(
586                style_color(&button_text_style(tester, "First child")),
587                Some(selected_color)
588            );
589            assert_eq!(icon_theme_color(tester, &check), Some(selected_color));
590
591            tester.pump_widget(themed(
592                ToggleButtonsThemeData::new().disabled_color(disabled_color),
593                ToggleButtons::new(row_with_check(), vec![false]).color(enabled_color),
594            ));
595            tester.pump_and_settle();
596            // Custom theme disabled color
597            assert_ne!(theme.disabled_color, disabled_color);
598            assert_eq!(
599                style_color(&button_text_style(tester, "First child")),
600                Some(disabled_color)
601            );
602            assert_eq!(icon_theme_color(tester, &check), Some(disabled_color));
603        });
604    }
605
606    // 'Theme button fillColor'
607    #[test]
608    fn the_theme_fill_color_fills_a_selected_button() {
609        test_widgets(|tester| {
610            let custom_fill_color = Colors::GREEN.primary();
611            tester.pump_widget(themed(
612                ToggleButtonsThemeData::new().fill_color(custom_fill_color),
613                ToggleButtons::new(texts(&["First child"]), vec![true])
614                    .on_pressed(|_app, _index| {}),
615            ));
616
617            let (color, material_type) =
618                button_material(tester, None, |material| (material.color, material.r#type));
619            assert_eq!(color, Some(custom_fill_color));
620            assert_eq!(material_type, MaterialType::Button);
621        });
622    }
623
624    // 'Custom Theme button fillColor in different states'
625    #[test]
626    fn a_widget_state_theme_fill_color_resolves_in_the_button_states() {
627        test_widgets(|tester| {
628            let enabled_fill_color = Colors::GREEN.primary();
629            let selected_fill_color = Colors::BLUE.primary();
630            let disabled_fill_color = Colors::YELLOW.primary();
631            let get_color = move |states: &WidgetStates| {
632                if states.contains(&WidgetState::Selected) {
633                    selected_fill_color
634                } else if states.contains(&WidgetState::Disabled) {
635                    disabled_fill_color
636                } else {
637                    enabled_fill_color
638                }
639            };
640
641            tester.pump_widget(themed(
642                ToggleButtonsThemeData::new().fill_color(WidgetStateColor::resolve_with(get_color)),
643                ToggleButtons::new(texts(&["First child", "Second child"]), vec![true, false])
644                    .on_pressed(|_app, _index| {}),
645            ));
646            tester.pump_and_settle();
647            assert_eq!(
648                material_color(tester, "First child"),
649                Some(selected_fill_color)
650            );
651            assert_eq!(
652                material_color(tester, "Second child"),
653                Some(enabled_fill_color)
654            );
655
656            tester.pump_widget(themed(
657                ToggleButtonsThemeData::new().fill_color(WidgetStateColor::resolve_with(get_color)),
658                ToggleButtons::new(texts(&["First child", "Second child"]), vec![true, false]),
659            ));
660            assert_eq!(
661                material_color(tester, "First child"),
662                Some(disabled_fill_color)
663            );
664            assert_eq!(
665                material_color(tester, "Second child"),
666                Some(disabled_fill_color)
667            );
668        });
669    }
670
671    // 'Theme InkWell colors'
672    #[test]
673    fn the_theme_ink_colors_are_used() {
674        test_widgets(|tester| {
675            let splash_color = Color::new(0xff4caf50);
676            let highlight_color = Color::new(0xffcddc39);
677            let hover_color = Color::new(0xffffeb3b);
678            let focus_color = Color::new(0xffffff00);
679            check_ink_colors(
680                tester,
681                |focus_node| {
682                    themed(
683                        ToggleButtonsThemeData::new()
684                            .splash_color(splash_color)
685                            .highlight_color(highlight_color)
686                            .hover_color(hover_color)
687                            .focus_color(focus_color),
688                        ToggleButtons::new(texts(&["First child"]), vec![true])
689                            .on_pressed(|_app, _index| {})
690                            .focus_nodes(vec![focus_node.erase()]),
691                    )
692                },
693                [hover_color, splash_color, focus_color],
694                true,
695            );
696        });
697    }
698
699    // 'Theme border width and border colors for enabled, selected and disabled states'
700    #[test]
701    fn the_theme_border_width_and_colors_follow_the_state() {
702        test_widgets(|tester| {
703            let border_color = Color::new(0xff4caf50);
704            let selected_border_color = Color::new(0xffcddc39);
705            let disabled_border_color = Color::new(0xffffeb3b);
706            const CUSTOM_WIDTH: f64 = 2.0;
707            let button = |selected: bool, enabled: bool| {
708                let buttons = ToggleButtons::new(texts(&["First child"]), vec![selected]);
709                if enabled {
710                    buttons.on_pressed(|_app, _index| {})
711                } else {
712                    buttons
713                }
714            };
715
716            // The physical model's path is dropped from the display list when the fill has no
717            // effect, as it has for an unselected button.
718            tester.pump_widget(themed(
719                ToggleButtonsThemeData::new()
720                    .border_color(border_color)
721                    .border_width(CUSTOM_WIDTH),
722                button(false, true),
723            ));
724            let render = select_toggle_button_objects(tester)[0].as_object();
725            assert!(paints_physical_model_then_strokes(
726                tester,
727                render,
728                false,
729                &[(border_color, CUSTOM_WIDTH)]
730            ));
731
732            tester.pump_widget(themed(
733                ToggleButtonsThemeData::new()
734                    .selected_border_color(selected_border_color)
735                    .border_width(CUSTOM_WIDTH),
736                button(true, true),
737            ));
738            let render = select_toggle_button_objects(tester)[0].as_object();
739            assert!(paints_physical_model_then_strokes(
740                tester,
741                render,
742                true,
743                &[(selected_border_color, CUSTOM_WIDTH)]
744            ));
745
746            tester.pump_widget(themed(
747                ToggleButtonsThemeData::new()
748                    .disabled_border_color(disabled_border_color)
749                    .border_width(CUSTOM_WIDTH),
750                button(false, false),
751            ));
752            let render = select_toggle_button_objects(tester)[0].as_object();
753            assert!(paints_physical_model_then_strokes(
754                tester,
755                render,
756                false,
757                &[(disabled_border_color, CUSTOM_WIDTH)]
758            ));
759        });
760    }
761    // Flutter's "Default ToggleButtonsThemeData debugFillProperties".
762    #[test]
763    fn a_toggle_buttons_theme_data_describes_nothing_by_default() {
764        assert_eq!(
765            waft_widgets::testing::describe_properties(|properties| {
766                waft_foundation::Diagnosticable::debug_fill_properties(
767                    &ToggleButtonsThemeData::new(),
768                    properties,
769                )
770            }),
771            Vec::<String>::new()
772        );
773    }
774
775    // toggle_buttons_theme_test.dart 'ToggleButtonsThemeData implements debugFillProperties'.
776    #[test]
777    fn an_assigned_toggle_buttons_theme_describes_every_property() {
778        let theme = ToggleButtonsThemeData::new()
779            .text_style(TextStyle::new().font_size(10.0))
780            .constraints(BoxConstraints::new().min_height(10.0).max_height(20.0))
781            .color(Color::new(0xfffffff0))
782            .selected_color(Color::new(0xfffffff1))
783            .disabled_color(Color::new(0xfffffff2))
784            .fill_color(Color::new(0xfffffff3))
785            .focus_color(Color::new(0xfffffff4))
786            .highlight_color(Color::new(0xfffffff5))
787            .hover_color(Color::new(0xfffffff6))
788            .splash_color(Color::new(0xfffffff7))
789            .border_color(Color::new(0xfffffff8))
790            .selected_border_color(Color::new(0xfffffff9))
791            .disabled_border_color(Color::new(0xfffffffa))
792            .border_radius(BorderRadius::circular(4.0))
793            .border_width(2.0);
794        let description = waft_widgets::testing::describe_properties(|properties| {
795            waft_foundation::Diagnosticable::debug_fill_properties(&theme, properties)
796        });
797        assert_eq!(
798            description,
799            [
800                "textStyle.inherit: true".to_string(),
801                "textStyle.size: 10.0".to_string(),
802                "constraints: BoxConstraints(0.0<=w<=Infinity, 10.0<=h<=20.0)".to_string(),
803                format!("color: {}", Color::new(0xfffffff0)),
804                format!("selectedColor: {}", Color::new(0xfffffff1)),
805                format!("disabledColor: {}", Color::new(0xfffffff2)),
806                format!("fillColor: {}", Color::new(0xfffffff3)),
807                format!("focusColor: {}", Color::new(0xfffffff4)),
808                format!("highlightColor: {}", Color::new(0xfffffff5)),
809                format!("hoverColor: {}", Color::new(0xfffffff6)),
810                format!("splashColor: {}", Color::new(0xfffffff7)),
811                format!("borderColor: {}", Color::new(0xfffffff8)),
812                format!("selectedBorderColor: {}", Color::new(0xfffffff9)),
813                format!("disabledBorderColor: {}", Color::new(0xfffffffa)),
814                "borderRadius: BorderRadius.circular(4.0)".to_string(),
815                "borderWidth: 2.0".to_string(),
816            ]
817        );
818    }
819}