Skip to main content

waft_widgets/widgets/
bottom_navigation_bar_item.rs

1//! Flutter counterpart: `widgets/bottom_navigation_bar_item.dart`.
2
3use waft_painting::AnyColor;
4
5use crate::framework::{IntoWidget, KeyRef, WidgetRef};
6
7/// An interactive button within either material's `BottomNavigationBar` or the iOS themed
8/// `CupertinoTabBar` with an icon and title.
9///
10/// This class is rarely used in isolation. It is typically embedded in one of the bottom
11/// navigation widgets above.
12///
13/// See also:
14///
15///  * `BottomNavigationBar`
16///  * <https://material.io/design/components/bottom-navigation.html>
17///  * `CupertinoTabBar`
18///  * <https://developer.apple.com/design/human-interface-guidelines/tab-bars/>
19#[derive(Clone, Debug)]
20pub struct BottomNavigationBarItem {
21    /// A key to be passed through to the resultant widget.
22    ///
23    /// This allows the identification of different [`BottomNavigationBarItem`]s through their
24    /// keys.
25    ///
26    /// When changing the number of bar items in response to a bar item being tapped, giving
27    /// each item a key will allow the inkwell / splash animation to be correctly positioned.
28    pub key: Option<KeyRef>,
29
30    /// The icon of the item.
31    ///
32    /// Typically the icon is an `Icon` or an `ImageIcon` widget. If another type of widget is
33    /// provided then it should configure itself to match the current `IconTheme` size and
34    /// color.
35    ///
36    /// If [`active_icon`](Self::active_icon) is provided, this will only be displayed when the
37    /// item is not selected.
38    ///
39    /// To make the bottom navigation bar more accessible, consider choosing an icon with a
40    /// stroked and filled version, such as `Icons.cloud` and `Icons.cloud_queue`.
41    /// [`icon`](Self::icon) should be set to the stroked version and
42    /// [`active_icon`](Self::active_icon) to the filled version.
43    ///
44    /// If a particular icon doesn't have a stroked or filled version, then don't pair
45    /// unrelated icons. Instead, make sure to use a `BottomNavigationBarType.shifting`.
46    pub icon: WidgetRef,
47
48    /// An alternative icon displayed when this bottom navigation item is selected.
49    ///
50    /// If this icon is not provided, the bottom navigation bar will display
51    /// [`icon`](Self::icon) in either state.
52    pub active_icon: WidgetRef,
53
54    /// The text label for this [`BottomNavigationBarItem`].
55    ///
56    /// This will be used to create a `Text` widget to put in the bottom navigation bar.
57    pub label: Option<String>,
58
59    /// The color of the background radial animation for material `BottomNavigationBar`.
60    ///
61    /// If the navigation bar's type is `BottomNavigationBarType.shifting`, then the entire bar
62    /// is flooded with the [`background_color`](Self::background_color) when this item is
63    /// tapped. This will override `BottomNavigationBar.backgroundColor`.
64    ///
65    /// Not used for `CupertinoTabBar`. Control the invariant bar color directly via
66    /// `CupertinoTabBar.backgroundColor`.
67    pub background_color: Option<AnyColor>,
68
69    /// The text to display in the `Tooltip` for this [`BottomNavigationBarItem`].
70    ///
71    /// A `Tooltip` will only appear on this item if [`tooltip`](Self::tooltip) is set to a
72    /// non-empty string.
73    ///
74    /// Defaults to `None`, in which case the tooltip is not shown.
75    pub tooltip: Option<String>,
76
77    /// The semantic label for this [`BottomNavigationBarItem`].
78    ///
79    /// This is used by accessibility tools to describe the item. When provided,
80    /// it overrides the default [`label`](Self::label) String when read by accessibility
81    /// tools.
82    ///
83    /// This is useful when the visual label does not fully describe the action
84    /// or destination, or when you want to provide additional context for
85    /// screen reader users.
86    ///
87    /// If null, the default semantic description is used.
88    pub semantics_label: Option<String>,
89}
90
91impl BottomNavigationBarItem {
92    /// Creates an item that is used with `BottomNavigationBar.items`; Dart's optional named
93    /// arguments are the setters.
94    ///
95    /// The argument `icon` should not be `None` and the argument `label` should not be `None`
96    /// when used in a Material Design's `BottomNavigationBar`.
97    #[cfg_attr(debug_assertions, track_caller)]
98    pub fn new<K>(icon: impl IntoWidget<K>) -> BottomNavigationBarItem {
99        let icon = icon.into_widget();
100        BottomNavigationBarItem {
101            key: None,
102            active_icon: icon.clone(),
103            icon,
104            label: None,
105            background_color: None,
106            tooltip: None,
107            semantics_label: None,
108        }
109    }
110
111    /// Dart `BottomNavigationBarItem(key:)`.
112    pub fn key(mut self, key: KeyRef) -> BottomNavigationBarItem {
113        self.key = Some(key);
114        self
115    }
116
117    /// Dart `BottomNavigationBarItem(label:)`.
118    pub fn label(mut self, label: impl Into<String>) -> BottomNavigationBarItem {
119        self.label = Some(label.into());
120        self
121    }
122
123    /// Dart `BottomNavigationBarItem(activeIcon:)`.
124    #[cfg_attr(debug_assertions, track_caller)]
125    pub fn active_icon<K>(mut self, active_icon: impl IntoWidget<K>) -> BottomNavigationBarItem {
126        self.active_icon = active_icon.into_widget();
127        self
128    }
129
130    /// Dart `BottomNavigationBarItem(backgroundColor:)`.
131    pub fn background_color(
132        mut self,
133        background_color: impl Into<AnyColor>,
134    ) -> BottomNavigationBarItem {
135        self.background_color = Some(background_color.into());
136        self
137    }
138
139    /// Dart `BottomNavigationBarItem(tooltip:)`.
140    pub fn tooltip(mut self, tooltip: impl Into<String>) -> BottomNavigationBarItem {
141        self.tooltip = Some(tooltip.into());
142        self
143    }
144
145    /// Dart `BottomNavigationBarItem(semanticsLabel:)`.
146    pub fn semantics_label(
147        mut self,
148        semantics_label: impl Into<String>,
149    ) -> BottomNavigationBarItem {
150        self.semantics_label = Some(semantics_label.into());
151        self
152    }
153}
154
155#[cfg(test)]
156mod tests {
157    use super::*;
158    use crate::framework::same_widget;
159    use crate::widgets::basic::SizedBox;
160
161    /// Without an active icon the item shows its icon in either state.
162    #[test]
163    fn the_active_icon_defaults_to_the_icon() {
164        let item = BottomNavigationBarItem::new(SizedBox::shrink());
165        assert!(same_widget(&item.icon, &item.active_icon));
166        let active = SizedBox::expand().into_widget();
167        let item = item.active_icon(active.clone());
168        assert!(same_widget(&item.active_icon, &active));
169        assert!(!same_widget(&item.icon, &item.active_icon));
170    }
171}