Skip to main content

waft_material/
list_tile_theme.rs

1//! Flutter counterpart: material_ui's `list_tile_theme.dart`.
2
3use waft_embedder::{Color, lerp_double};
4use waft_foundation::{App, DiagnosticsProperty, DoubleProperty, EnumProperty};
5use waft_painting::{AnyColor, ColorProperty, EdgeInsetsGeometry, ShapeBorderRef, TextStyle};
6use waft_services::MouseCursorRef;
7use waft_widgets::{
8    BuildContext, Builder, InheritedTheme, InheritedWidget, IntoWidget, KeyRef, WidgetRef,
9    WidgetStatePropertyRef,
10};
11
12use crate::list_tile::{ListTileControlAffinity, ListTileStyle, ListTileTitleAlignment};
13use crate::theme::Theme;
14use crate::theme_data::VisualDensity;
15
16/// Used with [`ListTileTheme`] to define default property values for
17/// descendant `ListTile` widgets, as well as classes that build
18/// `ListTile`s, like `CheckboxListTile`, `RadioListTile`, and
19/// `SwitchListTile`.
20///
21/// Descendant widgets obtain the current [`ListTileThemeData`] object
22/// using [`ListTileTheme::of`]. Instances of [`ListTileThemeData`] can be
23/// customized with [`ListTileThemeData::copy_with`].
24///
25/// A [`ListTileThemeData`] is often specified as part of the
26/// overall `Theme` with `ThemeData::list_tile_theme`.
27///
28/// All [`ListTileThemeData`] properties are `None` by default.
29/// When a theme property is `None`, the `ListTile` will provide its own
30/// default based on the overall `Theme`'s text theme and
31/// color scheme. See the individual `ListTile` properties for details.
32///
33/// The `Drawer` widget specifies a list tile theme for its children that
34/// defines [`style`](Self::style) to be [`ListTileStyle::Drawer`].
35///
36/// See also:
37///
38///  * `ThemeData`, which describes the overall theme information for the
39///    application.
40#[derive(Clone, Debug, Default)]
41pub struct ListTileThemeData {
42    /// Overrides the default value of `ListTile::dense`.
43    pub dense: Option<bool>,
44
45    /// Overrides the default value of `ListTile::shape`.
46    pub shape: Option<ShapeBorderRef>,
47
48    /// Overrides the default value of `ListTile::style`.
49    pub style: Option<ListTileStyle>,
50
51    /// Overrides the default value of `ListTile::selected_color`.
52    pub selected_color: Option<Color>,
53
54    /// Overrides the default value of `ListTile::icon_color`.
55    pub icon_color: Option<AnyColor>,
56
57    /// Overrides the default value of `ListTile::text_color`.
58    pub text_color: Option<AnyColor>,
59
60    /// Overrides the default value of `ListTile::title_text_style`.
61    pub title_text_style: Option<TextStyle>,
62
63    /// Overrides the default value of `ListTile::subtitle_text_style`.
64    pub subtitle_text_style: Option<TextStyle>,
65
66    /// Overrides the default value of `ListTile::leading_and_trailing_text_style`.
67    pub leading_and_trailing_text_style: Option<TextStyle>,
68
69    /// Overrides the default value of `ListTile::content_padding`.
70    pub content_padding: Option<EdgeInsetsGeometry>,
71
72    /// Overrides the default value of `ListTile::tile_color`.
73    pub tile_color: Option<Color>,
74
75    /// Overrides the default value of `ListTile::selected_tile_color`.
76    pub selected_tile_color: Option<Color>,
77
78    /// Overrides the default value of `ListTile::horizontal_title_gap`.
79    pub horizontal_title_gap: Option<f64>,
80
81    /// Overrides the default value of `ListTile::min_vertical_padding`.
82    pub min_vertical_padding: Option<f64>,
83
84    /// Overrides the default value of `ListTile::min_leading_width`.
85    pub min_leading_width: Option<f64>,
86
87    /// Overrides the default value of `ListTile::min_tile_height`.
88    pub min_tile_height: Option<f64>,
89
90    /// Overrides the default value of `ListTile::enable_feedback`.
91    pub enable_feedback: Option<bool>,
92
93    /// If specified, overrides the default value of `ListTile::mouse_cursor`.
94    pub mouse_cursor: Option<WidgetStatePropertyRef<Option<MouseCursorRef>>>,
95
96    /// If specified, overrides the default value of `ListTile::visual_density`.
97    pub visual_density: Option<VisualDensity>,
98
99    /// If specified, overrides the default value of `ListTile::title_alignment`.
100    pub title_alignment: Option<ListTileTitleAlignment>,
101
102    /// If specified, overrides the default value of `CheckboxListTile::control_affinity`
103    /// or `ExpansionTile::control_affinity` or `SwitchListTile::control_affinity` or
104    /// `RadioListTile::control_affinity`.
105    pub control_affinity: Option<ListTileControlAffinity>,
106
107    /// If specified, overrides the default value of `ListTile::is_three_line`
108    /// or `CheckboxListTile::is_three_line` or `RadioListTile::is_three_line` or
109    /// `SwitchListTile::is_three_line`.
110    pub is_three_line: Option<bool>,
111}
112
113crate::theme_data_getters::theme_data_getters! {
114    /// Dart's getters of `ListTileThemeData`: the theme data answers with its fields, and a defaults
115    /// subclass (Dart's `_...Defaults...(context)`) computes the ones it overrides when they
116    /// are read.
117    pub trait ListTileThemeDataGetters for ListTileThemeData, fields: list_tile_theme_data {
118        dense: Option<bool>,
119        shape: Option<ShapeBorderRef>,
120        style: Option<ListTileStyle>,
121        selected_color: Option<Color>,
122        icon_color: Option<AnyColor>,
123        text_color: Option<AnyColor>,
124        title_text_style: Option<TextStyle>,
125        subtitle_text_style: Option<TextStyle>,
126        leading_and_trailing_text_style: Option<TextStyle>,
127        content_padding: Option<EdgeInsetsGeometry>,
128        tile_color: Option<Color>,
129        selected_tile_color: Option<Color>,
130        horizontal_title_gap: Option<f64>,
131        min_vertical_padding: Option<f64>,
132        min_leading_width: Option<f64>,
133        min_tile_height: Option<f64>,
134        enable_feedback: Option<bool>,
135        mouse_cursor: Option<WidgetStatePropertyRef<Option<MouseCursorRef>>>,
136        visual_density: Option<VisualDensity>,
137        title_alignment: Option<ListTileTitleAlignment>,
138        control_affinity: Option<ListTileControlAffinity>,
139        is_three_line: Option<bool>,
140    }
141}
142
143/// The setters of the arguments Dart's `ListTileThemeData` constructor and
144/// `ListTileTheme.merge` share, each writing its field of `$data`.
145macro_rules! list_tile_theme_data_setters {
146    ($owner:ident, $dart:literal, |$this:ident| $data:expr, $($field:ident: $ty:ty => $name:literal),* $(,)?) => {
147        $(
148            #[doc = concat!("Dart `", $dart, "(", $name, ":)`.")]
149            pub fn $field(mut self, $field: impl Into<$ty>) -> $owner {
150                let $this = &mut self;
151                $data.$field = Some($field.into());
152                self
153            }
154        )*
155    };
156}
157
158/// Every setter `ListTileThemeData` and `ListTileTheme::merge` share.
159macro_rules! every_list_tile_theme_data_setter {
160    ($owner:ident, $dart:literal, |$this:ident| $data:expr) => {
161        list_tile_theme_data_setters!(
162            $owner,
163            $dart,
164            |$this| $data,
165            dense: bool => "dense",
166            shape: ShapeBorderRef => "shape",
167            style: ListTileStyle => "style",
168            selected_color: Color => "selectedColor",
169            icon_color: AnyColor => "iconColor",
170            text_color: AnyColor => "textColor",
171            title_text_style: TextStyle => "titleTextStyle",
172            subtitle_text_style: TextStyle => "subtitleTextStyle",
173            leading_and_trailing_text_style: TextStyle => "leadingAndTrailingTextStyle",
174            content_padding: EdgeInsetsGeometry => "contentPadding",
175            tile_color: Color => "tileColor",
176            selected_tile_color: Color => "selectedTileColor",
177            horizontal_title_gap: f64 => "horizontalTitleGap",
178            min_vertical_padding: f64 => "minVerticalPadding",
179            min_leading_width: f64 => "minLeadingWidth",
180            min_tile_height: f64 => "minTileHeight",
181            enable_feedback: bool => "enableFeedback",
182            mouse_cursor: WidgetStatePropertyRef<Option<MouseCursorRef>> => "mouseCursor",
183            visual_density: VisualDensity => "visualDensity",
184            title_alignment: ListTileTitleAlignment => "titleAlignment",
185            control_affinity: ListTileControlAffinity => "controlAffinity",
186            is_three_line: bool => "isThreeLine",
187        );
188    };
189}
190
191impl ListTileThemeData {
192    /// Creates a [`ListTileThemeData`]; Dart's optional named arguments are the fluent
193    /// setters.
194    pub fn new() -> ListTileThemeData {
195        ListTileThemeData::default()
196    }
197
198    every_list_tile_theme_data_setter!(ListTileThemeData, "ListTileThemeData", |this| this);
199
200    /// Creates a copy of this object with the given fields replaced with the
201    /// new values.
202    ///
203    /// Dart's named arguments are the fluent setters on the copy
204    /// (`data.copy_with().dense(true)`).
205    pub fn copy_with(&self) -> ListTileThemeData {
206        self.clone()
207    }
208
209    /// Linearly interpolate between ListTileThemeData objects.
210    pub fn lerp(
211        a: Option<&ListTileThemeData>,
212        b: Option<&ListTileThemeData>,
213        t: f64,
214    ) -> Option<ListTileThemeData> {
215        match (a, b) {
216            (None, None) => return None,
217            (Some(a), Some(b)) if std::ptr::eq(a, b) => return Some(a.clone()),
218            _ => {}
219        }
220        Some(ListTileThemeData {
221            dense: pick(a, b, t, |theme| theme.dense),
222            shape: ShapeBorderRef::lerp(
223                a.and_then(|a| a.shape.as_ref()),
224                b.and_then(|b| b.shape.as_ref()),
225                t,
226            ),
227            style: pick(a, b, t, |theme| theme.style),
228            selected_color: Color::lerp(
229                a.and_then(|a| a.selected_color),
230                b.and_then(|b| b.selected_color),
231                t,
232            ),
233            icon_color: AnyColor::lerp(
234                a.and_then(|a| a.icon_color.as_ref()),
235                b.and_then(|b| b.icon_color.as_ref()),
236                t,
237            )
238            .map(AnyColor::new),
239            text_color: AnyColor::lerp(
240                a.and_then(|a| a.text_color.as_ref()),
241                b.and_then(|b| b.text_color.as_ref()),
242                t,
243            )
244            .map(AnyColor::new),
245            title_text_style: TextStyle::lerp(
246                a.and_then(|a| a.title_text_style.as_ref()),
247                b.and_then(|b| b.title_text_style.as_ref()),
248                t,
249            ),
250            subtitle_text_style: TextStyle::lerp(
251                a.and_then(|a| a.subtitle_text_style.as_ref()),
252                b.and_then(|b| b.subtitle_text_style.as_ref()),
253                t,
254            ),
255            leading_and_trailing_text_style: TextStyle::lerp(
256                a.and_then(|a| a.leading_and_trailing_text_style.as_ref()),
257                b.and_then(|b| b.leading_and_trailing_text_style.as_ref()),
258                t,
259            ),
260            content_padding: EdgeInsetsGeometry::lerp(
261                a.and_then(|a| a.content_padding),
262                b.and_then(|b| b.content_padding),
263                t,
264            ),
265            tile_color: Color::lerp(
266                a.and_then(|a| a.tile_color),
267                b.and_then(|b| b.tile_color),
268                t,
269            ),
270            selected_tile_color: Color::lerp(
271                a.and_then(|a| a.selected_tile_color),
272                b.and_then(|b| b.selected_tile_color),
273                t,
274            ),
275            horizontal_title_gap: lerp_double(
276                a.and_then(|a| a.horizontal_title_gap),
277                b.and_then(|b| b.horizontal_title_gap),
278                t,
279            ),
280            min_vertical_padding: lerp_double(
281                a.and_then(|a| a.min_vertical_padding),
282                b.and_then(|b| b.min_vertical_padding),
283                t,
284            ),
285            min_leading_width: lerp_double(
286                a.and_then(|a| a.min_leading_width),
287                b.and_then(|b| b.min_leading_width),
288                t,
289            ),
290            min_tile_height: lerp_double(
291                a.and_then(|a| a.min_tile_height),
292                b.and_then(|b| b.min_tile_height),
293                t,
294            ),
295            enable_feedback: pick(a, b, t, |theme| theme.enable_feedback),
296            mouse_cursor: pick(a, b, t, |theme| theme.mouse_cursor.clone()),
297            visual_density: pick(a, b, t, |theme| theme.visual_density),
298            title_alignment: pick(a, b, t, |theme| theme.title_alignment),
299            control_affinity: pick(a, b, t, |theme| theme.control_affinity),
300            is_three_line: pick(a, b, t, |theme| theme.is_three_line),
301        })
302    }
303}
304
305/// Dart's `t < 0.5 ? a?.field : b?.field`.
306fn pick<T>(
307    a: Option<&ListTileThemeData>,
308    b: Option<&ListTileThemeData>,
309    t: f64,
310    field: impl Fn(&ListTileThemeData) -> Option<T>,
311) -> Option<T> {
312    if t < 0.5 {
313        a.and_then(&field)
314    } else {
315        b.and_then(&field)
316    }
317}
318
319impl PartialEq for ListTileThemeData {
320    fn eq(&self, other: &ListTileThemeData) -> bool {
321        if std::ptr::eq(self, other) {
322            return true;
323        }
324        other.dense == self.dense
325            && other.shape == self.shape
326            && other.style == self.style
327            && other.selected_color == self.selected_color
328            && other.icon_color == self.icon_color
329            && other.title_text_style == self.title_text_style
330            && other.subtitle_text_style == self.subtitle_text_style
331            && other.leading_and_trailing_text_style == self.leading_and_trailing_text_style
332            && other.text_color == self.text_color
333            && other.content_padding == self.content_padding
334            && other.tile_color == self.tile_color
335            && other.selected_tile_color == self.selected_tile_color
336            && other.horizontal_title_gap == self.horizontal_title_gap
337            && other.min_vertical_padding == self.min_vertical_padding
338            && other.min_leading_width == self.min_leading_width
339            && other.min_tile_height == self.min_tile_height
340            && other.enable_feedback == self.enable_feedback
341            && other.mouse_cursor == self.mouse_cursor
342            && other.visual_density == self.visual_density
343            && other.title_alignment == self.title_alignment
344            && other.control_affinity == self.control_affinity
345            && other.is_three_line == self.is_three_line
346    }
347}
348
349/// An inherited widget that defines color and style parameters for `ListTile`s
350/// in this widget's subtree.
351///
352/// Values specified here are used for `ListTile` properties that are not given
353/// an explicit non-null value.
354///
355/// The `Drawer` widget specifies a tile theme for its children which sets
356/// `style` to [`ListTileStyle::Drawer`].
357///
358/// The fields other than [`data`](Self::data) are Dart's obsolete constructor arguments,
359/// read through the `effective_*` methods that stand for Dart's getters of the same names.
360#[derive(Debug)]
361pub struct ListTileTheme {
362    pub key: Option<KeyRef>,
363    /// Dart's `_data`: the configuration given to the constructor.
364    pub data: Option<ListTileThemeData>,
365    pub dense: Option<bool>,
366    pub shape: Option<ShapeBorderRef>,
367    pub style: Option<ListTileStyle>,
368    pub selected_color: Option<Color>,
369    pub icon_color: Option<AnyColor>,
370    pub text_color: Option<AnyColor>,
371    pub content_padding: Option<EdgeInsetsGeometry>,
372    pub tile_color: Option<Color>,
373    pub selected_tile_color: Option<Color>,
374    pub enable_feedback: Option<bool>,
375    pub mouse_cursor: Option<WidgetStatePropertyRef<Option<MouseCursorRef>>>,
376    pub horizontal_title_gap: Option<f64>,
377    pub min_vertical_padding: Option<f64>,
378    pub min_leading_width: Option<f64>,
379    pub control_affinity: Option<ListTileControlAffinity>,
380    pub child: WidgetRef,
381}
382
383/// The setters of `ListTileTheme`'s obsolete constructor arguments, each of which Dart's
384/// constructor asserts is not given with `data`.
385macro_rules! obsolete_list_tile_theme_setters {
386    ($($field:ident: $ty:ty => $name:literal),* $(,)?) => {
387        $(
388            #[doc = concat!("Dart `ListTileTheme(", $name, ":)`.")]
389            pub fn $field(mut self, $field: impl Into<$ty>) -> ListTileTheme {
390                debug_assert!(self.data.is_none());
391                self.$field = Some($field.into());
392                self
393            }
394        )*
395    };
396}
397
398impl ListTileTheme {
399    /// Creates a list tile theme that defines the color and style parameters for
400    /// descendant `ListTile`s.
401    ///
402    /// Only the [`data`](Self::data) parameter should be used. The other parameters are
403    /// redundant (are now obsolete) and will be deprecated in a future update.
404    #[cfg_attr(debug_assertions, track_caller)]
405    pub fn new<K>(child: impl IntoWidget<K>) -> ListTileTheme {
406        ListTileTheme {
407            key: None,
408            data: None,
409            dense: None,
410            shape: None,
411            style: None,
412            selected_color: None,
413            icon_color: None,
414            text_color: None,
415            content_padding: None,
416            tile_color: None,
417            selected_tile_color: None,
418            enable_feedback: None,
419            mouse_cursor: None,
420            horizontal_title_gap: None,
421            min_vertical_padding: None,
422            min_leading_width: None,
423            control_affinity: None,
424            child: child.into_widget(),
425        }
426    }
427
428    /// Dart `ListTileTheme(key:)`.
429    pub fn key(mut self, key: KeyRef) -> ListTileTheme {
430        self.key = Some(key);
431        self
432    }
433
434    /// Dart `ListTileTheme(data:)`.
435    pub fn data(mut self, data: ListTileThemeData) -> ListTileTheme {
436        debug_assert!(
437            self.shape.is_none()
438                && self.selected_color.is_none()
439                && self.icon_color.is_none()
440                && self.text_color.is_none()
441                && self.content_padding.is_none()
442                && self.tile_color.is_none()
443                && self.selected_tile_color.is_none()
444                && self.enable_feedback.is_none()
445                && self.mouse_cursor.is_none()
446                && self.horizontal_title_gap.is_none()
447                && self.min_vertical_padding.is_none()
448                && self.min_leading_width.is_none()
449                && self.control_affinity.is_none()
450        );
451        self.data = Some(data);
452        self
453    }
454
455    /// Dart `ListTileTheme(dense:)`.
456    pub fn dense(mut self, dense: bool) -> ListTileTheme {
457        self.dense = Some(dense);
458        self
459    }
460
461    /// Dart `ListTileTheme(style:)`.
462    pub fn style(mut self, style: ListTileStyle) -> ListTileTheme {
463        self.style = Some(style);
464        self
465    }
466
467    obsolete_list_tile_theme_setters!(
468        shape: ShapeBorderRef => "shape",
469        selected_color: Color => "selectedColor",
470        icon_color: AnyColor => "iconColor",
471        text_color: AnyColor => "textColor",
472        content_padding: EdgeInsetsGeometry => "contentPadding",
473        tile_color: Color => "tileColor",
474        selected_tile_color: Color => "selectedTileColor",
475        enable_feedback: bool => "enableFeedback",
476        mouse_cursor: WidgetStatePropertyRef<Option<MouseCursorRef>> => "mouseCursor",
477        horizontal_title_gap: f64 => "horizontalTitleGap",
478        min_vertical_padding: f64 => "minVerticalPadding",
479        min_leading_width: f64 => "minLeadingWidth",
480        control_affinity: ListTileControlAffinity => "controlAffinity",
481    );
482
483    /// The configuration of this theme: Dart's `data` getter.
484    pub fn effective_data(&self) -> ListTileThemeData {
485        match &self.data {
486            Some(data) => data.clone(),
487            None => ListTileThemeData {
488                dense: self.dense,
489                shape: self.shape.clone(),
490                style: self.style,
491                selected_color: self.selected_color,
492                icon_color: self.icon_color.clone(),
493                text_color: self.text_color.clone(),
494                content_padding: self.content_padding,
495                tile_color: self.tile_color,
496                selected_tile_color: self.selected_tile_color,
497                enable_feedback: self.enable_feedback,
498                mouse_cursor: self.mouse_cursor.clone(),
499                horizontal_title_gap: self.horizontal_title_gap,
500                min_vertical_padding: self.min_vertical_padding,
501                min_leading_width: self.min_leading_width,
502                control_affinity: self.control_affinity,
503                ..ListTileThemeData::default()
504            },
505        }
506    }
507
508    /// Overrides the default value of `ListTile::dense`.
509    ///
510    /// This property is obsolete: please use the [`data`](Self::data)
511    /// [`ListTileThemeData::dense`] property instead.
512    pub fn effective_dense(&self) -> Option<bool> {
513        match &self.data {
514            Some(data) => data.dense,
515            None => self.dense,
516        }
517    }
518
519    /// Overrides the default value of `ListTile::shape`.
520    ///
521    /// This property is obsolete: please use the [`data`](Self::data)
522    /// [`ListTileThemeData::shape`] property instead.
523    pub fn effective_shape(&self) -> Option<ShapeBorderRef> {
524        match &self.data {
525            Some(data) => data.shape.clone(),
526            None => self.shape.clone(),
527        }
528    }
529
530    /// Overrides the default value of `ListTile::style`.
531    ///
532    /// This property is obsolete: please use the [`data`](Self::data)
533    /// [`ListTileThemeData::style`] property instead.
534    pub fn effective_style(&self) -> Option<ListTileStyle> {
535        match &self.data {
536            Some(data) => data.style,
537            None => self.style,
538        }
539    }
540
541    /// Overrides the default value of `ListTile::selected_color`.
542    ///
543    /// This property is obsolete: please use the [`data`](Self::data)
544    /// [`ListTileThemeData::selected_color`] property instead.
545    pub fn effective_selected_color(&self) -> Option<Color> {
546        match &self.data {
547            Some(data) => data.selected_color,
548            None => self.selected_color,
549        }
550    }
551
552    /// Overrides the default value of `ListTile::icon_color`.
553    ///
554    /// This property is obsolete: please use the [`data`](Self::data)
555    /// [`ListTileThemeData::icon_color`] property instead.
556    pub fn effective_icon_color(&self) -> Option<AnyColor> {
557        match &self.data {
558            Some(data) => data.icon_color.clone(),
559            None => self.icon_color.clone(),
560        }
561    }
562
563    /// Overrides the default value of `ListTile::text_color`.
564    ///
565    /// This property is obsolete: please use the [`data`](Self::data)
566    /// [`ListTileThemeData::text_color`] property instead.
567    pub fn effective_text_color(&self) -> Option<AnyColor> {
568        match &self.data {
569            Some(data) => data.text_color.clone(),
570            None => self.text_color.clone(),
571        }
572    }
573
574    /// Overrides the default value of `ListTile::content_padding`.
575    ///
576    /// This property is obsolete: please use the [`data`](Self::data)
577    /// [`ListTileThemeData::content_padding`] property instead.
578    pub fn effective_content_padding(&self) -> Option<EdgeInsetsGeometry> {
579        match &self.data {
580            Some(data) => data.content_padding,
581            None => self.content_padding,
582        }
583    }
584
585    /// Overrides the default value of `ListTile::tile_color`.
586    ///
587    /// This property is obsolete: please use the [`data`](Self::data)
588    /// [`ListTileThemeData::tile_color`] property instead.
589    pub fn effective_tile_color(&self) -> Option<Color> {
590        match &self.data {
591            Some(data) => data.tile_color,
592            None => self.tile_color,
593        }
594    }
595
596    /// Overrides the default value of `ListTile::selected_tile_color`.
597    ///
598    /// This property is obsolete: please use the [`data`](Self::data)
599    /// [`ListTileThemeData::selected_tile_color`] property instead.
600    pub fn effective_selected_tile_color(&self) -> Option<Color> {
601        match &self.data {
602            Some(data) => data.selected_tile_color,
603            None => self.selected_tile_color,
604        }
605    }
606
607    /// Overrides the default value of `ListTile::horizontal_title_gap`.
608    ///
609    /// This property is obsolete: please use the [`data`](Self::data)
610    /// [`ListTileThemeData::horizontal_title_gap`] property instead.
611    pub fn effective_horizontal_title_gap(&self) -> Option<f64> {
612        match &self.data {
613            Some(data) => data.horizontal_title_gap,
614            None => self.horizontal_title_gap,
615        }
616    }
617
618    /// Overrides the default value of `ListTile::min_vertical_padding`.
619    ///
620    /// This property is obsolete: please use the [`data`](Self::data)
621    /// [`ListTileThemeData::min_vertical_padding`] property instead.
622    pub fn effective_min_vertical_padding(&self) -> Option<f64> {
623        match &self.data {
624            Some(data) => data.min_vertical_padding,
625            None => self.min_vertical_padding,
626        }
627    }
628
629    /// Overrides the default value of `ListTile::min_leading_width`.
630    ///
631    /// This property is obsolete: please use the [`data`](Self::data)
632    /// [`ListTileThemeData::min_leading_width`] property instead.
633    pub fn effective_min_leading_width(&self) -> Option<f64> {
634        match &self.data {
635            Some(data) => data.min_leading_width,
636            None => self.min_leading_width,
637        }
638    }
639
640    /// Overrides the default value of `ListTile::enable_feedback`.
641    ///
642    /// This property is obsolete: please use the [`data`](Self::data)
643    /// [`ListTileThemeData::enable_feedback`] property instead.
644    pub fn effective_enable_feedback(&self) -> Option<bool> {
645        match &self.data {
646            Some(data) => data.enable_feedback,
647            None => self.enable_feedback,
648        }
649    }
650
651    /// Overrides the default value of `CheckboxListTile::control_affinity`
652    /// or `ExpansionTile::control_affinity` or `SwitchListTile::control_affinity` or
653    /// `RadioListTile::control_affinity`.
654    ///
655    /// This property is obsolete: please use the
656    /// [`ListTileThemeData::control_affinity`] property instead.
657    pub fn effective_control_affinity(&self) -> Option<ListTileControlAffinity> {
658        match &self.data {
659            Some(data) => data.control_affinity,
660            None => self.control_affinity,
661        }
662    }
663
664    /// The [`data`](Self::effective_data) property of the closest instance of this class that
665    /// encloses the given context.
666    ///
667    /// If there is no enclosing [`ListTileTheme`] widget, then
668    /// `ThemeData::list_tile_theme` is used (see `Theme::of`).
669    ///
670    /// Typical usage is as follows:
671    ///
672    /// ```text
673    /// let theme = ListTileTheme::of(app, context);
674    /// ```
675    pub fn of(app: &mut App, context: BuildContext) -> ListTileThemeData {
676        match context.depend_on_inherited_widget_of_exact_type::<ListTileTheme>(app) {
677            Some(result) => result.effective_data(),
678            None => Theme::of(app, context).list_tile_theme.clone(),
679        }
680    }
681
682    /// Creates a list tile theme that controls the color and style parameters for
683    /// `ListTile`s, and merges in the current list tile theme, if any.
684    ///
685    /// Dart's optional named arguments are the setters of the [`ListTileThemeMerge`] this
686    /// returns.
687    #[cfg_attr(debug_assertions, track_caller)]
688    pub fn merge<K>(child: impl IntoWidget<K>) -> ListTileThemeMerge {
689        ListTileThemeMerge {
690            key: None,
691            given: ListTileThemeData::new(),
692            child: child.into_widget(),
693        }
694    }
695}
696
697/// The arguments of Dart's `ListTileTheme.merge`, which becomes the widget it returns.
698pub struct ListTileThemeMerge {
699    key: Option<KeyRef>,
700    /// The arguments given, each `None` where the parent's value is kept.
701    given: ListTileThemeData,
702    child: WidgetRef,
703}
704
705impl ListTileThemeMerge {
706    /// Dart `ListTileTheme.merge(key:)`.
707    pub fn key(mut self, key: KeyRef) -> ListTileThemeMerge {
708        self.key = Some(key);
709        self
710    }
711
712    every_list_tile_theme_data_setter!(ListTileThemeMerge, "ListTileTheme.merge", |this| this
713        .given);
714}
715
716/// The kind tag of [`IntoWidget`] for a [`ListTileThemeMerge`].
717pub struct ListTileThemeMergeKind;
718
719impl IntoWidget<ListTileThemeMergeKind> for ListTileThemeMerge {
720    #[cfg_attr(debug_assertions, track_caller)]
721    fn into_widget(self) -> WidgetRef {
722        Builder::new(move |app, context| {
723            let parent = ListTileTheme::of(app, context);
724            let given = &self.given;
725            let theme = ListTileTheme::new(self.child.clone()).data(ListTileThemeData {
726                dense: given.dense.or(parent.dense),
727                shape: given.shape.clone().or(parent.shape),
728                style: given.style.or(parent.style),
729                selected_color: given.selected_color.or(parent.selected_color),
730                icon_color: given.icon_color.clone().or(parent.icon_color),
731                text_color: given.text_color.clone().or(parent.text_color),
732                title_text_style: given.title_text_style.clone().or(parent.title_text_style),
733                subtitle_text_style: given
734                    .subtitle_text_style
735                    .clone()
736                    .or(parent.subtitle_text_style),
737                leading_and_trailing_text_style: given
738                    .leading_and_trailing_text_style
739                    .clone()
740                    .or(parent.leading_and_trailing_text_style),
741                content_padding: given.content_padding.or(parent.content_padding),
742                tile_color: given.tile_color.or(parent.tile_color),
743                selected_tile_color: given.selected_tile_color.or(parent.selected_tile_color),
744                enable_feedback: given.enable_feedback.or(parent.enable_feedback),
745                horizontal_title_gap: given.horizontal_title_gap.or(parent.horizontal_title_gap),
746                min_vertical_padding: given.min_vertical_padding.or(parent.min_vertical_padding),
747                min_leading_width: given.min_leading_width.or(parent.min_leading_width),
748                min_tile_height: given.min_tile_height.or(parent.min_tile_height),
749                title_alignment: given.title_alignment.or(parent.title_alignment),
750                mouse_cursor: given.mouse_cursor.clone().or(parent.mouse_cursor),
751                visual_density: given.visual_density.or(parent.visual_density),
752                control_affinity: given.control_affinity.or(parent.control_affinity),
753                is_three_line: given.is_three_line.or(parent.is_three_line),
754            });
755            match &self.key {
756                Some(key) => theme.key(key.clone()),
757                None => theme,
758            }
759            .into_widget()
760        })
761        .into_widget()
762    }
763}
764
765impl InheritedTheme for ListTileTheme {
766    fn wrap(&self, _app: &mut App, _context: BuildContext, child: WidgetRef) -> WidgetRef {
767        ListTileTheme::new(child)
768            .data(ListTileThemeData {
769                dense: self.effective_dense(),
770                shape: self.effective_shape(),
771                style: self.effective_style(),
772                selected_color: self.effective_selected_color(),
773                icon_color: self.effective_icon_color(),
774                text_color: self.effective_text_color(),
775                content_padding: self.effective_content_padding(),
776                tile_color: self.effective_tile_color(),
777                selected_tile_color: self.effective_selected_tile_color(),
778                enable_feedback: self.effective_enable_feedback(),
779                horizontal_title_gap: self.effective_horizontal_title_gap(),
780                min_vertical_padding: self.effective_min_vertical_padding(),
781                min_leading_width: self.effective_min_leading_width(),
782                is_three_line: self.data.as_ref().and_then(|data| data.is_three_line),
783                ..ListTileThemeData::default()
784            })
785            .into_widget()
786    }
787}
788
789impl InheritedWidget for ListTileTheme {
790    fn key(&self) -> Option<&KeyRef> {
791        self.key.as_ref()
792    }
793
794    fn child(&self) -> &WidgetRef {
795        &self.child
796    }
797
798    fn update_should_notify(&self, old_widget: &ListTileTheme) -> bool {
799        self.effective_data() != old_widget.effective_data()
800    }
801
802    fn as_inherited_theme(&self) -> Option<&dyn InheritedTheme> {
803        Some(self)
804    }
805}
806
807impl waft_foundation::Diagnosticable for ListTileThemeData {
808    fn debug_fill_properties(&self, properties: &mut waft_foundation::DiagnosticPropertiesBuilder) {
809        properties.add(DiagnosticsProperty::<bool>::new("dense", self.dense).default_value(None));
810        properties.add(
811            DiagnosticsProperty::<ShapeBorderRef>::new("shape", self.shape.clone())
812                .default_value(None),
813        );
814        properties.add(EnumProperty::<ListTileStyle>::new("style", self.style).default_value(None));
815        properties
816            .add(ColorProperty::new("selectedColor", self.selected_color).default_value(None));
817        properties.add(
818            DiagnosticsProperty::<AnyColor>::new("iconColor", self.icon_color.clone())
819                .default_value(None),
820        );
821        properties.add(
822            DiagnosticsProperty::<AnyColor>::new("textColor", self.text_color.clone())
823                .default_value(None),
824        );
825        properties.add(
826            DiagnosticsProperty::<TextStyle>::new("titleTextStyle", self.title_text_style.clone())
827                .default_value(None),
828        );
829        properties.add(
830            DiagnosticsProperty::<TextStyle>::new(
831                "subtitleTextStyle",
832                self.subtitle_text_style.clone(),
833            )
834            .default_value(None),
835        );
836        properties.add(
837            DiagnosticsProperty::<TextStyle>::new(
838                "leadingAndTrailingTextStyle",
839                self.leading_and_trailing_text_style.clone(),
840            )
841            .default_value(None),
842        );
843        properties.add(
844            DiagnosticsProperty::<EdgeInsetsGeometry>::new("contentPadding", self.content_padding)
845                .default_value(None),
846        );
847        properties.add(ColorProperty::new("tileColor", self.tile_color).default_value(None));
848        properties.add(
849            ColorProperty::new("selectedTileColor", self.selected_tile_color).default_value(None),
850        );
851        properties.add(
852            DoubleProperty::new("horizontalTitleGap", self.horizontal_title_gap)
853                .default_value(None),
854        );
855        properties.add(
856            DoubleProperty::new("minVerticalPadding", self.min_vertical_padding)
857                .default_value(None),
858        );
859        properties.add(
860            DoubleProperty::new("minLeadingWidth", self.min_leading_width).default_value(None),
861        );
862        properties
863            .add(DoubleProperty::new("minTileHeight", self.min_tile_height).default_value(None));
864        properties.add(
865            DiagnosticsProperty::<bool>::new("enableFeedback", self.enable_feedback)
866                .default_value(None),
867        );
868        properties.add(
869            DiagnosticsProperty::<String>::new(
870                "mouseCursor",
871                self.mouse_cursor
872                    .as_deref()
873                    .map(|property| property.to_string()),
874            )
875            .default_value(None),
876        );
877        properties.add(
878            DiagnosticsProperty::<VisualDensity>::new("visualDensity", self.visual_density)
879                .default_value(None),
880        );
881        properties.add(
882            DiagnosticsProperty::<waft_foundation::DartEnum<ListTileTitleAlignment>>::new(
883                "titleAlignment",
884                self.title_alignment.map(waft_foundation::DartEnum),
885            )
886            .default_value(None),
887        );
888        properties.add(
889            DiagnosticsProperty::<waft_foundation::DartEnum<ListTileControlAffinity>>::new(
890                "controlAffinity",
891                self.control_affinity.map(waft_foundation::DartEnum),
892            )
893            .default_value(None),
894        );
895        properties.add(
896            DiagnosticsProperty::<bool>::new("isThreeLine", self.is_three_line).default_value(None),
897        );
898    }
899}
900
901impl std::fmt::Display for ListTileThemeData {
902    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
903        f.write_str(&waft_foundation::Diagnosticable::to_string(self))
904    }
905}
906
907#[cfg(test)]
908mod tests {
909    use std::cell::{Cell, RefCell};
910    use std::rc::Rc;
911    use std::time::Duration;
912
913    use waft_embedder::{FontStyle, Offset, Radius, Rect, Size};
914    use waft_foundation::Listener;
915    use waft_painting::{BorderRadius, EdgeInsets, RoundedRectangleBorder, StadiumBorder};
916    use waft_services::SystemMouseCursors;
917    use waft_widgets::{
918        Center, Container, GlobalKey, ListView, SizedBox, StatefulBuilder, Text, WidgetState,
919        WidgetStateColor, WidgetStateMouseCursor, WidgetStateProperty,
920    };
921
922    use waft_widgets::testing::{TestPointer, WidgetTester, find, test_widgets};
923
924    use super::*;
925    use crate::colors::Colors;
926    use crate::drawer::Drawer;
927    use crate::ink_well::InkWell;
928    use crate::list_tile::ListTile;
929    use crate::list_tile::tests::{
930        THIRTEEN_LINES, avatar, ink_tile_color, key_rect, m2, m3, placeholder, pump_app, sized,
931        style_color, test_icon, test_text, tile_rect,
932    };
933    use crate::material::Material;
934    use crate::scaffold::{Scaffold, ScaffoldState};
935    use crate::test_support::material_app;
936    use crate::theme_data::ThemeData;
937    use waft_widgets::WidgetStatePropertyAll;
938
939    /// Dart's `WidgetStateMouseCursor.clickable` as a `WidgetStateProperty<MouseCursor?>`.
940    fn clickable() -> WidgetStatePropertyRef<Option<MouseCursorRef>> {
941        <dyn WidgetStateProperty<Option<MouseCursorRef>>>::resolve_with(|states| {
942            Some(WidgetStateMouseCursor::clickable().resolve(states))
943        })
944    }
945
946    /// Dart's `WidgetStateMouseCursor.textable` as a `WidgetStateProperty<MouseCursor?>`.
947    fn textable() -> WidgetStatePropertyRef<Option<MouseCursorRef>> {
948        <dyn WidgetStateProperty<Option<MouseCursorRef>>>::resolve_with(|states| {
949            Some(WidgetStateMouseCursor::textable().resolve(states))
950        })
951    }
952
953    #[test]
954    fn list_tile_theme_data_copy_with_equality_basics() {
955        assert_eq!(
956            ListTileThemeData::new(),
957            ListTileThemeData::new().copy_with()
958        );
959    }
960
961    #[test]
962    fn list_tile_theme_data_lerp_special_cases() {
963        assert_eq!(ListTileThemeData::lerp(None, None, 0.0), None);
964        let data = ListTileThemeData::new();
965        // Dart's `identical`: the value comes back as it was.
966        assert_eq!(
967            ListTileThemeData::lerp(Some(&data), Some(&data), 0.5),
968            Some(data.clone())
969        );
970    }
971
972    #[test]
973    fn list_tile_theme_data_defaults() {
974        let theme_data = ListTileThemeData::new();
975        assert_eq!(theme_data.dense, None);
976        assert!(theme_data.shape.is_none());
977        assert_eq!(theme_data.style, None);
978        assert_eq!(theme_data.selected_color, None);
979        assert_eq!(theme_data.icon_color, None);
980        assert_eq!(theme_data.text_color, None);
981        assert_eq!(theme_data.title_text_style, None);
982        assert_eq!(theme_data.subtitle_text_style, None);
983        assert_eq!(theme_data.leading_and_trailing_text_style, None);
984        assert_eq!(theme_data.content_padding, None);
985        assert_eq!(theme_data.tile_color, None);
986        assert_eq!(theme_data.selected_tile_color, None);
987        assert_eq!(theme_data.horizontal_title_gap, None);
988        assert_eq!(theme_data.min_vertical_padding, None);
989        assert_eq!(theme_data.min_leading_width, None);
990        assert_eq!(theme_data.min_tile_height, None);
991        assert_eq!(theme_data.enable_feedback, None);
992        assert!(theme_data.mouse_cursor.is_none());
993        assert_eq!(theme_data.visual_density, None);
994        assert_eq!(theme_data.title_alignment, None);
995        assert_eq!(theme_data.is_three_line, None);
996    }
997
998    /// The theme a `ListTileTheme` gives its subtree, read in a builder under it.
999    fn theme_under(
1000        tester: &mut WidgetTester,
1001        theme: impl FnOnce(WidgetRef) -> ListTileTheme,
1002    ) -> ListTileThemeData {
1003        let seen = Rc::new(RefCell::new(None));
1004        pump_app(tester, m3, |_app| {
1005            let seen = Rc::clone(&seen);
1006            let probe = Builder::new(move |app, context| {
1007                *seen.borrow_mut() = Some(ListTileTheme::of(app, context));
1008                SizedBox::new().into_widget()
1009            });
1010            Material::new()
1011                .child(theme(Center::new().child(probe).into_widget()))
1012                .into_widget()
1013        });
1014        seen.borrow_mut().take().expect("built")
1015    }
1016
1017    #[test]
1018    fn list_tile_theme_backwards_compatibility_constructor() {
1019        test_widgets(|tester| {
1020            let mouse_cursor = clickable();
1021            let theme = theme_under(tester, |child| {
1022                ListTileTheme::new(child)
1023                    .dense(true)
1024                    .shape(StadiumBorder::default())
1025                    .style(ListTileStyle::Drawer)
1026                    .selected_color(Color::new(0x00000001))
1027                    .icon_color(Color::new(0x00000002))
1028                    .text_color(Color::new(0x00000003))
1029                    .content_padding(EdgeInsets::all(100.0))
1030                    .tile_color(Color::new(0x00000004))
1031                    .selected_tile_color(Color::new(0x00000005))
1032                    .horizontal_title_gap(200.0)
1033                    .min_vertical_padding(300.0)
1034                    .min_leading_width(400.0)
1035                    .enable_feedback(true)
1036                    .mouse_cursor(Rc::clone(&mouse_cursor))
1037            });
1038
1039            assert_eq!(theme.dense, Some(true));
1040            assert_eq!(
1041                theme.shape,
1042                Some(ShapeBorderRef::new(StadiumBorder::default()))
1043            );
1044            assert_eq!(theme.style, Some(ListTileStyle::Drawer));
1045            assert_eq!(theme.selected_color, Some(Color::new(0x00000001)));
1046            assert_eq!(
1047                theme.icon_color,
1048                Some(AnyColor::new(Color::new(0x00000002)))
1049            );
1050            assert_eq!(
1051                theme.text_color,
1052                Some(AnyColor::new(Color::new(0x00000003)))
1053            );
1054            assert_eq!(theme.content_padding, Some(EdgeInsets::all(100.0).into()));
1055            assert_eq!(theme.tile_color, Some(Color::new(0x00000004)));
1056            assert_eq!(theme.selected_tile_color, Some(Color::new(0x00000005)));
1057            assert_eq!(theme.horizontal_title_gap, Some(200.0));
1058            assert_eq!(theme.min_vertical_padding, Some(300.0));
1059            assert_eq!(theme.min_leading_width, Some(400.0));
1060            assert_eq!(theme.enable_feedback, Some(true));
1061            assert!(Rc::ptr_eq(
1062                theme.mouse_cursor.as_ref().expect("a mouse cursor"),
1063                &mouse_cursor
1064            ));
1065        });
1066    }
1067
1068    /// The tester of "ListTileTheme", and the colours its `TestIcon`s and `TestText`s and the
1069    /// theme its builder saw in the last frame.
1070    struct ThemedCase<'a> {
1071        tester: &'a mut WidgetTester,
1072        leading: Rc<Cell<Option<Color>>>,
1073        trailing: Rc<Cell<Option<Color>>>,
1074        title: Rc<RefCell<Option<TextStyle>>>,
1075        subtitle: Rc<RefCell<Option<TextStyle>>>,
1076        theme: Rc<RefCell<Option<Rc<ThemeData>>>>,
1077        leading_key: GlobalKey,
1078        trailing_key: GlobalKey,
1079        list_tile_key: GlobalKey,
1080    }
1081
1082    #[derive(Default)]
1083    struct ThemedArgs {
1084        enabled: Option<bool>,
1085        dense: bool,
1086        selected: bool,
1087        shape: Option<ShapeBorderRef>,
1088        selected_color: Option<Color>,
1089        icon_color: Option<Color>,
1090        text_color: Option<Color>,
1091    }
1092
1093    impl<'a> ThemedCase<'a> {
1094        fn new(tester: &'a mut WidgetTester) -> ThemedCase<'a> {
1095            ThemedCase {
1096                tester,
1097                leading: Rc::new(Cell::new(None)),
1098                trailing: Rc::new(Cell::new(None)),
1099                title: Rc::new(RefCell::new(None)),
1100                subtitle: Rc::new(RefCell::new(None)),
1101                theme: Rc::new(RefCell::new(None)),
1102                leading_key: GlobalKey::new(),
1103                trailing_key: GlobalKey::new(),
1104                list_tile_key: GlobalKey::new(),
1105            }
1106        }
1107
1108        /// Dart's `tester.pumpWidget(buildFrame(...))`.
1109        fn pump_frame(&mut self, args: ThemedArgs) {
1110            let mut data = ListTileThemeData::new()
1111                .dense(args.dense)
1112                .min_vertical_padding(25.0)
1113                .mouse_cursor(
1114                    <dyn WidgetStateProperty<Option<MouseCursorRef>>>::resolve_with(|states| {
1115                        if states.contains(&WidgetState::Disabled) {
1116                            return Some(SystemMouseCursors::FORBIDDEN.into());
1117                        }
1118
1119                        Some(SystemMouseCursors::CLICK.into())
1120                    }),
1121                )
1122                .visual_density(VisualDensity::COMPACT)
1123                .title_alignment(ListTileTitleAlignment::Bottom);
1124            data.shape = args.shape;
1125            data.selected_color = args.selected_color;
1126            data.icon_color = args.icon_color.map(AnyColor::new);
1127            data.text_color = args.text_color.map(AnyColor::new);
1128            let (leading, trailing, title, subtitle, theme) = (
1129                Rc::clone(&self.leading),
1130                Rc::clone(&self.trailing),
1131                Rc::clone(&self.title),
1132                Rc::clone(&self.subtitle),
1133                Rc::clone(&self.theme),
1134            );
1135            let (leading_key, trailing_key, list_tile_key) = (
1136                self.leading_key.clone(),
1137                self.trailing_key.clone(),
1138                self.list_tile_key.clone(),
1139            );
1140            let home = Material::new()
1141                .child(
1142                    Center::new().child(
1143                        ListTileTheme::new(Builder::new(move |app, context| {
1144                            *theme.borrow_mut() = Some(Theme::of(app, context));
1145                            ListTile::new()
1146                                .key(list_tile_key.clone().into())
1147                                .enabled(args.enabled.unwrap_or(true))
1148                                .selected(args.selected)
1149                                .leading(
1150                                    SizedBox::new()
1151                                        .key(leading_key.clone().into())
1152                                        .child(test_icon(&leading)),
1153                                )
1154                                .trailing(
1155                                    SizedBox::new()
1156                                        .key(trailing_key.clone().into())
1157                                        .child(test_icon(&trailing)),
1158                                )
1159                                .title(test_text("title", &title))
1160                                .subtitle(test_text("subtitle", &subtitle))
1161                                .into_widget()
1162                        }))
1163                        .data(data),
1164                    ),
1165                )
1166                .into_widget();
1167            let theme = m2(&self.tester.cell.borrow());
1168            self.tester.pump_widget(material_app(theme, home));
1169        }
1170
1171        fn colors(&self) -> [Option<Color>; 4] {
1172            [
1173                self.leading.get(),
1174                self.trailing.get(),
1175                style_color(&self.title),
1176                style_color(&self.subtitle),
1177            ]
1178        }
1179
1180        fn theme(&self) -> Rc<ThemeData> {
1181            self.theme.borrow().clone().expect("built")
1182        }
1183    }
1184
1185    /// "ListTileTheme". `TestIcon` sits in a keyed box: Dart keys the icon itself.
1186    #[test]
1187    fn list_tile_theme() {
1188        test_widgets(|tester| {
1189            const GREEN: Color = Color::new(0xFF00FF00);
1190            const RED: Color = Color::new(0xFFFF0000);
1191            let rounded_shape = RoundedRectangleBorder {
1192                border_radius: BorderRadius::all(Radius::circular(4.0)).into(),
1193                ..RoundedRectangleBorder::default()
1194            };
1195            let mut case = ThemedCase::new(tester);
1196
1197            // A selected ListTile's leading, trailing, and text get the primary color by default
1198            case.pump_frame(ThemedArgs {
1199                selected: true,
1200                ..ThemedArgs::default()
1201            });
1202            case.tester.pump_by(Duration::from_millis(300)); // DefaultTextStyle changes animate
1203            assert_eq!(case.colors(), [Some(case.theme().primary_color); 4]);
1204
1205            // A selected ListTile's leading, trailing, and text get the ListTileTheme's selectedColor
1206            case.pump_frame(ThemedArgs {
1207                selected: true,
1208                selected_color: Some(GREEN),
1209                ..ThemedArgs::default()
1210            });
1211            case.tester.pump_by(Duration::from_millis(300)); // DefaultTextStyle changes animate
1212            assert_eq!(case.colors(), [Some(GREEN); 4]);
1213
1214            // An unselected ListTile's leading and trailing get the ListTileTheme's iconColor
1215            // An unselected ListTile's title texts get the ListTileTheme's textColor
1216            case.pump_frame(ThemedArgs {
1217                icon_color: Some(RED),
1218                text_color: Some(GREEN),
1219                ..ThemedArgs::default()
1220            });
1221            case.tester.pump_by(Duration::from_millis(300)); // DefaultTextStyle changes animate
1222            assert_eq!(
1223                case.colors(),
1224                [Some(RED), Some(RED), Some(GREEN), Some(GREEN)]
1225            );
1226
1227            // If the item is disabled it's rendered with the theme's disabled color.
1228            case.pump_frame(ThemedArgs {
1229                enabled: Some(false),
1230                ..ThemedArgs::default()
1231            });
1232            case.tester.pump_by(Duration::from_millis(300)); // DefaultTextStyle changes animate
1233            assert_eq!(case.colors(), [Some(case.theme().disabled_color); 4]);
1234
1235            // If the item is disabled it's rendered with the theme's disabled color.
1236            // Even if it's selected.
1237            case.pump_frame(ThemedArgs {
1238                enabled: Some(false),
1239                selected: true,
1240                ..ThemedArgs::default()
1241            });
1242            case.tester.pump_by(Duration::from_millis(300)); // DefaultTextStyle changes animate
1243            assert_eq!(case.colors(), [Some(case.theme().disabled_color); 4]);
1244
1245            // A selected ListTile's InkWell gets the ListTileTheme's shape
1246            case.pump_frame(ThemedArgs {
1247                selected: true,
1248                shape: Some(ShapeBorderRef::new(rounded_shape)),
1249                ..ThemedArgs::default()
1250            });
1251            let ink_well = case.tester.element(find::by_type::<InkWell>().at(0));
1252            let custom_border = case.tester.widget(ink_well, |ink_well: &InkWell| {
1253                ink_well.ink_response.custom_border.clone()
1254            });
1255            assert_eq!(custom_border, Some(ShapeBorderRef::new(rounded_shape)));
1256
1257            // Cursor updates when hovering disabled ListTile
1258            case.pump_frame(ThemedArgs {
1259                enabled: Some(false),
1260                ..ThemedArgs::default()
1261            });
1262            let list_tile = case.tester.get_rect(find::text("title")).center();
1263            let mut gesture = TestPointer::mouse(1, 0);
1264            case.tester.add_pointer(&mut gesture, Offset::ZERO);
1265            case.tester.move_to(&mut gesture, list_tile, false);
1266            case.tester.pump_and_settle();
1267            let forbidden: MouseCursorRef = SystemMouseCursors::FORBIDDEN.into();
1268            assert!(*case.tester.active_cursor(1) == *forbidden);
1269
1270            // VisualDensity is respected
1271            assert_eq!(
1272                key_rect(case.tester, &case.list_tile_key).size(),
1273                Size::new(800.0, 80.0)
1274            );
1275
1276            // titleAlignment is respected.
1277            let title_offset = case.tester.get_rect(find::text("title")).top_left();
1278            let leading_offset = key_rect(case.tester, &case.leading_key).top_left();
1279            let trailing_offset = key_rect(case.tester, &case.trailing_key).top_right();
1280            assert_eq!(leading_offset.dy() - title_offset.dy(), 6.0);
1281            assert_eq!(trailing_offset.dy() - title_offset.dy(), 6.0);
1282        });
1283    }
1284
1285    #[test]
1286    fn list_tile_theme_colors_are_applied_to_leading_and_trailing_text_widgets() {
1287        test_widgets(|tester| {
1288            let selected_color = Colors::ORANGE.primary();
1289            let default_color = Colors::BLACK;
1290            let mut colors = |enabled: bool, selected: bool| {
1291                let leading = Rc::new(RefCell::new(None));
1292                let trailing = Rc::new(RefCell::new(None));
1293                let title = Rc::new(RefCell::new(None));
1294                let theme = Rc::new(RefCell::new(None));
1295                pump_app(tester, m3, |_app| {
1296                    let (leading, trailing, title, theme) = (
1297                        Rc::clone(&leading),
1298                        Rc::clone(&trailing),
1299                        Rc::clone(&title),
1300                        Rc::clone(&theme),
1301                    );
1302                    Material::new()
1303                        .child(
1304                            Center::new().child(
1305                                ListTileTheme::new(Builder::new(move |app, context| {
1306                                    *theme.borrow_mut() = Some(Theme::of(app, context));
1307                                    ListTile::new()
1308                                        .enabled(enabled)
1309                                        .selected(selected)
1310                                        .leading(test_text("leading", &leading))
1311                                        .title(test_text("title", &title))
1312                                        .trailing(test_text("trailing", &trailing))
1313                                        .into_widget()
1314                                }))
1315                                .data(
1316                                    ListTileThemeData::new()
1317                                        .selected_color(selected_color)
1318                                        .text_color(default_color),
1319                                ),
1320                            ),
1321                        )
1322                        .into_widget()
1323                });
1324                // Wait for text color to animate.
1325                tester.pump_and_settle();
1326                let theme: Rc<ThemeData> = theme.borrow_mut().take().expect("built");
1327                (style_color(&leading), style_color(&trailing), theme)
1328            };
1329
1330            let (leading, trailing, _) = colors(true, false);
1331            // Enabled color should use ListTileTheme.textColor.
1332            assert_eq!(leading, Some(default_color));
1333            assert_eq!(trailing, Some(default_color));
1334
1335            let (leading, trailing, _) = colors(true, true);
1336            // Selected color should use ListTileTheme.selectedColor.
1337            assert_eq!(leading, Some(selected_color));
1338            assert_eq!(trailing, Some(selected_color));
1339
1340            let (leading, trailing, theme) = colors(false, false);
1341            // Disabled color should be ThemeData.disabledColor.
1342            assert_eq!(leading, Some(theme.disabled_color));
1343            assert_eq!(trailing, Some(theme.disabled_color));
1344        });
1345    }
1346
1347    fn italic(font_size: f64, color: u32) -> TextStyle {
1348        TextStyle::new()
1349            .font_size(font_size)
1350            .color(Color::new(color))
1351            .font_style(FontStyle::Italic)
1352    }
1353
1354    /// The default text styles of the four slots under `theme`, for `tile`.
1355    fn slot_styles(
1356        tester: &mut WidgetTester,
1357        theme: impl FnOnce(&App) -> ThemeData,
1358        tile: ListTile,
1359    ) -> [TextStyle; 4] {
1360        let seen: [Rc<RefCell<Option<TextStyle>>>; 4] =
1361            std::array::from_fn(|_| Rc::new(RefCell::new(None)));
1362        pump_app(tester, theme, |_app| {
1363            Material::new()
1364                .child(
1365                    Center::new().child(
1366                        tile.leading(test_text("leading", &seen[0]))
1367                            .title(test_text("title", &seen[1]))
1368                            .subtitle(test_text("subtitle", &seen[2]))
1369                            .trailing(test_text("trailing", &seen[3])),
1370                    ),
1371                )
1372                .into_widget()
1373        });
1374        seen.map(|seen| seen.borrow_mut().take().expect("built"))
1375    }
1376
1377    fn assert_slot_styles(styles: &[TextStyle; 4], expected: [&TextStyle; 4]) {
1378        for (style, expected) in styles.iter().zip(expected) {
1379            assert_eq!(style.font_size, expected.font_size);
1380            assert_eq!(style.color, expected.color);
1381            assert_eq!(style.font_style, expected.font_style);
1382        }
1383    }
1384
1385    /// The text-style cases' four styles, Dart's `titleTextStyle`, `subtitleTextStyle` and
1386    /// `leadingAndTrailingTextStyle`.
1387    fn case_styles() -> (TextStyle, TextStyle, TextStyle) {
1388        (
1389            italic(23.0, 0xffff0000),
1390            italic(20.0, 0xff00ff00),
1391            italic(18.0, 0xff0000ff),
1392        )
1393    }
1394
1395    fn respects_theme_text_styles(tester: &mut WidgetTester, use_material3: bool) {
1396        let (title, subtitle, leading_and_trailing) = case_styles();
1397        let theme_data = ListTileThemeData::new()
1398            .title_text_style(title.clone())
1399            .subtitle_text_style(subtitle.clone())
1400            .leading_and_trailing_text_style(leading_and_trailing.clone());
1401        let styles = slot_styles(
1402            tester,
1403            move |app| {
1404                ThemeData::builder()
1405                    .use_material3(use_material3)
1406                    .list_tile_theme(theme_data)
1407                    .build(app)
1408            },
1409            ListTile::new(),
1410        );
1411        assert_slot_styles(
1412            &styles,
1413            [
1414                &leading_and_trailing,
1415                &title,
1416                &subtitle,
1417                &leading_and_trailing,
1418            ],
1419        );
1420    }
1421
1422    #[test]
1423    fn material3_list_tile_respects_list_tile_theme_text_styles() {
1424        test_widgets(|tester| respects_theme_text_styles(tester, true));
1425    }
1426
1427    #[test]
1428    fn material2_list_tile_respects_list_tile_theme_text_styles() {
1429        test_widgets(|tester| respects_theme_text_styles(tester, false));
1430    }
1431
1432    fn theme_text_styles_are_overridden(tester: &mut WidgetTester, use_material3: bool) {
1433        let (title, subtitle, leading_and_trailing) = case_styles();
1434        let styles = slot_styles(
1435            tester,
1436            move |app| {
1437                ThemeData::builder()
1438                    .use_material3(use_material3)
1439                    .list_tile_theme(
1440                        ListTileThemeData::new()
1441                            .title_text_style(TextStyle::new().font_size(20.0))
1442                            .subtitle_text_style(TextStyle::new().font_size(17.5))
1443                            .leading_and_trailing_text_style(TextStyle::new().font_size(15.0)),
1444                    )
1445                    .build(app)
1446            },
1447            ListTile::new()
1448                .title_text_style(title.clone())
1449                .subtitle_text_style(subtitle.clone())
1450                .leading_and_trailing_text_style(leading_and_trailing.clone()),
1451        );
1452        assert_slot_styles(
1453            &styles,
1454            [
1455                &leading_and_trailing,
1456                &title,
1457                &subtitle,
1458                &leading_and_trailing,
1459            ],
1460        );
1461    }
1462
1463    #[test]
1464    fn material3_list_tile_text_styles_are_overridden_by_list_tile_properties() {
1465        test_widgets(|tester| theme_text_styles_are_overridden(tester, true));
1466    }
1467
1468    #[test]
1469    fn material2_list_tile_text_styles_are_overridden_by_list_tile_properties() {
1470        test_widgets(|tester| theme_text_styles_are_overridden(tester, false));
1471    }
1472
1473    /// The colour of the first rect the material's ink features draw.
1474    fn tile_colors_before_and_after_tap(
1475        tester: &mut WidgetTester,
1476        theme_data: ListTileThemeData,
1477        tile: impl Fn() -> ListTile + 'static,
1478    ) -> (Option<Color>, Option<Color>) {
1479        let is_selected = Rc::new(Cell::new(false));
1480        pump_app(tester, m3, move |_app| {
1481            Material::new()
1482                .child(
1483                    ListTileTheme::new(Center::new().child(StatefulBuilder::new(
1484                        move |_app, _context, set_state| {
1485                            let toggled = Rc::clone(&is_selected);
1486                            tile()
1487                                .selected(is_selected.get())
1488                                .on_tap(Listener::new(move |app| {
1489                                    let toggled = Rc::clone(&toggled);
1490                                    set_state(app, &mut |_app| toggled.set(!toggled.get()));
1491                                }))
1492                                .title(Text::new("Title"))
1493                                .into_widget()
1494                        },
1495                    )))
1496                    .data(theme_data),
1497                )
1498                .into_widget()
1499        });
1500        let before = ink_tile_color(tester);
1501        // Tap on tile to change isSelected.
1502        let center = tile_rect(tester, 0).center();
1503        tester.tap_at(center, 1);
1504        tester.pump_and_settle();
1505        (before, ink_tile_color(tester))
1506    }
1507
1508    /// "ListTile respects ListTileTheme's tileColor & selectedTileColor": the colours are
1509    /// read from the tile's `Ink` decoration, whose rect Dart's `paints` matches.
1510    #[test]
1511    fn list_tile_respects_list_tile_theme_tile_color_and_selected_tile_color() {
1512        test_widgets(|tester| {
1513            let theme = ListTileThemeData::new()
1514                .tile_color(Colors::GREEN.shade500())
1515                .selected_tile_color(Colors::RED.shade500());
1516            assert_eq!(
1517                tile_colors_before_and_after_tap(tester, theme, ListTile::new),
1518                (Some(Colors::GREEN.shade500()), Some(Colors::RED.shade500()))
1519            );
1520        });
1521    }
1522
1523    /// "ListTileTheme's tileColor & selectedTileColor are overridden by ListTile properties".
1524    #[test]
1525    fn list_tile_theme_tile_colors_are_overridden_by_list_tile_properties() {
1526        test_widgets(|tester| {
1527            let tile_color = Colors::GREEN.shade500();
1528            let selected_tile_color = Colors::RED.shade500();
1529            let theme = ListTileThemeData::new()
1530                .selected_tile_color(Colors::GREEN.primary())
1531                .tile_color(Colors::RED.primary());
1532            assert_eq!(
1533                tile_colors_before_and_after_tap(tester, theme, move || {
1534                    ListTile::new()
1535                        .tile_color(tile_color)
1536                        .selected_tile_color(selected_tile_color)
1537                }),
1538                (Some(tile_color), Some(selected_tile_color))
1539            );
1540        });
1541    }
1542
1543    fn state_color() -> WidgetStateColor {
1544        WidgetStateColor::resolve_with(|states| {
1545            if states.contains(&WidgetState::Disabled) {
1546                return Colors::RED.primary();
1547            }
1548            if states.contains(&WidgetState::Selected) {
1549                return Colors::GREEN.primary();
1550            }
1551            Colors::BLUE.primary()
1552        })
1553    }
1554
1555    /// "ListTile uses ListTileTheme shape in a drawer".
1556    #[test]
1557    fn list_tile_uses_list_tile_theme_shape_in_a_drawer() {
1558        test_widgets(|tester| {
1559            // This is a regression test for https://github.com/flutter/flutter/issues/106303
1560
1561            let scaffold_key = GlobalKey::new();
1562            let shape_border = RoundedRectangleBorder {
1563                border_radius: BorderRadius::all(Radius::circular(20.0)).into(),
1564                ..RoundedRectangleBorder::default()
1565            };
1566
1567            pump_app(
1568                tester,
1569                |app| {
1570                    ThemeData::builder()
1571                        .list_tile_theme(ListTileThemeData::new().shape(shape_border))
1572                        .build(app)
1573                },
1574                |_app| {
1575                    Scaffold::new()
1576                        .key(scaffold_key.clone().into())
1577                        .drawer(Drawer::new().child(ListTile::new()))
1578                        .body(Container::new())
1579                        .into_widget()
1580                },
1581            );
1582            tester.pump_and_settle();
1583
1584            let scaffold_state = scaffold_key
1585                .current_state::<ScaffoldState>(&tester.cell.borrow_mut())
1586                .expect("a mounted scaffold");
1587            scaffold_state.open_drawer(&mut tester.cell.borrow_mut());
1588            // Start drawer animation.
1589            tester.pump();
1590
1591            let list_tile = tester.element(find::by_type::<ListTile>().at(0));
1592            let ink_well = find::by_type::<InkWell>()
1593                .evaluate(tester)
1594                .into_iter()
1595                .find(|ink_well| tester.ancestors(*ink_well).contains(&list_tile))
1596                .expect("the tile's InkWell");
1597            let ink_well_border = tester.widget(ink_well, |ink_well: &InkWell| {
1598                ink_well.ink_response.custom_border.clone()
1599            });
1600            // Test shape.
1601            assert_eq!(ink_well_border, Some(ShapeBorderRef::new(shape_border)));
1602        });
1603    }
1604
1605    #[test]
1606    fn list_tile_respects_widget_state_color_list_tile_theme_text_color() {
1607        test_widgets(|tester| {
1608            let mut title_color = |enabled: bool, selected: bool| {
1609                pump_app(
1610                    tester,
1611                    |app| {
1612                        ThemeData::builder()
1613                            .list_tile_theme(ListTileThemeData::new().text_color(state_color()))
1614                            .build(app)
1615                    },
1616                    |_app| {
1617                        Material::new()
1618                            .child(
1619                                Center::new().child(
1620                                    ListTile::new()
1621                                        .enabled(enabled)
1622                                        .selected(selected)
1623                                        .title(Text::new("title"))
1624                                        .subtitle(Text::new("subtitle")),
1625                                ),
1626                            )
1627                            .into_widget()
1628                    },
1629                );
1630                tester.pump_and_settle();
1631                tester.text_color("title")
1632            };
1633            // Test disabled state.
1634            assert_eq!(title_color(false, false), Colors::RED.primary());
1635            // Test enabled state.
1636            assert_eq!(title_color(true, false), Colors::BLUE.primary());
1637            // Test selected state.
1638            assert_eq!(title_color(true, true), Colors::GREEN.primary());
1639        });
1640    }
1641
1642    #[test]
1643    fn list_tile_respects_widget_state_color_list_tile_theme_icon_color() {
1644        test_widgets(|tester| {
1645            let mut icon_color = |enabled: bool, selected: bool| {
1646                let seen = Rc::new(Cell::new(None));
1647                pump_app(
1648                    tester,
1649                    |app| {
1650                        ThemeData::builder()
1651                            .list_tile_theme(ListTileThemeData::new().icon_color(state_color()))
1652                            .build(app)
1653                    },
1654                    |_app| {
1655                        Material::new()
1656                            .child(
1657                                Center::new().child(
1658                                    ListTile::new()
1659                                        .enabled(enabled)
1660                                        .selected(selected)
1661                                        .leading(test_icon(&seen)),
1662                                ),
1663                            )
1664                            .into_widget()
1665                    },
1666                );
1667                tester.pump_and_settle();
1668                seen.get()
1669            };
1670            // Test disabled state.
1671            assert_eq!(icon_color(false, false), Some(Colors::RED.primary()));
1672            // Test enabled state.
1673            assert_eq!(icon_color(true, false), Some(Colors::BLUE.primary()));
1674            // Test selected state.
1675            assert_eq!(icon_color(true, true), Some(Colors::GREEN.primary()));
1676        });
1677    }
1678
1679    #[test]
1680    fn list_tile_theme_data_copy_with_overrides_all_properties() {
1681        // This is a regression test for https://github.com/flutter/flutter/issues/119734
1682        let original = ListTileThemeData::new()
1683            .dense(true)
1684            .shape(StadiumBorder::default())
1685            .style(ListTileStyle::Drawer)
1686            .selected_color(Color::new(0x00000001))
1687            .icon_color(Color::new(0x00000002))
1688            .text_color(Color::new(0x00000003))
1689            .title_text_style(TextStyle::new().color(Color::new(0x00000004)))
1690            .subtitle_text_style(TextStyle::new().color(Color::new(0x00000005)))
1691            .leading_and_trailing_text_style(TextStyle::new().color(Color::new(0x00000006)))
1692            .content_padding(EdgeInsets::all(100.0))
1693            .tile_color(Color::new(0x00000007))
1694            .selected_tile_color(Color::new(0x00000008))
1695            .horizontal_title_gap(200.0)
1696            .min_vertical_padding(300.0)
1697            .min_leading_width(400.0)
1698            .min_tile_height(30.0)
1699            .enable_feedback(true)
1700            .title_alignment(ListTileTitleAlignment::Bottom)
1701            .is_three_line(true);
1702
1703        let copy = original
1704            .copy_with()
1705            .dense(false)
1706            .shape(RoundedRectangleBorder::default())
1707            .style(ListTileStyle::List)
1708            .selected_color(Color::new(0x00000009))
1709            .icon_color(Color::new(0x0000000A))
1710            .text_color(Color::new(0x0000000B))
1711            .title_text_style(TextStyle::new().color(Color::new(0x0000000C)))
1712            .subtitle_text_style(TextStyle::new().color(Color::new(0x0000000D)))
1713            .leading_and_trailing_text_style(TextStyle::new().color(Color::new(0x0000000E)))
1714            .content_padding(EdgeInsets::all(500.0))
1715            .tile_color(Color::new(0x0000000F))
1716            .selected_tile_color(Color::new(0x00000010))
1717            .horizontal_title_gap(600.0)
1718            .min_vertical_padding(700.0)
1719            .min_leading_width(800.0)
1720            .min_tile_height(80.0)
1721            .enable_feedback(false)
1722            .title_alignment(ListTileTitleAlignment::Top)
1723            .is_three_line(false);
1724
1725        assert_eq!(copy.dense, Some(false));
1726        assert_eq!(
1727            copy.shape,
1728            Some(ShapeBorderRef::new(RoundedRectangleBorder::default()))
1729        );
1730        assert_eq!(copy.style, Some(ListTileStyle::List));
1731        assert_eq!(copy.selected_color, Some(Color::new(0x00000009)));
1732        assert_eq!(copy.icon_color, Some(AnyColor::new(Color::new(0x0000000A))));
1733        assert_eq!(copy.text_color, Some(AnyColor::new(Color::new(0x0000000B))));
1734        assert_eq!(
1735            copy.title_text_style,
1736            Some(TextStyle::new().color(Color::new(0x0000000C)))
1737        );
1738        assert_eq!(
1739            copy.subtitle_text_style,
1740            Some(TextStyle::new().color(Color::new(0x0000000D)))
1741        );
1742        assert_eq!(
1743            copy.leading_and_trailing_text_style,
1744            Some(TextStyle::new().color(Color::new(0x0000000E)))
1745        );
1746        assert_eq!(copy.content_padding, Some(EdgeInsets::all(500.0).into()));
1747        assert_eq!(copy.tile_color, Some(Color::new(0x0000000F)));
1748        assert_eq!(copy.selected_tile_color, Some(Color::new(0x00000010)));
1749        assert_eq!(copy.horizontal_title_gap, Some(600.0));
1750        assert_eq!(copy.min_vertical_padding, Some(700.0));
1751        assert_eq!(copy.min_leading_width, Some(800.0));
1752        assert_eq!(copy.min_tile_height, Some(80.0));
1753        assert_eq!(copy.enable_feedback, Some(false));
1754        assert_eq!(copy.title_alignment, Some(ListTileTitleAlignment::Top));
1755        assert_eq!(copy.is_three_line, Some(false));
1756    }
1757
1758    #[test]
1759    fn list_tile_theme_title_alignment_is_overridden_by_list_tile_title_alignment() {
1760        test_widgets(|tester| {
1761            let leading_key = GlobalKey::new();
1762            let trailing_key = GlobalKey::new();
1763            pump_app(
1764                tester,
1765                |app| {
1766                    ThemeData::builder()
1767                        .list_tile_theme(
1768                            ListTileThemeData::new()
1769                                .title_alignment(ListTileTitleAlignment::Center),
1770                        )
1771                        .build(app)
1772                },
1773                |_app| {
1774                    Material::new()
1775                        .child(
1776                            Center::new().child(
1777                                ListTile::new()
1778                                    .title_alignment(ListTileTitleAlignment::Top)
1779                                    .leading(sized(&leading_key, 24.0, 24.0))
1780                                    .title(Text::new("\nHeadline Text\n"))
1781                                    .subtitle(Text::new("\nSupporting Text\n"))
1782                                    .trailing(sized(&trailing_key, 24.0, 24.0)),
1783                            ),
1784                        )
1785                        .into_widget()
1786                },
1787            );
1788            let tile_offset = tile_rect(tester, 0).top_left();
1789            let leading_offset = key_rect(tester, &leading_key).top_left();
1790            let trailing_offset = key_rect(tester, &trailing_key).top_right();
1791            assert_eq!(leading_offset.dy() - tile_offset.dy(), 8.0);
1792            assert_eq!(trailing_offset.dy() - tile_offset.dy(), 8.0);
1793        });
1794    }
1795
1796    #[test]
1797    fn list_tile_theme_merge_supports_all_properties() {
1798        test_widgets(|tester| {
1799            let parent_mouse_cursor = textable();
1800            let mouse_cursor = clickable();
1801            let seen = Rc::new(RefCell::new(None));
1802            pump_app(
1803                tester,
1804                {
1805                    let parent_mouse_cursor = Rc::clone(&parent_mouse_cursor);
1806                    move |app| {
1807                        ThemeData::builder()
1808                            .list_tile_theme(
1809                                ListTileThemeData::new()
1810                                    .dense(true)
1811                                    .shape(StadiumBorder::default())
1812                                    .style(ListTileStyle::Drawer)
1813                                    .selected_color(Color::new(0x00000001))
1814                                    .icon_color(Color::new(0x00000002))
1815                                    .text_color(Color::new(0x00000003))
1816                                    .title_text_style(
1817                                        TextStyle::new().color(Color::new(0x00000004)),
1818                                    )
1819                                    .subtitle_text_style(
1820                                        TextStyle::new().color(Color::new(0x00000005)),
1821                                    )
1822                                    .leading_and_trailing_text_style(
1823                                        TextStyle::new().color(Color::new(0x00000006)),
1824                                    )
1825                                    .content_padding(EdgeInsets::all(100.0))
1826                                    .tile_color(Color::new(0x00000007))
1827                                    .selected_tile_color(Color::new(0x00000008))
1828                                    .horizontal_title_gap(200.0)
1829                                    .min_vertical_padding(300.0)
1830                                    .min_leading_width(400.0)
1831                                    .min_tile_height(30.0)
1832                                    .enable_feedback(true)
1833                                    .title_alignment(ListTileTitleAlignment::Bottom)
1834                                    .mouse_cursor(parent_mouse_cursor)
1835                                    .visual_density(VisualDensity::COMFORTABLE)
1836                                    .is_three_line(true),
1837                            )
1838                            .build(app)
1839                    }
1840                },
1841                {
1842                    let mouse_cursor = Rc::clone(&mouse_cursor);
1843                    let seen = Rc::clone(&seen);
1844                    move |_app| {
1845                        let probe = Builder::new(move |app, context| {
1846                            *seen.borrow_mut() = Some(ListTileTheme::of(app, context));
1847                            ListTile::new().into_widget()
1848                        });
1849                        Material::new()
1850                            .child(
1851                                Center::new().child(
1852                                    ListTileTheme::merge(probe)
1853                                        .dense(false)
1854                                        .shape(RoundedRectangleBorder::default())
1855                                        .style(ListTileStyle::List)
1856                                        .selected_color(Color::new(0x00000009))
1857                                        .icon_color(Color::new(0x0000000A))
1858                                        .text_color(Color::new(0x0000000B))
1859                                        .title_text_style(
1860                                            TextStyle::new().color(Color::new(0x0000000C)),
1861                                        )
1862                                        .subtitle_text_style(
1863                                            TextStyle::new().color(Color::new(0x0000000D)),
1864                                        )
1865                                        .leading_and_trailing_text_style(
1866                                            TextStyle::new().color(Color::new(0x0000000E)),
1867                                        )
1868                                        .content_padding(EdgeInsets::all(500.0))
1869                                        .tile_color(Color::new(0x0000000F))
1870                                        .selected_tile_color(Color::new(0x00000010))
1871                                        .horizontal_title_gap(600.0)
1872                                        .min_vertical_padding(700.0)
1873                                        .min_leading_width(800.0)
1874                                        .min_tile_height(80.0)
1875                                        .enable_feedback(false)
1876                                        .title_alignment(ListTileTitleAlignment::Top)
1877                                        .mouse_cursor(mouse_cursor)
1878                                        .visual_density(VisualDensity::COMPACT)
1879                                        .is_three_line(false),
1880                                ),
1881                            )
1882                            .into_widget()
1883                    }
1884                },
1885            );
1886            let _ = tester;
1887            let theme: ListTileThemeData = seen.borrow_mut().take().expect("built");
1888            assert_eq!(theme.dense, Some(false));
1889            assert_eq!(
1890                theme.shape,
1891                Some(ShapeBorderRef::new(RoundedRectangleBorder::default()))
1892            );
1893            assert_eq!(theme.style, Some(ListTileStyle::List));
1894            assert_eq!(theme.selected_color, Some(Color::new(0x00000009)));
1895            assert_eq!(
1896                theme.icon_color,
1897                Some(AnyColor::new(Color::new(0x0000000A)))
1898            );
1899            assert_eq!(
1900                theme.text_color,
1901                Some(AnyColor::new(Color::new(0x0000000B)))
1902            );
1903            assert_eq!(
1904                theme.title_text_style,
1905                Some(TextStyle::new().color(Color::new(0x0000000C)))
1906            );
1907            assert_eq!(
1908                theme.subtitle_text_style,
1909                Some(TextStyle::new().color(Color::new(0x0000000D)))
1910            );
1911            assert_eq!(
1912                theme.leading_and_trailing_text_style,
1913                Some(TextStyle::new().color(Color::new(0x0000000E)))
1914            );
1915            assert_eq!(theme.content_padding, Some(EdgeInsets::all(500.0).into()));
1916            assert_eq!(theme.tile_color, Some(Color::new(0x0000000F)));
1917            assert_eq!(theme.selected_tile_color, Some(Color::new(0x00000010)));
1918            assert_eq!(theme.horizontal_title_gap, Some(600.0));
1919            assert_eq!(theme.min_vertical_padding, Some(700.0));
1920            assert_eq!(theme.min_leading_width, Some(800.0));
1921            assert_eq!(theme.min_tile_height, Some(80.0));
1922            assert_eq!(theme.enable_feedback, Some(false));
1923            assert_eq!(theme.title_alignment, Some(ListTileTitleAlignment::Top));
1924            assert!(Rc::ptr_eq(
1925                theme.mouse_cursor.as_ref().expect("a mouse cursor"),
1926                &mouse_cursor
1927            ));
1928            assert_eq!(theme.visual_density, Some(VisualDensity::COMPACT));
1929            assert_eq!(theme.is_three_line, Some(false));
1930        });
1931    }
1932
1933    const HEIGHT: f64 = 300.0;
1934    const AVATAR_TOP: f64 = 130.0;
1935    const PLACEHOLDER_TOP: f64 = 138.0;
1936
1937    fn two_line() -> [Rect; 6] {
1938        [
1939            Rect::from_ltwh(0.0, 0.0, 800.0, HEIGHT),
1940            Rect::from_ltwh(16.0, AVATAR_TOP, 40.0, 40.0),
1941            Rect::from_ltwh(800.0 - 24.0 - 24.0, PLACEHOLDER_TOP, 24.0, 24.0),
1942            Rect::from_ltwh(0.0, HEIGHT, 800.0, 72.0),
1943            Rect::from_ltwh(16.0, HEIGHT + 16.0, 40.0, 40.0),
1944            Rect::from_ltwh(800.0 - 24.0 - 24.0, HEIGHT + 24.0, 24.0, 24.0),
1945        ]
1946    }
1947
1948    fn three_line() -> [Rect; 6] {
1949        [
1950            Rect::from_ltwh(0.0, 0.0, 800.0, HEIGHT),
1951            Rect::from_ltwh(16.0, 8.0, 40.0, 40.0),
1952            Rect::from_ltwh(800.0 - 24.0 - 24.0, 8.0, 24.0, 24.0),
1953            Rect::from_ltwh(0.0, HEIGHT, 800.0, 88.0),
1954            Rect::from_ltwh(16.0, HEIGHT + 8.0, 40.0, 40.0),
1955            Rect::from_ltwh(800.0 - 24.0 - 24.0, HEIGHT + 8.0, 24.0, 24.0),
1956        ]
1957    }
1958
1959    /// The rects of the two tiles of the `isThreeLine` cases, with their avatars and
1960    /// placeholders.
1961    fn rects(
1962        tester: &mut WidgetTester,
1963        theme_data_is_three_line: Option<bool>,
1964        theme_is_three_line: Option<bool>,
1965    ) -> [Rect; 6] {
1966        let keys: [GlobalKey; 4] = std::array::from_fn(|_| GlobalKey::new());
1967        crate::list_tile::tests::pump_unique_app(
1968            tester,
1969            |app| {
1970                let mut theme = ThemeData::builder();
1971                if let Some(is_three_line) = theme_data_is_three_line {
1972                    theme = theme
1973                        .list_tile_theme(ListTileThemeData::new().is_three_line(is_three_line));
1974                }
1975                theme.build(app)
1976            },
1977            |_app| {
1978                let tile = |avatar_key: &GlobalKey, placeholder_key: &GlobalKey, subtitle: &str| {
1979                    ListTile::new()
1980                        .leading(avatar(avatar_key))
1981                        .trailing(placeholder(placeholder_key, 24.0, 24.0))
1982                        .title(Text::new("A"))
1983                        .subtitle(Text::new(subtitle))
1984                        .into_widget()
1985                };
1986                let list = ListView::new().children([
1987                    tile(&keys[0], &keys[1], THIRTEEN_LINES),
1988                    tile(&keys[2], &keys[3], "A"),
1989                ]);
1990                let child = match theme_is_three_line {
1991                    Some(is_three_line) => ListTileTheme::new(list)
1992                        .data(ListTileThemeData::new().is_three_line(is_three_line))
1993                        .into_widget(),
1994                    None => list.into_widget(),
1995                };
1996                Material::new().child(child).into_widget()
1997            },
1998        );
1999        [
2000            tile_rect(tester, 0),
2001            key_rect(tester, &keys[0]),
2002            key_rect(tester, &keys[1]),
2003            tile_rect(tester, 1),
2004            key_rect(tester, &keys[2]),
2005            key_rect(tester, &keys[3]),
2006        ]
2007    }
2008
2009    // Regression test for https://github.com/flutter/flutter/issues/165453
2010    #[test]
2011    fn list_tile_theme_data_is_three_line() {
2012        test_widgets(|tester| {
2013            assert_eq!(rects(tester, None, None), two_line());
2014            assert_eq!(rects(tester, Some(false), None), two_line());
2015            assert_eq!(rects(tester, Some(true), None), three_line());
2016        });
2017    }
2018
2019    // Regression test for https://github.com/flutter/flutter/issues/165453
2020    #[test]
2021    fn list_tile_theme_is_three_line() {
2022        test_widgets(|tester| {
2023            assert_eq!(rects(tester, Some(true), Some(false)), two_line());
2024            // THREE-LINE
2025            assert_eq!(rects(tester, None, Some(true)), three_line());
2026        });
2027    }
2028    // Flutter's "Default ListTileThemeData debugFillProperties".
2029    #[test]
2030    fn a_list_tile_theme_data_describes_nothing_by_default() {
2031        assert_eq!(
2032            waft_widgets::testing::describe_properties(|properties| {
2033                waft_foundation::Diagnosticable::debug_fill_properties(
2034                    &ListTileThemeData::new(),
2035                    properties,
2036                )
2037            }),
2038            Vec::<String>::new()
2039        );
2040    }
2041
2042    // list_tile_theme_test.dart 'ListTileThemeData implements debugFillProperties'.
2043    #[test]
2044    fn an_assigned_list_tile_theme_describes_every_property() {
2045        let mouse_cursor: WidgetStatePropertyRef<Option<MouseCursorRef>> = Rc::new(
2046            WidgetStatePropertyAll::new(Some(SystemMouseCursors::CLICK.into())),
2047        );
2048        let theme = ListTileThemeData::new()
2049            .dense(true)
2050            .shape(StadiumBorder::default())
2051            .style(ListTileStyle::Drawer)
2052            .selected_color(Color::new(0x00000001))
2053            .icon_color(AnyColor::new(Color::new(0x00000002)))
2054            .text_color(AnyColor::new(Color::new(0x00000003)))
2055            .title_text_style(TextStyle::new().color(Color::new(0x00000004)))
2056            .subtitle_text_style(TextStyle::new().color(Color::new(0x00000005)))
2057            .leading_and_trailing_text_style(TextStyle::new().color(Color::new(0x00000006)))
2058            .content_padding(EdgeInsetsGeometry::all(100.0))
2059            .tile_color(Color::new(0x00000007))
2060            .selected_tile_color(Color::new(0x00000008))
2061            .horizontal_title_gap(200.0)
2062            .min_vertical_padding(300.0)
2063            .min_leading_width(400.0)
2064            .min_tile_height(30.0)
2065            .enable_feedback(true)
2066            .mouse_cursor(mouse_cursor)
2067            .visual_density(VisualDensity::COMFORTABLE)
2068            .title_alignment(ListTileTitleAlignment::Top)
2069            .is_three_line(true);
2070        let description = waft_widgets::testing::describe_properties(|properties| {
2071            waft_foundation::Diagnosticable::debug_fill_properties(&theme, properties)
2072        });
2073        let description: Vec<String> = description
2074            .iter()
2075            .map(|line| waft_rendering::testing::ignoring_hash_codes(line))
2076            .collect();
2077        assert_eq!(
2078            description,
2079            [
2080                "dense: true".to_string(),
2081                format!("shape: {}", ShapeBorderRef::from(StadiumBorder::default())),
2082                "style: drawer".to_string(),
2083                format!("selectedColor: {}", Color::new(0x00000001)),
2084                format!("iconColor: {}", Color::new(0x00000002)),
2085                format!("textColor: {}", Color::new(0x00000003)),
2086                format!(
2087                    "titleTextStyle: {}",
2088                    TextStyle::new().color(Color::new(0x00000004))
2089                ),
2090                format!(
2091                    "subtitleTextStyle: {}",
2092                    TextStyle::new().color(Color::new(0x00000005))
2093                ),
2094                format!(
2095                    "leadingAndTrailingTextStyle: {}",
2096                    TextStyle::new().color(Color::new(0x00000006))
2097                ),
2098                "contentPadding: EdgeInsets.all(100.0)".to_string(),
2099                format!("tileColor: {}", Color::new(0x00000007)),
2100                format!("selectedTileColor: {}", Color::new(0x00000008)),
2101                "horizontalTitleGap: 200.0".to_string(),
2102                "minVerticalPadding: 300.0".to_string(),
2103                "minLeadingWidth: 400.0".to_string(),
2104                "minTileHeight: 30.0".to_string(),
2105                "enableFeedback: true".to_string(),
2106                "mouseCursor: WidgetStatePropertyAll(SystemMouseCursor(click))".to_string(),
2107                "visualDensity: VisualDensity#00000(h: -1.0, v: -1.0)(horizontal: -1.0, vertical: -1.0)"
2108                    .to_string(),
2109                "titleAlignment: ListTileTitleAlignment.top".to_string(),
2110                "isThreeLine: true".to_string(),
2111            ]
2112        );
2113    }
2114}