Skip to main content

waft_material/
drawer_header.rs

1//! Flutter counterpart: material_ui's `drawer_header.dart`.
2
3use std::rc::Rc;
4use std::time::Duration;
5
6use waft_animation::{Curve, Curves};
7use waft_foundation::App;
8use waft_painting::{Border, BoxDecoration, DecorationRef, EdgeInsets, EdgeInsetsGeometry};
9use waft_widgets::{
10    AnimatedContainer, BuildContext, Container, DefaultTextStyle, IntoWidget, KeyRef, MediaQuery,
11    Semantics, StatelessWidget, WidgetRef, debug_check_has_media_query,
12};
13
14use crate::debug::debug_check_has_material;
15use crate::divider::Divider;
16use crate::theme::Theme;
17
18const K_DRAWER_HEADER_HEIGHT: f64 = 160.0 + 1.0; // bottom edge
19
20/// The top-most region of a Material Design drawer. The header's [`child`](Self::child)
21/// widget, if any, is placed inside a `Container` whose [`decoration`](Self::decoration) can be
22/// passed as an argument, inset by the given [`padding`](Self::padding).
23///
24/// Part of the Material Design `Drawer`.
25///
26/// Requires one of its ancestors to be a `Material` widget. This condition is
27/// satisfied by putting the [`DrawerHeader`] in a `Drawer`.
28///
29/// See also:
30///
31///  * `UserAccountsDrawerHeader`, a variant of [`DrawerHeader`] that is
32///    specialized for showing user accounts.
33///  * <https://material.io/design/components/navigation-drawer.html>
34#[derive(Debug)]
35pub struct DrawerHeader {
36    pub key: Option<KeyRef>,
37
38    /// Decoration for the main drawer header `Container`; useful for applying
39    /// backgrounds.
40    ///
41    /// This decoration will extend under the system status bar.
42    ///
43    /// If this is changed, it will be animated according to [`duration`](Self::duration) and
44    /// [`curve`](Self::curve).
45    pub decoration: Option<DecorationRef>,
46
47    /// The padding by which to inset [`child`](Self::child).
48    ///
49    /// The [`DrawerHeader`] additionally offsets the child by the height of the
50    /// system status bar.
51    ///
52    /// If the child is null, the padding has no effect.
53    pub padding: EdgeInsetsGeometry,
54
55    /// The margin around the drawer header.
56    pub margin: Option<EdgeInsetsGeometry>,
57
58    /// The duration for animations of the [`decoration`](Self::decoration).
59    pub duration: Duration,
60
61    /// The curve for animations of the [`decoration`](Self::decoration).
62    pub curve: Rc<dyn Curve>,
63
64    /// A widget to be placed inside the drawer header, inset by the [`padding`](Self::padding).
65    ///
66    /// This widget will be sized to the size of the header. To position the child
67    /// precisely, consider using an `Align` or `Center` widget.
68    pub child: Option<WidgetRef>,
69}
70
71impl DrawerHeader {
72    /// Creates a Material Design drawer header; Dart's optional named arguments are the
73    /// setters.
74    ///
75    /// Requires one of its ancestors to be a `Material` widget.
76    pub fn new(child: Option<WidgetRef>) -> DrawerHeader {
77        DrawerHeader {
78            key: None,
79            decoration: None,
80            padding: EdgeInsetsGeometry::Insets(EdgeInsets::from_ltrb(16.0, 16.0, 16.0, 8.0)),
81            margin: Some(EdgeInsetsGeometry::Insets(EdgeInsets::only(
82                0.0, 0.0, 0.0, 8.0,
83            ))),
84            duration: Duration::from_millis(250),
85            curve: Curves::fast_out_slow_in(),
86            child,
87        }
88    }
89
90    /// Dart `DrawerHeader(key:)`.
91    pub fn key(mut self, key: KeyRef) -> DrawerHeader {
92        self.key = Some(key);
93        self
94    }
95
96    /// Dart `DrawerHeader(decoration:)`.
97    pub fn decoration(mut self, decoration: impl Into<DecorationRef>) -> DrawerHeader {
98        self.decoration = Some(decoration.into());
99        self
100    }
101
102    /// Dart `DrawerHeader(padding:)`.
103    pub fn padding(mut self, padding: impl Into<EdgeInsetsGeometry>) -> DrawerHeader {
104        let padding = padding.into();
105        self.padding = padding;
106        self
107    }
108
109    /// Dart `DrawerHeader(margin:)`.
110    pub fn margin(mut self, margin: Option<EdgeInsetsGeometry>) -> DrawerHeader {
111        self.margin = margin;
112        self
113    }
114
115    /// Dart `DrawerHeader(duration:)`.
116    pub fn duration(mut self, duration: Duration) -> DrawerHeader {
117        self.duration = duration;
118        self
119    }
120
121    /// Dart `DrawerHeader(curve:)`.
122    pub fn curve(mut self, curve: Rc<dyn Curve>) -> DrawerHeader {
123        self.curve = curve;
124        self
125    }
126}
127
128impl StatelessWidget for DrawerHeader {
129    fn key(&self) -> Option<&KeyRef> {
130        self.key.as_ref()
131    }
132
133    fn build(&self, app: &mut App, context: BuildContext) -> WidgetRef {
134        debug_assert!(debug_check_has_material(app, context));
135        debug_assert!(debug_check_has_media_query(app, context));
136        let theme = Theme::of(app, context);
137        let status_bar_height = MediaQuery::padding_of(app, context).top;
138        let mut container = Container::new()
139            .height(status_bar_height + K_DRAWER_HEADER_HEIGHT)
140            .decoration(BoxDecoration::new().border(Border {
141                bottom: Divider::create_border_side(app, Some(context), None, None),
142                ..Border::default()
143            }));
144        if let Some(margin) = self.margin {
145            container = container.margin(margin);
146        }
147
148        let mut animated_container = AnimatedContainer::new(self.duration)
149            .padding(
150                self.padding
151                    .add(EdgeInsetsGeometry::Insets(EdgeInsets::only(
152                        0.0,
153                        status_bar_height,
154                        0.0,
155                        0.0,
156                    ))),
157            )
158            .curve(Rc::clone(&self.curve));
159        animated_container.decoration = self.decoration.clone();
160        if let Some(child) = &self.child {
161            animated_container = animated_container.child(DefaultTextStyle::new(
162                theme
163                    .text_theme
164                    .body_large
165                    .clone()
166                    .expect("the text theme has a body large style"),
167                MediaQuery::remove_padding(app, context, false, true, false, false, child.clone())
168                    .into_widget(),
169            ));
170        }
171        Semantics::new()
172            .header(true)
173            .child(container.child(animated_container))
174            .into_widget()
175    }
176}