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