waft-material 0.6.0

Material Design widgets for Waft
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
//! Flutter counterpart: material_ui's `expand_icon.dart`.

use std::f64::consts::PI;
use std::fmt;

use waft_animation::{
    Animatable, AnimationBehavior, AnimationController, AnimationRef, CurveTween, Curves, Drive,
    Tween,
};
use waft_embedder::{Brightness, Color};
use waft_foundation::{App, Handle, Listener, Ref, ValueChanged};
use waft_painting::EdgeInsetsGeometry;
use waft_scheduler::{Ticker, TickerCallback, TickerProviderObject};
use waft_widgets::{
    BuildContext, Icon, IntoWidget, KeyRef, RotationTransition, Semantics,
    SingleTickerProviderStateMixin, SingleTickerProviderStateMixinData, State, StateData,
    StatefulWidget, WidgetRef,
};

use crate::colors::Colors;
use crate::debug::{debug_check_has_material, debug_check_has_material_localizations};
use crate::icon_button::IconButton;
use crate::icons::Icons;
use crate::material_localizations::MaterialLocalizations;
use crate::theme::{K_THEME_ANIMATION_DURATION, Theme};

/// A widget representing a rotating expand/collapse button. The icon rotates
/// 180 degrees when pressed, then reverts the animation on a second press.
/// The underlying icon is `Icons::expand_more`.
///
/// The expand icon does not include a semantic label for accessibility. In
/// order to be accessible it should be combined with a label using
/// `MergeSemantics`. This is done automatically by the `ExpansionPanel` widget.
///
/// See [`IconButton`] for a more general implementation of a pressable button
/// with an icon.
///
/// See also:
///
///  * <https://material.io/design/iconography/system-icons.html>
pub struct ExpandIcon {
    pub key: Option<KeyRef>,

    /// Whether the icon is in an expanded state.
    ///
    /// Rebuilding the widget with a different [`is_expanded`](Self::is_expanded) value will
    /// trigger the animation, but will not trigger the [`on_pressed`](Self::on_pressed)
    /// callback.
    pub is_expanded: bool,

    /// The size of the icon.
    ///
    /// Defaults to 24.
    pub size: f64,

    /// The callback triggered when the icon is pressed and the state changes
    /// between expanded and collapsed. The value passed to the current state.
    ///
    /// If this is set to `None`, the button will be disabled.
    pub on_pressed: Option<ValueChanged<bool>>,

    /// The padding around the icon. The entire padded icon will react to input
    /// gestures.
    ///
    /// Defaults to a padding of 8 on all sides.
    pub padding: EdgeInsetsGeometry,

    /// The color of the icon.
    ///
    /// Defaults to `Colors::BLACK54` when the theme's
    /// `ThemeData::brightness` is [`Brightness::Light`] and to
    /// `Colors::WHITE60` when it is [`Brightness::Dark`]. This adheres to the
    /// Material Design specifications for [icons](https://material.io/design/iconography/system-icons.html#color)
    /// and for [dark theme](https://material.io/design/color/dark-theme.html#ui-application)
    pub color: Option<Color>,

    /// The color of the icon when it is disabled,
    /// i.e. if [`on_pressed`](Self::on_pressed) is `None`.
    ///
    /// Defaults to `Colors::BLACK38` when the theme's
    /// `ThemeData::brightness` is [`Brightness::Light`] and to
    /// `Colors::WHITE38` when it is [`Brightness::Dark`]. This adheres to the
    /// Material Design specifications for [icons](https://material.io/design/iconography/system-icons.html#color)
    /// and for [dark theme](https://material.io/design/color/dark-theme.html#ui-application)
    pub disabled_color: Option<Color>,

    /// The color of the icon when the icon is expanded.
    ///
    /// Defaults to `Colors::BLACK54` when the theme's
    /// `ThemeData::brightness` is [`Brightness::Light`] and to
    /// `Colors::WHITE` when it is [`Brightness::Dark`]. This adheres to the
    /// Material Design specifications for [icons](https://material.io/design/iconography/system-icons.html#color)
    /// and for [dark theme](https://material.io/design/color/dark-theme.html#ui-application)
    pub expanded_color: Option<Color>,

    /// Defines the splash color of the IconButton.
    ///
    /// If `ThemeData::use_material3` is true, this field will be ignored,
    /// as `IconButton::splash_color` will be ignored, and you should use
    /// [`highlight_color`](Self::highlight_color) instead.
    ///
    /// Defaults to `ThemeData::splash_color`.
    pub splash_color: Option<Color>,

    /// Defines the highlight color of the IconButton.
    ///
    /// Defaults to `ThemeData::highlight_color`.
    pub highlight_color: Option<Color>,
}

impl ExpandIcon {
    /// Creates an [`ExpandIcon`] with the given padding, and a callback that is
    /// triggered when the icon is pressed; Dart's optional named arguments are the setters.
    pub fn new(on_pressed: Option<ValueChanged<bool>>) -> ExpandIcon {
        ExpandIcon {
            key: None,
            is_expanded: false,
            size: 24.0,
            on_pressed,
            padding: EdgeInsetsGeometry::all(8.0),
            color: None,
            disabled_color: None,
            expanded_color: None,
            splash_color: None,
            highlight_color: None,
        }
    }

    /// Dart `ExpandIcon(key:)`.
    pub fn key(mut self, key: KeyRef) -> ExpandIcon {
        self.key = Some(key);
        self
    }

    /// Dart `ExpandIcon(isExpanded:)`.
    pub fn is_expanded(mut self, is_expanded: bool) -> ExpandIcon {
        self.is_expanded = is_expanded;
        self
    }

    /// Dart `ExpandIcon(size:)`.
    pub fn size(mut self, size: f64) -> ExpandIcon {
        self.size = size;
        self
    }

    /// Dart `ExpandIcon(padding:)`.
    pub fn padding(mut self, padding: impl Into<EdgeInsetsGeometry>) -> ExpandIcon {
        self.padding = padding.into();
        self
    }

    /// Dart `ExpandIcon(color:)`.
    pub fn color(mut self, color: Color) -> ExpandIcon {
        self.color = Some(color);
        self
    }

    /// Dart `ExpandIcon(disabledColor:)`.
    pub fn disabled_color(mut self, disabled_color: Color) -> ExpandIcon {
        self.disabled_color = Some(disabled_color);
        self
    }

    /// Dart `ExpandIcon(expandedColor:)`.
    pub fn expanded_color(mut self, expanded_color: Color) -> ExpandIcon {
        self.expanded_color = Some(expanded_color);
        self
    }

    /// Dart `ExpandIcon(splashColor:)`.
    pub fn splash_color(mut self, splash_color: Color) -> ExpandIcon {
        self.splash_color = Some(splash_color);
        self
    }

    /// Dart `ExpandIcon(highlightColor:)`.
    pub fn highlight_color(mut self, highlight_color: Color) -> ExpandIcon {
        self.highlight_color = Some(highlight_color);
        self
    }
}

impl fmt::Debug for ExpandIcon {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("ExpandIcon")
            .field("is_expanded", &self.is_expanded)
            .field("size", &self.size)
            .field("enabled", &self.on_pressed.is_some())
            .field("padding", &self.padding)
            .field("color", &self.color)
            .field("disabled_color", &self.disabled_color)
            .field("expanded_color", &self.expanded_color)
            .field("splash_color", &self.splash_color)
            .field("highlight_color", &self.highlight_color)
            .finish()
    }
}

impl StatefulWidget for ExpandIcon {
    type State = ExpandIconState;

    fn key(&self) -> Option<&KeyRef> {
        self.key.as_ref()
    }

    fn create_state(&self) -> ExpandIconState {
        ExpandIconState {
            state: StateData::new(),
            single_ticker_provider: SingleTickerProviderStateMixinData::default(),
            controller: None,
            icon_turns: None,
            half_turn_tween: None,
            fast_out_slow_in_tween: None,
        }
    }
}

/// Dart's `_ExpandIconState`.
pub struct ExpandIconState {
    state: StateData<ExpandIcon>,
    single_ticker_provider: SingleTickerProviderStateMixinData,
    /// Created in `init_state`, because a state is created without the [`App`].
    controller: Option<Ref<AnimationController>>,
    icon_turns: Option<AnimationRef<f64>>,
    /// Dart's `static final Animatable<double> _iconTurnTween`, a `Tween` chained to a
    /// `CurveTween`, created in `init_state` because an arena object needs the [`App`].
    half_turn_tween: Option<Ref<Tween<f64>>>,
    fast_out_slow_in_tween: Option<Ref<CurveTween>>,
}

impl ExpandIconState {
    fn controller(self: Handle<Self>, app: &App) -> Handle<AnimationController> {
        app.get(self)
            .controller
            .as_ref()
            .expect("created in init_state")
            .handle()
    }

    fn icon_turns(self: Handle<Self>, app: &App) -> AnimationRef<f64> {
        app.get(self)
            .icon_turns
            .clone()
            .expect("created in init_state")
    }

    fn handle_pressed(self: Handle<Self>, app: &mut App) {
        let widget = self.widget(app);
        let is_expanded = widget.is_expanded;
        if let Some(on_pressed) = widget.on_pressed.clone() {
            on_pressed(app, is_expanded);
        }
    }

    /// Default icon colors and opacities for when `ThemeData::brightness` is set to
    /// [`Brightness::Light`] are based on the
    /// [Material Design system icon specifications](https://material.io/design/iconography/system-icons.html#color).
    /// Icon colors and opacities for [`Brightness::Dark`] are based on the
    /// [Material Design dark theme specifications](https://material.io/design/color/dark-theme.html#ui-application)
    fn icon_color(self: Handle<Self>, app: &mut App, context: BuildContext) -> Color {
        let widget = self.widget(app);
        if widget.is_expanded
            && let Some(expanded_color) = widget.expanded_color
        {
            return expanded_color;
        }

        if let Some(color) = widget.color {
            return color;
        }

        match Theme::brightness_of(app, context) {
            Brightness::Light => Colors::BLACK54,
            Brightness::Dark => Colors::WHITE60,
        }
    }
}

impl SingleTickerProviderStateMixin for ExpandIconState {
    fn single_ticker_provider_data(
        self: Handle<Self>,
        app: &App,
    ) -> &SingleTickerProviderStateMixinData {
        &app.get(self).single_ticker_provider
    }

    fn single_ticker_provider_data_mut(
        self: Handle<Self>,
        app: &mut App,
    ) -> &mut SingleTickerProviderStateMixinData {
        &mut app.get_mut(self).single_ticker_provider
    }
}

/// Dart's `vsync: this`.
impl TickerProviderObject for ExpandIconState {
    fn create_ticker(self: Handle<Self>, app: &mut App, on_tick: TickerCallback) -> Ref<Ticker> {
        SingleTickerProviderStateMixin::create_ticker(self, app, on_tick)
    }
}

impl State for ExpandIconState {
    type Widget = ExpandIcon;
    waft_widgets::state_accessors!();

    fn init_state(self: Handle<Self>, app: &mut App) {
        let half_turn_tween = Tween::new(app, Some(0.0), Some(0.5));
        let fast_out_slow_in_tween = CurveTween::new(app, Curves::fast_out_slow_in());
        let controller = AnimationController::new(
            app,
            None,
            Some(K_THEME_ANIMATION_DURATION),
            None,
            0.0,
            1.0,
            AnimationBehavior::Normal,
            self,
        );
        let icon_turns = controller.drive(
            app,
            half_turn_tween
                .clone()
                .chain(fast_out_slow_in_tween.clone()),
        );
        let state = app.get_mut(self);
        state.half_turn_tween = Some(half_turn_tween);
        state.fast_out_slow_in_tween = Some(fast_out_slow_in_tween);
        state.controller = Some(controller.clone());
        state.icon_turns = Some(icon_turns);
        // If the widget is initially expanded, rotate the icon without animating it.
        if self.widget(app).is_expanded {
            controller.set_value(app, PI);
        }
    }

    fn activate(self: Handle<Self>, app: &mut App) {
        SingleTickerProviderStateMixin::activate(self, app);
    }

    fn dispose(self: Handle<Self>, app: &mut App) {
        let controller = self.controller(app);
        controller.dispose(app);
        SingleTickerProviderStateMixin::dispose(self, app);
    }

    fn did_update_widget(self: Handle<Self>, app: &mut App, old_widget: &ExpandIcon) {
        let is_expanded = self.widget(app).is_expanded;
        if is_expanded != old_widget.is_expanded {
            let controller = self.controller(app);
            if is_expanded {
                controller.forward(app, None);
            } else {
                controller.reverse(app, None);
            }
        }
    }

    fn build(self: Handle<Self>, app: &mut App, context: BuildContext) -> WidgetRef {
        debug_assert!(debug_check_has_material(app, context));
        debug_assert!(debug_check_has_material_localizations(app, context));
        let localizations = <dyn MaterialLocalizations>::of(app, context);
        let on_tap_hint = if self.widget(app).is_expanded {
            localizations.expanded_icon_tap_hint()
        } else {
            localizations.collapsed_icon_tap_hint()
        };

        let icon_color = self.icon_color(app, context);
        let icon_turns = self.icon_turns(app);
        let widget = self.widget(app);
        let on_pressed = widget
            .on_pressed
            .as_ref()
            .map(|_| Listener::handle_method(self, Self::handle_pressed));
        let mut button = IconButton::new(
            on_pressed,
            RotationTransition::new(icon_turns).child(Icon::new(Some(Icons::expand_more()))),
        )
        .padding(widget.padding)
        .icon_size(widget.size)
        .color(icon_color);
        if let Some(highlight_color) = widget.highlight_color {
            button = button.highlight_color(highlight_color);
        }
        if let Some(splash_color) = widget.splash_color {
            button = button.splash_color(splash_color);
        }
        if let Some(disabled_color) = widget.disabled_color {
            button = button.disabled_color(disabled_color);
        }
        let mut semantics = Semantics::new();
        if widget.on_pressed.is_some() {
            semantics = semantics.on_tap_hint(on_tap_hint);
        }
        semantics.child(button).into_widget()
    }
}

impl fmt::Debug for ExpandIconState {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("ExpandIconState")
            .field("controller", &self.controller)
            .finish_non_exhaustive()
    }
}

#[cfg(test)]
mod tests {
    use std::cell::Cell;
    use std::rc::Rc;

    use waft_embedder::Size;
    use waft_widgets::testing::{WidgetTester, find, test_widgets};
    use waft_widgets::{Animated, Center, IconTheme, SizedBox, StatefulBuilder};

    use super::*;
    use crate::material::Material;
    use crate::scaffold::Scaffold;
    use crate::test_support::material_app;
    use crate::theme_data::ThemeData;

    /// Dart's `ThemeData()`.
    fn m3(app: &App) -> ThemeData {
        ThemeData::builder().build(app)
    }

    /// Dart's `ThemeData(brightness: Brightness.dark)`.
    fn m3_dark(app: &App) -> ThemeData {
        ThemeData::builder().brightness(Brightness::Dark).build(app)
    }

    /// Dart's `wrap(child:, theme:)`: `MaterialApp(theme:, home: Center(child: Material(child:)))`.
    fn wrap<K>(tester: &mut WidgetTester, theme: fn(&App) -> ThemeData, child: impl IntoWidget<K>) {
        let theme = theme(&tester.cell.borrow());
        tester.pump_widget(material_app(
            theme,
            Center::new()
                .child(Material::new().child(child))
                .into_widget(),
        ));
    }

    /// Dart's `tester.firstWidget<IconTheme>(find.byType(IconTheme).last).data.color`.
    fn last_icon_theme_color(tester: &WidgetTester) -> Option<Color> {
        let last = *find::by_type::<IconTheme>()
            .evaluate(tester)
            .last()
            .expect("an IconTheme");
        tester.widget(last, |icon_theme: &IconTheme| {
            icon_theme.data.color.as_deref().copied()
        })
    }

    /// Dart's `tester.tap(find.byType(ExpandIcon))`.
    fn tap_expand_icon(tester: &mut WidgetTester) {
        let element = find::by_type::<ExpandIcon>().evaluate(tester)[0];
        let center = tester.get_center(element);
        tester.tap_at(center, 1);
    }

    fn toggling(expanded: &Rc<Cell<bool>>) -> ValueChanged<bool> {
        let expanded = Rc::clone(expanded);
        Rc::new(move |_app, _is_expanded| expanded.set(!expanded.get()))
    }

    /// "ExpandIcon test".
    #[test]
    fn expand_icon_test() {
        test_widgets(|tester| {
            let expanded = Rc::new(Cell::new(false));

            // Light mode tests
            wrap(tester, m3, ExpandIcon::new(Some(toggling(&expanded))));
            tester.pump_and_settle();

            assert!(!expanded.get());
            assert_eq!(last_icon_theme_color(tester), Some(Colors::BLACK54));

            tap_expand_icon(tester);
            tester.pump_and_settle();
            assert!(expanded.get());
            assert_eq!(last_icon_theme_color(tester), Some(Colors::BLACK54));

            tap_expand_icon(tester);
            tester.pump_and_settle();
            assert!(!expanded.get());
            assert_eq!(last_icon_theme_color(tester), Some(Colors::BLACK54));

            // Dark mode tests
            wrap(tester, m3_dark, ExpandIcon::new(Some(toggling(&expanded))));
            tester.pump_and_settle();

            assert!(!expanded.get());
            assert_eq!(last_icon_theme_color(tester), Some(Colors::WHITE60));

            tap_expand_icon(tester);
            tester.pump_and_settle();
            assert!(expanded.get());
            assert_eq!(last_icon_theme_color(tester), Some(Colors::WHITE60));

            tap_expand_icon(tester);
            tester.pump_and_settle();
            assert!(!expanded.get());
            assert_eq!(last_icon_theme_color(tester), Some(Colors::WHITE60));
        });
    }

    /// "Material2 - ExpandIcon disabled".
    #[test]
    fn material2_expand_icon_disabled() {
        test_widgets(|tester| {
            // Test light mode.
            wrap(
                tester,
                |app| ThemeData::builder().use_material3(false).build(app),
                ExpandIcon::new(None),
            );
            tester.pump_and_settle();

            assert_eq!(last_icon_theme_color(tester), Some(Colors::BLACK38));

            // Test dark mode.
            wrap(
                tester,
                |app| {
                    ThemeData::builder()
                        .use_material3(false)
                        .brightness(Brightness::Dark)
                        .build(app)
                },
                ExpandIcon::new(None),
            );
            tester.pump_and_settle();

            assert_eq!(last_icon_theme_color(tester), Some(Colors::WHITE38));
        });
    }

    /// "Material3 - ExpandIcon disabled".
    #[test]
    #[allow(deprecated)]
    fn material3_expand_icon_disabled() {
        test_widgets(|tester| {
            // Test light mode.
            wrap(tester, m3, ExpandIcon::new(None));
            tester.pump_and_settle();

            let theme = m3(&tester.cell.borrow());
            assert_eq!(
                last_icon_theme_color(tester),
                Some(theme.color_scheme.on_surface.with_opacity(0.38))
            );

            // Test dark mode.
            wrap(tester, m3_dark, ExpandIcon::new(None));
            tester.pump_and_settle();

            let theme = m3_dark(&tester.cell.borrow());
            assert_eq!(
                last_icon_theme_color(tester),
                Some(theme.color_scheme.on_surface.with_opacity(0.38))
            );
        });
    }

    /// "ExpandIcon test isExpanded does not trigger callback".
    #[test]
    fn expand_icon_test_is_expanded_does_not_trigger_callback() {
        test_widgets(|tester| {
            let expanded = Rc::new(Cell::new(false));

            wrap(tester, m3, ExpandIcon::new(Some(toggling(&expanded))));

            wrap(
                tester,
                m3,
                ExpandIcon::new(Some(toggling(&expanded))).is_expanded(true),
            );

            assert!(!expanded.get());
        });
    }

    /// "ExpandIcon is rotated initially if isExpanded is true on first build".
    #[test]
    fn expand_icon_is_rotated_initially_if_is_expanded_is_true_on_first_build() {
        test_widgets(|tester| {
            wrap(
                tester,
                m3,
                ExpandIcon::new(Some(Rc::new(|_app, _is_expanded| {}))).is_expanded(true),
            );
            let rotation = find::by_type::<Animated<RotationTransition>>().evaluate(tester)[0];
            let turns = tester.widget(rotation, |rotation: &Animated<RotationTransition>| {
                rotation.0.turns()
            });
            assert_eq!(turns.value(&tester.cell.borrow()), 0.5);
        });
    }

    /// "ExpandIcon default size is 24".
    #[test]
    fn expand_icon_default_size_is_24() {
        test_widgets(|tester| {
            wrap(
                tester,
                m3,
                ExpandIcon::new(Some(Rc::new(|_app, _is_expanded| {}))),
            );

            let icon = find::by_type::<ExpandIcon>().evaluate(tester)[0];
            assert_eq!(tester.widget(icon, |icon: &ExpandIcon| icon.size), 24.0);
        });
    }

    /// "ExpandIcon has the correct given size".
    #[test]
    fn expand_icon_has_the_correct_given_size() {
        test_widgets(|tester| {
            wrap(
                tester,
                m3,
                ExpandIcon::new(Some(Rc::new(|_app, _is_expanded| {}))).size(36.0),
            );

            let icon = find::by_type::<ExpandIcon>().evaluate(tester)[0];
            assert_eq!(tester.widget(icon, |icon: &ExpandIcon| icon.size), 36.0);

            wrap(
                tester,
                m3,
                ExpandIcon::new(Some(Rc::new(|_app, _is_expanded| {}))).size(48.0),
            );

            let icon = find::by_type::<ExpandIcon>().evaluate(tester)[0];
            assert_eq!(tester.widget(icon, |icon: &ExpandIcon| icon.size), 48.0);
        });
    }

    /// The `StatefulBuilder` of "ExpandIcon uses custom icon color and expanded icon color",
    /// flipping `expanded` on every press.
    fn stateful_expand_icon(
        expanded: &Rc<Cell<bool>>,
        configure: fn(ExpandIcon) -> ExpandIcon,
    ) -> StatefulBuilder {
        let expanded = Rc::clone(expanded);
        StatefulBuilder::new(move |_app, _context, set_state| {
            let on_pressed: ValueChanged<bool> = {
                let expanded = Rc::clone(&expanded);
                Rc::new(move |app, is_expanded| {
                    set_state(app, &mut |_app| expanded.set(!is_expanded));
                })
            };
            configure(ExpandIcon::new(Some(on_pressed)).is_expanded(expanded.get())).into_widget()
        })
    }

    /// "ExpandIcon uses custom icon color and expanded icon color".
    #[test]
    fn expand_icon_uses_custom_icon_color_and_expanded_icon_color() {
        test_widgets(|tester| {
            let expanded = Rc::new(Cell::new(false));

            wrap(
                tester,
                m3,
                stateful_expand_icon(&expanded, |icon| icon.color(Colors::INDIGO.primary())),
            );
            tester.pump_and_settle();
            assert_eq!(
                last_icon_theme_color(tester),
                Some(Colors::INDIGO.primary())
            );

            tap_expand_icon(tester);
            tester.pump_and_settle();
            assert_eq!(
                last_icon_theme_color(tester),
                Some(Colors::INDIGO.primary())
            );

            expanded.set(false);

            wrap(
                tester,
                m3,
                stateful_expand_icon(&expanded, |icon| {
                    icon.color(Colors::INDIGO.primary())
                        .expanded_color(Colors::TEAL.primary())
                }),
            );
            tester.pump_and_settle();
            assert_eq!(
                last_icon_theme_color(tester),
                Some(Colors::INDIGO.primary())
            );

            tap_expand_icon(tester);
            tester.pump_and_settle();
            assert_eq!(last_icon_theme_color(tester), Some(Colors::TEAL.primary()));

            tap_expand_icon(tester);
            tester.pump_and_settle();
            assert_eq!(
                last_icon_theme_color(tester),
                Some(Colors::INDIGO.primary())
            );
        });
    }

    /// "ExpandIcon uses custom disabled icon color".
    #[test]
    fn expand_icon_uses_custom_disabled_icon_color() {
        test_widgets(|tester| {
            wrap(
                tester,
                m3,
                ExpandIcon::new(None).disabled_color(Colors::CYAN.primary()),
            );
            tester.pump_and_settle();
            assert_eq!(last_icon_theme_color(tester), Some(Colors::CYAN.primary()));

            wrap(
                tester,
                m3,
                ExpandIcon::new(None)
                    .color(Colors::INDIGO.primary())
                    .disabled_color(Colors::CYAN.primary()),
            );
            tester.pump_and_settle();
            assert_eq!(last_icon_theme_color(tester), Some(Colors::CYAN.primary()));

            wrap(
                tester,
                m3,
                ExpandIcon::new(None)
                    .is_expanded(true)
                    .disabled_color(Colors::CYAN.primary()),
            );
            wrap(
                tester,
                m3,
                ExpandIcon::new(None)
                    .is_expanded(true)
                    .expanded_color(Colors::TEAL.primary())
                    .disabled_color(Colors::CYAN.primary()),
            );
            tester.pump_and_settle();
            assert_eq!(last_icon_theme_color(tester), Some(Colors::CYAN.primary()));
        });
    }

    /// "Expand icon does not crash at zero area".
    #[test]
    fn expand_icon_does_not_crash_at_zero_area() {
        test_widgets(|tester| {
            let theme = m3(&tester.cell.borrow());
            tester.pump_widget(material_app(
                theme,
                Scaffold::new()
                    .body(Center::new().child(
                        SizedBox::shrink().child(ExpandIcon::new(Some(Rc::new(|_app, _value| {})))),
                    ))
                    .into_widget(),
            ));
            let icon = find::by_type::<ExpandIcon>().evaluate(tester)[0];
            let size = tester.get_size(icon);
            assert_eq!(size, Size::ZERO);
        });
    }
}