Skip to main content

waft_material/
expand_icon.rs

1//! Flutter counterpart: material_ui's `expand_icon.dart`.
2
3use std::f64::consts::PI;
4use std::fmt;
5
6use waft_animation::{
7    Animatable, AnimationBehavior, AnimationController, AnimationRef, CurveTween, Curves, Drive,
8    Tween,
9};
10use waft_embedder::{Brightness, Color};
11use waft_foundation::{App, Handle, Listener, Ref, ValueChanged};
12use waft_painting::EdgeInsetsGeometry;
13use waft_scheduler::{Ticker, TickerCallback, TickerProviderObject};
14use waft_widgets::{
15    BuildContext, Icon, IntoWidget, KeyRef, RotationTransition, Semantics,
16    SingleTickerProviderStateMixin, SingleTickerProviderStateMixinData, State, StateData,
17    StatefulWidget, WidgetRef,
18};
19
20use crate::colors::Colors;
21use crate::debug::{debug_check_has_material, debug_check_has_material_localizations};
22use crate::icon_button::IconButton;
23use crate::icons::Icons;
24use crate::material_localizations::MaterialLocalizations;
25use crate::theme::{K_THEME_ANIMATION_DURATION, Theme};
26
27/// A widget representing a rotating expand/collapse button. The icon rotates
28/// 180 degrees when pressed, then reverts the animation on a second press.
29/// The underlying icon is `Icons::expand_more`.
30///
31/// The expand icon does not include a semantic label for accessibility. In
32/// order to be accessible it should be combined with a label using
33/// `MergeSemantics`. This is done automatically by the `ExpansionPanel` widget.
34///
35/// See [`IconButton`] for a more general implementation of a pressable button
36/// with an icon.
37///
38/// See also:
39///
40///  * <https://material.io/design/iconography/system-icons.html>
41pub struct ExpandIcon {
42    pub key: Option<KeyRef>,
43
44    /// Whether the icon is in an expanded state.
45    ///
46    /// Rebuilding the widget with a different [`is_expanded`](Self::is_expanded) value will
47    /// trigger the animation, but will not trigger the [`on_pressed`](Self::on_pressed)
48    /// callback.
49    pub is_expanded: bool,
50
51    /// The size of the icon.
52    ///
53    /// Defaults to 24.
54    pub size: f64,
55
56    /// The callback triggered when the icon is pressed and the state changes
57    /// between expanded and collapsed. The value passed to the current state.
58    ///
59    /// If this is set to `None`, the button will be disabled.
60    pub on_pressed: Option<ValueChanged<bool>>,
61
62    /// The padding around the icon. The entire padded icon will react to input
63    /// gestures.
64    ///
65    /// Defaults to a padding of 8 on all sides.
66    pub padding: EdgeInsetsGeometry,
67
68    /// The color of the icon.
69    ///
70    /// Defaults to `Colors::BLACK54` when the theme's
71    /// `ThemeData::brightness` is [`Brightness::Light`] and to
72    /// `Colors::WHITE60` when it is [`Brightness::Dark`]. This adheres to the
73    /// Material Design specifications for [icons](https://material.io/design/iconography/system-icons.html#color)
74    /// and for [dark theme](https://material.io/design/color/dark-theme.html#ui-application)
75    pub color: Option<Color>,
76
77    /// The color of the icon when it is disabled,
78    /// i.e. if [`on_pressed`](Self::on_pressed) is `None`.
79    ///
80    /// Defaults to `Colors::BLACK38` when the theme's
81    /// `ThemeData::brightness` is [`Brightness::Light`] and to
82    /// `Colors::WHITE38` when it is [`Brightness::Dark`]. This adheres to the
83    /// Material Design specifications for [icons](https://material.io/design/iconography/system-icons.html#color)
84    /// and for [dark theme](https://material.io/design/color/dark-theme.html#ui-application)
85    pub disabled_color: Option<Color>,
86
87    /// The color of the icon when the icon is expanded.
88    ///
89    /// Defaults to `Colors::BLACK54` when the theme's
90    /// `ThemeData::brightness` is [`Brightness::Light`] and to
91    /// `Colors::WHITE` when it is [`Brightness::Dark`]. This adheres to the
92    /// Material Design specifications for [icons](https://material.io/design/iconography/system-icons.html#color)
93    /// and for [dark theme](https://material.io/design/color/dark-theme.html#ui-application)
94    pub expanded_color: Option<Color>,
95
96    /// Defines the splash color of the IconButton.
97    ///
98    /// If `ThemeData::use_material3` is true, this field will be ignored,
99    /// as `IconButton::splash_color` will be ignored, and you should use
100    /// [`highlight_color`](Self::highlight_color) instead.
101    ///
102    /// Defaults to `ThemeData::splash_color`.
103    pub splash_color: Option<Color>,
104
105    /// Defines the highlight color of the IconButton.
106    ///
107    /// Defaults to `ThemeData::highlight_color`.
108    pub highlight_color: Option<Color>,
109}
110
111impl ExpandIcon {
112    /// Creates an [`ExpandIcon`] with the given padding, and a callback that is
113    /// triggered when the icon is pressed; Dart's optional named arguments are the setters.
114    pub fn new(on_pressed: Option<ValueChanged<bool>>) -> ExpandIcon {
115        ExpandIcon {
116            key: None,
117            is_expanded: false,
118            size: 24.0,
119            on_pressed,
120            padding: EdgeInsetsGeometry::all(8.0),
121            color: None,
122            disabled_color: None,
123            expanded_color: None,
124            splash_color: None,
125            highlight_color: None,
126        }
127    }
128
129    /// Dart `ExpandIcon(key:)`.
130    pub fn key(mut self, key: KeyRef) -> ExpandIcon {
131        self.key = Some(key);
132        self
133    }
134
135    /// Dart `ExpandIcon(isExpanded:)`.
136    pub fn is_expanded(mut self, is_expanded: bool) -> ExpandIcon {
137        self.is_expanded = is_expanded;
138        self
139    }
140
141    /// Dart `ExpandIcon(size:)`.
142    pub fn size(mut self, size: f64) -> ExpandIcon {
143        self.size = size;
144        self
145    }
146
147    /// Dart `ExpandIcon(padding:)`.
148    pub fn padding(mut self, padding: impl Into<EdgeInsetsGeometry>) -> ExpandIcon {
149        self.padding = padding.into();
150        self
151    }
152
153    /// Dart `ExpandIcon(color:)`.
154    pub fn color(mut self, color: Color) -> ExpandIcon {
155        self.color = Some(color);
156        self
157    }
158
159    /// Dart `ExpandIcon(disabledColor:)`.
160    pub fn disabled_color(mut self, disabled_color: Color) -> ExpandIcon {
161        self.disabled_color = Some(disabled_color);
162        self
163    }
164
165    /// Dart `ExpandIcon(expandedColor:)`.
166    pub fn expanded_color(mut self, expanded_color: Color) -> ExpandIcon {
167        self.expanded_color = Some(expanded_color);
168        self
169    }
170
171    /// Dart `ExpandIcon(splashColor:)`.
172    pub fn splash_color(mut self, splash_color: Color) -> ExpandIcon {
173        self.splash_color = Some(splash_color);
174        self
175    }
176
177    /// Dart `ExpandIcon(highlightColor:)`.
178    pub fn highlight_color(mut self, highlight_color: Color) -> ExpandIcon {
179        self.highlight_color = Some(highlight_color);
180        self
181    }
182}
183
184impl fmt::Debug for ExpandIcon {
185    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
186        f.debug_struct("ExpandIcon")
187            .field("is_expanded", &self.is_expanded)
188            .field("size", &self.size)
189            .field("enabled", &self.on_pressed.is_some())
190            .field("padding", &self.padding)
191            .field("color", &self.color)
192            .field("disabled_color", &self.disabled_color)
193            .field("expanded_color", &self.expanded_color)
194            .field("splash_color", &self.splash_color)
195            .field("highlight_color", &self.highlight_color)
196            .finish()
197    }
198}
199
200impl StatefulWidget for ExpandIcon {
201    type State = ExpandIconState;
202
203    fn key(&self) -> Option<&KeyRef> {
204        self.key.as_ref()
205    }
206
207    fn create_state(&self) -> ExpandIconState {
208        ExpandIconState {
209            state: StateData::new(),
210            single_ticker_provider: SingleTickerProviderStateMixinData::default(),
211            controller: None,
212            icon_turns: None,
213            half_turn_tween: None,
214            fast_out_slow_in_tween: None,
215        }
216    }
217}
218
219/// Dart's `_ExpandIconState`.
220pub struct ExpandIconState {
221    state: StateData<ExpandIcon>,
222    single_ticker_provider: SingleTickerProviderStateMixinData,
223    /// Created in `init_state`, because a state is created without the [`App`].
224    controller: Option<Ref<AnimationController>>,
225    icon_turns: Option<AnimationRef<f64>>,
226    /// Dart's `static final Animatable<double> _iconTurnTween`, a `Tween` chained to a
227    /// `CurveTween`, created in `init_state` because an arena object needs the [`App`].
228    half_turn_tween: Option<Ref<Tween<f64>>>,
229    fast_out_slow_in_tween: Option<Ref<CurveTween>>,
230}
231
232impl ExpandIconState {
233    fn controller(self: Handle<Self>, app: &App) -> Handle<AnimationController> {
234        app.get(self)
235            .controller
236            .as_ref()
237            .expect("created in init_state")
238            .handle()
239    }
240
241    fn icon_turns(self: Handle<Self>, app: &App) -> AnimationRef<f64> {
242        app.get(self)
243            .icon_turns
244            .clone()
245            .expect("created in init_state")
246    }
247
248    fn handle_pressed(self: Handle<Self>, app: &mut App) {
249        let widget = self.widget(app);
250        let is_expanded = widget.is_expanded;
251        if let Some(on_pressed) = widget.on_pressed.clone() {
252            on_pressed(app, is_expanded);
253        }
254    }
255
256    /// Default icon colors and opacities for when `ThemeData::brightness` is set to
257    /// [`Brightness::Light`] are based on the
258    /// [Material Design system icon specifications](https://material.io/design/iconography/system-icons.html#color).
259    /// Icon colors and opacities for [`Brightness::Dark`] are based on the
260    /// [Material Design dark theme specifications](https://material.io/design/color/dark-theme.html#ui-application)
261    fn icon_color(self: Handle<Self>, app: &mut App, context: BuildContext) -> Color {
262        let widget = self.widget(app);
263        if widget.is_expanded
264            && let Some(expanded_color) = widget.expanded_color
265        {
266            return expanded_color;
267        }
268
269        if let Some(color) = widget.color {
270            return color;
271        }
272
273        match Theme::brightness_of(app, context) {
274            Brightness::Light => Colors::BLACK54,
275            Brightness::Dark => Colors::WHITE60,
276        }
277    }
278}
279
280impl SingleTickerProviderStateMixin for ExpandIconState {
281    fn single_ticker_provider_data(
282        self: Handle<Self>,
283        app: &App,
284    ) -> &SingleTickerProviderStateMixinData {
285        &app.get(self).single_ticker_provider
286    }
287
288    fn single_ticker_provider_data_mut(
289        self: Handle<Self>,
290        app: &mut App,
291    ) -> &mut SingleTickerProviderStateMixinData {
292        &mut app.get_mut(self).single_ticker_provider
293    }
294}
295
296/// Dart's `vsync: this`.
297impl TickerProviderObject for ExpandIconState {
298    fn create_ticker(self: Handle<Self>, app: &mut App, on_tick: TickerCallback) -> Ref<Ticker> {
299        SingleTickerProviderStateMixin::create_ticker(self, app, on_tick)
300    }
301}
302
303impl State for ExpandIconState {
304    type Widget = ExpandIcon;
305    waft_widgets::state_accessors!();
306
307    fn init_state(self: Handle<Self>, app: &mut App) {
308        let half_turn_tween = Tween::new(app, Some(0.0), Some(0.5));
309        let fast_out_slow_in_tween = CurveTween::new(app, Curves::fast_out_slow_in());
310        let controller = AnimationController::new(
311            app,
312            None,
313            Some(K_THEME_ANIMATION_DURATION),
314            None,
315            0.0,
316            1.0,
317            AnimationBehavior::Normal,
318            self,
319        );
320        let icon_turns = controller.drive(
321            app,
322            half_turn_tween
323                .clone()
324                .chain(fast_out_slow_in_tween.clone()),
325        );
326        let state = app.get_mut(self);
327        state.half_turn_tween = Some(half_turn_tween);
328        state.fast_out_slow_in_tween = Some(fast_out_slow_in_tween);
329        state.controller = Some(controller.clone());
330        state.icon_turns = Some(icon_turns);
331        // If the widget is initially expanded, rotate the icon without animating it.
332        if self.widget(app).is_expanded {
333            controller.set_value(app, PI);
334        }
335    }
336
337    fn activate(self: Handle<Self>, app: &mut App) {
338        SingleTickerProviderStateMixin::activate(self, app);
339    }
340
341    fn dispose(self: Handle<Self>, app: &mut App) {
342        let controller = self.controller(app);
343        controller.dispose(app);
344        SingleTickerProviderStateMixin::dispose(self, app);
345    }
346
347    fn did_update_widget(self: Handle<Self>, app: &mut App, old_widget: &ExpandIcon) {
348        let is_expanded = self.widget(app).is_expanded;
349        if is_expanded != old_widget.is_expanded {
350            let controller = self.controller(app);
351            if is_expanded {
352                controller.forward(app, None);
353            } else {
354                controller.reverse(app, None);
355            }
356        }
357    }
358
359    fn build(self: Handle<Self>, app: &mut App, context: BuildContext) -> WidgetRef {
360        debug_assert!(debug_check_has_material(app, context));
361        debug_assert!(debug_check_has_material_localizations(app, context));
362        let localizations = <dyn MaterialLocalizations>::of(app, context);
363        let on_tap_hint = if self.widget(app).is_expanded {
364            localizations.expanded_icon_tap_hint()
365        } else {
366            localizations.collapsed_icon_tap_hint()
367        };
368
369        let icon_color = self.icon_color(app, context);
370        let icon_turns = self.icon_turns(app);
371        let widget = self.widget(app);
372        let on_pressed = widget
373            .on_pressed
374            .as_ref()
375            .map(|_| Listener::handle_method(self, Self::handle_pressed));
376        let mut button = IconButton::new(
377            on_pressed,
378            RotationTransition::new(icon_turns).child(Icon::new(Some(Icons::expand_more()))),
379        )
380        .padding(widget.padding)
381        .icon_size(widget.size)
382        .color(icon_color);
383        if let Some(highlight_color) = widget.highlight_color {
384            button = button.highlight_color(highlight_color);
385        }
386        if let Some(splash_color) = widget.splash_color {
387            button = button.splash_color(splash_color);
388        }
389        if let Some(disabled_color) = widget.disabled_color {
390            button = button.disabled_color(disabled_color);
391        }
392        let mut semantics = Semantics::new();
393        if widget.on_pressed.is_some() {
394            semantics = semantics.on_tap_hint(on_tap_hint);
395        }
396        semantics.child(button).into_widget()
397    }
398}
399
400impl fmt::Debug for ExpandIconState {
401    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
402        f.debug_struct("ExpandIconState")
403            .field("controller", &self.controller)
404            .finish_non_exhaustive()
405    }
406}
407
408#[cfg(test)]
409mod tests {
410    use std::cell::Cell;
411    use std::rc::Rc;
412
413    use waft_embedder::Size;
414    use waft_widgets::testing::{WidgetTester, find, test_widgets};
415    use waft_widgets::{Animated, Center, IconTheme, SizedBox, StatefulBuilder};
416
417    use super::*;
418    use crate::material::Material;
419    use crate::scaffold::Scaffold;
420    use crate::test_support::material_app;
421    use crate::theme_data::ThemeData;
422
423    /// Dart's `ThemeData()`.
424    fn m3(app: &App) -> ThemeData {
425        ThemeData::builder().build(app)
426    }
427
428    /// Dart's `ThemeData(brightness: Brightness.dark)`.
429    fn m3_dark(app: &App) -> ThemeData {
430        ThemeData::builder().brightness(Brightness::Dark).build(app)
431    }
432
433    /// Dart's `wrap(child:, theme:)`: `MaterialApp(theme:, home: Center(child: Material(child:)))`.
434    fn wrap<K>(tester: &mut WidgetTester, theme: fn(&App) -> ThemeData, child: impl IntoWidget<K>) {
435        let theme = theme(&tester.cell.borrow());
436        tester.pump_widget(material_app(
437            theme,
438            Center::new()
439                .child(Material::new().child(child))
440                .into_widget(),
441        ));
442    }
443
444    /// Dart's `tester.firstWidget<IconTheme>(find.byType(IconTheme).last).data.color`.
445    fn last_icon_theme_color(tester: &WidgetTester) -> Option<Color> {
446        let last = *find::by_type::<IconTheme>()
447            .evaluate(tester)
448            .last()
449            .expect("an IconTheme");
450        tester.widget(last, |icon_theme: &IconTheme| {
451            icon_theme.data.color.as_deref().copied()
452        })
453    }
454
455    /// Dart's `tester.tap(find.byType(ExpandIcon))`.
456    fn tap_expand_icon(tester: &mut WidgetTester) {
457        let element = find::by_type::<ExpandIcon>().evaluate(tester)[0];
458        let center = tester.get_center(element);
459        tester.tap_at(center, 1);
460    }
461
462    fn toggling(expanded: &Rc<Cell<bool>>) -> ValueChanged<bool> {
463        let expanded = Rc::clone(expanded);
464        Rc::new(move |_app, _is_expanded| expanded.set(!expanded.get()))
465    }
466
467    /// "ExpandIcon test".
468    #[test]
469    fn expand_icon_test() {
470        test_widgets(|tester| {
471            let expanded = Rc::new(Cell::new(false));
472
473            // Light mode tests
474            wrap(tester, m3, ExpandIcon::new(Some(toggling(&expanded))));
475            tester.pump_and_settle();
476
477            assert!(!expanded.get());
478            assert_eq!(last_icon_theme_color(tester), Some(Colors::BLACK54));
479
480            tap_expand_icon(tester);
481            tester.pump_and_settle();
482            assert!(expanded.get());
483            assert_eq!(last_icon_theme_color(tester), Some(Colors::BLACK54));
484
485            tap_expand_icon(tester);
486            tester.pump_and_settle();
487            assert!(!expanded.get());
488            assert_eq!(last_icon_theme_color(tester), Some(Colors::BLACK54));
489
490            // Dark mode tests
491            wrap(tester, m3_dark, ExpandIcon::new(Some(toggling(&expanded))));
492            tester.pump_and_settle();
493
494            assert!(!expanded.get());
495            assert_eq!(last_icon_theme_color(tester), Some(Colors::WHITE60));
496
497            tap_expand_icon(tester);
498            tester.pump_and_settle();
499            assert!(expanded.get());
500            assert_eq!(last_icon_theme_color(tester), Some(Colors::WHITE60));
501
502            tap_expand_icon(tester);
503            tester.pump_and_settle();
504            assert!(!expanded.get());
505            assert_eq!(last_icon_theme_color(tester), Some(Colors::WHITE60));
506        });
507    }
508
509    /// "Material2 - ExpandIcon disabled".
510    #[test]
511    fn material2_expand_icon_disabled() {
512        test_widgets(|tester| {
513            // Test light mode.
514            wrap(
515                tester,
516                |app| ThemeData::builder().use_material3(false).build(app),
517                ExpandIcon::new(None),
518            );
519            tester.pump_and_settle();
520
521            assert_eq!(last_icon_theme_color(tester), Some(Colors::BLACK38));
522
523            // Test dark mode.
524            wrap(
525                tester,
526                |app| {
527                    ThemeData::builder()
528                        .use_material3(false)
529                        .brightness(Brightness::Dark)
530                        .build(app)
531                },
532                ExpandIcon::new(None),
533            );
534            tester.pump_and_settle();
535
536            assert_eq!(last_icon_theme_color(tester), Some(Colors::WHITE38));
537        });
538    }
539
540    /// "Material3 - ExpandIcon disabled".
541    #[test]
542    #[allow(deprecated)]
543    fn material3_expand_icon_disabled() {
544        test_widgets(|tester| {
545            // Test light mode.
546            wrap(tester, m3, ExpandIcon::new(None));
547            tester.pump_and_settle();
548
549            let theme = m3(&tester.cell.borrow());
550            assert_eq!(
551                last_icon_theme_color(tester),
552                Some(theme.color_scheme.on_surface().with_opacity(0.38))
553            );
554
555            // Test dark mode.
556            wrap(tester, m3_dark, ExpandIcon::new(None));
557            tester.pump_and_settle();
558
559            let theme = m3_dark(&tester.cell.borrow());
560            assert_eq!(
561                last_icon_theme_color(tester),
562                Some(theme.color_scheme.on_surface().with_opacity(0.38))
563            );
564        });
565    }
566
567    /// "ExpandIcon test isExpanded does not trigger callback".
568    #[test]
569    fn expand_icon_test_is_expanded_does_not_trigger_callback() {
570        test_widgets(|tester| {
571            let expanded = Rc::new(Cell::new(false));
572
573            wrap(tester, m3, ExpandIcon::new(Some(toggling(&expanded))));
574
575            wrap(
576                tester,
577                m3,
578                ExpandIcon::new(Some(toggling(&expanded))).is_expanded(true),
579            );
580
581            assert!(!expanded.get());
582        });
583    }
584
585    /// "ExpandIcon is rotated initially if isExpanded is true on first build".
586    #[test]
587    fn expand_icon_is_rotated_initially_if_is_expanded_is_true_on_first_build() {
588        test_widgets(|tester| {
589            wrap(
590                tester,
591                m3,
592                ExpandIcon::new(Some(Rc::new(|_app, _is_expanded| {}))).is_expanded(true),
593            );
594            let rotation = find::by_type::<Animated<RotationTransition>>().evaluate(tester)[0];
595            let turns = tester.widget(rotation, |rotation: &Animated<RotationTransition>| {
596                rotation.0.turns()
597            });
598            assert_eq!(turns.value(&tester.cell.borrow()), 0.5);
599        });
600    }
601
602    /// "ExpandIcon default size is 24".
603    #[test]
604    fn expand_icon_default_size_is_24() {
605        test_widgets(|tester| {
606            wrap(
607                tester,
608                m3,
609                ExpandIcon::new(Some(Rc::new(|_app, _is_expanded| {}))),
610            );
611
612            let icon = find::by_type::<ExpandIcon>().evaluate(tester)[0];
613            assert_eq!(tester.widget(icon, |icon: &ExpandIcon| icon.size), 24.0);
614        });
615    }
616
617    /// "ExpandIcon has the correct given size".
618    #[test]
619    fn expand_icon_has_the_correct_given_size() {
620        test_widgets(|tester| {
621            wrap(
622                tester,
623                m3,
624                ExpandIcon::new(Some(Rc::new(|_app, _is_expanded| {}))).size(36.0),
625            );
626
627            let icon = find::by_type::<ExpandIcon>().evaluate(tester)[0];
628            assert_eq!(tester.widget(icon, |icon: &ExpandIcon| icon.size), 36.0);
629
630            wrap(
631                tester,
632                m3,
633                ExpandIcon::new(Some(Rc::new(|_app, _is_expanded| {}))).size(48.0),
634            );
635
636            let icon = find::by_type::<ExpandIcon>().evaluate(tester)[0];
637            assert_eq!(tester.widget(icon, |icon: &ExpandIcon| icon.size), 48.0);
638        });
639    }
640
641    /// The `StatefulBuilder` of "ExpandIcon uses custom icon color and expanded icon color",
642    /// flipping `expanded` on every press.
643    fn stateful_expand_icon(
644        expanded: &Rc<Cell<bool>>,
645        configure: fn(ExpandIcon) -> ExpandIcon,
646    ) -> StatefulBuilder {
647        let expanded = Rc::clone(expanded);
648        StatefulBuilder::new(move |_app, _context, set_state| {
649            let on_pressed: ValueChanged<bool> = {
650                let expanded = Rc::clone(&expanded);
651                Rc::new(move |app, is_expanded| {
652                    set_state(app, &mut |_app| expanded.set(!is_expanded));
653                })
654            };
655            configure(ExpandIcon::new(Some(on_pressed)).is_expanded(expanded.get())).into_widget()
656        })
657    }
658
659    /// "ExpandIcon uses custom icon color and expanded icon color".
660    #[test]
661    fn expand_icon_uses_custom_icon_color_and_expanded_icon_color() {
662        test_widgets(|tester| {
663            let expanded = Rc::new(Cell::new(false));
664
665            wrap(
666                tester,
667                m3,
668                stateful_expand_icon(&expanded, |icon| icon.color(Colors::INDIGO.primary())),
669            );
670            tester.pump_and_settle();
671            assert_eq!(
672                last_icon_theme_color(tester),
673                Some(Colors::INDIGO.primary())
674            );
675
676            tap_expand_icon(tester);
677            tester.pump_and_settle();
678            assert_eq!(
679                last_icon_theme_color(tester),
680                Some(Colors::INDIGO.primary())
681            );
682
683            expanded.set(false);
684
685            wrap(
686                tester,
687                m3,
688                stateful_expand_icon(&expanded, |icon| {
689                    icon.color(Colors::INDIGO.primary())
690                        .expanded_color(Colors::TEAL.primary())
691                }),
692            );
693            tester.pump_and_settle();
694            assert_eq!(
695                last_icon_theme_color(tester),
696                Some(Colors::INDIGO.primary())
697            );
698
699            tap_expand_icon(tester);
700            tester.pump_and_settle();
701            assert_eq!(last_icon_theme_color(tester), Some(Colors::TEAL.primary()));
702
703            tap_expand_icon(tester);
704            tester.pump_and_settle();
705            assert_eq!(
706                last_icon_theme_color(tester),
707                Some(Colors::INDIGO.primary())
708            );
709        });
710    }
711
712    /// "ExpandIcon uses custom disabled icon color".
713    #[test]
714    fn expand_icon_uses_custom_disabled_icon_color() {
715        test_widgets(|tester| {
716            wrap(
717                tester,
718                m3,
719                ExpandIcon::new(None).disabled_color(Colors::CYAN.primary()),
720            );
721            tester.pump_and_settle();
722            assert_eq!(last_icon_theme_color(tester), Some(Colors::CYAN.primary()));
723
724            wrap(
725                tester,
726                m3,
727                ExpandIcon::new(None)
728                    .color(Colors::INDIGO.primary())
729                    .disabled_color(Colors::CYAN.primary()),
730            );
731            tester.pump_and_settle();
732            assert_eq!(last_icon_theme_color(tester), Some(Colors::CYAN.primary()));
733
734            wrap(
735                tester,
736                m3,
737                ExpandIcon::new(None)
738                    .is_expanded(true)
739                    .disabled_color(Colors::CYAN.primary()),
740            );
741            wrap(
742                tester,
743                m3,
744                ExpandIcon::new(None)
745                    .is_expanded(true)
746                    .expanded_color(Colors::TEAL.primary())
747                    .disabled_color(Colors::CYAN.primary()),
748            );
749            tester.pump_and_settle();
750            assert_eq!(last_icon_theme_color(tester), Some(Colors::CYAN.primary()));
751        });
752    }
753
754    /// "Expand icon does not crash at zero area".
755    #[test]
756    fn expand_icon_does_not_crash_at_zero_area() {
757        test_widgets(|tester| {
758            let theme = m3(&tester.cell.borrow());
759            tester.pump_widget(material_app(
760                theme,
761                Scaffold::new()
762                    .body(Center::new().child(
763                        SizedBox::shrink().child(ExpandIcon::new(Some(Rc::new(|_app, _value| {})))),
764                    ))
765                    .into_widget(),
766            ));
767            let icon = find::by_type::<ExpandIcon>().evaluate(tester)[0];
768            let size = tester.get_size(icon);
769            assert_eq!(size, Size::ZERO);
770        });
771    }
772}