Skip to main content

waft_material/
navigation_rail_theme.rs

1//! Flutter counterpart: material_ui's `navigation_rail_theme.dart`.
2
3use waft_embedder::{Color, lerp_double};
4use waft_foundation::{App, DiagnosticsProperty, DoubleProperty};
5use waft_painting::{ColorProperty, ShapeBorderRef, TextStyle};
6use waft_widgets::{
7    BuildContext, IconThemeData, InheritedTheme, InheritedWidget, IntoWidget, KeyRef, WidgetRef,
8};
9
10use crate::navigation_rail::NavigationRailLabelType;
11use crate::theme::Theme;
12
13/// Defines default property values for descendant `NavigationRail`
14/// widgets.
15///
16/// Descendant widgets obtain the current [`NavigationRailThemeData`] object
17/// using [`NavigationRailTheme::of`]. Instances of [`NavigationRailThemeData`]
18/// can be customized with [`NavigationRailThemeData::copy_with`].
19///
20/// Typically a [`NavigationRailThemeData`] is specified as part of the
21/// overall `Theme` with `ThemeData::navigation_rail_theme`.
22///
23/// All [`NavigationRailThemeData`] properties are `null` by default.
24/// When null, the `NavigationRail` will use the values from `ThemeData`
25/// if they exist, otherwise it will provide its own defaults based on the
26/// overall `Theme`'s text_theme and color_scheme. See the individual
27/// `NavigationRail` properties for details.
28///
29/// See also:
30///
31///  * `ThemeData`, which describes the overall theme information for the
32///    application.
33#[derive(Clone, Debug, Default, PartialEq)]
34pub struct NavigationRailThemeData {
35    /// Color to be used for the `NavigationRail`'s background.
36    pub background_color: Option<Color>,
37
38    /// The z-coordinate to be used for the `NavigationRail`'s elevation.
39    pub elevation: Option<f64>,
40
41    /// The style to merge with the default text style for
42    /// `NavigationRailDestination` labels, when the destination is not selected.
43    pub unselected_label_text_style: Option<TextStyle>,
44
45    /// The style to merge with the default text style for
46    /// `NavigationRailDestination` labels, when the destination is selected.
47    pub selected_label_text_style: Option<TextStyle>,
48
49    /// The theme to merge with the default icon theme for
50    /// `NavigationRailDestination` icons, when the destination is not selected.
51    pub unselected_icon_theme: Option<IconThemeData>,
52
53    /// The theme to merge with the default icon theme for
54    /// `NavigationRailDestination` icons, when the destination is selected.
55    pub selected_icon_theme: Option<IconThemeData>,
56
57    /// The alignment for the `NavigationRailDestination`s as they are positioned
58    /// within the `NavigationRail`.
59    pub group_alignment: Option<f64>,
60
61    /// The type that defines the layout and behavior of the labels in the
62    /// `NavigationRail`.
63    pub label_type: Option<NavigationRailLabelType>,
64
65    /// Whether or not the selected `NavigationRailDestination` should include a
66    /// `NavigationIndicator`.
67    pub use_indicator: Option<bool>,
68
69    /// Overrides the default value of `NavigationRail`'s selection indicator color,
70    /// when [`use_indicator`](Self::use_indicator) is true.
71    pub indicator_color: Option<Color>,
72
73    /// Overrides the default shape of the `NavigationRail`'s selection indicator.
74    pub indicator_shape: Option<ShapeBorderRef>,
75
76    /// Overrides the default value of `NavigationRail`'s minimum width when it
77    /// is not extended.
78    pub min_width: Option<f64>,
79
80    /// Overrides the default value of `NavigationRail`'s minimum width when it
81    /// is extended.
82    pub min_extended_width: Option<f64>,
83}
84
85crate::theme_data_getters::theme_data_getters! {
86    /// Dart's getters of `NavigationRailThemeData`: the theme data answers with its fields, and a defaults
87    /// subclass (Dart's `_...Defaults...(context)`) computes the ones it overrides when they
88    /// are read.
89    pub trait NavigationRailThemeDataGetters for NavigationRailThemeData, fields: navigation_rail_theme_data {
90        background_color: Option<Color>,
91        elevation: Option<f64>,
92        unselected_label_text_style: Option<TextStyle>,
93        selected_label_text_style: Option<TextStyle>,
94        unselected_icon_theme: Option<IconThemeData>,
95        selected_icon_theme: Option<IconThemeData>,
96        group_alignment: Option<f64>,
97        label_type: Option<NavigationRailLabelType>,
98        use_indicator: Option<bool>,
99        indicator_color: Option<Color>,
100        indicator_shape: Option<ShapeBorderRef>,
101        min_width: Option<f64>,
102        min_extended_width: Option<f64>,
103    }
104}
105
106impl NavigationRailThemeData {
107    /// Creates a theme that can be used for `ThemeData::navigation_rail_theme`; Dart's
108    /// optional named arguments are the fluent setters.
109    pub fn new() -> NavigationRailThemeData {
110        NavigationRailThemeData::default()
111    }
112
113    /// Dart `NavigationRailThemeData(backgroundColor:)`.
114    pub fn background_color(mut self, background_color: Color) -> NavigationRailThemeData {
115        self.background_color = Some(background_color);
116        self
117    }
118
119    /// Dart `NavigationRailThemeData(elevation:)`.
120    pub fn elevation(mut self, elevation: f64) -> NavigationRailThemeData {
121        self.elevation = Some(elevation);
122        self
123    }
124
125    /// Dart `NavigationRailThemeData(unselectedLabelTextStyle:)`.
126    pub fn unselected_label_text_style(
127        mut self,
128        unselected_label_text_style: TextStyle,
129    ) -> NavigationRailThemeData {
130        self.unselected_label_text_style = Some(unselected_label_text_style);
131        self
132    }
133
134    /// Dart `NavigationRailThemeData(selectedLabelTextStyle:)`.
135    pub fn selected_label_text_style(
136        mut self,
137        selected_label_text_style: TextStyle,
138    ) -> NavigationRailThemeData {
139        self.selected_label_text_style = Some(selected_label_text_style);
140        self
141    }
142
143    /// Dart `NavigationRailThemeData(unselectedIconTheme:)`.
144    pub fn unselected_icon_theme(
145        mut self,
146        unselected_icon_theme: IconThemeData,
147    ) -> NavigationRailThemeData {
148        self.unselected_icon_theme = Some(unselected_icon_theme);
149        self
150    }
151
152    /// Dart `NavigationRailThemeData(selectedIconTheme:)`.
153    pub fn selected_icon_theme(
154        mut self,
155        selected_icon_theme: IconThemeData,
156    ) -> NavigationRailThemeData {
157        self.selected_icon_theme = Some(selected_icon_theme);
158        self
159    }
160
161    /// Dart `NavigationRailThemeData(groupAlignment:)`.
162    pub fn group_alignment(mut self, group_alignment: f64) -> NavigationRailThemeData {
163        self.group_alignment = Some(group_alignment);
164        self
165    }
166
167    /// Dart `NavigationRailThemeData(labelType:)`.
168    pub fn label_type(mut self, label_type: NavigationRailLabelType) -> NavigationRailThemeData {
169        self.label_type = Some(label_type);
170        self
171    }
172
173    /// Dart `NavigationRailThemeData(useIndicator:)`.
174    pub fn use_indicator(mut self, use_indicator: bool) -> NavigationRailThemeData {
175        self.use_indicator = Some(use_indicator);
176        self
177    }
178
179    /// Dart `NavigationRailThemeData(indicatorColor:)`.
180    pub fn indicator_color(mut self, indicator_color: Color) -> NavigationRailThemeData {
181        self.indicator_color = Some(indicator_color);
182        self
183    }
184
185    /// Dart `NavigationRailThemeData(indicatorShape:)`.
186    pub fn indicator_shape(
187        mut self,
188        indicator_shape: impl Into<ShapeBorderRef>,
189    ) -> NavigationRailThemeData {
190        self.indicator_shape = Some(indicator_shape.into());
191        self
192    }
193
194    /// Dart `NavigationRailThemeData(minWidth:)`.
195    pub fn min_width(mut self, min_width: f64) -> NavigationRailThemeData {
196        self.min_width = Some(min_width);
197        self
198    }
199
200    /// Dart `NavigationRailThemeData(minExtendedWidth:)`.
201    pub fn min_extended_width(mut self, min_extended_width: f64) -> NavigationRailThemeData {
202        self.min_extended_width = Some(min_extended_width);
203        self
204    }
205
206    /// Creates a copy of this object with the given fields replaced with the
207    /// new values.
208    ///
209    /// Dart's named arguments are the fluent setters on the copy
210    /// (`data.copy_with().min_width(56.0)`).
211    pub fn copy_with(&self) -> NavigationRailThemeData {
212        self.clone()
213    }
214
215    /// Linearly interpolate between two navigation rail themes.
216    ///
217    /// If both arguments are null then null is returned.
218    ///
219    /// {@macro dart.ui.shadow.lerp}
220    pub fn lerp(
221        a: Option<&NavigationRailThemeData>,
222        b: Option<&NavigationRailThemeData>,
223        t: f64,
224    ) -> Option<NavigationRailThemeData> {
225        match (a, b) {
226            (None, None) => return None,
227            (Some(a), Some(b)) if std::ptr::eq(a, b) => return Some(a.clone()),
228            _ => {}
229        }
230        Some(NavigationRailThemeData {
231            background_color: Color::lerp(
232                a.and_then(|a| a.background_color),
233                b.and_then(|b| b.background_color),
234                t,
235            ),
236            elevation: lerp_double(a.and_then(|a| a.elevation), b.and_then(|b| b.elevation), t),
237            unselected_label_text_style: TextStyle::lerp(
238                a.and_then(|a| a.unselected_label_text_style.as_ref()),
239                b.and_then(|b| b.unselected_label_text_style.as_ref()),
240                t,
241            ),
242            selected_label_text_style: TextStyle::lerp(
243                a.and_then(|a| a.selected_label_text_style.as_ref()),
244                b.and_then(|b| b.selected_label_text_style.as_ref()),
245                t,
246            ),
247            unselected_icon_theme: lerp_icon_theme(
248                a.and_then(|a| a.unselected_icon_theme.as_ref()),
249                b.and_then(|b| b.unselected_icon_theme.as_ref()),
250                t,
251            ),
252            selected_icon_theme: lerp_icon_theme(
253                a.and_then(|a| a.selected_icon_theme.as_ref()),
254                b.and_then(|b| b.selected_icon_theme.as_ref()),
255                t,
256            ),
257            group_alignment: lerp_double(
258                a.and_then(|a| a.group_alignment),
259                b.and_then(|b| b.group_alignment),
260                t,
261            ),
262            label_type: if t < 0.5 {
263                a.and_then(|a| a.label_type)
264            } else {
265                b.and_then(|b| b.label_type)
266            },
267            use_indicator: if t < 0.5 {
268                a.and_then(|a| a.use_indicator)
269            } else {
270                b.and_then(|b| b.use_indicator)
271            },
272            indicator_color: Color::lerp(
273                a.and_then(|a| a.indicator_color),
274                b.and_then(|b| b.indicator_color),
275                t,
276            ),
277            indicator_shape: ShapeBorderRef::lerp(
278                a.and_then(|a| a.indicator_shape.as_ref()),
279                b.and_then(|b| b.indicator_shape.as_ref()),
280                t,
281            ),
282            min_width: lerp_double(a.and_then(|a| a.min_width), b.and_then(|b| b.min_width), t),
283            min_extended_width: lerp_double(
284                a.and_then(|a| a.min_extended_width),
285                b.and_then(|b| b.min_extended_width),
286                t,
287            ),
288        })
289    }
290}
291
292/// Dart's `a == null && b == null ? null : IconThemeData.lerp(a, b, t)`.
293fn lerp_icon_theme(
294    a: Option<&IconThemeData>,
295    b: Option<&IconThemeData>,
296    t: f64,
297) -> Option<IconThemeData> {
298    if a.is_none() && b.is_none() {
299        None
300    } else {
301        Some(IconThemeData::lerp(a, b, t))
302    }
303}
304
305/// An inherited widget that defines visual properties for `NavigationRail`s and
306/// `NavigationRailDestination`s in this widget's subtree.
307///
308/// Values specified here are used for `NavigationRail` properties that are not
309/// given an explicit non-null value.
310#[derive(Debug)]
311pub struct NavigationRailTheme {
312    pub key: Option<KeyRef>,
313    /// Specifies the background color, elevation, label text style, icon theme,
314    /// group alignment, and label type and border values for descendant
315    /// `NavigationRail` widgets.
316    pub data: NavigationRailThemeData,
317    pub child: WidgetRef,
318}
319
320impl NavigationRailTheme {
321    /// Creates a navigation rail theme that controls the
322    /// [`NavigationRailThemeData`] properties for a `NavigationRail`.
323    #[cfg_attr(debug_assertions, track_caller)]
324    pub fn new<K>(data: NavigationRailThemeData, child: impl IntoWidget<K>) -> NavigationRailTheme {
325        NavigationRailTheme {
326            key: None,
327            data,
328            child: child.into_widget(),
329        }
330    }
331
332    /// Dart `NavigationRailTheme(key:)`.
333    pub fn key(mut self, key: KeyRef) -> NavigationRailTheme {
334        self.key = Some(key);
335        self
336    }
337
338    /// Retrieves the [`NavigationRailThemeData`] from the closest ancestor
339    /// [`NavigationRailTheme`].
340    ///
341    /// If there is no enclosing [`NavigationRailTheme`] widget, then
342    /// `ThemeData::navigation_rail_theme` is used.
343    ///
344    /// Typical usage is as follows:
345    ///
346    /// ```text
347    /// let theme = NavigationRailTheme::of(app, context);
348    /// ```
349    pub fn of(app: &mut App, context: BuildContext) -> NavigationRailThemeData {
350        match context.depend_on_inherited_widget_of_exact_type::<NavigationRailTheme>(app) {
351            Some(navigation_rail_theme) => navigation_rail_theme.data.clone(),
352            None => Theme::of(app, context).navigation_rail_theme.clone(),
353        }
354    }
355}
356
357impl InheritedTheme for NavigationRailTheme {
358    fn wrap(&self, _app: &mut App, _context: BuildContext, child: WidgetRef) -> WidgetRef {
359        NavigationRailTheme::new(self.data.clone(), child).into_widget()
360    }
361}
362
363impl InheritedWidget for NavigationRailTheme {
364    fn key(&self) -> Option<&KeyRef> {
365        self.key.as_ref()
366    }
367
368    fn child(&self) -> &WidgetRef {
369        &self.child
370    }
371
372    fn update_should_notify(&self, old_widget: &NavigationRailTheme) -> bool {
373        self.data != old_widget.data
374    }
375
376    fn as_inherited_theme(&self) -> Option<&dyn InheritedTheme> {
377        Some(self)
378    }
379}
380
381impl waft_foundation::Diagnosticable for NavigationRailThemeData {
382    fn debug_fill_properties(&self, properties: &mut waft_foundation::DiagnosticPropertiesBuilder) {
383        let default_theme = NavigationRailThemeData::new();
384        properties.add(
385            ColorProperty::new("backgroundColor", self.background_color)
386                .default_value(default_theme.background_color),
387        );
388        properties.add(
389            DoubleProperty::new("elevation", self.elevation).default_value(default_theme.elevation),
390        );
391        properties.add(
392            DiagnosticsProperty::<TextStyle>::new(
393                "unselectedLabelTextStyle",
394                self.unselected_label_text_style.clone(),
395            )
396            .default_value(default_theme.unselected_label_text_style.clone()),
397        );
398        properties.add(
399            DiagnosticsProperty::<TextStyle>::new(
400                "selectedLabelTextStyle",
401                self.selected_label_text_style.clone(),
402            )
403            .default_value(default_theme.selected_label_text_style.clone()),
404        );
405        properties.add(
406            DiagnosticsProperty::<waft_widgets::IconThemeData>::new(
407                "unselectedIconTheme",
408                self.unselected_icon_theme.clone(),
409            )
410            .default_value(default_theme.unselected_icon_theme.clone()),
411        );
412        properties.add(
413            DiagnosticsProperty::<waft_widgets::IconThemeData>::new(
414                "selectedIconTheme",
415                self.selected_icon_theme.clone(),
416            )
417            .default_value(default_theme.selected_icon_theme.clone()),
418        );
419        properties.add(
420            DoubleProperty::new("groupAlignment", self.group_alignment)
421                .default_value(default_theme.group_alignment),
422        );
423        properties.add(
424            DiagnosticsProperty::<waft_foundation::DartEnum<NavigationRailLabelType>>::new(
425                "labelType",
426                self.label_type.map(waft_foundation::DartEnum),
427            )
428            .default_value(default_theme.label_type.map(waft_foundation::DartEnum)),
429        );
430        properties.add(
431            DiagnosticsProperty::<bool>::new("useIndicator", self.use_indicator)
432                .default_value(default_theme.use_indicator),
433        );
434        properties.add(
435            ColorProperty::new("indicatorColor", self.indicator_color)
436                .default_value(default_theme.indicator_color),
437        );
438        properties.add(
439            DiagnosticsProperty::<ShapeBorderRef>::new(
440                "indicatorShape",
441                self.indicator_shape.clone(),
442            )
443            .default_value(None),
444        );
445        properties.add(
446            DoubleProperty::new("minWidth", self.min_width).default_value(default_theme.min_width),
447        );
448        properties.add(
449            DoubleProperty::new("minExtendedWidth", self.min_extended_width)
450                .default_value(default_theme.min_extended_width),
451        );
452    }
453}
454
455impl std::fmt::Display for NavigationRailThemeData {
456    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
457        f.write_str(&waft_foundation::Diagnosticable::to_string(self))
458    }
459}
460
461#[cfg(test)]
462mod tests {
463    //! Ported from `test/navigation_rail_theme_test.dart`.
464
465    use waft_embedder::Size;
466    use waft_painting::ShapeBorder;
467    use waft_painting::{
468        Alignment, AlignmentGeometry, BorderSide, CircleBorder, RoundedRectangleBorder,
469        ShapeBorderRef, ShapeDecoration, StadiumBorder,
470    };
471    use waft_widgets::{AnyElement, Flexible, Icon, IconData, IconTheme, RichText, Text};
472
473    use waft_widgets::testing::{
474        WidgetTester, expect, find, finds_nothing, finds_widgets, test_widgets,
475    };
476
477    use super::*;
478    use crate::icons::Icons;
479    use crate::ink_decoration::Ink;
480    use crate::ink_well::InkResponse;
481    use crate::material::Material;
482    use crate::navigation_bar::NavigationIndicator;
483    use crate::navigation_rail::{NavigationRail, NavigationRailDestination};
484    use crate::scaffold::Scaffold;
485    use crate::test_support::material_app;
486    use crate::theme_data::ThemeData;
487
488    /// Dart's `_destinations()`.
489    fn destinations() -> Vec<NavigationRailDestination> {
490        vec![
491            NavigationRailDestination::new(
492                Icon::new(Some(Icons::favorite_border())),
493                Text::new("Abc"),
494            )
495            .selected_icon(Icon::new(Some(Icons::favorite()))),
496            NavigationRailDestination::new(Icon::new(Some(Icons::star_border())), Text::new("Def"))
497                .selected_icon(Icon::new(Some(Icons::star()))),
498        ]
499    }
500
501    fn pump(tester: &mut WidgetTester, theme: impl FnOnce(&App) -> ThemeData, body: WidgetRef) {
502        let root = {
503            let app = tester.cell.borrow();
504            material_app(theme(&app), Scaffold::new().body(body).into_widget())
505        };
506        tester.pump_widget(root);
507    }
508
509    fn rail(tester: &WidgetTester) -> AnyElement {
510        find::by_type::<NavigationRail>().evaluate(tester)[0]
511    }
512
513    /// Dart's `_railMaterial`: the first material is for the rail, and the rest are for the
514    /// destinations.
515    fn rail_material<R>(tester: &WidgetTester, read: impl FnOnce(&Material) -> R) -> R {
516        let material = tester.descendants_of_type::<Material>(rail(tester))[0];
517        tester.widget(material, read)
518    }
519
520    /// Dart's `_indicatorDecoration`.
521    fn indicator_decoration<R>(
522        tester: &WidgetTester,
523        read: impl FnOnce(&ShapeDecoration) -> R,
524    ) -> R {
525        let indicator = find::by_type::<NavigationIndicator>().evaluate(tester)[0];
526        let ink = tester.descendants_of_type::<Ink>(indicator)[0];
527        tester.widget(ink, |ink: &Ink| {
528            read(
529                ink.decoration
530                    .as_deref()
531                    .and_then(|decoration| decoration.as_any().downcast_ref())
532                    .expect("a ShapeDecoration"),
533            )
534        })
535    }
536
537    /// Dart's `_iconTheme`: the first IconTheme is the one added by the navigation rail.
538    fn icon_theme(tester: &WidgetTester, icon: IconData) -> IconThemeData {
539        let element = tester
540            .element(find::ancestor(find::by_icon(&icon), find::by_type::<IconTheme>()).first());
541        tester.widget(element, |icon_theme: &IconTheme| icon_theme.data.clone())
542    }
543
544    fn selected_icon_theme(tester: &WidgetTester) -> IconThemeData {
545        icon_theme(tester, Icons::favorite())
546    }
547
548    fn unselected_icon_theme(tester: &WidgetTester) -> IconThemeData {
549        icon_theme(tester, Icons::star_border())
550    }
551
552    fn label_style(tester: &WidgetTester, text: &str) -> TextStyle {
553        let text = find::text(text).evaluate(tester)[0];
554        let rich_text = tester.descendants_of_type::<RichText>(text)[0];
555        tester.widget(rich_text, |rich_text: &RichText| {
556            rich_text.text.style().cloned().expect("a styled label")
557        })
558    }
559
560    fn selected_label_style(tester: &WidgetTester) -> TextStyle {
561        label_style(tester, "Abc")
562    }
563
564    fn unselected_label_style(tester: &WidgetTester) -> TextStyle {
565        label_style(tester, "Def")
566    }
567
568    /// Dart's `_destinationSize`.
569    fn destination_size(tester: &WidgetTester) -> Size {
570        let material = tester.ancestor_of_type::<Material>(
571            tester.element(find::by_icon(&Icons::favorite()).first()),
572        );
573        let render = tester.render_box_of(material);
574        render.size(&tester.cell.borrow())
575    }
576
577    /// Dart's `_destinationsAlign(tester).alignment`: the first Flexible widget is the one
578    /// within the main Column for the rail content.
579    fn destinations_alignment(tester: &WidgetTester) -> AlignmentGeometry {
580        let flexible = find::by_type::<Flexible>().evaluate(tester)[0];
581        let align = tester.descendants_of_type::<waft_widgets::Align>(flexible)[0];
582        tester.widget(align, |align: &waft_widgets::Align| align.alignment)
583    }
584
585    /// Dart's `_visibilityAboveLabel(text)`: the `Visibility` above the label, if any.
586    fn visibility_above_label(tester: &WidgetTester, text: &str) -> Option<bool> {
587        let text = find::text(text).evaluate(tester)[0];
588        let ancestors = tester.ancestors(text);
589        let app = tester.cell.borrow();
590        ancestors.iter().find_map(|element| {
591            waft_widgets::downcast_widget::<waft_widgets::Visibility>(&**element.widget(&app))
592                .map(|visibility| visibility.visible)
593        })
594    }
595
596    /// Dart's `_labelType`.
597    fn label_type(tester: &WidgetTester) -> NavigationRailLabelType {
598        match (
599            visibility_above_label(tester, "Abc"),
600            visibility_above_label(tester, "Def"),
601        ) {
602            (Some(abc_visible), Some(_)) => {
603                if abc_visible {
604                    NavigationRailLabelType::Selected
605                } else {
606                    NavigationRailLabelType::None
607                }
608            }
609            _ => NavigationRailLabelType::All,
610        }
611    }
612
613    #[test]
614    fn copy_with_equality_basics() {
615        assert_eq!(
616            NavigationRailThemeData::new(),
617            NavigationRailThemeData::new().copy_with()
618        );
619    }
620
621    #[test]
622    fn material3_default_values_are_used_when_no_navigation_rail_or_navigation_rail_theme_data_properties_are_specified()
623     {
624        test_widgets(|tester| {
625            let theme = ThemeData::new(&tester.cell.borrow());
626            // Material 3 defaults
627            pump(
628                tester,
629                ThemeData::new,
630                NavigationRail::new(destinations(), Some(0)).into_widget(),
631            );
632
633            assert_eq!(
634                rail_material(tester, |material| material.color),
635                Some(theme.color_scheme.surface())
636            );
637            assert_eq!(rail_material(tester, |material| material.elevation), 0.0);
638            assert_eq!(destination_size(tester).width(), 80.0);
639            assert_eq!(selected_icon_theme(tester).size, Some(24.0));
640            assert_eq!(
641                selected_icon_theme(tester).color,
642                Some(theme.color_scheme.on_secondary_container().into())
643            );
644            assert_eq!(selected_icon_theme(tester).opacity, None);
645            assert_eq!(unselected_icon_theme(tester).size, Some(24.0));
646            assert_eq!(
647                unselected_icon_theme(tester).color,
648                Some(theme.color_scheme.on_surface_variant().into())
649            );
650            assert_eq!(unselected_icon_theme(tester).opacity, None);
651            assert_eq!(selected_label_style(tester).font_size, Some(14.0));
652            assert_eq!(unselected_label_style(tester).font_size, Some(14.0));
653            assert_eq!(
654                destinations_alignment(tester),
655                AlignmentGeometry::TOP_CENTER
656            );
657            assert_eq!(label_type(tester), NavigationRailLabelType::None);
658            expect(
659                tester,
660                &find::by_type::<NavigationIndicator>(),
661                finds_widgets(),
662            );
663            assert_eq!(
664                indicator_decoration(tester, |decoration| decoration.color.clone()),
665                Some(theme.color_scheme.secondary_container().into())
666            );
667            let stadium = StadiumBorder::new(BorderSide::NONE);
668            assert!(indicator_decoration(tester, |decoration| {
669                &*decoration.shape == &stadium as &dyn ShapeBorder
670            }));
671            let ink_response = find::by_type::<InkResponse>().evaluate(tester)[0];
672            assert!(tester.widget(ink_response, |ink_response: &InkResponse| {
673                ink_response.custom_border.as_deref() == Some(&stadium as &dyn ShapeBorder)
674            }));
675        });
676    }
677
678    #[test]
679    #[expect(
680        deprecated,
681        reason = "Dart's case calls the deprecated `copyWith(useMaterial3:)`"
682    )]
683    fn material2_default_values_are_used_when_no_navigation_rail_or_navigation_rail_theme_data_properties_are_specified()
684     {
685        test_widgets(|tester| {
686            // This test can be removed when `useMaterial3` is deprecated.
687            let theme = ThemeData::new(&tester.cell.borrow());
688            pump(
689                tester,
690                |app| {
691                    ThemeData::light(app, None)
692                        .copy_with()
693                        .use_material3(false)
694                        .build()
695                },
696                NavigationRail::new(destinations(), Some(0)).into_widget(),
697            );
698
699            assert_eq!(
700                rail_material(tester, |material| material.color),
701                Some(theme.color_scheme.surface())
702            );
703            assert_eq!(rail_material(tester, |material| material.elevation), 0.0);
704            assert_eq!(destination_size(tester).width(), 72.0);
705            assert_eq!(selected_icon_theme(tester).size, Some(24.0));
706            assert_eq!(
707                selected_icon_theme(tester).color,
708                Some(theme.color_scheme.primary().into())
709            );
710            assert_eq!(selected_icon_theme(tester).opacity, Some(1.0));
711            assert_eq!(unselected_icon_theme(tester).size, Some(24.0));
712            assert_eq!(
713                unselected_icon_theme(tester).color,
714                Some(theme.color_scheme.on_surface().into())
715            );
716            assert_eq!(unselected_icon_theme(tester).opacity, Some(0.64));
717            assert_eq!(selected_label_style(tester).font_size, Some(14.0));
718            assert_eq!(unselected_label_style(tester).font_size, Some(14.0));
719            assert_eq!(
720                destinations_alignment(tester),
721                AlignmentGeometry::TOP_CENTER
722            );
723            assert_eq!(label_type(tester), NavigationRailLabelType::None);
724            expect(
725                tester,
726                &find::by_type::<NavigationIndicator>(),
727                finds_nothing(),
728            );
729        });
730    }
731
732    #[test]
733    fn navigation_rail_theme_data_values_are_used_when_no_navigation_rail_properties_are_specified()
734    {
735        test_widgets(|tester| {
736            let background_color = Color::new(0x00000001);
737            let elevation = 7.0;
738            let selected_icon_size = 25.0;
739            let unselected_icon_size = 23.0;
740            let selected_icon_color = Color::new(0x00000002);
741            let unselected_icon_color = Color::new(0x00000003);
742            let selected_icon_opacity = 0.99;
743            let unselected_icon_opacity = 0.98;
744            let selected_label_font_size = 13.0;
745            let unselected_label_font_size = 11.0;
746            let group_alignment = 0.0;
747            let label_type_value = NavigationRailLabelType::All;
748            let use_indicator = true;
749            let indicator_color = Color::new(0x00000004);
750            let indicator_shape = RoundedRectangleBorder::default();
751
752            pump(
753                tester,
754                ThemeData::new,
755                NavigationRailTheme::new(
756                    NavigationRailThemeData::new()
757                        .background_color(background_color)
758                        .elevation(elevation)
759                        .selected_icon_theme(
760                            IconThemeData::new()
761                                .size(selected_icon_size)
762                                .color(selected_icon_color)
763                                .opacity(selected_icon_opacity),
764                        )
765                        .unselected_icon_theme(
766                            IconThemeData::new()
767                                .size(unselected_icon_size)
768                                .color(unselected_icon_color)
769                                .opacity(unselected_icon_opacity),
770                        )
771                        .selected_label_text_style(
772                            TextStyle::new().font_size(selected_label_font_size),
773                        )
774                        .unselected_label_text_style(
775                            TextStyle::new().font_size(unselected_label_font_size),
776                        )
777                        .group_alignment(group_alignment)
778                        .label_type(label_type_value)
779                        .use_indicator(use_indicator)
780                        .indicator_color(indicator_color)
781                        .indicator_shape(indicator_shape),
782                    NavigationRail::new(destinations(), Some(0)),
783                )
784                .into_widget(),
785            );
786
787            assert_eq!(
788                rail_material(tester, |material| material.color),
789                Some(background_color)
790            );
791            assert_eq!(
792                rail_material(tester, |material| material.elevation),
793                elevation
794            );
795            assert_eq!(selected_icon_theme(tester).size, Some(selected_icon_size));
796            assert_eq!(
797                selected_icon_theme(tester).color,
798                Some(selected_icon_color.into())
799            );
800            assert_eq!(
801                selected_icon_theme(tester).opacity,
802                Some(selected_icon_opacity)
803            );
804            assert_eq!(
805                unselected_icon_theme(tester).size,
806                Some(unselected_icon_size)
807            );
808            assert_eq!(
809                unselected_icon_theme(tester).color,
810                Some(unselected_icon_color.into())
811            );
812            assert_eq!(
813                unselected_icon_theme(tester).opacity,
814                Some(unselected_icon_opacity)
815            );
816            assert_eq!(
817                selected_label_style(tester).font_size,
818                Some(selected_label_font_size)
819            );
820            assert_eq!(
821                unselected_label_style(tester).font_size,
822                Some(unselected_label_font_size)
823            );
824            assert_eq!(
825                destinations_alignment(tester),
826                AlignmentGeometry::Alignment(Alignment::CENTER)
827            );
828            assert_eq!(label_type(tester), label_type_value);
829            expect(
830                tester,
831                &find::by_type::<NavigationIndicator>(),
832                finds_widgets(),
833            );
834            assert_eq!(
835                indicator_decoration(tester, |decoration| decoration.color.clone()),
836                Some(indicator_color.into())
837            );
838            assert!(indicator_decoration(tester, |decoration| {
839                &*decoration.shape == &indicator_shape as &dyn ShapeBorder
840            }));
841        });
842    }
843
844    #[test]
845    fn navigation_rail_values_take_priority_over_navigation_rail_theme_data_values_when_both_properties_are_specified()
846     {
847        test_widgets(|tester| {
848            let background_color = Color::new(0x00000001);
849            let elevation = 7.0;
850            let selected_icon_size = 25.0;
851            let unselected_icon_size = 23.0;
852            let selected_icon_color = Color::new(0x00000002);
853            let unselected_icon_color = Color::new(0x00000003);
854            let selected_icon_opacity = 0.99;
855            let unselected_icon_opacity = 0.98;
856            let selected_label_font_size = 13.0;
857            let unselected_label_font_size = 11.0;
858            let group_alignment = 0.0;
859            let label_type_value = NavigationRailLabelType::All;
860            let use_indicator = true;
861            let indicator_color = Color::new(0x00000004);
862
863            pump(
864                tester,
865                ThemeData::new,
866                NavigationRailTheme::new(
867                    NavigationRailThemeData::new()
868                        .background_color(Color::new(0x00000099))
869                        .elevation(5.0)
870                        .selected_icon_theme(
871                            IconThemeData::new()
872                                .size(31.0)
873                                .color(Color::new(0x00000098))
874                                .opacity(0.81),
875                        )
876                        .unselected_icon_theme(
877                            IconThemeData::new()
878                                .size(37.0)
879                                .color(Color::new(0x00000097))
880                                .opacity(0.82),
881                        )
882                        .selected_label_text_style(TextStyle::new().font_size(9.0))
883                        .unselected_label_text_style(TextStyle::new().font_size(7.0))
884                        .group_alignment(1.0)
885                        .label_type(NavigationRailLabelType::Selected)
886                        .use_indicator(false)
887                        .indicator_color(Color::new(0x00000096)),
888                    NavigationRail::new(destinations(), Some(0))
889                        .background_color(background_color)
890                        .elevation(elevation)
891                        .selected_icon_theme(
892                            IconThemeData::new()
893                                .size(selected_icon_size)
894                                .color(selected_icon_color)
895                                .opacity(selected_icon_opacity),
896                        )
897                        .unselected_icon_theme(
898                            IconThemeData::new()
899                                .size(unselected_icon_size)
900                                .color(unselected_icon_color)
901                                .opacity(unselected_icon_opacity),
902                        )
903                        .selected_label_text_style(
904                            TextStyle::new().font_size(selected_label_font_size),
905                        )
906                        .unselected_label_text_style(
907                            TextStyle::new().font_size(unselected_label_font_size),
908                        )
909                        .group_alignment(group_alignment)
910                        .label_type(label_type_value)
911                        .use_indicator(use_indicator)
912                        .indicator_color(indicator_color),
913                )
914                .into_widget(),
915            );
916
917            assert_eq!(
918                rail_material(tester, |material| material.color),
919                Some(background_color)
920            );
921            assert_eq!(
922                rail_material(tester, |material| material.elevation),
923                elevation
924            );
925            assert_eq!(selected_icon_theme(tester).size, Some(selected_icon_size));
926            assert_eq!(
927                selected_icon_theme(tester).color,
928                Some(selected_icon_color.into())
929            );
930            assert_eq!(
931                selected_icon_theme(tester).opacity,
932                Some(selected_icon_opacity)
933            );
934            assert_eq!(
935                unselected_icon_theme(tester).size,
936                Some(unselected_icon_size)
937            );
938            assert_eq!(
939                unselected_icon_theme(tester).color,
940                Some(unselected_icon_color.into())
941            );
942            assert_eq!(
943                unselected_icon_theme(tester).opacity,
944                Some(unselected_icon_opacity)
945            );
946            assert_eq!(
947                selected_label_style(tester).font_size,
948                Some(selected_label_font_size)
949            );
950            assert_eq!(
951                unselected_label_style(tester).font_size,
952                Some(unselected_label_font_size)
953            );
954            assert_eq!(
955                destinations_alignment(tester),
956                AlignmentGeometry::Alignment(Alignment::CENTER)
957            );
958            assert_eq!(label_type(tester), label_type_value);
959            expect(
960                tester,
961                &find::by_type::<NavigationIndicator>(),
962                finds_widgets(),
963            );
964            assert_eq!(
965                indicator_decoration(tester, |decoration| decoration.color.clone()),
966                Some(indicator_color.into())
967            );
968        });
969    }
970
971    /// Regression test for https://github.com/flutter/flutter/issues/118618.
972    #[test]
973    fn navigation_rail_theme_data_lerps_correctly_with_null_icon_themes() {
974        let lerp = NavigationRailThemeData::lerp(
975            Some(&NavigationRailThemeData::new()),
976            Some(&NavigationRailThemeData::new()),
977            0.5,
978        )
979        .expect("both given");
980
981        assert!(lerp.selected_icon_theme.is_none());
982        assert!(lerp.unselected_icon_theme.is_none());
983    }
984
985    #[test]
986    fn navigation_rail_theme_data_lerps_each_field() {
987        let a = NavigationRailThemeData::new()
988            .elevation(2.0)
989            .min_width(60.0)
990            .group_alignment(-1.0)
991            .label_type(NavigationRailLabelType::None)
992            .use_indicator(false)
993            .selected_icon_theme(IconThemeData::new().size(20.0));
994        let b = NavigationRailThemeData::new()
995            .elevation(6.0)
996            .min_width(100.0)
997            .group_alignment(1.0)
998            .label_type(NavigationRailLabelType::All)
999            .use_indicator(true);
1000
1001        let halfway = NavigationRailThemeData::lerp(Some(&a), Some(&b), 0.5).expect("both given");
1002        assert_eq!(halfway.elevation, Some(4.0));
1003        assert_eq!(halfway.min_width, Some(80.0));
1004        assert_eq!(halfway.group_alignment, Some(0.0));
1005        assert_eq!(halfway.label_type, Some(NavigationRailLabelType::All));
1006        assert_eq!(halfway.use_indicator, Some(true));
1007        assert_eq!(
1008            halfway.selected_icon_theme,
1009            Some(IconThemeData::lerp(
1010                a.selected_icon_theme.as_ref(),
1011                None,
1012                0.5
1013            ))
1014        );
1015        assert!(halfway.unselected_icon_theme.is_none());
1016
1017        let early = NavigationRailThemeData::lerp(Some(&a), Some(&b), 0.25).expect("both given");
1018        assert_eq!(early.label_type, Some(NavigationRailLabelType::None));
1019        assert_eq!(early.use_indicator, Some(false));
1020    }
1021
1022    #[test]
1023    fn theme_data_holds_the_navigation_rail_theme() {
1024        test_widgets(|tester| {
1025            let app = tester.cell.borrow();
1026            let theme = ThemeData::new(&app);
1027            assert_eq!(theme.navigation_rail_theme, NavigationRailThemeData::new());
1028
1029            let wide = ThemeData::builder()
1030                .navigation_rail_theme(NavigationRailThemeData::new().min_width(100.0))
1031                .build(&app);
1032            assert_eq!(wide.navigation_rail_theme.min_width, Some(100.0));
1033            assert_ne!(theme, wide);
1034            assert_eq!(
1035                theme
1036                    .copy_with()
1037                    .navigation_rail_theme(wide.navigation_rail_theme.clone())
1038                    .build(),
1039                wide
1040            );
1041
1042            let narrow = ThemeData::builder()
1043                .navigation_rail_theme(NavigationRailThemeData::new().min_width(60.0))
1044                .build(&app);
1045            assert_eq!(
1046                ThemeData::lerp(&narrow, &wide, 0.5)
1047                    .navigation_rail_theme
1048                    .min_width,
1049                Some(80.0)
1050            );
1051        });
1052    }
1053
1054    #[test]
1055    fn update_should_notify_only_when_the_data_differs() {
1056        let theme = |data: NavigationRailThemeData| {
1057            NavigationRailTheme::new(data, waft_widgets::SizedBox::shrink())
1058        };
1059        let old = theme(NavigationRailThemeData::new().min_width(1.0));
1060
1061        assert!(!theme(NavigationRailThemeData::new().min_width(1.0)).update_should_notify(&old));
1062        assert!(theme(NavigationRailThemeData::new().min_width(2.0)).update_should_notify(&old));
1063    }
1064    // Flutter's "Default NavigationRailThemeData debugFillProperties".
1065    #[test]
1066    fn a_navigation_rail_theme_data_describes_nothing_by_default() {
1067        assert_eq!(
1068            waft_widgets::testing::describe_properties(|properties| {
1069                waft_foundation::Diagnosticable::debug_fill_properties(
1070                    &NavigationRailThemeData::new(),
1071                    properties,
1072                )
1073            }),
1074            Vec::<String>::new()
1075        );
1076    }
1077
1078    // navigation_rail_theme_test.dart 'Custom debugFillProperties'.
1079    #[test]
1080    fn an_assigned_navigation_rail_theme_describes_every_property() {
1081        let theme = NavigationRailThemeData::new()
1082            .background_color(Color::new(0x00000099))
1083            .elevation(5.0)
1084            .selected_icon_theme(IconThemeData::new().color(Color::new(0x00000098)))
1085            .unselected_icon_theme(IconThemeData::new().color(Color::new(0x00000097)))
1086            .selected_label_text_style(TextStyle::new().font_size(9.0))
1087            .unselected_label_text_style(TextStyle::new().font_size(7.0))
1088            .group_alignment(1.0)
1089            .label_type(NavigationRailLabelType::Selected)
1090            .use_indicator(true)
1091            .indicator_color(Color::new(0x00000096))
1092            .indicator_shape(CircleBorder::default());
1093        let description = waft_widgets::testing::describe_properties(|properties| {
1094            waft_foundation::Diagnosticable::debug_fill_properties(&theme, properties)
1095        });
1096        let description: Vec<String> = description
1097            .iter()
1098            .map(|line| waft_rendering::testing::ignoring_hash_codes(line))
1099            .collect();
1100        assert_eq!(
1101            description,
1102            [
1103                format!("backgroundColor: {}", Color::new(0x00000099)),
1104                "elevation: 5.0".to_string(),
1105                "unselectedLabelTextStyle: TextStyle(inherit: true, size: 7.0)".to_string(),
1106                "selectedLabelTextStyle: TextStyle(inherit: true, size: 9.0)".to_string(),
1107                format!(
1108                    "unselectedIconTheme: IconThemeData#00000(color: {})",
1109                    Color::new(0x00000097)
1110                ),
1111                format!(
1112                    "selectedIconTheme: IconThemeData#00000(color: {})",
1113                    Color::new(0x00000098)
1114                ),
1115                "groupAlignment: 1.0".to_string(),
1116                "labelType: NavigationRailLabelType.selected".to_string(),
1117                "useIndicator: true".to_string(),
1118                format!("indicatorColor: {}", Color::new(0x00000096)),
1119                format!(
1120                    "indicatorShape: {}",
1121                    ShapeBorderRef::from(CircleBorder::default())
1122                ),
1123            ]
1124        );
1125    }
1126}