waft-material 0.18.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
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
//! Flutter counterpart: material_ui's `button_theme.dart`.

use std::fmt::{self, Debug};
use std::time::Duration;

use waft_embedder::{Brightness, Color, Radius};
use waft_foundation::{App, DiagnosticsProperty, DoubleProperty, EnumProperty, FlagProperty};
use waft_painting::{
    BorderRadiusGeometry, BorderSide, ColorProperty, EdgeInsetsGeometry, RoundedRectangleBorder,
    ShapeBorderRef,
};
use waft_rendering::BoxConstraints;
use waft_widgets::{BuildContext, InheritedTheme, InheritedWidget, IntoWidget, KeyRef, WidgetRef};

use crate::color_scheme::ColorScheme;
use crate::colors::Colors;
use crate::constants::K_THEME_CHANGE_DURATION;
use crate::material_button::MaterialButton;
use crate::theme::Theme;
use crate::theme_data::{MaterialTapTargetSize, ThemeData};

/// Used with [`ButtonTheme`] and [`ButtonThemeData`] to define a button's base colors, and the
/// defaults for the button's minimum size, internal padding, and shape.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub enum ButtonTextTheme {
    /// Button text is black or white depending on `ThemeData::brightness`.
    Normal,

    /// Button text is `ColorScheme::secondary`.
    Accent,

    /// Button text is based on `ThemeData::primary_color`.
    Primary,
}

/// Used with [`ButtonTheme`] and [`ButtonThemeData`] to define how the button bar should size
/// itself with either constraints or internal padding.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub enum ButtonBarLayoutBehavior {
    /// Button bars will be constrained to a minimum height of 52.
    ///
    /// This setting is require to create button bars which conform to the Material Design
    /// specification.
    Constrained,

    /// Button bars will calculate their padding from the button theme padding.
    Padded,
}

/// Used with [`ButtonThemeData`] to configure the color and geometry of buttons.
///
/// This class is planned to be deprecated in a future release. Please use one or more of these
/// buttons and associated themes instead:
///
///  * `ElevatedButton`, `ElevatedButtonTheme`, `ElevatedButtonThemeData`,
///  * `FilledButton`, `FilledButtonTheme`, `FilledButtonThemeData`,
///  * `OutlinedButton`, `OutlinedButtonTheme`, `OutlinedButtonThemeData`
///  * `TextButton`, `TextButtonTheme`, `TextButtonThemeData`,
///
/// A button theme can be specified as part of the overall Material theme using
/// `ThemeData::button_theme`. The Material theme's button theme data can be overridden with
/// [`ButtonTheme`].
///
/// The actual appearance of buttons depends on the button theme, the button's enabled state,
/// its elevation (if any), and the overall [`Theme`].
///
/// See also:
///
///  * `RawMaterialButton`, which can be used to configure a button that doesn't depend on any
///    inherited themes.
#[derive(Debug)]
pub struct ButtonTheme {
    pub key: Option<KeyRef>,
    /// Specifies the color and geometry of buttons.
    pub data: ButtonThemeData,
    pub child: WidgetRef,
}

impl ButtonTheme {
    /// Creates a button theme.
    #[cfg_attr(debug_assertions, track_caller)]
    pub fn new<K>(child: impl IntoWidget<K>) -> ButtonTheme {
        ButtonTheme::from_button_theme_data(ButtonThemeData::new(), child)
    }

    /// Creates a button theme from `data`.
    #[cfg_attr(debug_assertions, track_caller)]
    pub fn from_button_theme_data<K>(
        data: ButtonThemeData,
        child: impl IntoWidget<K>,
    ) -> ButtonTheme {
        ButtonTheme {
            key: None,
            data,
            child: child.into_widget(),
        }
    }

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

    /// Dart `ButtonTheme(textTheme:)`.
    pub fn text_theme(mut self, text_theme: ButtonTextTheme) -> ButtonTheme {
        self.data = self.data.with_text_theme(text_theme);
        self
    }

    /// Dart `ButtonTheme(layoutBehavior:)`.
    pub fn layout_behavior(mut self, layout_behavior: ButtonBarLayoutBehavior) -> ButtonTheme {
        self.data = self.data.with_layout_behavior(layout_behavior);
        self
    }

    /// Dart `ButtonTheme(minWidth:)`.
    pub fn min_width(mut self, min_width: f64) -> ButtonTheme {
        self.data = self.data.with_min_width(min_width);
        self
    }

    /// Dart `ButtonTheme(height:)`.
    pub fn height(mut self, height: f64) -> ButtonTheme {
        self.data = self.data.with_height(height);
        self
    }

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

    /// Dart `ButtonTheme(shape:)`.
    pub fn shape(mut self, shape: impl Into<ShapeBorderRef>) -> ButtonTheme {
        self.data = self.data.with_shape(shape);
        self
    }

    /// Dart `ButtonTheme(alignedDropdown:)`.
    pub fn aligned_dropdown(mut self, aligned_dropdown: bool) -> ButtonTheme {
        self.data = self.data.with_aligned_dropdown(aligned_dropdown);
        self
    }

    /// Dart `ButtonTheme(buttonColor:)`.
    pub fn button_color(mut self, button_color: Color) -> ButtonTheme {
        self.data = self.data.with_button_color(button_color);
        self
    }

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

    /// Dart `ButtonTheme(focusColor:)`.
    pub fn focus_color(mut self, focus_color: Color) -> ButtonTheme {
        self.data = self.data.with_focus_color(focus_color);
        self
    }

    /// Dart `ButtonTheme(hoverColor:)`.
    pub fn hover_color(mut self, hover_color: Color) -> ButtonTheme {
        self.data = self.data.with_hover_color(hover_color);
        self
    }

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

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

    /// Dart `ButtonTheme(colorScheme:)`.
    pub fn color_scheme(mut self, color_scheme: ColorScheme) -> ButtonTheme {
        self.data = self.data.with_color_scheme(color_scheme);
        self
    }

    /// Dart `ButtonTheme(materialTapTargetSize:)`.
    pub fn material_tap_target_size(
        mut self,
        material_tap_target_size: MaterialTapTargetSize,
    ) -> ButtonTheme {
        self.data = self
            .data
            .with_material_tap_target_size(material_tap_target_size);
        self
    }

    /// Retrieves the [`ButtonThemeData`] from the closest ancestor [`ButtonTheme`] widget.
    ///
    /// Typical usage is as follows:
    ///
    /// ```text
    /// let theme = ButtonTheme::of(app, context);
    /// ```
    pub fn of(app: &mut App, context: BuildContext) -> ButtonThemeData {
        let inherited_button_theme =
            context.depend_on_inherited_widget_of_exact_type::<ButtonTheme>(app);
        let mut button_theme = inherited_button_theme.map(|theme| theme.data.clone());
        if button_theme
            .as_ref()
            .is_none_or(|button_theme| button_theme.color_scheme.is_none())
        {
            // if buttonTheme or buttonTheme.colorScheme is null
            let theme = Theme::of(app, context);
            let mut theme_button_theme = button_theme.unwrap_or_else(|| theme.button_theme.clone());
            if theme_button_theme.color_scheme.is_none() {
                theme_button_theme = theme_button_theme.copy_with().with_color_scheme(
                    theme
                        .button_theme
                        .color_scheme
                        .clone()
                        .unwrap_or_else(|| theme.color_scheme.clone()),
                );
                debug_assert!(theme_button_theme.color_scheme.is_some());
            }
            button_theme = Some(theme_button_theme);
        }
        button_theme.expect("filled above")
    }
}

impl InheritedTheme for ButtonTheme {
    fn wrap(&self, _app: &mut App, _context: BuildContext, child: WidgetRef) -> WidgetRef {
        ButtonTheme::from_button_theme_data(self.data.clone(), child).into_widget()
    }
}

impl InheritedWidget for ButtonTheme {
    fn key(&self) -> Option<&KeyRef> {
        self.key.as_ref()
    }

    fn child(&self) -> &WidgetRef {
        &self.child
    }

    fn update_should_notify(&self, old_widget: &ButtonTheme) -> bool {
        self.data != old_widget.data
    }

    fn as_inherited_theme(&self) -> Option<&dyn InheritedTheme> {
        Some(self)
    }
}

/// Used with [`ButtonTheme`] to configure the color and geometry of buttons.
///
/// This class is planned to be deprecated in a future release. Please use one or more of these
/// buttons and associated themes instead:
///
///  * `TextButton`, `TextButtonTheme`, `TextButtonThemeData`,
///  * `ElevatedButton`, `ElevatedButtonTheme`, `ElevatedButtonThemeData`,
///  * `OutlinedButton`, `OutlinedButtonTheme`, `OutlinedButtonThemeData`
///
/// A button theme can be specified as part of the overall Material theme using
/// `ThemeData::button_theme`. The Material theme's button theme data can be overridden with
/// [`ButtonTheme`].
///
/// [`padding`](Self::padding) and [`shape`](Self::shape) are getters that fall back to a
/// default for the [`text_theme`](Self::text_theme), so the fields hold only what was given;
/// [`copy_with`](Self::copy_with), as Dart's `copyWith`, keeps the worked-out values.
#[derive(Clone)]
pub struct ButtonThemeData {
    /// The minimum width for buttons.
    ///
    /// The actual horizontal space allocated for a button's child is at least this value less
    /// the theme's horizontal [`padding`](Self::padding).
    ///
    /// Defaults to 88.0 logical pixels.
    pub min_width: f64,

    /// The minimum height for buttons.
    ///
    /// Defaults to 36.0 logical pixels.
    pub height: f64,

    /// Defines a button's base colors, and the defaults for the button's minimum size, internal
    /// padding, and shape.
    ///
    /// Despite the name, this property is not a `TextTheme`, its value is not a collection of
    /// `TextStyle`s.
    pub text_theme: ButtonTextTheme,

    /// Defines whether a `ButtonBar` should size itself with a minimum size constraint or with
    /// padding.
    ///
    /// Defaults to [`ButtonBarLayoutBehavior::Padded`].
    pub layout_behavior: ButtonBarLayoutBehavior,

    padding: Option<EdgeInsetsGeometry>,

    shape: Option<ShapeBorderRef>,

    /// If true, then a `DropdownButton` menu's width will match the button's width.
    ///
    /// If false (the default), then the dropdown's menu will be wider than its button. In
    /// either case the dropdown button will line up the leading edge of the menu's value with
    /// the leading edge of the values displayed by the menu items.
    ///
    /// This property only affects `DropdownButton` and its menu.
    pub aligned_dropdown: bool,

    /// The background fill color.
    ///
    /// This property is null by default.
    ///
    /// If the button is in the focused, hovering, or highlighted state, then the `focus_color`,
    /// `hover_color`, or `highlight_color` will take precedence over the `button_color`.
    button_color: Option<Color>,

    /// The background fill color when disabled.
    ///
    /// This property is null by default.
    disabled_color: Option<Color>,

    /// The fill color of the button when it has the input focus.
    ///
    /// This property is null by default.
    ///
    /// If the button is in the hovering or highlighted state, then the `hover_color` or
    /// `highlight_color` will take precedence over the `focus_color`.
    focus_color: Option<Color>,

    /// The fill color of the button when a pointer is hovering over it.
    ///
    /// This property is null by default.
    ///
    /// If the button is in the highlighted state, then the `highlight_color` will take
    /// precedence over the `hover_color`.
    hover_color: Option<Color>,

    /// The color of the overlay that appears when a button is pressed.
    ///
    /// This property is null by default.
    highlight_color: Option<Color>,

    /// The color of the ink "splash" overlay that appears when a button is tapped.
    ///
    /// This property is null by default.
    splash_color: Option<Color>,

    /// A set of thirteen colors that can be used to derive the button theme's colors.
    ///
    /// This property was added much later than the theme's set of highly specific colors, like
    /// `ThemeData::highlight_color` and `ThemeData::splash_color` etc.
    ///
    /// The colors for new button classes can be defined exclusively in terms of
    /// [`color_scheme`](Self::color_scheme). When it's possible, the existing buttons will
    /// (continue to) gradually migrate to it.
    pub color_scheme: Option<ColorScheme>,

    // The minimum size of a button's tap target.
    //
    // This property is null by default.
    material_tap_target_size: Option<MaterialTapTargetSize>,
}

impl ButtonThemeData {
    /// Create a button theme object that can be used with [`ButtonTheme`] or `ThemeData`.
    ///
    /// The [`min_width`](Self::min_width) and [`height`](Self::height) parameters must greater
    /// than or equal to zero.
    pub fn new() -> ButtonThemeData {
        ButtonThemeData {
            min_width: 88.0,
            height: 36.0,
            text_theme: ButtonTextTheme::Normal,
            layout_behavior: ButtonBarLayoutBehavior::Padded,
            padding: None,
            shape: None,
            aligned_dropdown: false,
            button_color: None,
            disabled_color: None,
            focus_color: None,
            hover_color: None,
            highlight_color: None,
            splash_color: None,
            color_scheme: None,
            material_tap_target_size: None,
        }
    }

    /// Dart `ButtonThemeData(textTheme:)`.
    pub fn with_text_theme(mut self, text_theme: ButtonTextTheme) -> ButtonThemeData {
        self.text_theme = text_theme;
        self
    }

    /// Dart `ButtonThemeData(minWidth:)`.
    pub fn with_min_width(mut self, min_width: f64) -> ButtonThemeData {
        debug_assert!(min_width >= 0.0);
        self.min_width = min_width;
        self
    }

    /// Dart `ButtonThemeData(height:)`.
    pub fn with_height(mut self, height: f64) -> ButtonThemeData {
        debug_assert!(height >= 0.0);
        self.height = height;
        self
    }

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

    /// Dart `ButtonThemeData(shape:)`.
    pub fn with_shape(mut self, shape: impl Into<ShapeBorderRef>) -> ButtonThemeData {
        self.shape = Some(shape.into());
        self
    }

    /// Dart `ButtonThemeData(layoutBehavior:)`.
    pub fn with_layout_behavior(
        mut self,
        layout_behavior: ButtonBarLayoutBehavior,
    ) -> ButtonThemeData {
        self.layout_behavior = layout_behavior;
        self
    }

    /// Dart `ButtonThemeData(alignedDropdown:)`.
    pub fn with_aligned_dropdown(mut self, aligned_dropdown: bool) -> ButtonThemeData {
        self.aligned_dropdown = aligned_dropdown;
        self
    }

    /// Dart `ButtonThemeData(buttonColor:)`.
    pub fn with_button_color(mut self, button_color: Color) -> ButtonThemeData {
        self.button_color = Some(button_color);
        self
    }

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

    /// Dart `ButtonThemeData(focusColor:)`.
    pub fn with_focus_color(mut self, focus_color: Color) -> ButtonThemeData {
        self.focus_color = Some(focus_color);
        self
    }

    /// Dart `ButtonThemeData(hoverColor:)`.
    pub fn with_hover_color(mut self, hover_color: Color) -> ButtonThemeData {
        self.hover_color = Some(hover_color);
        self
    }

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

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

    /// Dart `ButtonThemeData(colorScheme:)`.
    pub fn with_color_scheme(mut self, color_scheme: ColorScheme) -> ButtonThemeData {
        self.color_scheme = Some(color_scheme);
        self
    }

    /// Dart `ButtonThemeData(materialTapTargetSize:)`.
    pub fn with_material_tap_target_size(
        mut self,
        material_tap_target_size: MaterialTapTargetSize,
    ) -> ButtonThemeData {
        self.material_tap_target_size = Some(material_tap_target_size);
        self
    }

    /// Convenience that returns [`min_width`](Self::min_width) and [`height`](Self::height) as
    /// a [`BoxConstraints`] object.
    pub fn constraints(&self) -> BoxConstraints {
        BoxConstraints::new()
            .min_width(self.min_width)
            .min_height(self.height)
    }

    /// Padding for a button's child (typically the button's label).
    ///
    /// Defaults to 24.0 on the left and right if [`text_theme`](Self::text_theme) is
    /// [`ButtonTextTheme::Primary`], 16.0 on the left and right otherwise.
    pub fn padding(&self) -> EdgeInsetsGeometry {
        self.padding.unwrap_or(match self.text_theme {
            ButtonTextTheme::Normal => EdgeInsetsGeometry::symmetric(0.0, 16.0),
            ButtonTextTheme::Accent => EdgeInsetsGeometry::symmetric(0.0, 16.0),
            ButtonTextTheme::Primary => EdgeInsetsGeometry::symmetric(0.0, 24.0),
        })
    }

    /// The shape of a button's material.
    ///
    /// The button's highlight and splash are clipped to this shape. If the button has an
    /// elevation, then its drop shadow is defined by this shape as well.
    ///
    /// Defaults to a rounded rectangle with circular corner radii of 4.0 if
    /// [`text_theme`](Self::text_theme) is [`ButtonTextTheme::Primary`], a rounded rectangle
    /// with circular corner radii of 2.0 otherwise.
    pub fn shape(&self) -> ShapeBorderRef {
        match &self.shape {
            Some(shape) => shape.clone(),
            None => match self.text_theme {
                ButtonTextTheme::Normal | ButtonTextTheme::Accent => {
                    ShapeBorderRef::new(RoundedRectangleBorder::new(
                        BorderSide::NONE,
                        BorderRadiusGeometry::all(Radius::circular(2.0)),
                    ))
                }
                ButtonTextTheme::Primary => ShapeBorderRef::new(RoundedRectangleBorder::new(
                    BorderSide::NONE,
                    BorderRadiusGeometry::all(Radius::circular(4.0)),
                )),
            },
        }
    }

    fn color_scheme(&self) -> &ColorScheme {
        self.color_scheme
            .as_ref()
            .expect("a button theme has a color scheme")
    }

    /// The `button`'s overall brightness.
    ///
    /// Returns the button's `colorBrightness` if it is non-null, otherwise the color scheme's
    /// `brightness`.
    pub fn get_brightness(&self, button: &MaterialButton) -> Brightness {
        button
            .color_brightness
            .unwrap_or_else(|| self.color_scheme().brightness())
    }

    /// Defines the `button`'s base colors, and the defaults for the button's minimum size,
    /// internal padding, and shape.
    pub fn get_text_theme(&self, button: &MaterialButton) -> ButtonTextTheme {
        button.text_theme.unwrap_or(self.text_theme)
    }

    /// The foreground color of the `button`'s text and icon when `on_pressed` is null.
    #[expect(deprecated, reason = "Dart calls the deprecated `Color.withOpacity`")]
    pub fn get_disabled_text_color(&self, button: &MaterialButton) -> Color {
        button
            .text_color
            .or(button.disabled_text_color)
            .unwrap_or_else(|| self.color_scheme().on_surface().with_opacity(0.38))
    }

    /// The `button`'s background color when `on_pressed` is null.
    #[expect(deprecated, reason = "Dart calls the deprecated `Color.withOpacity`")]
    pub fn get_disabled_fill_color(&self, button: &MaterialButton) -> Color {
        button
            .disabled_color
            .or(self.disabled_color)
            .unwrap_or_else(|| self.color_scheme().on_surface().with_opacity(0.38))
    }

    /// The button's background fill color, or `None` for buttons that don't have a background
    /// color.
    pub fn get_fill_color(&self, button: &MaterialButton) -> Option<Color> {
        // Dart goes on to the theme's colors only for a subclass of `MaterialButton`, and a Rust
        // type has no subclass.
        if button.enabled() {
            button.color
        } else {
            button.disabled_color
        }
    }

    /// The foreground color of the `button`'s text and icon.
    pub fn get_text_color(&self, button: &MaterialButton) -> Color {
        if !button.enabled() {
            return self.get_disabled_text_color(button);
        }
        if let Some(text_color) = button.text_color {
            return text_color;
        }
        match self.get_text_theme(button) {
            ButtonTextTheme::Normal => {
                if self.get_brightness(button) == Brightness::Dark {
                    Colors::WHITE
                } else {
                    Colors::BLACK87
                }
            }
            ButtonTextTheme::Accent => self.color_scheme().secondary(),
            ButtonTextTheme::Primary => {
                let fill_is_dark = match self.get_fill_color(button) {
                    Some(fill_color) => {
                        ThemeData::estimate_brightness_for_color(fill_color) == Brightness::Dark
                    }
                    None => self.get_brightness(button) == Brightness::Dark,
                };
                if fill_is_dark {
                    Colors::WHITE
                } else {
                    Colors::BLACK
                }
            }
        }
    }

    /// The color of the ink "splash" overlay that appears when the (enabled) `button` is
    /// tapped.
    #[expect(deprecated, reason = "Dart calls the deprecated `Color.withOpacity`")]
    pub fn get_splash_color(&self, button: &MaterialButton) -> Color {
        if let Some(splash_color) = button.splash_color {
            return splash_color;
        }
        if let Some(splash_color) = self.splash_color
            && matches!(
                self.get_text_theme(button),
                ButtonTextTheme::Normal | ButtonTextTheme::Accent
            )
        {
            return splash_color;
        }
        self.get_text_color(button).with_opacity(0.12)
    }

    /// The fill color of the button when it has input focus.
    #[expect(deprecated, reason = "Dart calls the deprecated `Color.withOpacity`")]
    pub fn get_focus_color(&self, button: &MaterialButton) -> Color {
        button
            .focus_color
            .or(self.focus_color)
            .unwrap_or_else(|| self.get_text_color(button).with_opacity(0.12))
    }

    /// The fill color of the button when a pointer is hovering over it.
    #[expect(deprecated, reason = "Dart calls the deprecated `Color.withOpacity`")]
    pub fn get_hover_color(&self, button: &MaterialButton) -> Color {
        button
            .hover_color
            .or(self.hover_color)
            .unwrap_or_else(|| self.get_text_color(button).with_opacity(0.04))
    }

    /// The color of the overlay that appears when the `button` is pressed.
    #[expect(deprecated, reason = "Dart calls the deprecated `Color.withOpacity`")]
    pub fn get_highlight_color(&self, button: &MaterialButton) -> Color {
        if let Some(highlight_color) = button.highlight_color {
            return highlight_color;
        }
        match self.get_text_theme(button) {
            ButtonTextTheme::Normal | ButtonTextTheme::Accent => self
                .highlight_color
                .unwrap_or_else(|| self.get_text_color(button).with_opacity(0.16)),
            ButtonTextTheme::Primary => Colors::TRANSPARENT,
        }
    }

    /// The `button`'s elevation when it is enabled and has not been pressed.
    pub fn get_elevation(&self, button: &MaterialButton) -> f64 {
        button.elevation.unwrap_or(2.0)
    }

    /// The `button`'s elevation when it is enabled and has focus.
    pub fn get_focus_elevation(&self, button: &MaterialButton) -> f64 {
        button.focus_elevation.unwrap_or(4.0)
    }

    /// The `button`'s elevation when it is enabled and hovered.
    pub fn get_hover_elevation(&self, button: &MaterialButton) -> f64 {
        button.hover_elevation.unwrap_or(4.0)
    }

    /// The `button`'s elevation when it is enabled and pressed.
    pub fn get_highlight_elevation(&self, button: &MaterialButton) -> f64 {
        button.highlight_elevation.unwrap_or(8.0)
    }

    /// The `button`'s elevation when `on_pressed` is null.
    pub fn get_disabled_elevation(&self, button: &MaterialButton) -> f64 {
        button.disabled_elevation.unwrap_or(0.0)
    }

    /// Padding for the `button`'s child (typically the button's label).
    pub fn get_padding(&self, button: &MaterialButton) -> EdgeInsetsGeometry {
        button
            .padding
            .or(self.padding)
            .unwrap_or_else(|| match self.get_text_theme(button) {
                ButtonTextTheme::Normal | ButtonTextTheme::Accent => {
                    EdgeInsetsGeometry::symmetric(0.0, 16.0)
                }
                ButtonTextTheme::Primary => EdgeInsetsGeometry::symmetric(0.0, 24.0),
            })
    }

    /// The shape of the `button`'s `Material`.
    pub fn get_shape(&self, button: &MaterialButton) -> ShapeBorderRef {
        button.shape.clone().unwrap_or_else(|| self.shape())
    }

    /// The duration of the `button`'s highlight animation.
    pub fn get_animation_duration(&self, button: &MaterialButton) -> Duration {
        button.animation_duration.unwrap_or(K_THEME_CHANGE_DURATION)
    }

    /// The `BoxConstraints` that define the `button`'s size.
    pub fn get_constraints(&self, _button: &MaterialButton) -> BoxConstraints {
        self.constraints()
    }

    /// The minimum size of the `button`'s tap target.
    pub fn get_material_tap_target_size(&self, button: &MaterialButton) -> MaterialTapTargetSize {
        button
            .material_tap_target_size
            .or(self.material_tap_target_size)
            .unwrap_or(MaterialTapTargetSize::Padded)
    }

    /// Creates a copy of this button theme data object with the matching fields replaced with
    /// the non-null parameter values.
    ///
    /// The copy holds this theme's [`padding`](Self::padding) and [`shape`](Self::shape) as
    /// given values, their defaults included, as Dart's `copyWith` passes `this.padding` and
    /// `this.shape` on.
    pub fn copy_with(&self) -> ButtonThemeData {
        let mut copy = self.clone();
        copy.padding = Some(self.padding());
        copy.shape = Some(self.shape());
        copy
    }
}

impl Default for ButtonThemeData {
    fn default() -> ButtonThemeData {
        ButtonThemeData::new()
    }
}

impl PartialEq for ButtonThemeData {
    fn eq(&self, other: &ButtonThemeData) -> bool {
        other.text_theme == self.text_theme
            && other.min_width == self.min_width
            && other.height == self.height
            && other.padding() == self.padding()
            && other.shape() == self.shape()
            && other.aligned_dropdown == self.aligned_dropdown
            && other.button_color == self.button_color
            && other.disabled_color == self.disabled_color
            && other.focus_color == self.focus_color
            && other.hover_color == self.hover_color
            && other.highlight_color == self.highlight_color
            && other.splash_color == self.splash_color
            && other.color_scheme == self.color_scheme
            && other.material_tap_target_size == self.material_tap_target_size
    }
}

impl Debug for ButtonThemeData {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        let default_theme = ButtonThemeData::new();
        let mut properties = f.debug_struct("ButtonThemeData");
        if self.text_theme != default_theme.text_theme {
            properties.field("textTheme", &self.text_theme);
        }
        if self.min_width != default_theme.min_width {
            properties.field("minWidth", &self.min_width);
        }
        if self.height != default_theme.height {
            properties.field("height", &self.height);
        }
        if self.padding() != default_theme.padding() {
            properties.field("padding", &self.padding());
        }
        if self.shape() != default_theme.shape() {
            properties.field("shape", &self.shape());
        }
        if self.aligned_dropdown != default_theme.aligned_dropdown {
            properties.field("alignedDropdown", &"dropdown width matches button");
        }
        if let Some(button_color) = &self.button_color {
            properties.field("buttonColor", button_color);
        }
        if let Some(disabled_color) = &self.disabled_color {
            properties.field("disabledColor", disabled_color);
        }
        if let Some(focus_color) = &self.focus_color {
            properties.field("focusColor", focus_color);
        }
        if let Some(hover_color) = &self.hover_color {
            properties.field("hoverColor", hover_color);
        }
        if let Some(highlight_color) = &self.highlight_color {
            properties.field("highlightColor", highlight_color);
        }
        if let Some(splash_color) = &self.splash_color {
            properties.field("splashColor", splash_color);
        }
        if self.color_scheme.is_some() {
            properties.field("colorScheme", &"ColorScheme");
        }
        if let Some(material_tap_target_size) = &self.material_tap_target_size {
            properties.field("materialTapTargetSize", material_tap_target_size);
        }
        properties.finish()
    }
}

impl waft_foundation::Diagnosticable for ButtonThemeData {
    fn debug_fill_properties(&self, properties: &mut waft_foundation::DiagnosticPropertiesBuilder) {
        let default_theme = ButtonThemeData::default();
        properties.add(
            EnumProperty::<ButtonTextTheme>::new("textTheme", Some(self.text_theme))
                .default_value(Some(default_theme.text_theme)),
        );
        properties.add(
            DoubleProperty::new("minWidth", self.min_width)
                .default_value(Some(default_theme.min_width)),
        );
        properties.add(
            DoubleProperty::new("height", self.height).default_value(Some(default_theme.height)),
        );
        properties.add(
            DiagnosticsProperty::<EdgeInsetsGeometry>::new("padding", Some(self.padding()))
                .default_value(Some(default_theme.padding())),
        );
        properties.add(
            DiagnosticsProperty::<ShapeBorderRef>::new("shape", Some(self.shape()))
                .default_value(Some(default_theme.shape())),
        );
        properties.add(
            FlagProperty::new("alignedDropdown", self.aligned_dropdown)
                .default_value(Some(default_theme.aligned_dropdown))
                .if_true("dropdown width matches button"),
        );
        properties.add(ColorProperty::new("buttonColor", self.button_color).default_value(None));
        properties
            .add(ColorProperty::new("disabledColor", self.disabled_color).default_value(None));
        properties.add(ColorProperty::new("focusColor", self.focus_color).default_value(None));
        properties.add(ColorProperty::new("hoverColor", self.hover_color).default_value(None));
        properties
            .add(ColorProperty::new("highlightColor", self.highlight_color).default_value(None));
        properties.add(ColorProperty::new("splashColor", self.splash_color).default_value(None));
        properties.add(
            DiagnosticsProperty::<ColorScheme>::new("colorScheme", self.color_scheme.clone())
                .default_value(default_theme.color_scheme.clone()),
        );
        properties.add(
            DiagnosticsProperty::<waft_foundation::DartEnum<MaterialTapTargetSize>>::new(
                "materialTapTargetSize",
                self.material_tap_target_size.map(waft_foundation::DartEnum),
            )
            .default_value(None),
        );
    }
}

impl std::fmt::Display for ButtonThemeData {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.write_str(&waft_foundation::Diagnosticable::to_string(self))
    }
}

#[cfg(test)]
mod tests {
    use waft_painting::StadiumBorder;

    use super::*;

    fn rounded(radius: f64) -> ShapeBorderRef {
        ShapeBorderRef::new(RoundedRectangleBorder::new(
            BorderSide::NONE,
            BorderRadiusGeometry::all(Radius::circular(radius)),
        ))
    }

    fn rounded_rectangle_border() -> ShapeBorderRef {
        ShapeBorderRef::new(RoundedRectangleBorder::new(
            BorderSide::NONE,
            BorderRadiusGeometry::ZERO,
        ))
    }

    // button_theme_test.dart 'ButtonThemeData defaults'.
    #[test]
    fn a_new_button_theme_data_has_dart_defaults() {
        let theme = ButtonThemeData::new();
        assert_eq!(theme.text_theme, ButtonTextTheme::Normal);
        assert_eq!(
            theme.constraints(),
            BoxConstraints::new().min_width(88.0).min_height(36.0)
        );
        assert_eq!(theme.padding(), EdgeInsetsGeometry::symmetric(0.0, 16.0));
        assert_eq!(Some(theme.shape()), Some(rounded(2.0)));
        assert!(!theme.aligned_dropdown);
        assert_eq!(theme.layout_behavior, ButtonBarLayoutBehavior::Padded);
    }

    // button_theme_test.dart 'ButtonThemeData default overrides'.
    #[test]
    fn the_given_values_override_the_defaults() {
        let theme = ButtonThemeData::new()
            .with_text_theme(ButtonTextTheme::Primary)
            .with_min_width(100.0)
            .with_height(200.0)
            .with_padding(EdgeInsetsGeometry::ZERO)
            .with_shape(rounded_rectangle_border())
            .with_aligned_dropdown(true);
        assert_eq!(theme.text_theme, ButtonTextTheme::Primary);
        assert_eq!(
            theme.constraints(),
            BoxConstraints::new().min_width(100.0).min_height(200.0)
        );
        assert_eq!(theme.padding(), EdgeInsetsGeometry::ZERO);
        assert_eq!(Some(theme.shape()), Some(rounded_rectangle_border()));
        assert!(theme.aligned_dropdown);
    }

    // button_theme_test.dart 'ButtonThemeData.copyWith'.
    #[test]
    fn copy_with_keeps_every_value_and_replaces_the_given_ones() {
        let theme = ButtonThemeData::new().copy_with();
        assert_eq!(theme.text_theme, ButtonTextTheme::Normal);
        assert_eq!(theme.layout_behavior, ButtonBarLayoutBehavior::Padded);
        assert_eq!(
            theme.constraints(),
            BoxConstraints::new().min_width(88.0).min_height(36.0)
        );
        assert_eq!(theme.padding(), EdgeInsetsGeometry::symmetric(0.0, 16.0));
        assert_eq!(Some(theme.shape()), Some(rounded(2.0)));
        assert!(!theme.aligned_dropdown);
        assert!(theme.color_scheme.is_none());

        let theme = ButtonThemeData::new()
            .copy_with()
            .with_text_theme(ButtonTextTheme::Primary)
            .with_layout_behavior(ButtonBarLayoutBehavior::Constrained)
            .with_min_width(100.0)
            .with_height(200.0)
            .with_padding(EdgeInsetsGeometry::ZERO)
            .with_shape(StadiumBorder::new(BorderSide::NONE))
            .with_aligned_dropdown(true)
            .with_color_scheme(ColorScheme::dark())
            .with_material_tap_target_size(MaterialTapTargetSize::ShrinkWrap);
        assert_eq!(theme.text_theme, ButtonTextTheme::Primary);
        assert_eq!(theme.layout_behavior, ButtonBarLayoutBehavior::Constrained);
        assert_eq!(
            theme.constraints(),
            BoxConstraints::new().min_width(100.0).min_height(200.0)
        );
        assert_eq!(theme.padding(), EdgeInsetsGeometry::ZERO);
        let stadium: ShapeBorderRef = ShapeBorderRef::new(StadiumBorder::new(BorderSide::NONE));
        assert_eq!(Some(theme.shape()), Some(stadium));
        assert!(theme.aligned_dropdown);
        assert_eq!(theme.color_scheme, Some(ColorScheme::dark()));
    }

    // Dart's `copyWith` passes the `padding` and `shape` getters on, so a copy keeps the
    // defaults of the text theme it was made from.
    #[test]
    fn a_copy_keeps_the_padding_and_shape_of_the_original_text_theme() {
        let theme = ButtonThemeData::new()
            .copy_with()
            .with_text_theme(ButtonTextTheme::Primary);
        assert_eq!(theme.padding(), EdgeInsetsGeometry::symmetric(0.0, 16.0));
        assert_eq!(Some(theme.shape()), Some(rounded(2.0)));
        assert_ne!(
            theme,
            ButtonThemeData::new().with_text_theme(ButtonTextTheme::Primary)
        );
    }
}