Skip to main content

waft_material/
navigation_bar_theme.rs

1//! Flutter counterpart: material_ui's `navigation_bar_theme.dart`.
2
3use waft_embedder::{Color, lerp_double};
4use waft_foundation::{App, DiagnosticsProperty, DoubleProperty};
5use waft_painting::{ColorProperty, EdgeInsetsGeometry, ShapeBorderRef, TextStyle};
6use waft_widgets::{
7    BuildContext, IconThemeData, InheritedTheme, InheritedWidget, IntoWidget, KeyRef, WidgetRef,
8    WidgetStateProperty, WidgetStatePropertyRef,
9};
10
11use crate::navigation_bar::NavigationDestinationLabelBehavior;
12use crate::theme::Theme;
13
14/// Defines default property values for descendant `NavigationBar`
15/// widgets.
16///
17/// Descendant widgets obtain the current [`NavigationBarThemeData`] object
18/// using [`NavigationBarTheme::of`]. Instances of [`NavigationBarThemeData`] can be
19/// customized with [`NavigationBarThemeData::copy_with`].
20///
21/// Typically a [`NavigationBarThemeData`] is specified as part of the
22/// overall `Theme` with `ThemeData::navigation_bar_theme`. Alternatively, a
23/// [`NavigationBarTheme`] inherited widget can be used to theme `NavigationBar`s
24/// in a subtree of widgets.
25///
26/// All [`NavigationBarThemeData`] properties are `null` by default.
27/// When null, the `NavigationBar` will provide its own defaults based on the
28/// overall `Theme`'s textTheme and colorScheme. See the individual
29/// `NavigationBar` properties for details.
30///
31/// See also:
32///
33///  * `ThemeData`, which describes the overall theme information for the
34///    application.
35#[derive(Clone, Debug, Default)]
36pub struct NavigationBarThemeData {
37    /// Overrides the default value of `NavigationBar::height`.
38    pub height: Option<f64>,
39
40    /// Overrides the default value of `NavigationBar::background_color`.
41    pub background_color: Option<Color>,
42
43    /// Overrides the default value of `NavigationBar::elevation`.
44    pub elevation: Option<f64>,
45
46    /// Overrides the default value of `NavigationBar::shadow_color`.
47    pub shadow_color: Option<Color>,
48
49    /// Overrides the default value of `NavigationBar::surface_tint_color`.
50    pub surface_tint_color: Option<Color>,
51
52    /// Overrides the default value of `NavigationBar`'s selection indicator.
53    pub indicator_color: Option<Color>,
54
55    /// Overrides the default shape of the `NavigationBar`'s selection indicator.
56    pub indicator_shape: Option<ShapeBorderRef>,
57
58    /// The style to merge with the default text style for
59    /// `NavigationDestination` labels.
60    ///
61    /// You can use this to specify a different style when the label is selected.
62    pub label_text_style: Option<WidgetStatePropertyRef<Option<TextStyle>>>,
63
64    /// The theme to merge with the default icon theme for
65    /// `NavigationDestination` icons.
66    ///
67    /// You can use this to specify a different icon theme when the icon is
68    /// selected.
69    pub icon_theme: Option<WidgetStatePropertyRef<Option<IconThemeData>>>,
70
71    /// Overrides the default value of `NavigationBar::label_behavior`.
72    pub label_behavior: Option<NavigationDestinationLabelBehavior>,
73
74    /// Overrides the default value of `NavigationBar::overlay_color`.
75    pub overlay_color: Option<WidgetStatePropertyRef<Option<Color>>>,
76
77    /// Overrides the default value of `NavigationBar::label_padding`.
78    pub label_padding: Option<EdgeInsetsGeometry>,
79}
80
81crate::theme_data_getters::theme_data_getters! {
82    /// Dart's getters of `NavigationBarThemeData`: the theme data answers with its fields, and a defaults
83    /// subclass (Dart's `_...Defaults...(context)`) computes the ones it overrides when they
84    /// are read.
85    pub trait NavigationBarThemeDataGetters for NavigationBarThemeData, fields: navigation_bar_theme_data {
86        height: Option<f64>,
87        background_color: Option<Color>,
88        elevation: Option<f64>,
89        shadow_color: Option<Color>,
90        surface_tint_color: Option<Color>,
91        indicator_color: Option<Color>,
92        indicator_shape: Option<ShapeBorderRef>,
93        label_text_style: Option<WidgetStatePropertyRef<Option<TextStyle>>>,
94        icon_theme: Option<WidgetStatePropertyRef<Option<IconThemeData>>>,
95        label_behavior: Option<NavigationDestinationLabelBehavior>,
96        overlay_color: Option<WidgetStatePropertyRef<Option<Color>>>,
97        label_padding: Option<EdgeInsetsGeometry>,
98    }
99}
100
101impl NavigationBarThemeData {
102    /// Creates a theme that can be used for `ThemeData::navigation_bar_theme` and
103    /// [`NavigationBarTheme`]; Dart's optional named arguments are the fluent setters.
104    pub fn new() -> NavigationBarThemeData {
105        NavigationBarThemeData::default()
106    }
107
108    /// Dart `NavigationBarThemeData(height:)`.
109    pub fn height(mut self, height: f64) -> NavigationBarThemeData {
110        self.height = Some(height);
111        self
112    }
113
114    /// Dart `NavigationBarThemeData(backgroundColor:)`.
115    pub fn background_color(mut self, background_color: Color) -> NavigationBarThemeData {
116        self.background_color = Some(background_color);
117        self
118    }
119
120    /// Dart `NavigationBarThemeData(elevation:)`.
121    pub fn elevation(mut self, elevation: f64) -> NavigationBarThemeData {
122        self.elevation = Some(elevation);
123        self
124    }
125
126    /// Dart `NavigationBarThemeData(shadowColor:)`.
127    pub fn shadow_color(mut self, shadow_color: Color) -> NavigationBarThemeData {
128        self.shadow_color = Some(shadow_color);
129        self
130    }
131
132    /// Dart `NavigationBarThemeData(surfaceTintColor:)`.
133    pub fn surface_tint_color(mut self, surface_tint_color: Color) -> NavigationBarThemeData {
134        self.surface_tint_color = Some(surface_tint_color);
135        self
136    }
137
138    /// Dart `NavigationBarThemeData(indicatorColor:)`.
139    pub fn indicator_color(mut self, indicator_color: Color) -> NavigationBarThemeData {
140        self.indicator_color = Some(indicator_color);
141        self
142    }
143
144    /// Dart `NavigationBarThemeData(indicatorShape:)`.
145    pub fn indicator_shape(
146        mut self,
147        indicator_shape: impl Into<ShapeBorderRef>,
148    ) -> NavigationBarThemeData {
149        self.indicator_shape = Some(indicator_shape.into());
150        self
151    }
152
153    /// Dart `NavigationBarThemeData(labelTextStyle:)`.
154    pub fn label_text_style(
155        mut self,
156        label_text_style: WidgetStatePropertyRef<Option<TextStyle>>,
157    ) -> NavigationBarThemeData {
158        self.label_text_style = Some(label_text_style);
159        self
160    }
161
162    /// Dart `NavigationBarThemeData(iconTheme:)`.
163    pub fn icon_theme(
164        mut self,
165        icon_theme: WidgetStatePropertyRef<Option<IconThemeData>>,
166    ) -> NavigationBarThemeData {
167        self.icon_theme = Some(icon_theme);
168        self
169    }
170
171    /// Dart `NavigationBarThemeData(labelBehavior:)`.
172    pub fn label_behavior(
173        mut self,
174        label_behavior: NavigationDestinationLabelBehavior,
175    ) -> NavigationBarThemeData {
176        self.label_behavior = Some(label_behavior);
177        self
178    }
179
180    /// Dart `NavigationBarThemeData(overlayColor:)`.
181    pub fn overlay_color(
182        mut self,
183        overlay_color: WidgetStatePropertyRef<Option<Color>>,
184    ) -> NavigationBarThemeData {
185        self.overlay_color = Some(overlay_color);
186        self
187    }
188
189    /// Dart `NavigationBarThemeData(labelPadding:)`.
190    pub fn label_padding(
191        mut self,
192        label_padding: impl Into<EdgeInsetsGeometry>,
193    ) -> NavigationBarThemeData {
194        self.label_padding = Some(label_padding.into());
195        self
196    }
197
198    /// Creates a copy of this object with the given fields replaced with the
199    /// new values.
200    ///
201    /// Dart's named arguments are the fluent setters on the copy
202    /// (`data.copy_with().height(64.0)`).
203    pub fn copy_with(&self) -> NavigationBarThemeData {
204        self.clone()
205    }
206
207    /// Linearly interpolate between two navigation rail themes.
208    ///
209    /// If both arguments are null then null is returned.
210    ///
211    /// {@macro dart.ui.shadow.lerp}
212    pub fn lerp(
213        a: Option<&NavigationBarThemeData>,
214        b: Option<&NavigationBarThemeData>,
215        t: f64,
216    ) -> Option<NavigationBarThemeData> {
217        match (a, b) {
218            (None, None) => return None,
219            (Some(a), Some(b)) if std::ptr::eq(a, b) => return Some(a.clone()),
220            _ => {}
221        }
222        Some(NavigationBarThemeData {
223            height: lerp_double(a.and_then(|a| a.height), b.and_then(|b| b.height), t),
224            background_color: Color::lerp(
225                a.and_then(|a| a.background_color),
226                b.and_then(|b| b.background_color),
227                t,
228            ),
229            elevation: lerp_double(a.and_then(|a| a.elevation), b.and_then(|b| b.elevation), t),
230            shadow_color: Color::lerp(
231                a.and_then(|a| a.shadow_color),
232                b.and_then(|b| b.shadow_color),
233                t,
234            ),
235            surface_tint_color: Color::lerp(
236                a.and_then(|a| a.surface_tint_color),
237                b.and_then(|b| b.surface_tint_color),
238                t,
239            ),
240            indicator_color: Color::lerp(
241                a.and_then(|a| a.indicator_color),
242                b.and_then(|b| b.indicator_color),
243                t,
244            ),
245            indicator_shape: ShapeBorderRef::lerp(
246                a.and_then(|a| a.indicator_shape.as_ref()),
247                b.and_then(|b| b.indicator_shape.as_ref()),
248                t,
249            ),
250            label_text_style: <dyn WidgetStateProperty<Option<TextStyle>>>::lerp_optional(
251                a.and_then(|a| a.label_text_style.clone()),
252                b.and_then(|b| b.label_text_style.clone()),
253                t,
254                |a, b, t| TextStyle::lerp(a.as_ref(), b.as_ref(), t),
255            ),
256            icon_theme: <dyn WidgetStateProperty<Option<IconThemeData>>>::lerp_optional(
257                a.and_then(|a| a.icon_theme.clone()),
258                b.and_then(|b| b.icon_theme.clone()),
259                t,
260                |a, b, t| Some(IconThemeData::lerp(a.as_ref(), b.as_ref(), t)),
261            ),
262            label_behavior: if t < 0.5 {
263                a.and_then(|a| a.label_behavior)
264            } else {
265                b.and_then(|b| b.label_behavior)
266            },
267            overlay_color: <dyn WidgetStateProperty<Option<Color>>>::lerp_optional(
268                a.and_then(|a| a.overlay_color.clone()),
269                b.and_then(|b| b.overlay_color.clone()),
270                t,
271                Color::lerp,
272            ),
273            label_padding: EdgeInsetsGeometry::lerp(
274                a.and_then(|a| a.label_padding),
275                b.and_then(|b| b.label_padding),
276                t,
277            ),
278        })
279    }
280}
281
282impl PartialEq for NavigationBarThemeData {
283    fn eq(&self, other: &NavigationBarThemeData) -> bool {
284        if std::ptr::eq(self, other) {
285            return true;
286        }
287        other.height == self.height
288            && other.background_color == self.background_color
289            && other.elevation == self.elevation
290            && other.shadow_color == self.shadow_color
291            && other.surface_tint_color == self.surface_tint_color
292            && other.indicator_color == self.indicator_color
293            && other.indicator_shape == self.indicator_shape
294            && other.label_text_style == self.label_text_style
295            && other.icon_theme == self.icon_theme
296            && other.label_behavior == self.label_behavior
297            && other.overlay_color == self.overlay_color
298            && other.label_padding == self.label_padding
299    }
300}
301
302/// An inherited widget that defines visual properties for `NavigationBar`s and
303/// `NavigationDestination`s in this widget's subtree.
304///
305/// Values specified here are used for `NavigationBar` properties that are not
306/// given an explicit non-null value.
307///
308/// See also:
309///
310///  * `ThemeData::navigation_bar_theme`, which describes the
311///    [`NavigationBarThemeData`] in the overall theme for the application.
312#[derive(Debug)]
313pub struct NavigationBarTheme {
314    pub key: Option<KeyRef>,
315
316    /// Specifies the background color, label text style, icon theme, and label
317    /// type values for descendant `NavigationBar` widgets.
318    pub data: NavigationBarThemeData,
319
320    pub child: WidgetRef,
321}
322
323impl NavigationBarTheme {
324    /// Creates a navigation rail theme that controls the
325    /// [`NavigationBarThemeData`] properties for a `NavigationBar`.
326    #[cfg_attr(debug_assertions, track_caller)]
327    pub fn new<K>(data: NavigationBarThemeData, child: impl IntoWidget<K>) -> NavigationBarTheme {
328        NavigationBarTheme {
329            key: None,
330            data,
331            child: child.into_widget(),
332        }
333    }
334
335    /// Dart `NavigationBarTheme(key:)`.
336    pub fn key(mut self, key: KeyRef) -> NavigationBarTheme {
337        self.key = Some(key);
338        self
339    }
340
341    /// Retrieves the [`NavigationBarThemeData`] from the closest ancestor [`NavigationBarTheme`].
342    ///
343    /// If there is no enclosing [`NavigationBarTheme`] widget, then
344    /// `ThemeData::navigation_bar_theme` is used.
345    ///
346    /// Typical usage is as follows:
347    ///
348    /// ```text
349    /// let theme = NavigationBarTheme::of(app, context);
350    /// ```
351    pub fn of(app: &mut App, context: BuildContext) -> NavigationBarThemeData {
352        match context.depend_on_inherited_widget_of_exact_type::<NavigationBarTheme>(app) {
353            Some(navigation_bar_theme) => navigation_bar_theme.data.clone(),
354            None => Theme::of(app, context).navigation_bar_theme.clone(),
355        }
356    }
357}
358
359impl InheritedTheme for NavigationBarTheme {
360    fn wrap(&self, _app: &mut App, _context: BuildContext, child: WidgetRef) -> WidgetRef {
361        NavigationBarTheme::new(self.data.clone(), child).into_widget()
362    }
363}
364
365impl InheritedWidget for NavigationBarTheme {
366    fn key(&self) -> Option<&KeyRef> {
367        self.key.as_ref()
368    }
369
370    fn child(&self) -> &WidgetRef {
371        &self.child
372    }
373
374    fn update_should_notify(&self, old_widget: &NavigationBarTheme) -> bool {
375        self.data != old_widget.data
376    }
377
378    fn as_inherited_theme(&self) -> Option<&dyn InheritedTheme> {
379        Some(self)
380    }
381}
382
383impl waft_foundation::Diagnosticable for NavigationBarThemeData {
384    fn debug_fill_properties(&self, properties: &mut waft_foundation::DiagnosticPropertiesBuilder) {
385        properties.add(DoubleProperty::new("height", self.height).default_value(None));
386        properties
387            .add(ColorProperty::new("backgroundColor", self.background_color).default_value(None));
388        properties.add(DoubleProperty::new("elevation", self.elevation).default_value(None));
389        properties.add(ColorProperty::new("shadowColor", self.shadow_color).default_value(None));
390        properties.add(
391            ColorProperty::new("surfaceTintColor", self.surface_tint_color).default_value(None),
392        );
393        properties
394            .add(ColorProperty::new("indicatorColor", self.indicator_color).default_value(None));
395        properties.add(
396            DiagnosticsProperty::<ShapeBorderRef>::new(
397                "indicatorShape",
398                self.indicator_shape.clone(),
399            )
400            .default_value(None),
401        );
402        properties.add(
403            DiagnosticsProperty::<String>::new(
404                "labelTextStyle",
405                self.label_text_style
406                    .as_deref()
407                    .map(|property| property.to_string()),
408            )
409            .default_value(None),
410        );
411        properties.add(
412            DiagnosticsProperty::<String>::new(
413                "iconTheme",
414                self.icon_theme
415                    .as_deref()
416                    .map(|property| property.to_string()),
417            )
418            .default_value(None),
419        );
420        properties.add(DiagnosticsProperty::<waft_foundation::DartEnum<NavigationDestinationLabelBehavior>>::new("labelBehavior", self.label_behavior.map(waft_foundation::DartEnum)).default_value(None));
421        properties.add(
422            DiagnosticsProperty::<String>::new(
423                "overlayColor",
424                self.overlay_color
425                    .as_deref()
426                    .map(|property| property.to_string()),
427            )
428            .default_value(None),
429        );
430        properties.add(
431            DiagnosticsProperty::<EdgeInsetsGeometry>::new("labelPadding", self.label_padding)
432                .default_value(None),
433        );
434    }
435}
436
437impl std::fmt::Display for NavigationBarThemeData {
438    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
439        f.write_str(&waft_foundation::Diagnosticable::to_string(self))
440    }
441}
442
443#[cfg(test)]
444mod tests {
445    use std::rc::Rc;
446
447    use waft_painting::ShapeBorder;
448    use waft_painting::{
449        BorderSide, CircleBorder, EdgeInsets, EdgeInsetsGeometry, ShapeBorderRef, TextStyle,
450    };
451    use waft_widgets::{
452        Icon, IconData, IconThemeData, Opacity, WidgetState, WidgetStatePropertyAll,
453    };
454
455    use waft_widgets::testing::{WidgetTester, find, test_widgets};
456
457    use super::*;
458    use crate::icons::Icons;
459    use crate::material::Material;
460    use crate::navigation_bar::NavigationBar;
461    use crate::navigation_bar::NavigationDestination;
462    use crate::navigation_bar::tests::{
463        bar_material, bar_size, indicator_decoration, label_padding, label_style,
464        respects_overlay_color, state_overlay_color, text,
465    };
466    use crate::scaffold::Scaffold;
467    use crate::theme_data::ThemeData;
468    use crate::tooltip::tests::{m3, pump_app};
469
470    #[test]
471    fn copy_with_equality_basics() {
472        assert_eq!(
473            NavigationBarThemeData::new(),
474            NavigationBarThemeData::new().copy_with()
475        );
476    }
477
478    #[test]
479    fn navigation_bar_theme_data_lerp_special_cases() {
480        assert_eq!(NavigationBarThemeData::lerp(None, None, 0.0), None);
481        let data = NavigationBarThemeData::new();
482        assert_eq!(
483            NavigationBarThemeData::lerp(Some(&data), Some(&data), 0.5),
484            Some(data)
485        );
486    }
487
488    #[test]
489    fn navigation_bar_theme_data_lerps_each_field() {
490        let a = NavigationBarThemeData::new()
491            .height(60.0)
492            .background_color(Color::new(0xff000000))
493            .elevation(2.0)
494            .label_behavior(NavigationDestinationLabelBehavior::AlwaysShow)
495            .label_padding(EdgeInsets::all(0.0))
496            .label_text_style(<dyn WidgetStateProperty<Option<TextStyle>>>::all(Some(
497                TextStyle::new().font_size(10.0),
498            )));
499        let b = NavigationBarThemeData::new()
500            .height(100.0)
501            .background_color(Color::new(0xffffffff))
502            .elevation(6.0)
503            .label_behavior(NavigationDestinationLabelBehavior::AlwaysHide)
504            .label_padding(EdgeInsets::all(8.0))
505            .label_text_style(<dyn WidgetStateProperty<Option<TextStyle>>>::all(Some(
506                TextStyle::new().font_size(20.0),
507            )));
508
509        let halfway = NavigationBarThemeData::lerp(Some(&a), Some(&b), 0.5).expect("both given");
510        assert_eq!(halfway.height, Some(80.0));
511        assert_eq!(
512            halfway.background_color,
513            Color::lerp(
514                Some(Color::new(0xff000000)),
515                Some(Color::new(0xffffffff)),
516                0.5
517            )
518        );
519        assert_eq!(halfway.elevation, Some(4.0));
520        assert_eq!(
521            halfway.label_behavior,
522            Some(NavigationDestinationLabelBehavior::AlwaysHide)
523        );
524        assert_eq!(halfway.label_padding, Some(EdgeInsets::all(4.0).into()));
525        assert_eq!(
526            halfway
527                .label_text_style
528                .expect("both sides give one")
529                .resolve(&Default::default())
530                .and_then(|style| style.font_size),
531            Some(15.0)
532        );
533        assert_eq!(
534            NavigationBarThemeData::lerp(Some(&a), Some(&b), 0.25)
535                .expect("both given")
536                .label_behavior,
537            Some(NavigationDestinationLabelBehavior::AlwaysShow)
538        );
539    }
540
541    /// Dart's `_destinations()`.
542    fn destinations() -> Vec<WidgetRef> {
543        let icon = |icon: IconData| Icon::new(Some(icon));
544        vec![
545            NavigationDestination::new(icon(Icons::favorite_border()), "Abc")
546                .selected_icon(icon(Icons::favorite()))
547                .into_widget(),
548            NavigationDestination::new(icon(Icons::star_border()), "Def")
549                .selected_icon(icon(Icons::star()))
550                .into_widget(),
551        ]
552    }
553
554    /// Dart's `_iconTheme(tester, icon)`: the nearest `IconTheme` above the icon.
555    fn icon_theme(tester: &WidgetTester, icon: &IconData) -> IconThemeData {
556        let icon_theme = tester.element(
557            find::ancestor(
558                find::by_icon(icon),
559                find::by_type::<waft_widgets::IconTheme>(),
560            )
561            .first(),
562        );
563        tester.widget(icon_theme, |icon_theme: &waft_widgets::IconTheme| {
564            icon_theme.data.clone()
565        })
566    }
567
568    /// Dart's `_labelBehavior(tester)`: `AlwaysShow` unless an `Opacity` is above both labels.
569    fn label_behavior(tester: &WidgetTester) -> NavigationDestinationLabelBehavior {
570        let opacity_above = |label: &str| {
571            let element = text(tester, label);
572            tester.ancestors(element).into_iter().find(|ancestor| {
573                let app = tester.cell.borrow();
574                waft_widgets::downcast_widget::<Opacity>(&**ancestor.widget(&app)).is_some()
575            })
576        };
577        match (opacity_above("Abc"), opacity_above("Def")) {
578            (Some(abc), Some(_)) => {
579                if tester.widget(abc, |opacity: &Opacity| opacity.opacity) == 1.0 {
580                    NavigationDestinationLabelBehavior::OnlyShowSelected
581                } else {
582                    NavigationDestinationLabelBehavior::AlwaysHide
583                }
584            }
585            _ => NavigationDestinationLabelBehavior::AlwaysShow,
586        }
587    }
588
589    #[test]
590    fn navigation_bar_theme_data_values_are_used_when_no_navigation_bar_properties_are_specified() {
591        test_widgets(|tester| {
592            let height = 200.0;
593            let background_color = Color::new(0x00000001);
594            let elevation = 42.0;
595            let indicator_color = Color::new(0x00000002);
596            let indicator_shape = || CircleBorder::new(BorderSide::NONE, 0.0);
597            let selected_icon_size = 25.0;
598            let unselected_icon_size = 23.0;
599            let selected_icon_color = Color::new(0x00000003);
600            let unselected_icon_color = Color::new(0x00000004);
601            let selected_icon_opacity = 0.99;
602            let unselected_icon_opacity = 0.98;
603            let selected_label_font_size = 13.0;
604            let unselected_label_font_size = 11.0;
605            let behavior = NavigationDestinationLabelBehavior::AlwaysShow;
606            let padding = EdgeInsetsGeometry::all(8.0);
607
608            pump_app(tester, m3, |_app| {
609                Scaffold::new()
610                    .bottom_navigation_bar(NavigationBarTheme::new(
611                        NavigationBarThemeData::new()
612                            .height(height)
613                            .background_color(background_color)
614                            .elevation(elevation)
615                            .indicator_color(indicator_color)
616                            .indicator_shape(indicator_shape())
617                            .icon_theme(
618                                <dyn WidgetStateProperty<Option<IconThemeData>>>::resolve_with(
619                                    move |states| {
620                                        Some(if states.contains(&WidgetState::Selected) {
621                                            IconThemeData::new()
622                                                .size(selected_icon_size)
623                                                .color(selected_icon_color)
624                                                .opacity(selected_icon_opacity)
625                                        } else {
626                                            IconThemeData::new()
627                                                .size(unselected_icon_size)
628                                                .color(unselected_icon_color)
629                                                .opacity(unselected_icon_opacity)
630                                        })
631                                    },
632                                ),
633                            )
634                            .label_text_style(
635                                <dyn WidgetStateProperty<Option<TextStyle>>>::resolve_with(
636                                    move |states| {
637                                        Some(if states.contains(&WidgetState::Selected) {
638                                            TextStyle::new().font_size(selected_label_font_size)
639                                        } else {
640                                            TextStyle::new().font_size(unselected_label_font_size)
641                                        })
642                                    },
643                                ),
644                            )
645                            .label_behavior(behavior)
646                            .label_padding(padding),
647                        NavigationBar::new(destinations()),
648                    ))
649                    .into_widget()
650            });
651
652            assert_eq!(bar_size(tester).height(), height);
653            assert_eq!(
654                bar_material(tester, |material: &Material| material.color),
655                Some(background_color)
656            );
657            assert_eq!(
658                bar_material(tester, |material: &Material| material.elevation),
659                elevation
660            );
661            let (color, shape) = indicator_decoration(tester);
662            assert_eq!(color, Some(indicator_color));
663            assert_eq!(&*shape, &indicator_shape() as &dyn ShapeBorder);
664            let selected = icon_theme(tester, &Icons::favorite());
665            assert_eq!(selected.size, Some(selected_icon_size));
666            assert_eq!(
667                selected.color.as_deref().copied(),
668                Some(selected_icon_color)
669            );
670            assert_eq!(selected.opacity, Some(selected_icon_opacity));
671            let unselected = icon_theme(tester, &Icons::star_border());
672            assert_eq!(unselected.size, Some(unselected_icon_size));
673            assert_eq!(
674                unselected.color.as_deref().copied(),
675                Some(unselected_icon_color)
676            );
677            assert_eq!(unselected.opacity, Some(unselected_icon_opacity));
678            assert_eq!(
679                label_style(tester, "Abc").font_size,
680                Some(selected_label_font_size)
681            );
682            assert_eq!(
683                label_style(tester, "Def").font_size,
684                Some(unselected_label_font_size)
685            );
686            assert_eq!(label_behavior(tester), behavior);
687            assert_eq!(label_padding(tester, "Abc"), padding);
688            assert_eq!(label_padding(tester, "Def"), padding);
689        });
690    }
691
692    #[test]
693    fn navigation_bar_values_take_priority_over_navigation_bar_theme_data_values_when_both_properties_are_specified()
694     {
695        test_widgets(|tester| {
696            let height = 200.0;
697            let background_color = Color::new(0x00000001);
698            let elevation = 42.0;
699            let behavior = NavigationDestinationLabelBehavior::AlwaysShow;
700            let padding = EdgeInsetsGeometry::symmetric(0.0, 16.0);
701
702            pump_app(tester, m3, |_app| {
703                Scaffold::new()
704                    .bottom_navigation_bar(NavigationBarTheme::new(
705                        NavigationBarThemeData::new()
706                            .height(100.0)
707                            .elevation(18.0)
708                            .background_color(Color::new(0x00000099))
709                            .label_behavior(NavigationDestinationLabelBehavior::AlwaysHide)
710                            .label_padding(EdgeInsets::all(8.0)),
711                        NavigationBar::new(destinations())
712                            .height(height)
713                            .elevation(elevation)
714                            .background_color(background_color)
715                            .label_behavior(behavior)
716                            .label_padding(padding),
717                    ))
718                    .into_widget()
719            });
720
721            assert_eq!(bar_size(tester).height(), height);
722            assert_eq!(
723                bar_material(tester, |material: &Material| material.color),
724                Some(background_color)
725            );
726            assert_eq!(
727                bar_material(tester, |material: &Material| material.elevation),
728                elevation
729            );
730            assert_eq!(label_behavior(tester), behavior);
731            assert_eq!(label_padding(tester, "Abc"), padding);
732            assert_eq!(label_padding(tester, "Def"), padding);
733        });
734    }
735
736    #[test]
737    fn navigation_bar_respects_navigation_bar_theme_overlay_color_in_active_pressed_hovered_states()
738    {
739        test_widgets(|tester| {
740            tester.set_highlight_strategy(waft_widgets::FocusHighlightStrategy::AlwaysTraditional);
741            pump_app(
742                tester,
743                |app| {
744                    ThemeData::builder()
745                        .navigation_bar_theme(
746                            NavigationBarThemeData::new().overlay_color(state_overlay_color()),
747                        )
748                        .build(app)
749                },
750                |_app| {
751                    Scaffold::new()
752                        .bottom_navigation_bar(
753                            waft_widgets::RepaintBoundary::new().child(
754                                NavigationBar::new(crate::navigation_bar::tests::ac_alarm())
755                                    .on_destination_selected(|_app, _i| {}),
756                            ),
757                        )
758                        .into_widget()
759                },
760            );
761
762            respects_overlay_color(tester);
763        });
764    }
765
766    #[test]
767    fn of_returns_the_closest_navigation_bar_theme_over_theme_data() {
768        test_widgets(|tester| {
769            pump_app(
770                tester,
771                |app| {
772                    ThemeData::builder()
773                        .navigation_bar_theme(NavigationBarThemeData::new().height(10.0))
774                        .build(app)
775                },
776                |_app| {
777                    Scaffold::new()
778                        .bottom_navigation_bar(NavigationBarTheme::new(
779                            NavigationBarThemeData::new().height(64.0),
780                            NavigationBar::new(destinations()),
781                        ))
782                        .into_widget()
783                },
784            );
785            assert_eq!(bar_size(tester).height(), 64.0);
786        });
787    }
788
789    #[test]
790    fn update_should_notify_only_when_the_data_differs() {
791        let theme = |data: NavigationBarThemeData| {
792            NavigationBarTheme::new(data, waft_widgets::SizedBox::shrink())
793        };
794        let old = theme(NavigationBarThemeData::new().height(1.0));
795
796        assert!(!theme(NavigationBarThemeData::new().height(1.0)).update_should_notify(&old));
797        assert!(theme(NavigationBarThemeData::new().height(2.0)).update_should_notify(&old));
798    }
799    // Flutter's "Default NavigationBarThemeData debugFillProperties".
800    #[test]
801    fn a_navigation_bar_theme_data_describes_nothing_by_default() {
802        assert_eq!(
803            waft_widgets::testing::describe_properties(|properties| {
804                waft_foundation::Diagnosticable::debug_fill_properties(
805                    &NavigationBarThemeData::new(),
806                    properties,
807                )
808            }),
809            Vec::<String>::new()
810        );
811    }
812
813    // navigation_bar_theme_test.dart 'NavigationBarThemeData implements debugFillProperties'.
814    #[test]
815    fn an_assigned_navigation_bar_theme_describes_every_property() {
816        let theme = NavigationBarThemeData::new()
817            .height(200.0)
818            .background_color(Color::new(0x00000099))
819            .elevation(20.0)
820            .shadow_color(Color::new(0x00000098))
821            .surface_tint_color(Color::new(0x00000097))
822            .indicator_color(Color::new(0x00000096))
823            .indicator_shape(CircleBorder::default())
824            .label_text_style(Rc::new(WidgetStatePropertyAll::new(Some(
825                TextStyle::new().font_size(7.0),
826            ))))
827            .icon_theme(Rc::new(WidgetStatePropertyAll::new(Some(
828                IconThemeData::new().color(Color::new(0x00000097)),
829            ))))
830            .label_behavior(NavigationDestinationLabelBehavior::AlwaysHide)
831            .overlay_color(Rc::new(WidgetStatePropertyAll::new(Some(Color::new(
832                0x00000095,
833            )))))
834            .label_padding(EdgeInsetsGeometry::all(8.0));
835        let description = waft_widgets::testing::describe_properties(|properties| {
836            waft_foundation::Diagnosticable::debug_fill_properties(&theme, properties)
837        });
838        let description: Vec<String> = description
839            .iter()
840            .map(|line| waft_rendering::testing::ignoring_hash_codes(line))
841            .collect();
842        assert_eq!(
843            description,
844            [
845                "height: 200.0".to_string(),
846                format!("backgroundColor: {}", Color::new(0x00000099)),
847                "elevation: 20.0".to_string(),
848                format!("shadowColor: {}", Color::new(0x00000098)),
849                format!("surfaceTintColor: {}", Color::new(0x00000097)),
850                format!("indicatorColor: {}", Color::new(0x00000096)),
851                format!(
852                    "indicatorShape: {}",
853                    ShapeBorderRef::from(CircleBorder::default())
854                ),
855                "labelTextStyle: WidgetStatePropertyAll(TextStyle(inherit: true, size: 7.0))"
856                    .to_string(),
857                format!(
858                    "iconTheme: WidgetStatePropertyAll(IconThemeData#00000(color: {}))",
859                    Color::new(0x00000097)
860                ),
861                "labelBehavior: NavigationDestinationLabelBehavior.alwaysHide".to_string(),
862                format!(
863                    "overlayColor: WidgetStatePropertyAll({})",
864                    Color::new(0x00000095)
865                ),
866                "labelPadding: EdgeInsets.all(8.0)".to_string(),
867            ]
868        );
869    }
870}