1use std::any::{Any, TypeId};
8use std::fmt::{self, Debug};
9use std::hash::{Hash, Hasher};
10use std::rc::Rc;
11
12use indexmap::IndexMap;
13use waft_cupertino::{
14 CupertinoThemeData, CupertinoThemeDataOverrides, NoDefaultCupertinoThemeData,
15};
16use waft_embedder::{Brightness, Color, Offset, TargetPlatform, clamp_double, lerp_double};
17use waft_foundation::{
18 App, DartEnum, DiagnosticLevel, DiagnosticPropertiesBuilder, Diagnosticable,
19 DiagnosticsProperty, EnumProperty, InstanceOf, IterableProperty, K_IS_WEB,
20 PRECISION_ERROR_TOLERANCE,
21};
22use waft_painting::AnyColor;
23use waft_rendering::{BoxConstraints, Constraints};
24use waft_widgets::{BuildContext, IconThemeData};
25
26use crate::action_icons_theme::ActionIconThemeData;
27use crate::app_bar_theme::AppBarThemeData;
28use crate::badge_theme::BadgeThemeData;
29use crate::banner_theme::MaterialBannerThemeData;
30use crate::bottom_navigation_bar_theme::BottomNavigationBarThemeData;
31use crate::bottom_sheet_theme::BottomSheetThemeData;
32#[allow(deprecated)]
33use crate::button_bar_theme::ButtonBarThemeData;
34use crate::button_theme::ButtonThemeData;
35use crate::card_theme::CardThemeData;
36use crate::carousel_theme::CarouselViewThemeData;
37use crate::checkbox_theme::CheckboxThemeData;
38use crate::chip_theme::ChipThemeData;
39use crate::color_scheme::ColorScheme;
40use crate::colors::{Colors, MaterialColor};
41use crate::constants::{K_DEFAULT_ICON_DARK_COLOR, K_DEFAULT_ICON_LIGHT_COLOR};
42use crate::data_table_theme::DataTableThemeData;
43use crate::date_picker_theme::DatePickerThemeData;
44use crate::dialog_theme::DialogThemeData;
45use crate::divider_theme::DividerThemeData;
46use crate::drawer_theme::DrawerThemeData;
47use crate::dropdown_menu_theme::DropdownMenuThemeData;
48use crate::elevated_button_theme::ElevatedButtonThemeData;
49use crate::expansion_tile_theme::ExpansionTileThemeData;
50use crate::filled_button_theme::FilledButtonThemeData;
51use crate::floating_action_button_theme::FloatingActionButtonThemeData;
52use crate::generated::color_scheme_defaults::{
53 color_scheme_dark_high_contrast_m3, color_scheme_dark_m3, color_scheme_dark_medium_contrast_m3,
54 color_scheme_light_high_contrast_m3, color_scheme_light_m3,
55 color_scheme_light_medium_contrast_m3,
56};
57use crate::icon_button_theme::IconButtonThemeData;
58use crate::ink_ripple::InkRipple;
59use crate::ink_sparkle::InkSparkle;
60use crate::ink_splash::InkSplash;
61use crate::ink_well::InteractiveInkFeatureFactoryRef;
62use crate::input_decorator::InputDecorationThemeData;
63use crate::list_tile_theme::ListTileThemeData;
64use crate::menu_bar_theme::MenuBarThemeData;
65use crate::menu_button_theme::MenuButtonThemeData;
66use crate::menu_theme::MenuThemeData;
67use crate::navigation_bar_theme::NavigationBarThemeData;
68use crate::navigation_drawer_theme::NavigationDrawerThemeData;
69use crate::navigation_rail_theme::NavigationRailThemeData;
70use crate::outlined_button_theme::OutlinedButtonThemeData;
71use crate::page_transitions_theme::PageTransitionsTheme;
72use crate::popup_menu_theme::PopupMenuThemeData;
73use crate::progress_indicator_theme::ProgressIndicatorThemeData;
74use crate::radio_theme::RadioThemeData;
75use crate::scrollbar_theme::ScrollbarThemeData;
76use crate::search_bar_theme::SearchBarThemeData;
77use crate::search_view_theme::SearchViewThemeData;
78use crate::segmented_button_theme::SegmentedButtonThemeData;
79use crate::slider_theme::SliderThemeData;
80use crate::snack_bar_theme::SnackBarThemeData;
81use crate::switch_theme::SwitchThemeData;
82use crate::tab_bar_theme::TabBarThemeData;
83use crate::text_button_theme::TextButtonThemeData;
84use crate::text_selection_theme::TextSelectionThemeData;
85use crate::text_theme::TextTheme;
86use crate::time_picker_theme::TimePickerThemeData;
87use crate::toggle_buttons_theme::ToggleButtonsThemeData;
88use crate::tooltip_theme::TooltipThemeData;
89use crate::typography::Typography;
90
91pub trait Adaptation<T: 'static>: Debug + 'static {
98 fn adapt(&self, theme: &ThemeData, default_value: T) -> T {
109 let _ = theme;
110 default_value
111 }
112}
113
114#[derive(Clone)]
120pub struct AdaptationRef {
121 adaptation_type: TypeId,
122 adaptation: Rc<dyn ErasedAdaptation>,
123}
124
125impl AdaptationRef {
126 pub fn new<T: 'static>(adaptation: impl Adaptation<T>) -> AdaptationRef {
128 AdaptationRef {
129 adaptation_type: TypeId::of::<T>(),
130 adaptation: Rc::new(TypedAdaptation::<T>(Box::new(adaptation))),
131 }
132 }
133
134 pub fn adaptation_type(&self) -> TypeId {
136 self.adaptation_type
137 }
138
139 pub fn downcast<T: 'static>(&self) -> Option<&dyn Adaptation<T>> {
141 self.adaptation
142 .as_any()
143 .downcast_ref::<TypedAdaptation<T>>()
144 .map(|typed| &*typed.0)
145 }
146}
147
148impl PartialEq for AdaptationRef {
149 fn eq(&self, other: &AdaptationRef) -> bool {
150 Rc::ptr_eq(&self.adaptation, &other.adaptation)
151 }
152}
153
154impl Debug for AdaptationRef {
155 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
156 Debug::fmt(&*self.adaptation, f)
157 }
158}
159
160impl fmt::Display for AdaptationRef {
161 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
163 Debug::fmt(self, f)
164 }
165}
166
167trait ErasedAdaptation: Debug {
169 fn as_any(&self) -> &dyn Any;
170}
171
172struct TypedAdaptation<T: 'static>(Box<dyn Adaptation<T>>);
173
174impl<T: 'static> ErasedAdaptation for TypedAdaptation<T> {
175 fn as_any(&self) -> &dyn Any {
176 self
177 }
178}
179
180impl<T: 'static> Debug for TypedAdaptation<T> {
181 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
182 Debug::fmt(&*self.0, f)
183 }
184}
185
186pub trait ThemeExtension: Any + Debug {
199 fn as_any(&self) -> &dyn Any;
202
203 fn extension_type(&self) -> TypeId {
205 self.as_any().type_id()
206 }
207
208 fn copy_with(&self) -> ThemeExtensionRef;
213
214 fn lerp(self: Rc<Self>, other: Option<&dyn ThemeExtension>, t: f64) -> ThemeExtensionRef;
222
223 fn eq_extension(&self, other: &dyn ThemeExtension) -> bool {
225 std::ptr::addr_eq(self as *const Self, other as *const dyn ThemeExtension)
226 }
227}
228
229pub type ThemeExtensionRef = Rc<dyn ThemeExtension>;
231
232#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
257pub enum MaterialTapTargetSize {
258 Padded,
264
265 ShrinkWrap,
268}
269
270#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
272pub enum StyleVariant {
273 Material3,
275
276 Material3Expressive,
286}
287
288pub struct ContrastLevel;
308
309impl ContrastLevel {
310 pub const STANDARD: f64 = 0.0;
312
313 pub const MEDIUM: f64 = 0.5;
315
316 pub const HIGH: f64 = 1.0;
318}
319
320#[derive(Clone, Debug)]
347pub struct ThemeData {
348 pub apply_elevation_overlay_color: bool,
390 pub cupertino_override_theme: Option<NoDefaultCupertinoThemeData>,
402 pub extensions: IndexMap<TypeId, ThemeExtensionRef>,
414 pub adaptation_map: IndexMap<TypeId, AdaptationRef>,
419 pub input_decoration_theme: InputDecorationThemeData,
424 pub material_tap_target_size: MaterialTapTargetSize,
430 pub page_transitions_theme: PageTransitionsTheme,
436 pub platform: TargetPlatform,
452 pub scrollbar_theme: ScrollbarThemeData,
454 pub splash_factory: InteractiveInkFeatureFactoryRef,
466 pub use_material3: bool,
501 pub visual_density: VisualDensity,
536
537 pub canvas_color: Color,
540 pub card_color: Color,
542 pub color_scheme: ColorScheme,
552 pub disabled_color: Color,
556 pub divider_color: Color,
562 pub focus_color: Color,
564 pub highlight_color: Color,
567 pub hint_color: Color,
570 pub hover_color: Color,
573 pub primary_color: Color,
580 pub primary_color_dark: Color,
582 pub primary_color_light: Color,
584 pub scaffold_background_color: Color,
587 pub secondary_header_color: Color,
592 pub shadow_color: Color,
602 pub splash_color: Color,
607 pub unselected_widget_color: Color,
611
612 pub icon_theme: IconThemeData,
615 pub primary_icon_theme: IconThemeData,
617 pub primary_text_theme: TextTheme,
619 pub text_theme: TextTheme,
621 pub typography: Typography,
626
627 pub action_icon_theme: Option<ActionIconThemeData>,
631 pub app_bar_theme: AppBarThemeData,
634 pub badge_theme: BadgeThemeData,
636 pub banner_theme: MaterialBannerThemeData,
638 pub bottom_navigation_bar_theme: BottomNavigationBarThemeData,
641 pub bottom_sheet_theme: BottomSheetThemeData,
643 pub button_theme: ButtonThemeData,
646 pub card_theme: CardThemeData,
650
651 pub carousel_view_theme: CarouselViewThemeData,
653 pub checkbox_theme: CheckboxThemeData,
655 pub chip_theme: ChipThemeData,
659 pub data_table_theme: DataTableThemeData,
663 pub date_picker_theme: DatePickerThemeData,
667 pub dialog_theme: DialogThemeData,
669 pub divider_theme: DividerThemeData,
673 pub drawer_theme: DrawerThemeData,
675 pub dropdown_menu_theme: DropdownMenuThemeData,
677 pub elevated_button_theme: ElevatedButtonThemeData,
680 pub expansion_tile_theme: ExpansionTileThemeData,
682 pub filled_button_theme: FilledButtonThemeData,
685 pub floating_action_button_theme: FloatingActionButtonThemeData,
688 pub icon_button_theme: IconButtonThemeData,
691 pub list_tile_theme: ListTileThemeData,
693 pub menu_bar_theme: MenuBarThemeData,
696 pub menu_button_theme: MenuButtonThemeData,
699 pub menu_theme: MenuThemeData,
702 pub navigation_bar_theme: NavigationBarThemeData,
705 pub navigation_drawer_theme: NavigationDrawerThemeData,
708 pub navigation_rail_theme: NavigationRailThemeData,
711 pub outlined_button_theme: OutlinedButtonThemeData,
714 pub popup_menu_theme: PopupMenuThemeData,
717 pub progress_indicator_theme: ProgressIndicatorThemeData,
719 pub radio_theme: RadioThemeData,
721 pub search_bar_theme: SearchBarThemeData,
723 pub search_view_theme: SearchViewThemeData,
726 pub segmented_button_theme: SegmentedButtonThemeData,
728 pub slider_theme: SliderThemeData,
732 pub snack_bar_theme: SnackBarThemeData,
734 pub switch_theme: SwitchThemeData,
736 pub tab_bar_theme: TabBarThemeData,
738 pub text_button_theme: TextButtonThemeData,
741 pub text_selection_theme: TextSelectionThemeData,
744 pub time_picker_theme: TimePickerThemeData,
746 pub toggle_buttons_theme: ToggleButtonsThemeData,
748 pub tooltip_theme: TooltipThemeData,
750
751 #[deprecated(
754 note = "Use OverflowBar instead. This feature was deprecated after v3.21.0-10.0.pre."
755 )]
756 #[allow(deprecated)]
757 pub button_bar_theme: ButtonBarThemeData,
758 #[deprecated(
760 note = "Use DialogThemeData.backgroundColor instead. This feature was deprecated after v3.27.0-0.1.pre."
761 )]
762 pub dialog_background_color: Color,
763 #[deprecated(
765 note = "Use TabBarThemeData.indicatorColor instead. This feature was deprecated after v3.28.0-1.0.pre."
766 )]
767 pub indicator_color: Color,
768}
769
770impl ThemeData {
771 pub fn builder() -> ThemeDataBuilder {
822 ThemeDataBuilder::default()
823 }
824
825 pub fn new(app: &App) -> ThemeData {
827 ThemeData::builder().build(app)
828 }
829
830 #[allow(clippy::should_implement_trait)]
845 pub fn from(color_scheme: ColorScheme) -> ThemeDataFromBuilder {
846 ThemeDataFromBuilder {
847 color_scheme,
848 text_theme: None,
849 use_material3: None,
850 }
851 }
852
853 pub fn light(app: &App, use_material3: Option<bool>) -> ThemeData {
858 let builder = ThemeData::builder().brightness(Brightness::Light);
859 match use_material3 {
860 Some(use_material3) => builder.use_material3(use_material3),
861 None => builder,
862 }
863 .build(app)
864 }
865
866 pub fn dark(app: &App, use_material3: Option<bool>) -> ThemeData {
871 let builder = ThemeData::builder().brightness(Brightness::Dark);
872 match use_material3 {
873 Some(use_material3) => builder.use_material3(use_material3),
874 None => builder,
875 }
876 .build(app)
877 }
878
879 pub fn fallback(app: &App, use_material3: Option<bool>) -> ThemeData {
889 ThemeData::light(app, use_material3)
890 }
891
892 pub fn get_adaptation<T: 'static>(&self) -> Option<&dyn Adaptation<T>> {
896 self.adaptation_map
897 .get(&TypeId::of::<T>())
898 .and_then(AdaptationRef::downcast::<T>)
899 }
900
901 fn create_adaptation_map(adaptations: Vec<AdaptationRef>) -> IndexMap<TypeId, AdaptationRef> {
902 adaptations
903 .into_iter()
904 .map(|adaptation| (adaptation.adaptation_type(), adaptation))
905 .collect()
906 }
907
908 pub fn brightness(&self) -> Brightness {
915 self.color_scheme.brightness()
916 }
917
918 pub fn extension<T: ThemeExtension>(&self) -> Option<&T> {
922 self.extensions
923 .get(&TypeId::of::<T>())
924 .and_then(|extension| extension.as_any().downcast_ref::<T>())
925 }
926
927 pub fn copy_with(&self) -> ThemeDataCopyWith {
934 ThemeDataCopyWith {
935 theme: Box::new(self.clone()),
936 color_scheme: None,
937 brightness: None,
938 }
939 }
940
941 const LOCALIZED_THEME_DATA_CACHE_SIZE: usize = 5;
946
947 pub fn localize(
955 app: &mut App,
956 base_theme: &Rc<ThemeData>,
957 local_text_geometry: &Rc<TextTheme>,
958 ) -> Rc<ThemeData> {
959 let localized_theme_data_cache = app.singleton::<LocalizedThemeDataCache>();
973 app.get_mut(localized_theme_data_cache).0.put_if_absent(
974 IdentityThemeDataCacheKey {
975 base_theme: Rc::clone(base_theme),
976 local_text_geometry: Rc::clone(local_text_geometry),
977 },
978 || {
979 Rc::new(
980 base_theme
981 .copy_with()
982 .primary_text_theme(
983 local_text_geometry.merge(Some(&base_theme.primary_text_theme)),
984 )
985 .text_theme(local_text_geometry.merge(Some(&base_theme.text_theme)))
986 .build(),
987 )
988 },
989 )
990 }
991
992 pub fn estimate_brightness_for_color(color: Color) -> Brightness {
998 let relative_luminance = color.compute_luminance();
999
1000 const K_THRESHOLD: f64 = 0.15;
1007 if (relative_luminance + 0.05) * (relative_luminance + 0.05) > K_THRESHOLD {
1008 return Brightness::Light;
1009 }
1010 Brightness::Dark
1011 }
1012
1013 fn lerp_theme_extensions(
1019 a: &ThemeData,
1020 b: &ThemeData,
1021 t: f64,
1022 ) -> IndexMap<TypeId, ThemeExtensionRef> {
1023 let mut new_extensions: IndexMap<TypeId, ThemeExtensionRef> = a
1025 .extensions
1026 .iter()
1027 .map(|(&id, extension_a)| {
1028 let extension_b = b.extensions.get(&id);
1029 (
1030 id,
1031 Rc::clone(extension_a).lerp(extension_b.map(|extension| &**extension), t),
1032 )
1033 })
1034 .collect();
1035 new_extensions.extend(
1037 b.extensions
1038 .iter()
1039 .filter(|(id, _)| !a.extensions.contains_key(*id))
1040 .map(|(&id, extension)| (id, Rc::clone(extension))),
1041 );
1042
1043 new_extensions
1044 }
1045
1046 fn theme_extension_iterable_to_map(
1050 extensions_iterable: Vec<ThemeExtensionRef>,
1051 ) -> IndexMap<TypeId, ThemeExtensionRef> {
1052 extensions_iterable
1053 .into_iter()
1054 .map(|extension| (extension.extension_type(), extension))
1055 .collect()
1056 }
1057
1058 #[allow(deprecated)]
1062 pub fn lerp(a: &ThemeData, b: &ThemeData, t: f64) -> ThemeData {
1063 if std::ptr::eq(a, b) {
1064 return a.clone();
1065 }
1066 let color = |a: Color, b: Color| Color::lerp(Some(a), Some(b), t).expect("both sides");
1067 ThemeData {
1068 adaptation_map: if t < 0.5 {
1075 a.adaptation_map.clone()
1076 } else {
1077 b.adaptation_map.clone()
1078 },
1079 apply_elevation_overlay_color: if t < 0.5 {
1080 a.apply_elevation_overlay_color
1081 } else {
1082 b.apply_elevation_overlay_color
1083 },
1084 cupertino_override_theme: if t < 0.5 {
1085 a.cupertino_override_theme.clone()
1086 } else {
1087 b.cupertino_override_theme.clone()
1088 },
1089 extensions: ThemeData::lerp_theme_extensions(a, b, t),
1090 input_decoration_theme: if t < 0.5 {
1091 a.input_decoration_theme.clone()
1092 } else {
1093 b.input_decoration_theme.clone()
1094 },
1095 material_tap_target_size: if t < 0.5 {
1096 a.material_tap_target_size
1097 } else {
1098 b.material_tap_target_size
1099 },
1100 page_transitions_theme: if t < 0.5 {
1101 a.page_transitions_theme.clone()
1102 } else {
1103 b.page_transitions_theme.clone()
1104 },
1105 platform: if t < 0.5 { a.platform } else { b.platform },
1106 scrollbar_theme: ScrollbarThemeData::lerp(
1107 Some(&a.scrollbar_theme),
1108 Some(&b.scrollbar_theme),
1109 t,
1110 ),
1111 splash_factory: if t < 0.5 {
1112 Rc::clone(&a.splash_factory)
1113 } else {
1114 Rc::clone(&b.splash_factory)
1115 },
1116 use_material3: if t < 0.5 {
1117 a.use_material3
1118 } else {
1119 b.use_material3
1120 },
1121 visual_density: VisualDensity::lerp(a.visual_density, b.visual_density, t),
1122 canvas_color: color(a.canvas_color, b.canvas_color),
1124 card_color: color(a.card_color, b.card_color),
1125 color_scheme: ColorScheme::lerp(&a.color_scheme, &b.color_scheme, t),
1126 disabled_color: color(a.disabled_color, b.disabled_color),
1127 divider_color: color(a.divider_color, b.divider_color),
1128 focus_color: color(a.focus_color, b.focus_color),
1129 highlight_color: color(a.highlight_color, b.highlight_color),
1130 hint_color: color(a.hint_color, b.hint_color),
1131 hover_color: color(a.hover_color, b.hover_color),
1132 primary_color: color(a.primary_color, b.primary_color),
1133 primary_color_dark: color(a.primary_color_dark, b.primary_color_dark),
1134 primary_color_light: color(a.primary_color_light, b.primary_color_light),
1135 scaffold_background_color: color(
1136 a.scaffold_background_color,
1137 b.scaffold_background_color,
1138 ),
1139 secondary_header_color: color(a.secondary_header_color, b.secondary_header_color),
1140 shadow_color: color(a.shadow_color, b.shadow_color),
1141 splash_color: color(a.splash_color, b.splash_color),
1142 unselected_widget_color: color(a.unselected_widget_color, b.unselected_widget_color),
1143 icon_theme: IconThemeData::lerp(Some(&a.icon_theme), Some(&b.icon_theme), t),
1145 primary_icon_theme: IconThemeData::lerp(
1146 Some(&a.primary_icon_theme),
1147 Some(&b.primary_icon_theme),
1148 t,
1149 ),
1150 primary_text_theme: TextTheme::lerp(
1151 Some(&a.primary_text_theme),
1152 Some(&b.primary_text_theme),
1153 t,
1154 ),
1155 text_theme: TextTheme::lerp(Some(&a.text_theme), Some(&b.text_theme), t),
1156 typography: Typography::lerp(&a.typography, &b.typography, t),
1157 action_icon_theme: ActionIconThemeData::lerp(
1159 a.action_icon_theme.as_ref(),
1160 b.action_icon_theme.as_ref(),
1161 t,
1162 ),
1163 app_bar_theme: AppBarThemeData::lerp(&a.app_bar_theme, &b.app_bar_theme, t),
1164 badge_theme: BadgeThemeData::lerp(Some(&a.badge_theme), Some(&b.badge_theme), t),
1165 banner_theme: MaterialBannerThemeData::lerp(
1166 Some(&a.banner_theme),
1167 Some(&b.banner_theme),
1168 t,
1169 ),
1170 bottom_navigation_bar_theme: BottomNavigationBarThemeData::lerp(
1171 Some(&a.bottom_navigation_bar_theme),
1172 Some(&b.bottom_navigation_bar_theme),
1173 t,
1174 ),
1175 bottom_sheet_theme: BottomSheetThemeData::lerp(
1176 Some(&a.bottom_sheet_theme),
1177 Some(&b.bottom_sheet_theme),
1178 t,
1179 )
1180 .expect("both sides"),
1181 button_theme: if t < 0.5 {
1182 a.button_theme.clone()
1183 } else {
1184 b.button_theme.clone()
1185 },
1186 card_theme: CardThemeData::lerp(Some(&a.card_theme), Some(&b.card_theme), t),
1187 carousel_view_theme: CarouselViewThemeData::lerp(
1188 Some(&a.carousel_view_theme),
1189 Some(&b.carousel_view_theme),
1190 t,
1191 ),
1192 checkbox_theme: CheckboxThemeData::lerp(
1193 Some(&a.checkbox_theme),
1194 Some(&b.checkbox_theme),
1195 t,
1196 ),
1197 chip_theme: ChipThemeData::lerp(Some(&a.chip_theme), Some(&b.chip_theme), t)
1198 .expect("both sides"),
1199 data_table_theme: DataTableThemeData::lerp(&a.data_table_theme, &b.data_table_theme, t),
1200 date_picker_theme: DatePickerThemeData::lerp(
1201 Some(&a.date_picker_theme),
1202 Some(&b.date_picker_theme),
1203 t,
1204 ),
1205 dialog_theme: DialogThemeData::lerp(Some(&a.dialog_theme), Some(&b.dialog_theme), t),
1206 divider_theme: DividerThemeData::lerp(
1207 Some(&a.divider_theme),
1208 Some(&b.divider_theme),
1209 t,
1210 ),
1211 drawer_theme: DrawerThemeData::lerp(Some(&a.drawer_theme), Some(&b.drawer_theme), t)
1212 .expect("both sides"),
1213 dropdown_menu_theme: DropdownMenuThemeData::lerp(
1214 Some(&a.dropdown_menu_theme),
1215 Some(&b.dropdown_menu_theme),
1216 t,
1217 ),
1218 elevated_button_theme: ElevatedButtonThemeData::lerp(
1219 Some(&a.elevated_button_theme),
1220 Some(&b.elevated_button_theme),
1221 t,
1222 )
1223 .expect("both sides"),
1224 expansion_tile_theme: ExpansionTileThemeData::lerp(
1225 Some(&a.expansion_tile_theme),
1226 Some(&b.expansion_tile_theme),
1227 t,
1228 )
1229 .expect("both sides"),
1230 filled_button_theme: FilledButtonThemeData::lerp(
1231 Some(&a.filled_button_theme),
1232 Some(&b.filled_button_theme),
1233 t,
1234 )
1235 .expect("both sides"),
1236 floating_action_button_theme: FloatingActionButtonThemeData::lerp(
1237 Some(&a.floating_action_button_theme),
1238 Some(&b.floating_action_button_theme),
1239 t,
1240 )
1241 .expect("both sides"),
1242 icon_button_theme: IconButtonThemeData::lerp(
1243 Some(&a.icon_button_theme),
1244 Some(&b.icon_button_theme),
1245 t,
1246 )
1247 .expect("both sides"),
1248 list_tile_theme: ListTileThemeData::lerp(
1249 Some(&a.list_tile_theme),
1250 Some(&b.list_tile_theme),
1251 t,
1252 )
1253 .expect("both sides"),
1254 menu_bar_theme: MenuBarThemeData::lerp(
1255 Some(&a.menu_bar_theme),
1256 Some(&b.menu_bar_theme),
1257 t,
1258 )
1259 .expect("both sides"),
1260 menu_button_theme: MenuButtonThemeData::lerp(
1261 Some(&a.menu_button_theme),
1262 Some(&b.menu_button_theme),
1263 t,
1264 )
1265 .expect("both sides"),
1266 menu_theme: MenuThemeData::lerp(Some(&a.menu_theme), Some(&b.menu_theme), t)
1267 .expect("both sides"),
1268 navigation_bar_theme: NavigationBarThemeData::lerp(
1269 Some(&a.navigation_bar_theme),
1270 Some(&b.navigation_bar_theme),
1271 t,
1272 )
1273 .expect("both sides"),
1274 navigation_drawer_theme: NavigationDrawerThemeData::lerp(
1275 Some(&a.navigation_drawer_theme),
1276 Some(&b.navigation_drawer_theme),
1277 t,
1278 )
1279 .expect("both sides"),
1280 navigation_rail_theme: NavigationRailThemeData::lerp(
1281 Some(&a.navigation_rail_theme),
1282 Some(&b.navigation_rail_theme),
1283 t,
1284 )
1285 .expect("both sides"),
1286 outlined_button_theme: OutlinedButtonThemeData::lerp(
1287 Some(&a.outlined_button_theme),
1288 Some(&b.outlined_button_theme),
1289 t,
1290 )
1291 .expect("both sides"),
1292 popup_menu_theme: PopupMenuThemeData::lerp(
1293 Some(&a.popup_menu_theme),
1294 Some(&b.popup_menu_theme),
1295 t,
1296 )
1297 .expect("both sides"),
1298 progress_indicator_theme: ProgressIndicatorThemeData::lerp(
1299 Some(&a.progress_indicator_theme),
1300 Some(&b.progress_indicator_theme),
1301 t,
1302 )
1303 .expect("both sides"),
1304 radio_theme: RadioThemeData::lerp(Some(&a.radio_theme), Some(&b.radio_theme), t),
1305 search_bar_theme: SearchBarThemeData::lerp(
1306 Some(&a.search_bar_theme),
1307 Some(&b.search_bar_theme),
1308 t,
1309 )
1310 .expect("both sides"),
1311 search_view_theme: SearchViewThemeData::lerp(
1312 Some(&a.search_view_theme),
1313 Some(&b.search_view_theme),
1314 t,
1315 )
1316 .expect("both sides"),
1317 segmented_button_theme: SegmentedButtonThemeData::lerp(
1318 Some(&a.segmented_button_theme),
1319 Some(&b.segmented_button_theme),
1320 t,
1321 ),
1322 slider_theme: SliderThemeData::lerp(&a.slider_theme, &b.slider_theme, t),
1323 snack_bar_theme: SnackBarThemeData::lerp(
1324 Some(&a.snack_bar_theme),
1325 Some(&b.snack_bar_theme),
1326 t,
1327 ),
1328 switch_theme: SwitchThemeData::lerp(Some(&a.switch_theme), Some(&b.switch_theme), t),
1329 tab_bar_theme: TabBarThemeData::lerp(&a.tab_bar_theme, &b.tab_bar_theme, t),
1330 text_button_theme: TextButtonThemeData::lerp(
1331 Some(&a.text_button_theme),
1332 Some(&b.text_button_theme),
1333 t,
1334 )
1335 .expect("both sides"),
1336 text_selection_theme: TextSelectionThemeData::lerp(
1337 Some(&a.text_selection_theme),
1338 Some(&b.text_selection_theme),
1339 t,
1340 )
1341 .expect("both sides"),
1342 time_picker_theme: TimePickerThemeData::lerp(
1343 Some(&a.time_picker_theme),
1344 Some(&b.time_picker_theme),
1345 t,
1346 ),
1347 toggle_buttons_theme: ToggleButtonsThemeData::lerp(
1348 Some(&a.toggle_buttons_theme),
1349 Some(&b.toggle_buttons_theme),
1350 t,
1351 )
1352 .expect("both sides"),
1353 tooltip_theme: TooltipThemeData::lerp(
1354 Some(&a.tooltip_theme),
1355 Some(&b.tooltip_theme),
1356 t,
1357 )
1358 .expect("both sides"),
1359 button_bar_theme: ButtonBarThemeData::lerp(
1361 Some(&a.button_bar_theme),
1362 Some(&b.button_bar_theme),
1363 t,
1364 )
1365 .expect("both sides"),
1366 dialog_background_color: color(a.dialog_background_color, b.dialog_background_color),
1367 indicator_color: color(a.indicator_color, b.indicator_color),
1368 }
1369 }
1370}
1371
1372fn extensions_equal(
1374 a: &IndexMap<TypeId, ThemeExtensionRef>,
1375 b: &IndexMap<TypeId, ThemeExtensionRef>,
1376) -> bool {
1377 a.len() == b.len()
1378 && a.iter().all(|(id, extension)| {
1379 b.get(id)
1380 .is_some_and(|other| extension.eq_extension(&**other))
1381 })
1382}
1383
1384impl PartialEq for ThemeData {
1385 #[allow(deprecated)]
1386 fn eq(&self, other: &ThemeData) -> bool {
1387 other.adaptation_map == self.adaptation_map
1392 && other.apply_elevation_overlay_color == self.apply_elevation_overlay_color
1393 && other.cupertino_override_theme == self.cupertino_override_theme
1394 && extensions_equal(&other.extensions, &self.extensions)
1395 && other.input_decoration_theme == self.input_decoration_theme
1396 && other.material_tap_target_size == self.material_tap_target_size
1397 && other.page_transitions_theme == self.page_transitions_theme
1398 && other.platform == self.platform
1399 && other.scrollbar_theme == self.scrollbar_theme
1400 && Rc::ptr_eq(&other.splash_factory, &self.splash_factory)
1401 && other.use_material3 == self.use_material3
1402 && other.visual_density == self.visual_density
1403 && other.canvas_color == self.canvas_color
1405 && other.card_color == self.card_color
1406 && other.color_scheme == self.color_scheme
1407 && other.disabled_color == self.disabled_color
1408 && other.divider_color == self.divider_color
1409 && other.focus_color == self.focus_color
1410 && other.highlight_color == self.highlight_color
1411 && other.hint_color == self.hint_color
1412 && other.hover_color == self.hover_color
1413 && other.primary_color == self.primary_color
1414 && other.primary_color_dark == self.primary_color_dark
1415 && other.primary_color_light == self.primary_color_light
1416 && other.scaffold_background_color == self.scaffold_background_color
1417 && other.secondary_header_color == self.secondary_header_color
1418 && other.shadow_color == self.shadow_color
1419 && other.splash_color == self.splash_color
1420 && other.unselected_widget_color == self.unselected_widget_color
1421 && other.icon_theme == self.icon_theme
1423 && other.primary_icon_theme == self.primary_icon_theme
1424 && other.primary_text_theme == self.primary_text_theme
1425 && other.text_theme == self.text_theme
1426 && other.typography == self.typography
1427 && other.action_icon_theme == self.action_icon_theme
1429 && other.app_bar_theme == self.app_bar_theme
1430 && other.badge_theme == self.badge_theme
1431 && other.banner_theme == self.banner_theme
1432 && other.bottom_navigation_bar_theme == self.bottom_navigation_bar_theme
1433 && other.bottom_sheet_theme == self.bottom_sheet_theme
1434 && other.button_theme == self.button_theme
1435 && other.card_theme == self.card_theme
1436 && other.carousel_view_theme == self.carousel_view_theme
1437 && other.checkbox_theme == self.checkbox_theme
1438 && other.chip_theme == self.chip_theme
1439 && other.data_table_theme == self.data_table_theme
1440 && other.date_picker_theme == self.date_picker_theme
1441 && other.dialog_theme == self.dialog_theme
1442 && other.divider_theme == self.divider_theme
1443 && other.drawer_theme == self.drawer_theme
1444 && other.dropdown_menu_theme == self.dropdown_menu_theme
1445 && other.elevated_button_theme == self.elevated_button_theme
1446 && other.expansion_tile_theme == self.expansion_tile_theme
1447 && other.filled_button_theme == self.filled_button_theme
1448 && other.floating_action_button_theme == self.floating_action_button_theme
1449 && other.icon_button_theme == self.icon_button_theme
1450 && other.list_tile_theme == self.list_tile_theme
1451 && other.menu_bar_theme == self.menu_bar_theme
1452 && other.menu_button_theme == self.menu_button_theme
1453 && other.menu_theme == self.menu_theme
1454 && other.navigation_bar_theme == self.navigation_bar_theme
1455 && other.navigation_drawer_theme == self.navigation_drawer_theme
1456 && other.navigation_rail_theme == self.navigation_rail_theme
1457 && other.outlined_button_theme == self.outlined_button_theme
1458 && other.popup_menu_theme == self.popup_menu_theme
1459 && other.progress_indicator_theme == self.progress_indicator_theme
1460 && other.radio_theme == self.radio_theme
1461 && other.search_bar_theme == self.search_bar_theme
1462 && other.search_view_theme == self.search_view_theme
1463 && other.segmented_button_theme == self.segmented_button_theme
1464 && other.slider_theme == self.slider_theme
1465 && other.snack_bar_theme == self.snack_bar_theme
1466 && other.switch_theme == self.switch_theme
1467 && other.tab_bar_theme == self.tab_bar_theme
1468 && other.text_button_theme == self.text_button_theme
1469 && other.text_selection_theme == self.text_selection_theme
1470 && other.time_picker_theme == self.time_picker_theme
1471 && other.toggle_buttons_theme == self.toggle_buttons_theme
1472 && other.tooltip_theme == self.tooltip_theme
1473 && other.button_bar_theme == self.button_bar_theme
1475 && other.dialog_background_color == self.dialog_background_color
1476 && other.indicator_color == self.indicator_color
1477 }
1478}
1479
1480#[derive(Clone, Debug, Default)]
1483pub struct ThemeDataBuilder {
1484 adaptations: Option<Vec<AdaptationRef>>,
1491 apply_elevation_overlay_color: Option<bool>,
1492 cupertino_override_theme: Option<Box<NoDefaultCupertinoThemeData>>,
1493 extensions: Option<Vec<ThemeExtensionRef>>,
1494 input_decoration_theme: Option<Box<InputDecorationThemeData>>,
1495 material_tap_target_size: Option<MaterialTapTargetSize>,
1496 page_transitions_theme: Option<PageTransitionsTheme>,
1497 platform: Option<TargetPlatform>,
1498 scrollbar_theme: Option<ScrollbarThemeData>,
1499 splash_factory: Option<InteractiveInkFeatureFactoryRef>,
1500 use_material3: Option<bool>,
1501 visual_density: Option<VisualDensity>,
1502 color_scheme: Option<ColorScheme>,
1504 brightness: Option<Brightness>,
1505 color_scheme_seed: Option<Color>,
1506 contrast_level: Option<f64>,
1507 canvas_color: Option<Color>,
1511 card_color: Option<Color>,
1512 disabled_color: Option<Color>,
1513 divider_color: Option<Color>,
1514 focus_color: Option<Color>,
1515 highlight_color: Option<Color>,
1516 hint_color: Option<Color>,
1517 hover_color: Option<Color>,
1518 primary_color: Option<Color>,
1519 primary_color_dark: Option<Color>,
1520 primary_color_light: Option<Color>,
1521 primary_swatch: Option<MaterialColor>,
1522 scaffold_background_color: Option<Color>,
1523 secondary_header_color: Option<Color>,
1524 shadow_color: Option<Color>,
1525 splash_color: Option<Color>,
1526 unselected_widget_color: Option<Color>,
1527 font_family: Option<String>,
1529 font_family_fallback: Option<Vec<String>>,
1530 package: Option<String>,
1531 icon_theme: Option<IconThemeData>,
1532 primary_icon_theme: Option<IconThemeData>,
1533 primary_text_theme: Option<Box<TextTheme>>,
1534 text_theme: Option<Box<TextTheme>>,
1535 typography: Option<Box<Typography>>,
1536 action_icon_theme: Option<ActionIconThemeData>,
1538 app_bar_theme: Option<Box<AppBarThemeData>>,
1539 badge_theme: Option<Box<BadgeThemeData>>,
1540 banner_theme: Option<Box<MaterialBannerThemeData>>,
1541 bottom_navigation_bar_theme: Option<Box<BottomNavigationBarThemeData>>,
1542 bottom_sheet_theme: Option<Box<BottomSheetThemeData>>,
1543 button_theme: Option<Box<ButtonThemeData>>,
1544 card_theme: Option<Box<CardThemeData>>,
1545 carousel_view_theme: Option<Box<CarouselViewThemeData>>,
1546 checkbox_theme: Option<Box<CheckboxThemeData>>,
1547 chip_theme: Option<Box<ChipThemeData>>,
1548 data_table_theme: Option<Box<DataTableThemeData>>,
1549 date_picker_theme: Option<Box<DatePickerThemeData>>,
1550 dialog_theme: Option<Box<DialogThemeData>>,
1551 divider_theme: Option<Box<DividerThemeData>>,
1552 drawer_theme: Option<Box<DrawerThemeData>>,
1553 dropdown_menu_theme: Option<Box<DropdownMenuThemeData>>,
1554 elevated_button_theme: Option<Box<ElevatedButtonThemeData>>,
1555 expansion_tile_theme: Option<Box<ExpansionTileThemeData>>,
1556 filled_button_theme: Option<Box<FilledButtonThemeData>>,
1557 floating_action_button_theme: Option<Box<FloatingActionButtonThemeData>>,
1558 icon_button_theme: Option<Box<IconButtonThemeData>>,
1559 list_tile_theme: Option<Box<ListTileThemeData>>,
1560 menu_bar_theme: Option<Box<MenuBarThemeData>>,
1561 menu_button_theme: Option<Box<MenuButtonThemeData>>,
1562 menu_theme: Option<Box<MenuThemeData>>,
1563 navigation_bar_theme: Option<Box<NavigationBarThemeData>>,
1564 navigation_drawer_theme: Option<Box<NavigationDrawerThemeData>>,
1565 navigation_rail_theme: Option<Box<NavigationRailThemeData>>,
1566 outlined_button_theme: Option<Box<OutlinedButtonThemeData>>,
1567 popup_menu_theme: Option<Box<PopupMenuThemeData>>,
1568 progress_indicator_theme: Option<Box<ProgressIndicatorThemeData>>,
1569 radio_theme: Option<Box<RadioThemeData>>,
1570 search_bar_theme: Option<Box<SearchBarThemeData>>,
1571 search_view_theme: Option<Box<SearchViewThemeData>>,
1572 segmented_button_theme: Option<Box<SegmentedButtonThemeData>>,
1573 slider_theme: Option<Box<SliderThemeData>>,
1574 snack_bar_theme: Option<Box<SnackBarThemeData>>,
1575 switch_theme: Option<Box<SwitchThemeData>>,
1576 tab_bar_theme: Option<Box<TabBarThemeData>>,
1577 text_button_theme: Option<Box<TextButtonThemeData>>,
1578 text_selection_theme: Option<Box<TextSelectionThemeData>>,
1579 time_picker_theme: Option<Box<TimePickerThemeData>>,
1580 toggle_buttons_theme: Option<Box<ToggleButtonsThemeData>>,
1581 tooltip_theme: Option<Box<TooltipThemeData>>,
1582 #[allow(deprecated)]
1584 button_bar_theme: Option<Box<ButtonBarThemeData>>,
1585 dialog_background_color: Option<Color>,
1586 indicator_color: Option<Color>,
1587}
1588
1589impl ThemeDataBuilder {
1590 pub fn adaptations(mut self, adaptations: Vec<AdaptationRef>) -> ThemeDataBuilder {
1592 self.adaptations = Some(adaptations);
1593 self
1594 }
1595
1596 pub fn apply_elevation_overlay_color(
1598 mut self,
1599 apply_elevation_overlay_color: bool,
1600 ) -> ThemeDataBuilder {
1601 self.apply_elevation_overlay_color = Some(apply_elevation_overlay_color);
1602 self
1603 }
1604
1605 pub fn cupertino_override_theme(
1608 mut self,
1609 cupertino_override_theme: NoDefaultCupertinoThemeData,
1610 ) -> ThemeDataBuilder {
1611 self.cupertino_override_theme = Some(Box::new(cupertino_override_theme));
1612 self
1613 }
1614
1615 pub fn extensions(mut self, extensions: Vec<ThemeExtensionRef>) -> ThemeDataBuilder {
1617 self.extensions = Some(extensions);
1618 self
1619 }
1620
1621 pub fn input_decoration_theme(
1624 mut self,
1625 input_decoration_theme: impl Into<InputDecorationThemeData>,
1626 ) -> ThemeDataBuilder {
1627 self.input_decoration_theme = Some(Box::new(input_decoration_theme.into()));
1628 self
1629 }
1630
1631 pub fn material_tap_target_size(
1633 mut self,
1634 material_tap_target_size: MaterialTapTargetSize,
1635 ) -> ThemeDataBuilder {
1636 self.material_tap_target_size = Some(material_tap_target_size);
1637 self
1638 }
1639
1640 pub fn page_transitions_theme(
1642 mut self,
1643 page_transitions_theme: PageTransitionsTheme,
1644 ) -> ThemeDataBuilder {
1645 self.page_transitions_theme = Some(page_transitions_theme);
1646 self
1647 }
1648
1649 pub fn platform(mut self, platform: TargetPlatform) -> ThemeDataBuilder {
1651 self.platform = Some(platform);
1652 self
1653 }
1654
1655 pub fn scrollbar_theme(mut self, scrollbar_theme: ScrollbarThemeData) -> ThemeDataBuilder {
1657 self.scrollbar_theme = Some(scrollbar_theme);
1658 self
1659 }
1660
1661 pub fn splash_factory(
1663 mut self,
1664 splash_factory: InteractiveInkFeatureFactoryRef,
1665 ) -> ThemeDataBuilder {
1666 self.splash_factory = Some(splash_factory);
1667 self
1668 }
1669
1670 pub fn use_material3(mut self, use_material3: bool) -> ThemeDataBuilder {
1672 self.use_material3 = Some(use_material3);
1673 self
1674 }
1675
1676 pub fn visual_density(mut self, visual_density: VisualDensity) -> ThemeDataBuilder {
1678 self.visual_density = Some(visual_density);
1679 self
1680 }
1681
1682 pub fn color_scheme(mut self, color_scheme: ColorScheme) -> ThemeDataBuilder {
1684 self.color_scheme = Some(color_scheme);
1685 self
1686 }
1687
1688 pub fn brightness(mut self, brightness: Brightness) -> ThemeDataBuilder {
1690 self.brightness = Some(brightness);
1691 self
1692 }
1693
1694 pub fn color_scheme_seed(mut self, color_scheme_seed: Color) -> ThemeDataBuilder {
1696 self.color_scheme_seed = Some(color_scheme_seed);
1697 self
1698 }
1699
1700 pub fn contrast_level(mut self, contrast_level: f64) -> ThemeDataBuilder {
1702 self.contrast_level = Some(contrast_level);
1703 self
1704 }
1705
1706 pub fn canvas_color(mut self, canvas_color: Color) -> ThemeDataBuilder {
1708 self.canvas_color = Some(canvas_color);
1709 self
1710 }
1711
1712 pub fn card_color(mut self, card_color: Color) -> ThemeDataBuilder {
1714 self.card_color = Some(card_color);
1715 self
1716 }
1717
1718 pub fn disabled_color(mut self, disabled_color: Color) -> ThemeDataBuilder {
1720 self.disabled_color = Some(disabled_color);
1721 self
1722 }
1723
1724 pub fn divider_color(mut self, divider_color: Color) -> ThemeDataBuilder {
1726 self.divider_color = Some(divider_color);
1727 self
1728 }
1729
1730 pub fn focus_color(mut self, focus_color: Color) -> ThemeDataBuilder {
1732 self.focus_color = Some(focus_color);
1733 self
1734 }
1735
1736 pub fn highlight_color(mut self, highlight_color: Color) -> ThemeDataBuilder {
1738 self.highlight_color = Some(highlight_color);
1739 self
1740 }
1741
1742 pub fn hint_color(mut self, hint_color: Color) -> ThemeDataBuilder {
1744 self.hint_color = Some(hint_color);
1745 self
1746 }
1747
1748 pub fn hover_color(mut self, hover_color: Color) -> ThemeDataBuilder {
1750 self.hover_color = Some(hover_color);
1751 self
1752 }
1753
1754 pub fn primary_color(mut self, primary_color: Color) -> ThemeDataBuilder {
1756 self.primary_color = Some(primary_color);
1757 self
1758 }
1759
1760 pub fn primary_color_dark(mut self, primary_color_dark: Color) -> ThemeDataBuilder {
1762 self.primary_color_dark = Some(primary_color_dark);
1763 self
1764 }
1765
1766 pub fn primary_color_light(mut self, primary_color_light: Color) -> ThemeDataBuilder {
1768 self.primary_color_light = Some(primary_color_light);
1769 self
1770 }
1771
1772 pub fn primary_swatch(mut self, primary_swatch: MaterialColor) -> ThemeDataBuilder {
1774 self.primary_swatch = Some(primary_swatch);
1775 self
1776 }
1777
1778 pub fn scaffold_background_color(
1780 mut self,
1781 scaffold_background_color: Color,
1782 ) -> ThemeDataBuilder {
1783 self.scaffold_background_color = Some(scaffold_background_color);
1784 self
1785 }
1786
1787 pub fn secondary_header_color(mut self, secondary_header_color: Color) -> ThemeDataBuilder {
1789 self.secondary_header_color = Some(secondary_header_color);
1790 self
1791 }
1792
1793 pub fn shadow_color(mut self, shadow_color: Color) -> ThemeDataBuilder {
1795 self.shadow_color = Some(shadow_color);
1796 self
1797 }
1798
1799 pub fn splash_color(mut self, splash_color: Color) -> ThemeDataBuilder {
1801 self.splash_color = Some(splash_color);
1802 self
1803 }
1804
1805 pub fn unselected_widget_color(mut self, unselected_widget_color: Color) -> ThemeDataBuilder {
1807 self.unselected_widget_color = Some(unselected_widget_color);
1808 self
1809 }
1810
1811 pub fn font_family(mut self, font_family: impl Into<String>) -> ThemeDataBuilder {
1813 self.font_family = Some(font_family.into());
1814 self
1815 }
1816
1817 pub fn font_family_fallback(mut self, font_family_fallback: Vec<String>) -> ThemeDataBuilder {
1819 self.font_family_fallback = Some(font_family_fallback);
1820 self
1821 }
1822
1823 pub fn package(mut self, package: impl Into<String>) -> ThemeDataBuilder {
1825 self.package = Some(package.into());
1826 self
1827 }
1828
1829 pub fn icon_theme(mut self, icon_theme: IconThemeData) -> ThemeDataBuilder {
1831 self.icon_theme = Some(icon_theme);
1832 self
1833 }
1834
1835 pub fn primary_icon_theme(mut self, primary_icon_theme: IconThemeData) -> ThemeDataBuilder {
1837 self.primary_icon_theme = Some(primary_icon_theme);
1838 self
1839 }
1840
1841 pub fn primary_text_theme(mut self, primary_text_theme: TextTheme) -> ThemeDataBuilder {
1843 self.primary_text_theme = Some(Box::new(primary_text_theme));
1844 self
1845 }
1846
1847 pub fn text_theme(mut self, text_theme: TextTheme) -> ThemeDataBuilder {
1849 self.text_theme = Some(Box::new(text_theme));
1850 self
1851 }
1852
1853 pub fn typography(mut self, typography: Typography) -> ThemeDataBuilder {
1855 self.typography = Some(Box::new(typography));
1856 self
1857 }
1858
1859 pub fn action_icon_theme(mut self, action_icon_theme: ActionIconThemeData) -> ThemeDataBuilder {
1861 self.action_icon_theme = Some(action_icon_theme);
1862 self
1863 }
1864
1865 pub fn app_bar_theme(mut self, app_bar_theme: impl Into<AppBarThemeData>) -> ThemeDataBuilder {
1867 self.app_bar_theme = Some(Box::new(app_bar_theme.into()));
1868 self
1869 }
1870
1871 pub fn badge_theme(mut self, badge_theme: BadgeThemeData) -> ThemeDataBuilder {
1873 self.badge_theme = Some(Box::new(badge_theme));
1874 self
1875 }
1876
1877 pub fn banner_theme(mut self, banner_theme: MaterialBannerThemeData) -> ThemeDataBuilder {
1879 self.banner_theme = Some(Box::new(banner_theme));
1880 self
1881 }
1882
1883 pub fn bottom_navigation_bar_theme(
1885 mut self,
1886 bottom_navigation_bar_theme: BottomNavigationBarThemeData,
1887 ) -> ThemeDataBuilder {
1888 self.bottom_navigation_bar_theme = Some(Box::new(bottom_navigation_bar_theme));
1889 self
1890 }
1891
1892 pub fn bottom_sheet_theme(
1894 mut self,
1895 bottom_sheet_theme: BottomSheetThemeData,
1896 ) -> ThemeDataBuilder {
1897 self.bottom_sheet_theme = Some(Box::new(bottom_sheet_theme));
1898 self
1899 }
1900
1901 pub fn button_theme(mut self, button_theme: ButtonThemeData) -> ThemeDataBuilder {
1903 self.button_theme = Some(Box::new(button_theme));
1904 self
1905 }
1906
1907 pub fn card_theme(mut self, card_theme: CardThemeData) -> ThemeDataBuilder {
1909 self.card_theme = Some(Box::new(card_theme));
1910 self
1911 }
1912
1913 pub fn carousel_view_theme(
1915 mut self,
1916 carousel_view_theme: CarouselViewThemeData,
1917 ) -> ThemeDataBuilder {
1918 self.carousel_view_theme = Some(Box::new(carousel_view_theme));
1919 self
1920 }
1921
1922 pub fn checkbox_theme(mut self, checkbox_theme: CheckboxThemeData) -> ThemeDataBuilder {
1924 self.checkbox_theme = Some(Box::new(checkbox_theme));
1925 self
1926 }
1927
1928 pub fn chip_theme(mut self, chip_theme: ChipThemeData) -> ThemeDataBuilder {
1930 self.chip_theme = Some(Box::new(chip_theme));
1931 self
1932 }
1933
1934 pub fn data_table_theme(mut self, data_table_theme: DataTableThemeData) -> ThemeDataBuilder {
1936 self.data_table_theme = Some(Box::new(data_table_theme));
1937 self
1938 }
1939
1940 pub fn date_picker_theme(mut self, date_picker_theme: DatePickerThemeData) -> ThemeDataBuilder {
1942 self.date_picker_theme = Some(Box::new(date_picker_theme));
1943 self
1944 }
1945
1946 pub fn dialog_theme(mut self, dialog_theme: DialogThemeData) -> ThemeDataBuilder {
1948 self.dialog_theme = Some(Box::new(dialog_theme));
1949 self
1950 }
1951
1952 pub fn divider_theme(mut self, divider_theme: DividerThemeData) -> ThemeDataBuilder {
1954 self.divider_theme = Some(Box::new(divider_theme));
1955 self
1956 }
1957
1958 pub fn drawer_theme(mut self, drawer_theme: DrawerThemeData) -> ThemeDataBuilder {
1960 self.drawer_theme = Some(Box::new(drawer_theme));
1961 self
1962 }
1963
1964 pub fn dropdown_menu_theme(
1966 mut self,
1967 dropdown_menu_theme: DropdownMenuThemeData,
1968 ) -> ThemeDataBuilder {
1969 self.dropdown_menu_theme = Some(Box::new(dropdown_menu_theme));
1970 self
1971 }
1972
1973 pub fn elevated_button_theme(
1975 mut self,
1976 elevated_button_theme: ElevatedButtonThemeData,
1977 ) -> ThemeDataBuilder {
1978 self.elevated_button_theme = Some(Box::new(elevated_button_theme));
1979 self
1980 }
1981
1982 pub fn expansion_tile_theme(
1984 mut self,
1985 expansion_tile_theme: ExpansionTileThemeData,
1986 ) -> ThemeDataBuilder {
1987 self.expansion_tile_theme = Some(Box::new(expansion_tile_theme));
1988 self
1989 }
1990
1991 pub fn filled_button_theme(
1993 mut self,
1994 filled_button_theme: FilledButtonThemeData,
1995 ) -> ThemeDataBuilder {
1996 self.filled_button_theme = Some(Box::new(filled_button_theme));
1997 self
1998 }
1999
2000 pub fn floating_action_button_theme(
2002 mut self,
2003 floating_action_button_theme: FloatingActionButtonThemeData,
2004 ) -> ThemeDataBuilder {
2005 self.floating_action_button_theme = Some(Box::new(floating_action_button_theme));
2006 self
2007 }
2008
2009 pub fn icon_button_theme(mut self, icon_button_theme: IconButtonThemeData) -> ThemeDataBuilder {
2011 self.icon_button_theme = Some(Box::new(icon_button_theme));
2012 self
2013 }
2014
2015 pub fn list_tile_theme(mut self, list_tile_theme: ListTileThemeData) -> ThemeDataBuilder {
2017 self.list_tile_theme = Some(Box::new(list_tile_theme));
2018 self
2019 }
2020
2021 pub fn menu_bar_theme(mut self, menu_bar_theme: MenuBarThemeData) -> ThemeDataBuilder {
2023 self.menu_bar_theme = Some(Box::new(menu_bar_theme));
2024 self
2025 }
2026
2027 pub fn menu_button_theme(mut self, menu_button_theme: MenuButtonThemeData) -> ThemeDataBuilder {
2029 self.menu_button_theme = Some(Box::new(menu_button_theme));
2030 self
2031 }
2032
2033 pub fn menu_theme(mut self, menu_theme: MenuThemeData) -> ThemeDataBuilder {
2035 self.menu_theme = Some(Box::new(menu_theme));
2036 self
2037 }
2038
2039 pub fn navigation_bar_theme(
2041 mut self,
2042 navigation_bar_theme: NavigationBarThemeData,
2043 ) -> ThemeDataBuilder {
2044 self.navigation_bar_theme = Some(Box::new(navigation_bar_theme));
2045 self
2046 }
2047
2048 pub fn navigation_drawer_theme(
2050 mut self,
2051 navigation_drawer_theme: NavigationDrawerThemeData,
2052 ) -> ThemeDataBuilder {
2053 self.navigation_drawer_theme = Some(Box::new(navigation_drawer_theme));
2054 self
2055 }
2056
2057 pub fn navigation_rail_theme(
2059 mut self,
2060 navigation_rail_theme: NavigationRailThemeData,
2061 ) -> ThemeDataBuilder {
2062 self.navigation_rail_theme = Some(Box::new(navigation_rail_theme));
2063 self
2064 }
2065
2066 pub fn outlined_button_theme(
2068 mut self,
2069 outlined_button_theme: OutlinedButtonThemeData,
2070 ) -> ThemeDataBuilder {
2071 self.outlined_button_theme = Some(Box::new(outlined_button_theme));
2072 self
2073 }
2074
2075 pub fn popup_menu_theme(mut self, popup_menu_theme: PopupMenuThemeData) -> ThemeDataBuilder {
2077 self.popup_menu_theme = Some(Box::new(popup_menu_theme));
2078 self
2079 }
2080
2081 pub fn progress_indicator_theme(
2083 mut self,
2084 progress_indicator_theme: ProgressIndicatorThemeData,
2085 ) -> ThemeDataBuilder {
2086 self.progress_indicator_theme = Some(Box::new(progress_indicator_theme));
2087 self
2088 }
2089
2090 pub fn radio_theme(mut self, radio_theme: RadioThemeData) -> ThemeDataBuilder {
2092 self.radio_theme = Some(Box::new(radio_theme));
2093 self
2094 }
2095
2096 pub fn search_bar_theme(mut self, search_bar_theme: SearchBarThemeData) -> ThemeDataBuilder {
2098 self.search_bar_theme = Some(Box::new(search_bar_theme));
2099 self
2100 }
2101
2102 pub fn search_view_theme(mut self, search_view_theme: SearchViewThemeData) -> ThemeDataBuilder {
2104 self.search_view_theme = Some(Box::new(search_view_theme));
2105 self
2106 }
2107
2108 pub fn segmented_button_theme(
2110 mut self,
2111 segmented_button_theme: SegmentedButtonThemeData,
2112 ) -> ThemeDataBuilder {
2113 self.segmented_button_theme = Some(Box::new(segmented_button_theme));
2114 self
2115 }
2116
2117 pub fn slider_theme(mut self, slider_theme: SliderThemeData) -> ThemeDataBuilder {
2119 self.slider_theme = Some(Box::new(slider_theme));
2120 self
2121 }
2122
2123 pub fn snack_bar_theme(mut self, snack_bar_theme: SnackBarThemeData) -> ThemeDataBuilder {
2125 self.snack_bar_theme = Some(Box::new(snack_bar_theme));
2126 self
2127 }
2128
2129 pub fn switch_theme(mut self, switch_theme: SwitchThemeData) -> ThemeDataBuilder {
2131 self.switch_theme = Some(Box::new(switch_theme));
2132 self
2133 }
2134
2135 pub fn tab_bar_theme(mut self, tab_bar_theme: TabBarThemeData) -> ThemeDataBuilder {
2137 self.tab_bar_theme = Some(Box::new(tab_bar_theme));
2138 self
2139 }
2140
2141 pub fn text_button_theme(mut self, text_button_theme: TextButtonThemeData) -> ThemeDataBuilder {
2143 self.text_button_theme = Some(Box::new(text_button_theme));
2144 self
2145 }
2146
2147 pub fn text_selection_theme(
2149 mut self,
2150 text_selection_theme: TextSelectionThemeData,
2151 ) -> ThemeDataBuilder {
2152 self.text_selection_theme = Some(Box::new(text_selection_theme));
2153 self
2154 }
2155
2156 pub fn time_picker_theme(mut self, time_picker_theme: TimePickerThemeData) -> ThemeDataBuilder {
2158 self.time_picker_theme = Some(Box::new(time_picker_theme));
2159 self
2160 }
2161
2162 pub fn toggle_buttons_theme(
2164 mut self,
2165 toggle_buttons_theme: ToggleButtonsThemeData,
2166 ) -> ThemeDataBuilder {
2167 self.toggle_buttons_theme = Some(Box::new(toggle_buttons_theme));
2168 self
2169 }
2170
2171 pub fn tooltip_theme(mut self, tooltip_theme: TooltipThemeData) -> ThemeDataBuilder {
2173 self.tooltip_theme = Some(Box::new(tooltip_theme));
2174 self
2175 }
2176
2177 #[deprecated(
2179 note = "Use OverflowBar instead. This feature was deprecated after v3.21.0-10.0.pre."
2180 )]
2181 #[allow(deprecated)]
2182 pub fn button_bar_theme(mut self, button_bar_theme: ButtonBarThemeData) -> ThemeDataBuilder {
2183 self.button_bar_theme = Some(Box::new(button_bar_theme));
2184 self
2185 }
2186
2187 #[deprecated(
2189 note = "Use DialogThemeData.backgroundColor instead. This feature was deprecated after v3.27.0-0.1.pre."
2190 )]
2191 pub fn dialog_background_color(mut self, dialog_background_color: Color) -> ThemeDataBuilder {
2192 self.dialog_background_color = Some(dialog_background_color);
2193 self
2194 }
2195
2196 #[deprecated(
2198 note = "Use TabBarThemeData.indicatorColor instead. This feature was deprecated after v3.28.0-1.0.pre."
2199 )]
2200 pub fn indicator_color(mut self, indicator_color: Color) -> ThemeDataBuilder {
2201 self.indicator_color = Some(indicator_color);
2202 self
2203 }
2204
2205 pub fn build(self, app: &App) -> ThemeData {
2208 self.build_for_platform(app.platform().target_platform())
2209 }
2210
2211 #[allow(deprecated)]
2216 pub(crate) fn build_for_platform(self, default_platform: TargetPlatform) -> ThemeData {
2217 let ThemeDataBuilder {
2218 adaptations,
2219 mut apply_elevation_overlay_color,
2220 cupertino_override_theme,
2221 extensions,
2222 input_decoration_theme,
2223 mut material_tap_target_size,
2224 page_transitions_theme,
2225 platform,
2226 scrollbar_theme,
2227 splash_factory,
2228 use_material3,
2229 visual_density,
2230 mut color_scheme,
2231 brightness,
2232 color_scheme_seed,
2233 contrast_level,
2234 mut canvas_color,
2235 mut card_color,
2236 disabled_color,
2237 mut divider_color,
2238 focus_color,
2239 highlight_color,
2240 hint_color,
2241 hover_color,
2242 mut primary_color,
2243 primary_color_dark,
2244 primary_color_light,
2245 primary_swatch,
2246 mut scaffold_background_color,
2247 secondary_header_color,
2248 shadow_color,
2249 splash_color,
2250 unselected_widget_color,
2251 font_family,
2252 font_family_fallback,
2253 package,
2254 icon_theme,
2255 primary_icon_theme,
2256 primary_text_theme,
2257 text_theme,
2258 typography,
2259 action_icon_theme,
2260 app_bar_theme,
2261 badge_theme,
2262 banner_theme,
2263 bottom_navigation_bar_theme,
2264 bottom_sheet_theme,
2265 button_theme,
2266 card_theme,
2267 carousel_view_theme,
2268 checkbox_theme,
2269 chip_theme,
2270 data_table_theme,
2271 date_picker_theme,
2272 dialog_theme,
2273 divider_theme,
2274 drawer_theme,
2275 dropdown_menu_theme,
2276 elevated_button_theme,
2277 expansion_tile_theme,
2278 filled_button_theme,
2279 floating_action_button_theme,
2280 icon_button_theme,
2281 list_tile_theme,
2282 menu_bar_theme,
2283 menu_button_theme,
2284 menu_theme,
2285 navigation_bar_theme,
2286 navigation_drawer_theme,
2287 navigation_rail_theme,
2288 outlined_button_theme,
2289 popup_menu_theme,
2290 progress_indicator_theme,
2291 radio_theme,
2292 search_bar_theme,
2293 search_view_theme,
2294 segmented_button_theme,
2295 slider_theme,
2296 snack_bar_theme,
2297 switch_theme,
2298 tab_bar_theme,
2299 text_button_theme,
2300 text_selection_theme,
2301 time_picker_theme,
2302 toggle_buttons_theme,
2303 tooltip_theme,
2304 button_bar_theme,
2305 mut dialog_background_color,
2306 mut indicator_color,
2307 } = self;
2308
2309 let cupertino_override_theme =
2311 cupertino_override_theme.map(|override_theme| override_theme.no_default());
2312 let extensions = extensions.unwrap_or_default();
2313 let adaptations = adaptations.unwrap_or_default();
2314 let platform = platform.unwrap_or(default_platform);
2315 match platform {
2316 TargetPlatform::Android | TargetPlatform::Fuchsia | TargetPlatform::IOS => {
2317 material_tap_target_size.get_or_insert(MaterialTapTargetSize::Padded);
2318 }
2319 TargetPlatform::Linux | TargetPlatform::MacOS | TargetPlatform::Windows => {
2320 material_tap_target_size.get_or_insert(MaterialTapTargetSize::ShrinkWrap);
2321 }
2322 }
2323 let page_transitions_theme = page_transitions_theme.unwrap_or_default();
2324 let scrollbar_theme = scrollbar_theme.unwrap_or_default();
2325 let visual_density =
2326 visual_density.unwrap_or_else(|| VisualDensity::default_density_for_platform(platform));
2327 let use_material3 = use_material3.unwrap_or(true);
2328 debug_assert!(
2329 contrast_level.is_none_or(|contrast_level| {
2330 contrast_level + PRECISION_ERROR_TOLERANCE >= -1.0
2331 && contrast_level - PRECISION_ERROR_TOLERANCE <= 1.0
2332 }),
2333 "contrastLevel must be between -1.0 and 1.0 inclusive."
2334 );
2335 let contrast_level = contrast_level.unwrap_or(ContrastLevel::STANDARD);
2336 debug_assert!(
2337 color_scheme_seed.is_some() || debug_is_using_predefined_contrast_level(contrast_level),
2338 "colorSchemeSeed must be provided to use a custom contrastLevel. \
2339 Without a colorSchemeSeed, contrastLevel must be \
2340 ContrastLevel.standard (0.0), ContrastLevel.medium (0.5), or \
2341 ContrastLevel.high (1.0)."
2342 );
2343 let use_ink_sparkle = platform == TargetPlatform::Android && !K_IS_WEB;
2344 let splash_factory = splash_factory.unwrap_or_else(|| {
2345 if use_material3 {
2346 if use_ink_sparkle {
2347 InkSparkle::splash_factory()
2348 } else {
2349 InkRipple::splash_factory()
2350 }
2351 } else {
2352 InkSplash::splash_factory()
2353 }
2354 });
2355
2356 debug_assert!(
2358 color_scheme.is_none()
2359 || brightness.is_none()
2360 || color_scheme.as_ref().map(|scheme| scheme.brightness()) == brightness,
2361 "ThemeData.brightness does not match ColorScheme.brightness. \
2362 Either override ColorScheme.brightness or ThemeData.brightness to \
2363 match the other."
2364 );
2365 debug_assert!(color_scheme_seed.is_none() || color_scheme.is_none());
2366 debug_assert!(color_scheme_seed.is_none() || primary_swatch.is_none());
2367 debug_assert!(color_scheme_seed.is_none() || primary_color.is_none());
2368 let effective_brightness = brightness
2369 .or(color_scheme.as_ref().map(|scheme| scheme.brightness()))
2370 .unwrap_or(Brightness::Light);
2371 let is_dark = effective_brightness == Brightness::Dark;
2372 if color_scheme_seed.is_some() || use_material3 {
2373 if let Some(color_scheme_seed) = color_scheme_seed {
2374 color_scheme = Some(
2375 ColorScheme::from_seed(color_scheme_seed)
2376 .brightness(effective_brightness)
2377 .contrast_level(contrast_level)
2378 .build(),
2379 );
2380 }
2381 let color_scheme = color_scheme.get_or_insert_with(|| {
2382 default_m3_color_scheme(effective_brightness, contrast_level)
2383 });
2384
2385 let primary_surface_color = if is_dark {
2387 color_scheme.surface()
2388 } else {
2389 color_scheme.primary()
2390 };
2391 let on_primary_surface_color = if is_dark {
2392 color_scheme.on_surface()
2393 } else {
2394 color_scheme.on_primary()
2395 };
2396
2397 primary_color.get_or_insert(primary_surface_color);
2399 canvas_color.get_or_insert(color_scheme.surface());
2400 scaffold_background_color.get_or_insert(color_scheme.surface());
2401 card_color.get_or_insert(color_scheme.surface());
2402 divider_color.get_or_insert(color_scheme.outline());
2403 dialog_background_color.get_or_insert(color_scheme.surface());
2404 indicator_color.get_or_insert(on_primary_surface_color);
2405 apply_elevation_overlay_color.get_or_insert(brightness == Some(Brightness::Dark));
2406 }
2407 let apply_elevation_overlay_color = apply_elevation_overlay_color.unwrap_or(false);
2408 let primary_swatch = primary_swatch.unwrap_or(Colors::BLUE);
2409 let primary_color = primary_color.unwrap_or_else(|| {
2410 if is_dark {
2411 Colors::GREY.shade900()
2412 } else {
2413 primary_swatch.primary()
2414 }
2415 });
2416 let estimated_primary_color_brightness =
2417 ThemeData::estimate_brightness_for_color(primary_color);
2418 let primary_color_light = primary_color_light.unwrap_or_else(|| {
2419 if is_dark {
2420 Colors::GREY.shade500()
2421 } else {
2422 primary_swatch.shade100()
2423 }
2424 });
2425 let primary_color_dark = primary_color_dark.unwrap_or_else(|| {
2426 if is_dark {
2427 Colors::BLACK
2428 } else {
2429 primary_swatch.shade700()
2430 }
2431 });
2432 let primary_is_dark = estimated_primary_color_brightness == Brightness::Dark;
2433 let focus_color = focus_color.unwrap_or_else(|| {
2434 if is_dark {
2435 Colors::WHITE.with_opacity(0.12)
2436 } else {
2437 Colors::BLACK.with_opacity(0.12)
2438 }
2439 });
2440 let hover_color = hover_color.unwrap_or_else(|| {
2441 if is_dark {
2442 Colors::WHITE.with_opacity(0.04)
2443 } else {
2444 Colors::BLACK.with_opacity(0.04)
2445 }
2446 });
2447 let shadow_color = shadow_color.unwrap_or(Colors::BLACK);
2448 let canvas_color = canvas_color.unwrap_or_else(|| {
2449 if is_dark {
2450 grey(850)
2451 } else {
2452 Colors::GREY.shade50()
2453 }
2454 });
2455 let scaffold_background_color = scaffold_background_color.unwrap_or(canvas_color);
2456 let card_color = card_color.unwrap_or_else(|| {
2457 if is_dark {
2458 Colors::GREY.shade800()
2459 } else {
2460 Colors::WHITE
2461 }
2462 });
2463 let divider_color = divider_color.unwrap_or(if is_dark {
2464 Color::new(0x1FFFFFFF)
2465 } else {
2466 Color::new(0x1F000000)
2467 });
2468 let color_scheme = color_scheme.unwrap_or_else(|| {
2471 ColorScheme::from_swatch()
2472 .primary_swatch(primary_swatch.clone())
2473 .accent_color(if is_dark {
2474 Colors::TEAL_ACCENT.shade200()
2475 } else {
2476 primary_swatch.shade500()
2477 })
2478 .card_color(card_color)
2479 .background_color(if is_dark {
2480 Colors::GREY.shade700()
2481 } else {
2482 primary_swatch.shade200()
2483 })
2484 .error_color(Colors::RED.shade700())
2485 .brightness(effective_brightness)
2486 .build()
2487 });
2488 let unselected_widget_color = unselected_widget_color.unwrap_or(if is_dark {
2489 Colors::WHITE70
2490 } else {
2491 Colors::BLACK54
2492 });
2493 let secondary_header_color = secondary_header_color.unwrap_or_else(|| {
2495 if is_dark {
2496 Colors::GREY.shade700()
2497 } else {
2498 primary_swatch.shade50()
2499 }
2500 });
2501 let hint_color = hint_color.unwrap_or_else(|| {
2502 if is_dark {
2503 Colors::WHITE60
2504 } else {
2505 Colors::BLACK.with_opacity(0.6)
2506 }
2507 });
2508 let button_theme = button_theme
2511 .map(|button_theme| *button_theme)
2512 .unwrap_or_else(|| {
2513 let mut button_theme = ButtonThemeData::new()
2514 .with_color_scheme(color_scheme.clone())
2515 .with_button_color(if is_dark {
2516 primary_swatch.shade600()
2517 } else {
2518 Colors::GREY.shade300()
2519 })
2520 .with_focus_color(focus_color)
2521 .with_hover_color(hover_color);
2522 if let Some(disabled_color) = disabled_color {
2523 button_theme = button_theme.with_disabled_color(disabled_color);
2524 }
2525 if let Some(highlight_color) = highlight_color {
2526 button_theme = button_theme.with_highlight_color(highlight_color);
2527 }
2528 if let Some(splash_color) = splash_color {
2529 button_theme = button_theme.with_splash_color(splash_color);
2530 }
2531 if let Some(material_tap_target_size) = material_tap_target_size {
2532 button_theme =
2533 button_theme.with_material_tap_target_size(material_tap_target_size);
2534 }
2535 button_theme
2536 });
2537 let disabled_color = disabled_color.unwrap_or(if is_dark {
2538 Colors::WHITE38
2539 } else {
2540 Colors::BLACK38
2541 });
2542 let highlight_color = highlight_color.unwrap_or(if is_dark {
2543 Color::new(0x40CCCCCC)
2544 } else {
2545 Color::new(0x66BCBCBC)
2546 });
2547 let splash_color = splash_color.unwrap_or(if is_dark {
2548 Color::new(0x40CCCCCC)
2549 } else {
2550 Color::new(0x66C8C8C8)
2551 });
2552
2553 let typography = typography.map(|typography| *typography).unwrap_or_else(|| {
2555 if use_material3 {
2556 Typography::material2021()
2557 .platform(Some(platform))
2558 .color_scheme(color_scheme.clone())
2559 .build()
2560 } else {
2561 Typography::material2014().platform(Some(platform)).build()
2562 }
2563 });
2564 let mut default_text_theme = TextTheme::clone(if is_dark {
2565 &typography.white
2566 } else {
2567 &typography.black
2568 });
2569 let mut default_primary_text_theme = TextTheme::clone(if primary_is_dark {
2570 &typography.white
2571 } else {
2572 &typography.black
2573 });
2574 if let Some(font_family) = &font_family {
2575 default_text_theme = default_text_theme
2576 .apply()
2577 .font_family(font_family.clone())
2578 .into_text_theme();
2579 default_primary_text_theme = default_primary_text_theme
2580 .apply()
2581 .font_family(font_family.clone())
2582 .into_text_theme();
2583 }
2584 if let Some(font_family_fallback) = &font_family_fallback {
2585 default_text_theme = default_text_theme
2586 .apply()
2587 .font_family_fallback(font_family_fallback.clone())
2588 .into_text_theme();
2589 default_primary_text_theme = default_primary_text_theme
2590 .apply()
2591 .font_family_fallback(font_family_fallback.clone())
2592 .into_text_theme();
2593 }
2594 if let Some(package) = &package {
2595 default_text_theme = default_text_theme
2596 .apply()
2597 .package(package.clone())
2598 .into_text_theme();
2599 default_primary_text_theme = default_primary_text_theme
2600 .apply()
2601 .package(package.clone())
2602 .into_text_theme();
2603 }
2604 let text_theme = default_text_theme.merge(text_theme.as_deref());
2605 let primary_text_theme = default_primary_text_theme.merge(primary_text_theme.as_deref());
2606 let icon_theme = icon_theme.unwrap_or_else(|| {
2607 if is_dark {
2608 IconThemeData::new().color(K_DEFAULT_ICON_LIGHT_COLOR)
2609 } else {
2610 IconThemeData::new().color(K_DEFAULT_ICON_DARK_COLOR)
2611 }
2612 });
2613 let primary_icon_theme = primary_icon_theme.unwrap_or_else(|| {
2614 if primary_is_dark {
2615 IconThemeData::new().color(Colors::WHITE)
2616 } else {
2617 IconThemeData::new().color(Colors::BLACK)
2618 }
2619 });
2620
2621 let button_bar_theme = unboxed_or_default(button_bar_theme);
2623 let dialog_background_color = dialog_background_color.unwrap_or_else(|| {
2624 if is_dark {
2625 Colors::GREY.shade800()
2626 } else {
2627 Colors::WHITE
2628 }
2629 });
2630 let indicator_color =
2631 indicator_color.unwrap_or(if color_scheme.secondary() == primary_color {
2632 Colors::WHITE
2633 } else {
2634 color_scheme.secondary()
2635 });
2636
2637 ThemeData {
2638 adaptation_map: ThemeData::create_adaptation_map(adaptations),
2645 apply_elevation_overlay_color,
2646 cupertino_override_theme,
2647 extensions: ThemeData::theme_extension_iterable_to_map(extensions),
2648 input_decoration_theme: unboxed_or_default(input_decoration_theme),
2649 material_tap_target_size: material_tap_target_size.expect("defaulted per platform"),
2650 page_transitions_theme,
2651 platform,
2652 scrollbar_theme,
2653 splash_factory,
2654 use_material3,
2655 visual_density,
2656 canvas_color,
2658 card_color,
2659 color_scheme,
2660 disabled_color,
2661 divider_color,
2662 focus_color,
2663 highlight_color,
2664 hint_color,
2665 hover_color,
2666 primary_color,
2667 primary_color_dark,
2668 primary_color_light,
2669 scaffold_background_color,
2670 secondary_header_color,
2671 shadow_color,
2672 splash_color,
2673 unselected_widget_color,
2674 icon_theme,
2676 primary_text_theme,
2677 text_theme,
2678 typography,
2679 primary_icon_theme,
2680 action_icon_theme,
2682 app_bar_theme: unboxed_or_default(app_bar_theme),
2683 badge_theme: unboxed_or_default(badge_theme),
2684 banner_theme: unboxed_or_default(banner_theme),
2685 bottom_navigation_bar_theme: unboxed_or_default(bottom_navigation_bar_theme),
2686 bottom_sheet_theme: unboxed_or_default(bottom_sheet_theme),
2687 button_theme,
2688 card_theme: unboxed_or_default(card_theme),
2689 carousel_view_theme: unboxed_or_default(carousel_view_theme),
2690 checkbox_theme: unboxed_or_default(checkbox_theme),
2691 chip_theme: unboxed_or_default(chip_theme),
2692 data_table_theme: unboxed_or_default(data_table_theme),
2693 date_picker_theme: unboxed_or_default(date_picker_theme),
2694 dialog_theme: unboxed_or_default(dialog_theme),
2695 divider_theme: unboxed_or_default(divider_theme),
2696 drawer_theme: unboxed_or_default(drawer_theme),
2697 dropdown_menu_theme: unboxed_or_default(dropdown_menu_theme),
2698 elevated_button_theme: unboxed_or_default(elevated_button_theme),
2699 expansion_tile_theme: unboxed_or_default(expansion_tile_theme),
2700 filled_button_theme: unboxed_or_default(filled_button_theme),
2701 floating_action_button_theme: unboxed_or_default(floating_action_button_theme),
2702 icon_button_theme: unboxed_or_default(icon_button_theme),
2703 list_tile_theme: unboxed_or_default(list_tile_theme),
2704 menu_bar_theme: unboxed_or_default(menu_bar_theme),
2705 menu_button_theme: unboxed_or_default(menu_button_theme),
2706 menu_theme: unboxed_or_default(menu_theme),
2707 navigation_bar_theme: unboxed_or_default(navigation_bar_theme),
2708 navigation_drawer_theme: unboxed_or_default(navigation_drawer_theme),
2709 navigation_rail_theme: unboxed_or_default(navigation_rail_theme),
2710 outlined_button_theme: unboxed_or_default(outlined_button_theme),
2711 popup_menu_theme: unboxed_or_default(popup_menu_theme),
2712 progress_indicator_theme: unboxed_or_default(progress_indicator_theme),
2713 radio_theme: unboxed_or_default(radio_theme),
2714 search_bar_theme: unboxed_or_default(search_bar_theme),
2715 search_view_theme: unboxed_or_default(search_view_theme),
2716 segmented_button_theme: unboxed_or_default(segmented_button_theme),
2717 slider_theme: unboxed_or_default(slider_theme),
2718 snack_bar_theme: unboxed_or_default(snack_bar_theme),
2719 switch_theme: unboxed_or_default(switch_theme),
2720 tab_bar_theme: unboxed_or_default(tab_bar_theme),
2721 text_button_theme: unboxed_or_default(text_button_theme),
2722 text_selection_theme: unboxed_or_default(text_selection_theme),
2723 time_picker_theme: unboxed_or_default(time_picker_theme),
2724 toggle_buttons_theme: unboxed_or_default(toggle_buttons_theme),
2725 tooltip_theme: unboxed_or_default(tooltip_theme),
2726 button_bar_theme,
2728 dialog_background_color,
2729 indicator_color,
2730 }
2731 }
2732}
2733
2734fn unboxed_or_default<T: Default>(value: Option<Box<T>>) -> T {
2736 value.map_or_else(T::default, |value| *value)
2737}
2738
2739fn grey(shade: i32) -> Color {
2741 Colors::GREY
2742 .get(&shade)
2743 .expect("the grey swatch has this shade")
2744}
2745
2746#[derive(Clone, Debug)]
2748pub struct ThemeDataFromBuilder {
2749 color_scheme: ColorScheme,
2750 text_theme: Option<Box<TextTheme>>,
2751 use_material3: Option<bool>,
2752}
2753
2754impl ThemeDataFromBuilder {
2755 pub fn text_theme(mut self, text_theme: TextTheme) -> ThemeDataFromBuilder {
2757 self.text_theme = Some(Box::new(text_theme));
2758 self
2759 }
2760
2761 pub fn use_material3(mut self, use_material3: bool) -> ThemeDataFromBuilder {
2763 self.use_material3 = Some(use_material3);
2764 self
2765 }
2766
2767 #[allow(deprecated)]
2769 pub fn build(self, app: &App) -> ThemeData {
2770 let color_scheme = self.color_scheme;
2771 let is_dark = color_scheme.brightness() == Brightness::Dark;
2772
2773 let primary_surface_color = if is_dark {
2775 color_scheme.surface()
2776 } else {
2777 color_scheme.primary()
2778 };
2779 let on_primary_surface_color = if is_dark {
2780 color_scheme.on_surface()
2781 } else {
2782 color_scheme.on_primary()
2783 };
2784
2785 let mut builder = ThemeData::builder()
2786 .brightness(color_scheme.brightness())
2787 .primary_color(primary_surface_color)
2788 .canvas_color(color_scheme.surface())
2789 .scaffold_background_color(color_scheme.surface())
2790 .card_color(color_scheme.surface())
2791 .divider_color(color_scheme.on_surface().with_opacity(0.12))
2792 .dialog_background_color(color_scheme.surface())
2793 .indicator_color(on_primary_surface_color)
2794 .apply_elevation_overlay_color(is_dark)
2795 .color_scheme(color_scheme);
2796 if let Some(text_theme) = self.text_theme {
2797 builder = builder.text_theme(*text_theme);
2798 }
2799 if let Some(use_material3) = self.use_material3 {
2800 builder = builder.use_material3(use_material3);
2801 }
2802 builder.build(app)
2803 }
2804}
2805
2806#[derive(Clone, Debug)]
2810pub struct ThemeDataCopyWith {
2811 theme: Box<ThemeData>,
2812 color_scheme: Option<ColorScheme>,
2813 brightness: Option<Brightness>,
2814}
2815
2816impl ThemeDataCopyWith {
2817 pub fn adaptations(mut self, adaptations: Vec<AdaptationRef>) -> ThemeDataCopyWith {
2819 self.theme.adaptation_map = ThemeData::create_adaptation_map(adaptations);
2820 self
2821 }
2822
2823 pub fn apply_elevation_overlay_color(
2825 mut self,
2826 apply_elevation_overlay_color: bool,
2827 ) -> ThemeDataCopyWith {
2828 self.theme.apply_elevation_overlay_color = apply_elevation_overlay_color;
2829 self
2830 }
2831
2832 pub fn cupertino_override_theme(
2834 mut self,
2835 cupertino_override_theme: NoDefaultCupertinoThemeData,
2836 ) -> ThemeDataCopyWith {
2837 self.theme.cupertino_override_theme = Some(cupertino_override_theme.no_default());
2838 self
2839 }
2840
2841 pub fn extensions(mut self, extensions: Vec<ThemeExtensionRef>) -> ThemeDataCopyWith {
2843 self.theme.extensions = ThemeData::theme_extension_iterable_to_map(extensions);
2844 self
2845 }
2846
2847 pub fn input_decoration_theme(
2850 mut self,
2851 input_decoration_theme: impl Into<InputDecorationThemeData>,
2852 ) -> ThemeDataCopyWith {
2853 self.theme.input_decoration_theme = input_decoration_theme.into();
2854 self
2855 }
2856
2857 pub fn material_tap_target_size(
2859 mut self,
2860 material_tap_target_size: MaterialTapTargetSize,
2861 ) -> ThemeDataCopyWith {
2862 self.theme.material_tap_target_size = material_tap_target_size;
2863 self
2864 }
2865
2866 pub fn page_transitions_theme(
2868 mut self,
2869 page_transitions_theme: PageTransitionsTheme,
2870 ) -> ThemeDataCopyWith {
2871 self.theme.page_transitions_theme = page_transitions_theme;
2872 self
2873 }
2874
2875 pub fn platform(mut self, platform: TargetPlatform) -> ThemeDataCopyWith {
2877 self.theme.platform = platform;
2878 self
2879 }
2880
2881 pub fn scrollbar_theme(mut self, scrollbar_theme: ScrollbarThemeData) -> ThemeDataCopyWith {
2883 self.theme.scrollbar_theme = scrollbar_theme;
2884 self
2885 }
2886
2887 pub fn splash_factory(
2889 mut self,
2890 splash_factory: InteractiveInkFeatureFactoryRef,
2891 ) -> ThemeDataCopyWith {
2892 self.theme.splash_factory = splash_factory;
2893 self
2894 }
2895
2896 pub fn visual_density(mut self, visual_density: VisualDensity) -> ThemeDataCopyWith {
2898 self.theme.visual_density = visual_density;
2899 self
2900 }
2901
2902 pub fn color_scheme(mut self, color_scheme: ColorScheme) -> ThemeDataCopyWith {
2904 self.color_scheme = Some(color_scheme);
2905 self
2906 }
2907
2908 pub fn brightness(mut self, brightness: Brightness) -> ThemeDataCopyWith {
2910 self.brightness = Some(brightness);
2911 self
2912 }
2913
2914 pub fn canvas_color(mut self, canvas_color: Color) -> ThemeDataCopyWith {
2916 self.theme.canvas_color = canvas_color;
2917 self
2918 }
2919
2920 pub fn card_color(mut self, card_color: Color) -> ThemeDataCopyWith {
2922 self.theme.card_color = card_color;
2923 self
2924 }
2925
2926 pub fn disabled_color(mut self, disabled_color: Color) -> ThemeDataCopyWith {
2928 self.theme.disabled_color = disabled_color;
2929 self
2930 }
2931
2932 pub fn divider_color(mut self, divider_color: Color) -> ThemeDataCopyWith {
2934 self.theme.divider_color = divider_color;
2935 self
2936 }
2937
2938 pub fn focus_color(mut self, focus_color: Color) -> ThemeDataCopyWith {
2940 self.theme.focus_color = focus_color;
2941 self
2942 }
2943
2944 pub fn highlight_color(mut self, highlight_color: Color) -> ThemeDataCopyWith {
2946 self.theme.highlight_color = highlight_color;
2947 self
2948 }
2949
2950 pub fn hint_color(mut self, hint_color: Color) -> ThemeDataCopyWith {
2952 self.theme.hint_color = hint_color;
2953 self
2954 }
2955
2956 pub fn hover_color(mut self, hover_color: Color) -> ThemeDataCopyWith {
2958 self.theme.hover_color = hover_color;
2959 self
2960 }
2961
2962 pub fn primary_color(mut self, primary_color: Color) -> ThemeDataCopyWith {
2964 self.theme.primary_color = primary_color;
2965 self
2966 }
2967
2968 pub fn primary_color_dark(mut self, primary_color_dark: Color) -> ThemeDataCopyWith {
2970 self.theme.primary_color_dark = primary_color_dark;
2971 self
2972 }
2973
2974 pub fn primary_color_light(mut self, primary_color_light: Color) -> ThemeDataCopyWith {
2976 self.theme.primary_color_light = primary_color_light;
2977 self
2978 }
2979
2980 pub fn scaffold_background_color(
2982 mut self,
2983 scaffold_background_color: Color,
2984 ) -> ThemeDataCopyWith {
2985 self.theme.scaffold_background_color = scaffold_background_color;
2986 self
2987 }
2988
2989 pub fn secondary_header_color(mut self, secondary_header_color: Color) -> ThemeDataCopyWith {
2991 self.theme.secondary_header_color = secondary_header_color;
2992 self
2993 }
2994
2995 pub fn shadow_color(mut self, shadow_color: Color) -> ThemeDataCopyWith {
2997 self.theme.shadow_color = shadow_color;
2998 self
2999 }
3000
3001 pub fn splash_color(mut self, splash_color: Color) -> ThemeDataCopyWith {
3003 self.theme.splash_color = splash_color;
3004 self
3005 }
3006
3007 pub fn unselected_widget_color(mut self, unselected_widget_color: Color) -> ThemeDataCopyWith {
3009 self.theme.unselected_widget_color = unselected_widget_color;
3010 self
3011 }
3012
3013 pub fn icon_theme(mut self, icon_theme: IconThemeData) -> ThemeDataCopyWith {
3015 self.theme.icon_theme = icon_theme;
3016 self
3017 }
3018
3019 pub fn primary_icon_theme(mut self, primary_icon_theme: IconThemeData) -> ThemeDataCopyWith {
3021 self.theme.primary_icon_theme = primary_icon_theme;
3022 self
3023 }
3024
3025 pub fn primary_text_theme(mut self, primary_text_theme: TextTheme) -> ThemeDataCopyWith {
3027 self.theme.primary_text_theme = primary_text_theme;
3028 self
3029 }
3030
3031 pub fn text_theme(mut self, text_theme: TextTheme) -> ThemeDataCopyWith {
3033 self.theme.text_theme = text_theme;
3034 self
3035 }
3036
3037 pub fn typography(mut self, typography: Typography) -> ThemeDataCopyWith {
3039 self.theme.typography = typography;
3040 self
3041 }
3042
3043 pub fn action_icon_theme(
3045 mut self,
3046 action_icon_theme: ActionIconThemeData,
3047 ) -> ThemeDataCopyWith {
3048 self.theme.action_icon_theme = Some(action_icon_theme);
3049 self
3050 }
3051
3052 pub fn app_bar_theme(mut self, app_bar_theme: impl Into<AppBarThemeData>) -> ThemeDataCopyWith {
3054 self.theme.app_bar_theme = app_bar_theme.into();
3055 self
3056 }
3057
3058 pub fn badge_theme(mut self, badge_theme: BadgeThemeData) -> ThemeDataCopyWith {
3060 self.theme.badge_theme = badge_theme;
3061 self
3062 }
3063
3064 pub fn banner_theme(mut self, banner_theme: MaterialBannerThemeData) -> ThemeDataCopyWith {
3066 self.theme.banner_theme = banner_theme;
3067 self
3068 }
3069
3070 pub fn bottom_navigation_bar_theme(
3072 mut self,
3073 bottom_navigation_bar_theme: BottomNavigationBarThemeData,
3074 ) -> ThemeDataCopyWith {
3075 self.theme.bottom_navigation_bar_theme = bottom_navigation_bar_theme;
3076 self
3077 }
3078
3079 pub fn bottom_sheet_theme(
3081 mut self,
3082 bottom_sheet_theme: BottomSheetThemeData,
3083 ) -> ThemeDataCopyWith {
3084 self.theme.bottom_sheet_theme = bottom_sheet_theme;
3085 self
3086 }
3087
3088 pub fn button_theme(mut self, button_theme: ButtonThemeData) -> ThemeDataCopyWith {
3090 self.theme.button_theme = button_theme;
3091 self
3092 }
3093
3094 pub fn card_theme(mut self, card_theme: CardThemeData) -> ThemeDataCopyWith {
3096 self.theme.card_theme = card_theme;
3097 self
3098 }
3099
3100 pub fn carousel_view_theme(
3101 mut self,
3102 carousel_view_theme: CarouselViewThemeData,
3103 ) -> ThemeDataCopyWith {
3104 self.theme.carousel_view_theme = carousel_view_theme;
3105 self
3106 }
3107
3108 pub fn checkbox_theme(mut self, checkbox_theme: CheckboxThemeData) -> ThemeDataCopyWith {
3110 self.theme.checkbox_theme = checkbox_theme;
3111 self
3112 }
3113
3114 pub fn chip_theme(mut self, chip_theme: ChipThemeData) -> ThemeDataCopyWith {
3116 self.theme.chip_theme = chip_theme;
3117 self
3118 }
3119
3120 pub fn data_table_theme(mut self, data_table_theme: DataTableThemeData) -> ThemeDataCopyWith {
3122 self.theme.data_table_theme = data_table_theme;
3123 self
3124 }
3125
3126 pub fn date_picker_theme(
3128 mut self,
3129 date_picker_theme: DatePickerThemeData,
3130 ) -> ThemeDataCopyWith {
3131 self.theme.date_picker_theme = date_picker_theme;
3132 self
3133 }
3134
3135 pub fn dialog_theme(mut self, dialog_theme: DialogThemeData) -> ThemeDataCopyWith {
3137 self.theme.dialog_theme = dialog_theme;
3138 self
3139 }
3140
3141 pub fn divider_theme(mut self, divider_theme: DividerThemeData) -> ThemeDataCopyWith {
3143 self.theme.divider_theme = divider_theme;
3144 self
3145 }
3146
3147 pub fn drawer_theme(mut self, drawer_theme: DrawerThemeData) -> ThemeDataCopyWith {
3149 self.theme.drawer_theme = drawer_theme;
3150 self
3151 }
3152
3153 pub fn dropdown_menu_theme(
3155 mut self,
3156 dropdown_menu_theme: DropdownMenuThemeData,
3157 ) -> ThemeDataCopyWith {
3158 self.theme.dropdown_menu_theme = dropdown_menu_theme;
3159 self
3160 }
3161
3162 pub fn elevated_button_theme(
3164 mut self,
3165 elevated_button_theme: ElevatedButtonThemeData,
3166 ) -> ThemeDataCopyWith {
3167 self.theme.elevated_button_theme = elevated_button_theme;
3168 self
3169 }
3170
3171 pub fn expansion_tile_theme(
3173 mut self,
3174 expansion_tile_theme: ExpansionTileThemeData,
3175 ) -> ThemeDataCopyWith {
3176 self.theme.expansion_tile_theme = expansion_tile_theme;
3177 self
3178 }
3179
3180 pub fn filled_button_theme(
3182 mut self,
3183 filled_button_theme: FilledButtonThemeData,
3184 ) -> ThemeDataCopyWith {
3185 self.theme.filled_button_theme = filled_button_theme;
3186 self
3187 }
3188
3189 pub fn floating_action_button_theme(
3191 mut self,
3192 floating_action_button_theme: FloatingActionButtonThemeData,
3193 ) -> ThemeDataCopyWith {
3194 self.theme.floating_action_button_theme = floating_action_button_theme;
3195 self
3196 }
3197
3198 pub fn icon_button_theme(
3200 mut self,
3201 icon_button_theme: IconButtonThemeData,
3202 ) -> ThemeDataCopyWith {
3203 self.theme.icon_button_theme = icon_button_theme;
3204 self
3205 }
3206
3207 pub fn list_tile_theme(mut self, list_tile_theme: ListTileThemeData) -> ThemeDataCopyWith {
3209 self.theme.list_tile_theme = list_tile_theme;
3210 self
3211 }
3212
3213 pub fn menu_bar_theme(mut self, menu_bar_theme: MenuBarThemeData) -> ThemeDataCopyWith {
3215 self.theme.menu_bar_theme = menu_bar_theme;
3216 self
3217 }
3218
3219 pub fn menu_button_theme(
3221 mut self,
3222 menu_button_theme: MenuButtonThemeData,
3223 ) -> ThemeDataCopyWith {
3224 self.theme.menu_button_theme = menu_button_theme;
3225 self
3226 }
3227
3228 pub fn menu_theme(mut self, menu_theme: MenuThemeData) -> ThemeDataCopyWith {
3230 self.theme.menu_theme = menu_theme;
3231 self
3232 }
3233
3234 pub fn navigation_bar_theme(
3236 mut self,
3237 navigation_bar_theme: NavigationBarThemeData,
3238 ) -> ThemeDataCopyWith {
3239 self.theme.navigation_bar_theme = navigation_bar_theme;
3240 self
3241 }
3242
3243 pub fn navigation_drawer_theme(
3245 mut self,
3246 navigation_drawer_theme: NavigationDrawerThemeData,
3247 ) -> ThemeDataCopyWith {
3248 self.theme.navigation_drawer_theme = navigation_drawer_theme;
3249 self
3250 }
3251
3252 pub fn navigation_rail_theme(
3254 mut self,
3255 navigation_rail_theme: NavigationRailThemeData,
3256 ) -> ThemeDataCopyWith {
3257 self.theme.navigation_rail_theme = navigation_rail_theme;
3258 self
3259 }
3260
3261 pub fn outlined_button_theme(
3263 mut self,
3264 outlined_button_theme: OutlinedButtonThemeData,
3265 ) -> ThemeDataCopyWith {
3266 self.theme.outlined_button_theme = outlined_button_theme;
3267 self
3268 }
3269
3270 pub fn popup_menu_theme(mut self, popup_menu_theme: PopupMenuThemeData) -> ThemeDataCopyWith {
3272 self.theme.popup_menu_theme = popup_menu_theme;
3273 self
3274 }
3275
3276 pub fn progress_indicator_theme(
3278 mut self,
3279 progress_indicator_theme: ProgressIndicatorThemeData,
3280 ) -> ThemeDataCopyWith {
3281 self.theme.progress_indicator_theme = progress_indicator_theme;
3282 self
3283 }
3284
3285 pub fn radio_theme(mut self, radio_theme: RadioThemeData) -> ThemeDataCopyWith {
3287 self.theme.radio_theme = radio_theme;
3288 self
3289 }
3290
3291 pub fn search_bar_theme(mut self, search_bar_theme: SearchBarThemeData) -> ThemeDataCopyWith {
3293 self.theme.search_bar_theme = search_bar_theme;
3294 self
3295 }
3296
3297 pub fn search_view_theme(
3299 mut self,
3300 search_view_theme: SearchViewThemeData,
3301 ) -> ThemeDataCopyWith {
3302 self.theme.search_view_theme = search_view_theme;
3303 self
3304 }
3305
3306 pub fn segmented_button_theme(
3308 mut self,
3309 segmented_button_theme: SegmentedButtonThemeData,
3310 ) -> ThemeDataCopyWith {
3311 self.theme.segmented_button_theme = segmented_button_theme;
3312 self
3313 }
3314
3315 pub fn slider_theme(mut self, slider_theme: SliderThemeData) -> ThemeDataCopyWith {
3317 self.theme.slider_theme = slider_theme;
3318 self
3319 }
3320
3321 pub fn snack_bar_theme(mut self, snack_bar_theme: SnackBarThemeData) -> ThemeDataCopyWith {
3323 self.theme.snack_bar_theme = snack_bar_theme;
3324 self
3325 }
3326
3327 pub fn switch_theme(mut self, switch_theme: SwitchThemeData) -> ThemeDataCopyWith {
3329 self.theme.switch_theme = switch_theme;
3330 self
3331 }
3332
3333 pub fn tab_bar_theme(mut self, tab_bar_theme: TabBarThemeData) -> ThemeDataCopyWith {
3335 self.theme.tab_bar_theme = tab_bar_theme;
3336 self
3337 }
3338
3339 pub fn text_button_theme(
3341 mut self,
3342 text_button_theme: TextButtonThemeData,
3343 ) -> ThemeDataCopyWith {
3344 self.theme.text_button_theme = text_button_theme;
3345 self
3346 }
3347
3348 pub fn text_selection_theme(
3350 mut self,
3351 text_selection_theme: TextSelectionThemeData,
3352 ) -> ThemeDataCopyWith {
3353 self.theme.text_selection_theme = text_selection_theme;
3354 self
3355 }
3356
3357 pub fn time_picker_theme(
3359 mut self,
3360 time_picker_theme: TimePickerThemeData,
3361 ) -> ThemeDataCopyWith {
3362 self.theme.time_picker_theme = time_picker_theme;
3363 self
3364 }
3365
3366 pub fn toggle_buttons_theme(
3368 mut self,
3369 toggle_buttons_theme: ToggleButtonsThemeData,
3370 ) -> ThemeDataCopyWith {
3371 self.theme.toggle_buttons_theme = toggle_buttons_theme;
3372 self
3373 }
3374
3375 pub fn tooltip_theme(mut self, tooltip_theme: TooltipThemeData) -> ThemeDataCopyWith {
3377 self.theme.tooltip_theme = tooltip_theme;
3378 self
3379 }
3380
3381 #[deprecated(
3383 note = "Use a ThemeData constructor (.from, .light, or .dark) instead. These constructors all have a useMaterial3 argument, and they set appropriate default values based on its value. See the useMaterial3 API documentation for full details. This feature was deprecated after v3.13.0-0.2.pre."
3384 )]
3385 pub fn use_material3(mut self, use_material3: bool) -> ThemeDataCopyWith {
3386 self.theme.use_material3 = use_material3;
3387 self
3388 }
3389
3390 #[deprecated(
3392 note = "Use OverflowBar instead. This feature was deprecated after v3.21.0-10.0.pre."
3393 )]
3394 #[allow(deprecated)]
3395 pub fn button_bar_theme(mut self, button_bar_theme: ButtonBarThemeData) -> ThemeDataCopyWith {
3396 self.theme.button_bar_theme = button_bar_theme;
3397 self
3398 }
3399
3400 #[deprecated(
3402 note = "Use DialogThemeData.backgroundColor instead. This feature was deprecated after v3.27.0-0.1.pre."
3403 )]
3404 #[allow(deprecated)]
3405 pub fn dialog_background_color(mut self, dialog_background_color: Color) -> ThemeDataCopyWith {
3406 self.theme.dialog_background_color = dialog_background_color;
3407 self
3408 }
3409
3410 #[deprecated(
3412 note = "Use TabBarThemeData.indicatorColor instead. This feature was deprecated after v3.28.0-1.0.pre."
3413 )]
3414 #[allow(deprecated)]
3415 pub fn indicator_color(mut self, indicator_color: Color) -> ThemeDataCopyWith {
3416 self.theme.indicator_color = indicator_color;
3417 self
3418 }
3419
3420 pub fn build(self) -> ThemeData {
3422 let mut theme = *self.theme;
3423 let mut color_scheme = self.color_scheme.unwrap_or(theme.color_scheme).copy_with();
3424 if let Some(brightness) = self.brightness {
3425 color_scheme = color_scheme.with_brightness(brightness);
3426 }
3427 theme.color_scheme = color_scheme;
3428 theme
3429 }
3430}
3431
3432struct LocalizedThemeDataCache(FifoCache<IdentityThemeDataCacheKey, Rc<ThemeData>>);
3434
3435impl Default for LocalizedThemeDataCache {
3436 fn default() -> LocalizedThemeDataCache {
3437 LocalizedThemeDataCache(FifoCache::new(ThemeData::LOCALIZED_THEME_DATA_CACHE_SIZE))
3438 }
3439}
3440
3441struct IdentityThemeDataCacheKey {
3445 base_theme: Rc<ThemeData>,
3446 local_text_geometry: Rc<TextTheme>,
3447}
3448
3449impl Hash for IdentityThemeDataCacheKey {
3450 fn hash<H: Hasher>(&self, state: &mut H) {
3453 (Rc::as_ptr(&self.base_theme).addr() ^ Rc::as_ptr(&self.local_text_geometry).addr())
3454 .hash(state);
3455 }
3456}
3457
3458impl PartialEq for IdentityThemeDataCacheKey {
3459 fn eq(&self, other: &IdentityThemeDataCacheKey) -> bool {
3460 Rc::ptr_eq(&other.base_theme, &self.base_theme)
3461 && Rc::ptr_eq(&other.local_text_geometry, &self.local_text_geometry)
3462 }
3463}
3464
3465impl Eq for IdentityThemeDataCacheKey {}
3466
3467struct FifoCache<K, V> {
3473 cache: IndexMap<K, V>,
3476
3477 maximum_size: usize,
3482}
3483
3484impl<K: Hash + Eq, V: Clone> FifoCache<K, V> {
3485 fn new(maximum_size: usize) -> FifoCache<K, V> {
3486 debug_assert!(maximum_size > 0);
3487 FifoCache {
3488 cache: IndexMap::new(),
3489 maximum_size,
3490 }
3491 }
3492
3493 fn put_if_absent(&mut self, key: K, loader: impl FnOnce() -> V) -> V {
3496 if let Some(result) = self.cache.get(&key) {
3497 return result.clone();
3498 }
3499 if self.cache.len() == self.maximum_size {
3500 self.cache.shift_remove_index(0);
3501 }
3502 let result = loader();
3503 self.cache.insert(key, result.clone());
3504 result
3505 }
3506}
3507
3508fn default_m3_color_scheme(brightness: Brightness, contrast_level: f64) -> ColorScheme {
3509 match (brightness, contrast_level) {
3510 (Brightness::Light, ContrastLevel::MEDIUM) => color_scheme_light_medium_contrast_m3(),
3511 (Brightness::Dark, ContrastLevel::MEDIUM) => color_scheme_dark_medium_contrast_m3(),
3512 (Brightness::Light, ContrastLevel::HIGH) => color_scheme_light_high_contrast_m3(),
3513 (Brightness::Dark, ContrastLevel::HIGH) => color_scheme_dark_high_contrast_m3(),
3514 (Brightness::Light, _) => color_scheme_light_m3(),
3515 (Brightness::Dark, _) => color_scheme_dark_m3(),
3516 }
3517}
3518
3519fn debug_is_using_predefined_contrast_level(contrast_level: f64) -> bool {
3520 contrast_level == ContrastLevel::STANDARD
3521 || contrast_level == ContrastLevel::MEDIUM
3522 || contrast_level == ContrastLevel::HIGH
3523}
3524
3525#[derive(Debug)]
3567pub struct MaterialBasedCupertinoThemeData {
3568 material_theme: Rc<ThemeData>,
3569}
3570
3571impl MaterialBasedCupertinoThemeData {
3572 #[expect(
3575 clippy::new_ret_no_self,
3576 reason = "Dart's subclass constructor; the value is the base type with the overrides set"
3577 )]
3578 pub fn new(material_theme: &Rc<ThemeData>) -> CupertinoThemeData {
3579 MaterialBasedCupertinoThemeData::raw(
3580 Rc::clone(material_theme),
3581 material_theme
3582 .cupertino_override_theme
3583 .clone()
3584 .unwrap_or_else(|| CupertinoThemeData::new().no_default()),
3585 )
3586 }
3587
3588 fn raw(
3589 material_theme: Rc<ThemeData>,
3590 cupertino_override_theme: NoDefaultCupertinoThemeData,
3591 ) -> CupertinoThemeData {
3592 let selection_handle_color =
3596 cupertino_override_theme
3597 .selection_handle_color
3598 .clone()
3599 .or(material_theme
3600 .text_selection_theme
3601 .selection_handle_color
3602 .map(AnyColor::new));
3603 CupertinoThemeData::raw_with_overrides(
3604 NoDefaultCupertinoThemeData {
3605 selection_handle_color,
3606 ..cupertino_override_theme
3607 },
3608 Rc::new(MaterialBasedCupertinoThemeData { material_theme }),
3609 )
3610 }
3611}
3612
3613impl CupertinoThemeDataOverrides for MaterialBasedCupertinoThemeData {
3614 fn as_any(&self) -> &dyn Any {
3615 self
3616 }
3617
3618 fn brightness(
3619 &self,
3620 cupertino_override_theme: &NoDefaultCupertinoThemeData,
3621 ) -> Option<Brightness> {
3622 Some(
3623 cupertino_override_theme
3624 .brightness
3625 .unwrap_or_else(|| self.material_theme.brightness()),
3626 )
3627 }
3628
3629 fn primary_color(&self, cupertino_override_theme: &NoDefaultCupertinoThemeData) -> AnyColor {
3630 cupertino_override_theme
3631 .primary_color
3632 .clone()
3633 .unwrap_or_else(|| AnyColor::new(self.material_theme.color_scheme.primary()))
3634 }
3635
3636 fn primary_contrasting_color(
3637 &self,
3638 cupertino_override_theme: &NoDefaultCupertinoThemeData,
3639 ) -> AnyColor {
3640 cupertino_override_theme
3641 .primary_contrasting_color
3642 .clone()
3643 .unwrap_or_else(|| AnyColor::new(self.material_theme.color_scheme.on_primary()))
3644 }
3645
3646 fn scaffold_background_color(
3647 &self,
3648 cupertino_override_theme: &NoDefaultCupertinoThemeData,
3649 ) -> AnyColor {
3650 cupertino_override_theme
3651 .scaffold_background_color
3652 .clone()
3653 .unwrap_or_else(|| AnyColor::new(self.material_theme.scaffold_background_color))
3654 }
3655
3656 fn resolve_from(
3657 &self,
3658 this: &CupertinoThemeData,
3659 app: &mut App,
3660 context: BuildContext,
3661 ) -> CupertinoThemeData {
3662 let mut cupertino_override_theme_with_text_theme = this.no_default().copy_with();
3666 cupertino_override_theme_with_text_theme.text_theme = Some(this.text_theme());
3667 MaterialBasedCupertinoThemeData::raw(
3668 Rc::clone(&self.material_theme),
3669 cupertino_override_theme_with_text_theme.resolve_from(app, context),
3670 )
3671 }
3672}
3673
3674#[derive(Clone, Debug)]
3684pub struct CupertinoBasedMaterialThemeData {
3685 pub material_theme: Rc<ThemeData>,
3687}
3688
3689impl CupertinoBasedMaterialThemeData {
3690 pub fn new(app: &App, theme_data: &CupertinoThemeData) -> CupertinoBasedMaterialThemeData {
3693 CupertinoBasedMaterialThemeData {
3694 material_theme: Rc::new(
3695 ThemeData::builder()
3696 .color_scheme(
3697 ColorScheme::from_seed(theme_data.primary_color().color())
3698 .brightness(theme_data.brightness().unwrap_or(Brightness::Light))
3699 .build()
3700 .with_primary(theme_data.primary_color().color())
3701 .with_on_primary(theme_data.primary_contrasting_color().color()),
3702 )
3703 .build(app),
3704 ),
3705 }
3706 }
3707}
3708
3709#[derive(Clone, Copy, Debug, PartialEq)]
3751pub struct VisualDensity {
3752 pub horizontal: f64,
3767 pub vertical: f64,
3782}
3783
3784impl VisualDensity {
3785 pub const fn new(horizontal: f64, vertical: f64) -> VisualDensity {
3791 debug_assert!(vertical <= VisualDensity::MAXIMUM_DENSITY);
3792 debug_assert!(vertical >= VisualDensity::MINIMUM_DENSITY);
3793 debug_assert!(horizontal <= VisualDensity::MAXIMUM_DENSITY);
3794 debug_assert!(horizontal >= VisualDensity::MINIMUM_DENSITY);
3795 VisualDensity {
3796 horizontal,
3797 vertical,
3798 }
3799 }
3800
3801 pub const MINIMUM_DENSITY: f64 = -4.0;
3803
3804 pub const MAXIMUM_DENSITY: f64 = 4.0;
3806
3807 pub const STANDARD: VisualDensity = VisualDensity::new(0.0, 0.0);
3813
3814 pub const COMFORTABLE: VisualDensity = VisualDensity::new(-1.0, -1.0);
3823
3824 pub const COMPACT: VisualDensity = VisualDensity::new(-2.0, -2.0);
3833
3834 pub fn adaptive_platform_density(app: &App) -> VisualDensity {
3846 VisualDensity::default_density_for_platform(app.platform().target_platform())
3847 }
3848
3849 pub fn default_density_for_platform(platform: TargetPlatform) -> VisualDensity {
3860 match platform {
3861 TargetPlatform::Android | TargetPlatform::IOS | TargetPlatform::Fuchsia => {
3862 VisualDensity::STANDARD
3863 }
3864 TargetPlatform::Linux | TargetPlatform::MacOS | TargetPlatform::Windows => {
3865 VisualDensity::COMPACT
3866 }
3867 }
3868 }
3869
3870 pub fn copy_with(self, horizontal: Option<f64>, vertical: Option<f64>) -> VisualDensity {
3873 VisualDensity::new(
3874 horizontal.unwrap_or(self.horizontal),
3875 vertical.unwrap_or(self.vertical),
3876 )
3877 }
3878
3879 pub fn base_size_adjustment(&self) -> Offset {
3887 const INTERVAL: f64 = 4.0;
3891
3892 Offset::new(self.horizontal, self.vertical) * INTERVAL
3893 }
3894
3895 pub fn lerp(a: VisualDensity, b: VisualDensity, t: f64) -> VisualDensity {
3897 VisualDensity::new(
3898 lerp_double(Some(a.horizontal), Some(b.horizontal), t).expect("both sides"),
3899 lerp_double(Some(a.vertical), Some(b.vertical), t).expect("both sides"),
3900 )
3901 }
3902
3903 pub fn effective_constraints(&self, constraints: BoxConstraints) -> BoxConstraints {
3909 debug_assert!(constraints.debug_assert_is_valid(false));
3910 constraints
3911 .copy_with()
3912 .min_width(clamp_double(
3913 constraints.min_width + self.base_size_adjustment().dx(),
3914 0.0,
3915 constraints.max_width,
3916 ))
3917 .min_height(clamp_double(
3918 constraints.min_height + self.base_size_adjustment().dy(),
3919 0.0,
3920 constraints.max_height,
3921 ))
3922 }
3923}
3924
3925impl Default for VisualDensity {
3926 fn default() -> VisualDensity {
3927 VisualDensity::STANDARD
3928 }
3929}
3930
3931impl waft_foundation::Diagnosticable for VisualDensity {
3932 fn to_string_short(&self) -> String {
3933 format!(
3934 "{}(h: {}, v: {})",
3935 waft_foundation::describe_identity("VisualDensity", self.identity_hash()),
3936 waft_foundation::debug_format_double(Some(self.horizontal)),
3937 waft_foundation::debug_format_double(Some(self.vertical)),
3938 )
3939 }
3940
3941 fn debug_fill_properties(&self, properties: &mut waft_foundation::DiagnosticPropertiesBuilder) {
3942 properties.add(
3943 waft_foundation::DoubleProperty::new("horizontal", self.horizontal)
3944 .default_value(Some(0.0)),
3945 );
3946 properties.add(
3947 waft_foundation::DoubleProperty::new("vertical", self.vertical)
3948 .default_value(Some(0.0)),
3949 );
3950 }
3951}
3952
3953impl fmt::Display for VisualDensity {
3954 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
3955 f.write_str(&waft_foundation::Diagnosticable::to_string(self))
3956 }
3957}
3958
3959#[allow(deprecated)]
3960impl Diagnosticable for ThemeData {
3961 #[allow(
3962 clippy::large_unopt_stack_frames,
3963 reason = "Dart builds one ThemeData fallback by value"
3964 )]
3965 fn debug_fill_properties(&self, properties: &mut DiagnosticPropertiesBuilder) {
3966 let default_data = ThemeData::builder()
3969 .brightness(Brightness::Light)
3970 .platform(self.platform)
3971 .build_for_platform(self.platform);
3972
3973 properties.add(
3975 IterableProperty::<AdaptationRef>::new(
3976 "adaptations",
3977 Some(self.adaptation_map.values().cloned().collect()),
3978 )
3979 .default_value(Some(
3980 default_data.adaptation_map.values().cloned().collect(),
3981 ))
3982 .with_level(DiagnosticLevel::Debug),
3983 );
3984 properties.add(
3985 DiagnosticsProperty::<bool>::new(
3986 "applyElevationOverlayColor",
3987 self.apply_elevation_overlay_color,
3988 )
3989 .with_level(DiagnosticLevel::Debug),
3990 );
3991 properties.add(
3992 DiagnosticsProperty::<InstanceOf<NoDefaultCupertinoThemeData>>::new(
3993 "cupertinoOverrideTheme",
3994 self.cupertino_override_theme.clone().map(InstanceOf),
3995 )
3996 .default_value(
3997 default_data
3998 .cupertino_override_theme
3999 .clone()
4000 .map(InstanceOf),
4001 )
4002 .with_level(DiagnosticLevel::Debug),
4003 );
4004 properties.add(
4005 IterableProperty::<String>::new(
4006 "extensions",
4007 Some(
4008 self.extensions
4009 .values()
4010 .map(|extension| format!("{extension:?}"))
4011 .collect(),
4012 ),
4013 )
4014 .default_value(Some(
4015 default_data
4016 .extensions
4017 .values()
4018 .map(|extension| format!("{extension:?}"))
4019 .collect(),
4020 ))
4021 .with_level(DiagnosticLevel::Debug),
4022 );
4023 properties.add(
4024 DiagnosticsProperty::<InputDecorationThemeData>::new(
4025 "inputDecorationTheme",
4026 Some(self.input_decoration_theme.clone()),
4027 )
4028 .with_level(DiagnosticLevel::Debug),
4029 );
4030 properties.add(
4031 DiagnosticsProperty::<DartEnum<MaterialTapTargetSize>>::new(
4032 "materialTapTargetSize",
4033 Some(DartEnum(self.material_tap_target_size)),
4034 )
4035 .with_level(DiagnosticLevel::Debug),
4036 );
4037 properties.add(
4038 DiagnosticsProperty::<PageTransitionsTheme>::new(
4039 "pageTransitionsTheme",
4040 self.page_transitions_theme.clone(),
4041 )
4042 .with_level(DiagnosticLevel::Debug),
4043 );
4044 properties.add(
4045 EnumProperty::<TargetPlatform>::new("platform", Some(self.platform))
4046 .default_value(Some(default_data.platform))
4047 .with_level(DiagnosticLevel::Debug),
4048 );
4049 properties.add(
4050 DiagnosticsProperty::<ScrollbarThemeData>::new(
4051 "scrollbarTheme",
4052 self.scrollbar_theme.clone(),
4053 )
4054 .default_value(Some(default_data.scrollbar_theme.clone()))
4055 .with_level(DiagnosticLevel::Debug),
4056 );
4057 properties.add(
4058 DiagnosticsProperty::<InstanceOf<InteractiveInkFeatureFactoryRef>>::new(
4059 "splashFactory",
4060 Some(InstanceOf(self.splash_factory.clone())),
4061 )
4062 .default_value(Some(InstanceOf(default_data.splash_factory.clone())))
4063 .with_level(DiagnosticLevel::Debug),
4064 );
4065 properties.add(
4066 DiagnosticsProperty::<bool>::new("useMaterial3", self.use_material3)
4067 .default_value(Some(default_data.use_material3))
4068 .with_level(DiagnosticLevel::Debug),
4069 );
4070 properties.add(
4071 DiagnosticsProperty::<VisualDensity>::new("visualDensity", self.visual_density)
4072 .default_value(Some(default_data.visual_density))
4073 .with_level(DiagnosticLevel::Debug),
4074 );
4075
4076 macro_rules! color_property {
4078 ($($name:literal => $field:ident),* $(,)?) => {
4079 $(
4080 properties.add(
4081 waft_painting::ColorProperty::new($name, Some(self.$field))
4082 .default_value(Some(default_data.$field))
4083 .with_level(DiagnosticLevel::Debug),
4084 );
4085 )*
4086 };
4087 }
4088 color_property!(
4089 "canvasColor" => canvas_color,
4090 "cardColor" => card_color,
4091 );
4092 properties.add(
4093 DiagnosticsProperty::<ColorScheme>::new("colorScheme", self.color_scheme.clone())
4094 .default_value(Some(default_data.color_scheme.clone()))
4095 .with_level(DiagnosticLevel::Debug),
4096 );
4097 color_property!(
4098 "disabledColor" => disabled_color,
4099 "dividerColor" => divider_color,
4100 "focusColor" => focus_color,
4101 "highlightColor" => highlight_color,
4102 "hintColor" => hint_color,
4103 "hoverColor" => hover_color,
4104 "primaryColorDark" => primary_color_dark,
4105 "primaryColorLight" => primary_color_light,
4106 "primaryColor" => primary_color,
4107 "scaffoldBackgroundColor" => scaffold_background_color,
4108 "secondaryHeaderColor" => secondary_header_color,
4109 "shadowColor" => shadow_color,
4110 "splashColor" => splash_color,
4111 "unselectedWidgetColor" => unselected_widget_color,
4112 );
4113
4114 properties.add(
4116 DiagnosticsProperty::<IconThemeData>::new("iconTheme", Some(self.icon_theme.clone()))
4117 .with_level(DiagnosticLevel::Debug),
4118 );
4119 properties.add(
4120 DiagnosticsProperty::<IconThemeData>::new(
4121 "primaryIconTheme",
4122 Some(self.primary_icon_theme.clone()),
4123 )
4124 .with_level(DiagnosticLevel::Debug),
4125 );
4126 properties.add(
4127 DiagnosticsProperty::<TextTheme>::new(
4128 "primaryTextTheme",
4129 self.primary_text_theme.clone(),
4130 )
4131 .with_level(DiagnosticLevel::Debug),
4132 );
4133 properties.add(
4134 DiagnosticsProperty::<TextTheme>::new("textTheme", self.text_theme.clone())
4135 .with_level(DiagnosticLevel::Debug),
4136 );
4137 properties.add(
4138 DiagnosticsProperty::<Typography>::new("typography", self.typography.clone())
4139 .default_value(Some(default_data.typography.clone()))
4140 .with_level(DiagnosticLevel::Debug),
4141 );
4142
4143 properties.add(
4145 DiagnosticsProperty::<ActionIconThemeData>::new(
4146 "actionIconTheme",
4147 self.action_icon_theme.clone(),
4148 )
4149 .with_level(DiagnosticLevel::Debug),
4150 );
4151 macro_rules! component_property {
4152 ($($name:literal => $field:ident),* $(,)?) => {
4153 $(
4154 properties.add(
4155 DiagnosticsProperty::<_>::new(
4156 $name,
4157 self.$field.clone(),
4158 )
4159 .default_value(Some(default_data.$field.clone()))
4160 .with_level(DiagnosticLevel::Debug),
4161 );
4162 )*
4163 };
4164 }
4165 component_property!(
4166 "appBarTheme" => app_bar_theme,
4167 "badgeTheme" => badge_theme,
4168 "bannerTheme" => banner_theme,
4169 "bottomNavigationBarTheme" => bottom_navigation_bar_theme,
4170 "bottomSheetTheme" => bottom_sheet_theme,
4171 "cardTheme" => card_theme,
4172 "carouselViewTheme" => carousel_view_theme,
4173 "checkboxTheme" => checkbox_theme,
4174 "dataTableTheme" => data_table_theme,
4175 "datePickerTheme" => date_picker_theme,
4176 "dialogTheme" => dialog_theme,
4177 "dividerTheme" => divider_theme,
4178 "drawerTheme" => drawer_theme,
4179 "dropdownMenuTheme" => dropdown_menu_theme,
4180 "elevatedButtonTheme" => elevated_button_theme,
4181 "filledButtonTheme" => filled_button_theme,
4182 "floatingActionButtonTheme" => floating_action_button_theme,
4183 "iconButtonTheme" => icon_button_theme,
4184 "listTileTheme" => list_tile_theme,
4185 "menuBarTheme" => menu_bar_theme,
4186 "menuButtonTheme" => menu_button_theme,
4187 "menuTheme" => menu_theme,
4188 "navigationBarTheme" => navigation_bar_theme,
4189 "navigationDrawerTheme" => navigation_drawer_theme,
4190 "navigationRailTheme" => navigation_rail_theme,
4191 "outlinedButtonTheme" => outlined_button_theme,
4192 "popupMenuTheme" => popup_menu_theme,
4193 "progressIndicatorTheme" => progress_indicator_theme,
4194 "radioTheme" => radio_theme,
4195 "searchBarTheme" => search_bar_theme,
4196 "searchViewTheme" => search_view_theme,
4197 "segmentedButtonTheme" => segmented_button_theme,
4198 "snackBarTheme" => snack_bar_theme,
4199 "switchTheme" => switch_theme,
4200 "textButtonTheme" => text_button_theme,
4201 "textSelectionTheme" => text_selection_theme,
4202 "timePickerTheme" => time_picker_theme,
4203 );
4204 macro_rules! component_property_without_default {
4205 ($($name:literal => $field:ident),* $(,)?) => {
4206 $(
4207 properties.add(
4208 DiagnosticsProperty::<_>::new(
4209 $name,
4210 self.$field.clone(),
4211 )
4212 .with_level(DiagnosticLevel::Debug),
4213 );
4214 )*
4215 };
4216 }
4217 component_property_without_default!(
4218 "buttonTheme" => button_theme,
4219 "chipTheme" => chip_theme,
4220 "expansionTileTheme" => expansion_tile_theme,
4221 "sliderTheme" => slider_theme,
4222 "tabBarTheme" => tab_bar_theme,
4223 "toggleButtonsTheme" => toggle_buttons_theme,
4224 "tooltipTheme" => tooltip_theme,
4225 );
4226
4227 properties.add(
4229 DiagnosticsProperty::<ButtonBarThemeData>::new(
4230 "buttonBarTheme",
4231 self.button_bar_theme.clone(),
4232 )
4233 .default_value(Some(default_data.button_bar_theme.clone()))
4234 .with_level(DiagnosticLevel::Debug),
4235 );
4236 color_property!(
4237 "dialogBackgroundColor" => dialog_background_color,
4238 "indicatorColor" => indicator_color,
4239 );
4240 }
4241}
4242
4243impl fmt::Display for ThemeData {
4244 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
4245 f.write_str(&Diagnosticable::to_string(self))
4246 }
4247}
4248
4249#[cfg(test)]
4250#[allow(deprecated)]
4251mod tests {
4252 use std::cell::Ref;
4253
4254 use waft_painting::TextStyle;
4255
4256 use waft_widgets::MainAxisAlignment;
4257 use waft_widgets::testing::{WidgetTester, test_widgets};
4258
4259 use super::*;
4260 use crate::bottom_navigation_bar::BottomNavigationBarType;
4261 use crate::elevated_button::ElevatedButton;
4262 use crate::filled_button::FilledButton;
4263 use crate::icon_button::IconButton;
4264 use crate::input_border::{InputBorderRef, OutlineInputBorder};
4265 use crate::material_state::MaterialStatePropertyAll;
4266 use crate::menu_anchor::MenuItemButton;
4267 use crate::menu_style::MenuStyle;
4268 use crate::outlined_button::OutlinedButton;
4269 use crate::text_button::TextButton;
4270 use waft_cupertino::CupertinoPageTransitionsBuilder;
4271 use waft_widgets::PageTransitionsBuilderRef;
4272
4273 const ALL_PLATFORMS: [TargetPlatform; 6] = [
4274 TargetPlatform::Android,
4275 TargetPlatform::Fuchsia,
4276 TargetPlatform::IOS,
4277 TargetPlatform::Linux,
4278 TargetPlatform::MacOS,
4279 TargetPlatform::Windows,
4280 ];
4281
4282 fn on<T>(platform: TargetPlatform, body: impl FnOnce(&App) -> T + Send) {
4284 test_widgets(|_tester| {
4285 let tester = WidgetTester::on(platform);
4286 let app: Ref<'_, App> = tester.cell.borrow();
4287 body(&app);
4288 });
4289 }
4290
4291 fn android<T>(body: impl FnOnce(&App) -> T + Send) {
4293 test_widgets(|tester| {
4294 let app: Ref<'_, App> = tester.cell.borrow();
4295 body(&app);
4296 });
4297 }
4298
4299 fn title_large_color(theme: &TextTheme) -> Option<AnyColor> {
4300 theme
4301 .title_large
4302 .as_ref()
4303 .expect("a title style")
4304 .color
4305 .clone()
4306 }
4307
4308 #[derive(Debug)]
4309 struct MyThemeExtensionA {
4310 color1: Option<Color>,
4311 color2: Option<Color>,
4312 }
4313
4314 impl ThemeExtension for MyThemeExtensionA {
4315 fn as_any(&self) -> &dyn Any {
4316 self
4317 }
4318
4319 fn copy_with(&self) -> ThemeExtensionRef {
4320 Rc::new(MyThemeExtensionA {
4321 color1: self.color1,
4322 color2: self.color2,
4323 })
4324 }
4325
4326 fn lerp(self: Rc<Self>, other: Option<&dyn ThemeExtension>, t: f64) -> ThemeExtensionRef {
4327 let Some(other) = other.and_then(|other| other.as_any().downcast_ref::<Self>()) else {
4328 return self;
4329 };
4330 Rc::new(MyThemeExtensionA {
4331 color1: Color::lerp(self.color1, other.color1, t),
4332 color2: Color::lerp(self.color2, other.color2, t),
4333 })
4334 }
4335 }
4336
4337 #[derive(Debug)]
4338 struct MyThemeExtensionB {
4339 text_style: Option<TextStyle>,
4340 }
4341
4342 impl ThemeExtension for MyThemeExtensionB {
4343 fn as_any(&self) -> &dyn Any {
4344 self
4345 }
4346
4347 fn copy_with(&self) -> ThemeExtensionRef {
4348 Rc::new(MyThemeExtensionB {
4349 text_style: self.text_style.clone(),
4350 })
4351 }
4352
4353 fn lerp(self: Rc<Self>, other: Option<&dyn ThemeExtension>, t: f64) -> ThemeExtensionRef {
4354 let Some(other) = other.and_then(|other| other.as_any().downcast_ref::<Self>()) else {
4355 return self;
4356 };
4357 Rc::new(MyThemeExtensionB {
4358 text_style: TextStyle::lerp(self.text_style.as_ref(), other.text_style.as_ref(), t),
4359 })
4360 }
4361 }
4362
4363 #[derive(Debug)]
4364 struct StringAdaptation;
4365
4366 impl Adaptation<String> for StringAdaptation {
4367 fn adapt(&self, _theme: &ThemeData, _default_value: String) -> String {
4368 "Adaptive theme.".to_string()
4369 }
4370 }
4371
4372 #[test]
4373 fn theme_data_control_test() {
4374 android(|app| {
4375 let dark = ThemeData::dark(app, None);
4376 assert_eq!(dark, dark.copy_with().build());
4377
4378 let light = ThemeData::new(app);
4379 let dawn = ThemeData::lerp(&dark, &light, 0.25);
4380
4381 assert_eq!(dawn.brightness(), Brightness::Dark);
4382 assert_eq!(
4383 dawn.primary_color,
4384 Color::lerp(Some(dark.primary_color), Some(light.primary_color), 0.25)
4385 .expect("both sides")
4386 );
4387 });
4388 }
4389
4390 #[test]
4391 fn theme_data_objects_with_style_from_members_are_equal() {
4392 android(|app| {
4393 let create_theme_data = || {
4394 ThemeData::builder()
4395 .elevated_button_theme(
4396 ElevatedButtonThemeData::new().style(
4397 ElevatedButton::style_from()
4398 .foreground_color(Colors::BLACK)
4399 .background_color(Colors::BLACK)
4400 .elevation(1.0)
4401 .build(),
4402 ),
4403 )
4404 .filled_button_theme(
4405 FilledButtonThemeData::new().style(
4406 FilledButton::style_from()
4407 .foreground_color(Colors::BLACK)
4408 .disabled_foreground_color(Colors::BLACK)
4409 .background_color(Colors::BLACK)
4410 .disabled_background_color(Colors::BLACK)
4411 .overlay_color(Colors::BLACK)
4412 .build(),
4413 ),
4414 )
4415 .icon_button_theme(
4416 IconButtonThemeData::new().style(
4417 IconButton::style_from()
4418 .hover_color(Colors::BLACK)
4419 .focus_color(Colors::BLACK)
4420 .highlight_color(Colors::BLACK)
4421 .build(),
4422 ),
4423 )
4424 .text_button_theme(
4425 TextButtonThemeData::new().style(
4426 TextButton::style_from()
4427 .enabled_mouse_cursor(<dyn waft_services::MouseCursor>::defer())
4428 .disabled_mouse_cursor(
4429 <dyn waft_services::MouseCursor>::uncontrolled(),
4430 )
4431 .build(),
4432 ),
4433 )
4434 .build(app)
4435 };
4436
4437 assert!(create_theme_data() == create_theme_data());
4438 });
4439 }
4440
4441 #[test]
4442 fn defaults_to_the_default_typography_for_the_platform() {
4443 android(|app| {
4444 for platform in ALL_PLATFORMS {
4445 let theme = ThemeData::builder()
4446 .platform(platform)
4447 .use_material3(false)
4448 .build(app);
4449 let typography = Typography::material2018().platform(Some(platform)).build();
4450 assert_eq!(
4451 theme.text_theme,
4452 typography
4453 .black
4454 .apply()
4455 .decoration(waft_embedder::TextDecoration::NONE)
4456 .into_text_theme(),
4457 "Not using default typography for {platform:?}"
4458 );
4459 }
4460 });
4461 }
4462
4463 #[test]
4464 fn default_text_and_icon_themes_contrast_with_brightness() {
4465 android(|app| {
4466 let light_theme = ThemeData::builder()
4467 .brightness(Brightness::Light)
4468 .use_material3(false)
4469 .build(app);
4470 let dark_theme = ThemeData::builder()
4471 .brightness(Brightness::Dark)
4472 .use_material3(false)
4473 .build(app);
4474 let typography = Typography::material2018()
4475 .platform(Some(light_theme.platform))
4476 .build();
4477
4478 assert_eq!(
4479 title_large_color(&light_theme.text_theme),
4480 title_large_color(&typography.black)
4481 );
4482 assert_eq!(
4483 title_large_color(&dark_theme.text_theme),
4484 title_large_color(&typography.white)
4485 );
4486 });
4487 }
4488
4489 #[test]
4490 fn default_primary_text_and_icon_themes_contrast_with_primary_brightness() {
4491 android(|app| {
4492 let light_theme = ThemeData::builder()
4493 .primary_color(Colors::WHITE)
4494 .use_material3(false)
4495 .build(app);
4496 let dark_theme = ThemeData::builder()
4497 .primary_color(Colors::BLACK)
4498 .use_material3(false)
4499 .build(app);
4500 let typography = Typography::material2018()
4501 .platform(Some(light_theme.platform))
4502 .build();
4503
4504 assert_eq!(
4505 title_large_color(&light_theme.primary_text_theme),
4506 title_large_color(&typography.black)
4507 );
4508 assert_eq!(
4509 title_large_color(&dark_theme.primary_text_theme),
4510 title_large_color(&typography.white)
4511 );
4512 });
4513 }
4514
4515 #[test]
4516 fn light_dark_and_fallback_constructors_support_use_material3() {
4517 android(|app| {
4518 let light_theme = ThemeData::new(app);
4519 assert!(light_theme.use_material3);
4520 assert_eq!(
4521 light_theme.typography,
4522 Typography::material2021()
4523 .color_scheme(light_theme.color_scheme.clone())
4524 .build()
4525 );
4526
4527 let dark_theme = ThemeData::dark(app, None);
4528 assert!(dark_theme.use_material3);
4529 assert_eq!(
4530 dark_theme.typography,
4531 Typography::material2021()
4532 .color_scheme(dark_theme.color_scheme.clone())
4533 .build()
4534 );
4535
4536 let fallback_theme = ThemeData::fallback(app, None);
4537 assert!(fallback_theme.use_material3);
4538 assert_eq!(
4539 fallback_theme.typography,
4540 Typography::material2021()
4541 .color_scheme(fallback_theme.color_scheme.clone())
4542 .build()
4543 );
4544 });
4545 }
4546
4547 #[test]
4548 fn theme_data_supports_material_3_contrast_levels() {
4549 android(|app| {
4550 let theme = |brightness, contrast_level| {
4551 ThemeData::builder()
4552 .brightness(brightness)
4553 .contrast_level(contrast_level)
4554 .build(app)
4555 .color_scheme
4556 };
4557 let standard = ThemeData::new(app).color_scheme;
4558 let medium = theme(Brightness::Light, ContrastLevel::MEDIUM);
4559 let high = theme(Brightness::Light, ContrastLevel::HIGH);
4560 let dark_medium = theme(Brightness::Dark, ContrastLevel::MEDIUM);
4561 let dark_high = theme(Brightness::Dark, ContrastLevel::HIGH);
4562
4563 assert_ne!(standard.primary(), medium.primary());
4564 assert_eq!(medium.primary(), Color::new(0xFF4F378B));
4565 assert_eq!(medium.on_surface(), Color::new(0xFF000000));
4566 assert_eq!(high.primary(), Color::new(0xFF381E72));
4567 assert_eq!(high.on_surface_variant(), Color::new(0xFF000000));
4568 assert_eq!(dark_medium.primary(), Color::new(0xFFEADDFF));
4569 assert_eq!(dark_medium.on_surface(), Color::new(0xFFFFFFFF));
4570 assert_eq!(dark_high.primary(), Color::new(0xFFF6EDFF));
4571 assert_eq!(dark_high.on_surface_variant(), Color::new(0xFFFFFFFF));
4572 });
4573 }
4574
4575 #[test]
4576 fn theme_data_supports_custom_material_3_contrast_levels_from_seed_colors() {
4577 android(|app| {
4578 let contrast_level = 0.25;
4579 let theme = ThemeData::builder()
4580 .color_scheme_seed(Colors::BLUE.primary())
4581 .contrast_level(contrast_level)
4582 .build(app);
4583
4584 assert_eq!(
4585 theme.color_scheme,
4586 ColorScheme::from_seed(Colors::BLUE.primary())
4587 .contrast_level(contrast_level)
4588 .build()
4589 );
4590 });
4591 }
4592
4593 #[test]
4594 #[should_panic(expected = "colorSchemeSeed must be provided to use a custom contrastLevel")]
4595 fn theme_data_requires_a_seed_color_for_custom_contrast_levels() {
4596 android(|app| ThemeData::builder().contrast_level(0.25).build(app));
4597 }
4598
4599 #[test]
4600 #[should_panic(expected = "colorSchemeSeed must be provided to use a custom contrastLevel")]
4601 fn theme_data_requires_a_seed_color_for_custom_contrast_levels_with_a_color_scheme() {
4602 android(|app| {
4603 ThemeData::builder()
4604 .color_scheme(ColorScheme::light())
4605 .contrast_level(0.25)
4606 .build(app)
4607 });
4608 }
4609
4610 #[test]
4611 #[should_panic(expected = "contrastLevel must be between -1.0 and 1.0 inclusive.")]
4612 fn theme_data_throws_for_contrast_levels_below_the_valid_range() {
4613 android(|app| ThemeData::builder().contrast_level(-1.5).build(app));
4614 }
4615
4616 #[test]
4617 #[should_panic(expected = "contrastLevel must be between -1.0 and 1.0 inclusive.")]
4618 fn theme_data_throws_for_contrast_levels_above_the_valid_range() {
4619 android(|app| ThemeData::builder().contrast_level(1.5).build(app));
4620 }
4621
4622 #[test]
4623 fn theme_data_allows_precision_errors_at_contrast_level_limits() {
4624 android(|app| {
4625 let precision_error = PRECISION_ERROR_TOLERANCE / 2.0;
4626 for contrast_level in [-1.0 - precision_error, 1.0 + precision_error] {
4627 ThemeData::builder()
4628 .color_scheme_seed(Colors::BLUE.primary())
4629 .contrast_level(contrast_level)
4630 .build(app);
4631 }
4632 });
4633 }
4634
4635 #[test]
4636 fn defaults_to_padded_tap_targets_on_mobile_platforms_and_shrink_wrap_on_desktop() {
4637 for platform in ALL_PLATFORMS {
4638 on(platform, |app| {
4639 let theme_data = ThemeData::builder()
4640 .platform(app.platform().target_platform())
4641 .build(app);
4642 let expected = match platform {
4643 TargetPlatform::Android | TargetPlatform::Fuchsia | TargetPlatform::IOS => {
4644 MaterialTapTargetSize::Padded
4645 }
4646 TargetPlatform::Linux | TargetPlatform::MacOS | TargetPlatform::Windows => {
4647 MaterialTapTargetSize::ShrinkWrap
4648 }
4649 };
4650 assert_eq!(
4651 theme_data.material_tap_target_size, expected,
4652 "{platform:?}"
4653 );
4654 });
4655 }
4656 }
4657
4658 #[test]
4659 fn can_control_font_family_default() {
4660 android(|app| {
4661 let theme_data = ThemeData::builder()
4662 .font_family("FlutterTest")
4663 .text_theme(TextTheme::new().title_large(TextStyle::new().font_family("Roboto")))
4664 .build(app);
4665
4666 let family = |style: &Option<TextStyle>| {
4667 style.as_ref().and_then(|style| style.font_family.clone())
4668 };
4669 assert_eq!(
4670 family(&theme_data.text_theme.body_large).as_deref(),
4671 Some("FlutterTest")
4672 );
4673 assert_eq!(
4674 family(&theme_data.primary_text_theme.display_small).as_deref(),
4675 Some("FlutterTest")
4676 );
4677
4678 assert_eq!(
4680 family(&theme_data.text_theme.title_large).as_deref(),
4681 Some("Roboto")
4682 );
4683 });
4684 }
4685
4686 #[test]
4687 fn can_estimate_brightness_directly() {
4688 let estimate = ThemeData::estimate_brightness_for_color;
4689 assert_eq!(estimate(Colors::WHITE), Brightness::Light);
4690 assert_eq!(estimate(Colors::BLACK), Brightness::Dark);
4691 assert_eq!(estimate(Colors::BLUE.primary()), Brightness::Dark);
4692 assert_eq!(estimate(Colors::YELLOW.primary()), Brightness::Light);
4693 assert_eq!(estimate(Colors::DEEP_ORANGE.primary()), Brightness::Dark);
4694 assert_eq!(estimate(Colors::ORANGE.primary()), Brightness::Light);
4695 assert_eq!(estimate(Colors::LIME.primary()), Brightness::Light);
4696 assert_eq!(estimate(Colors::GREY.primary()), Brightness::Light);
4697 assert_eq!(estimate(Colors::TEAL.primary()), Brightness::Dark);
4698 assert_eq!(estimate(Colors::INDIGO.primary()), Brightness::Dark);
4699 }
4700
4701 #[test]
4702 #[should_panic]
4703 fn a_color_scheme_seed_rules_out_a_color_scheme() {
4704 android(|app| {
4705 ThemeData::builder()
4706 .color_scheme_seed(Colors::BLUE.primary())
4707 .color_scheme(ColorScheme::light())
4708 .build(app)
4709 });
4710 }
4711
4712 #[test]
4713 #[should_panic]
4714 fn a_color_scheme_seed_rules_out_a_primary_color() {
4715 android(|app| {
4716 ThemeData::builder()
4717 .color_scheme_seed(Colors::BLUE.primary())
4718 .primary_color(Colors::GREEN.primary())
4719 .build(app)
4720 });
4721 }
4722
4723 #[test]
4724 #[should_panic]
4725 fn a_color_scheme_seed_rules_out_a_primary_swatch() {
4726 android(|app| {
4727 ThemeData::builder()
4728 .color_scheme_seed(Colors::BLUE.primary())
4729 .primary_swatch(Colors::GREEN)
4730 .build(app)
4731 });
4732 }
4733
4734 #[test]
4735 fn theme_data_can_generate_a_light_color_scheme_from_color_scheme_seed() {
4736 android(|app| {
4737 let theme = ThemeData::builder()
4738 .color_scheme_seed(Colors::BLUE.primary())
4739 .build(app);
4740 let scheme = &theme.color_scheme;
4741 let expected = [
4742 (scheme.primary(), Color::new(0xFF36618E)),
4743 (scheme.on_primary(), Color::new(0xFFFFFFFF)),
4744 (scheme.primary_container(), Color::new(0xFFD1E4FF)),
4745 (scheme.on_primary_container(), Color::new(0xFF194975)),
4746 (scheme.primary_fixed(), Color::new(0xFFD1E4FF)),
4747 (scheme.primary_fixed_dim(), Color::new(0xFFA0CAFD)),
4748 (scheme.on_primary_fixed(), Color::new(0xFF001D36)),
4749 (scheme.on_primary_fixed_variant(), Color::new(0xFF194975)),
4750 (scheme.secondary(), Color::new(0xFF535F70)),
4751 (scheme.on_secondary(), Color::new(0xFFFFFFFF)),
4752 (scheme.secondary_container(), Color::new(0xFFD7E3F7)),
4753 (scheme.on_secondary_container(), Color::new(0xFF3B4858)),
4754 (scheme.secondary_fixed(), Color::new(0xFFD7E3F7)),
4755 (scheme.secondary_fixed_dim(), Color::new(0xFFBBC7DB)),
4756 (scheme.on_secondary_fixed(), Color::new(0xFF101C2B)),
4757 (scheme.on_secondary_fixed_variant(), Color::new(0xFF3B4858)),
4758 (scheme.tertiary(), Color::new(0xFF6B5778)),
4759 (scheme.on_tertiary(), Color::new(0xFFFFFFFF)),
4760 (scheme.tertiary_container(), Color::new(0xFFF2DAFF)),
4761 (scheme.on_tertiary_container(), Color::new(0xFF523F5F)),
4762 (scheme.tertiary_fixed(), Color::new(0xFFF2DAFF)),
4763 (scheme.tertiary_fixed_dim(), Color::new(0xFFD6BEE4)),
4764 (scheme.on_tertiary_fixed(), Color::new(0xFF251431)),
4765 (scheme.on_tertiary_fixed_variant(), Color::new(0xFF523F5F)),
4766 (scheme.error(), Color::new(0xFFBA1A1A)),
4767 (scheme.on_error(), Color::new(0xFFFFFFFF)),
4768 (scheme.error_container(), Color::new(0xFFFFDAD6)),
4769 (scheme.on_error_container(), Color::new(0xFF93000A)),
4770 (scheme.outline(), Color::new(0xFF73777F)),
4771 (scheme.outline_variant(), Color::new(0xFFC3C7CF)),
4772 (scheme.background(), Color::new(0xFFF8F9FF)),
4773 (scheme.on_background(), Color::new(0xFF191C20)),
4774 (scheme.surface(), Color::new(0xFFF8F9FF)),
4775 (scheme.surface_bright(), Color::new(0xFFF8F9FF)),
4776 (scheme.surface_dim(), Color::new(0xFFD8DAE0)),
4777 (scheme.surface_container_lowest(), Color::new(0xFFFFFFFF)),
4778 (scheme.surface_container_low(), Color::new(0xFFF2F3FA)),
4779 (scheme.surface_container(), Color::new(0xFFECEEF4)),
4780 (scheme.surface_container_high(), Color::new(0xFFE6E8EE)),
4781 (scheme.surface_container_highest(), Color::new(0xFFE1E2E8)),
4782 (scheme.on_surface(), Color::new(0xFF191C20)),
4783 (scheme.surface_variant(), Color::new(0xFFDFE2EB)),
4784 (scheme.on_surface_variant(), Color::new(0xFF43474E)),
4785 (scheme.inverse_surface(), Color::new(0xFF2E3135)),
4786 (scheme.on_inverse_surface(), Color::new(0xFFEFF0F7)),
4787 (scheme.inverse_primary(), Color::new(0xFFA0CAFD)),
4788 (scheme.shadow(), Color::new(0xFF000000)),
4789 (scheme.scrim(), Color::new(0xFF000000)),
4790 (scheme.surface_tint(), Color::new(0xFF36618E)),
4791 ];
4792 for (index, (actual, expected)) in expected.into_iter().enumerate() {
4793 assert_eq!(actual, expected, "colour {index}");
4794 }
4795 assert_eq!(scheme.brightness(), Brightness::Light);
4796
4797 assert_eq!(theme.primary_color, scheme.primary());
4798 assert_eq!(theme.canvas_color, scheme.surface());
4799 assert_eq!(theme.scaffold_background_color, scheme.surface());
4800 assert_eq!(theme.card_color, scheme.surface());
4801 assert_eq!(theme.divider_color, scheme.outline());
4802 assert_eq!(theme.dialog_background_color, scheme.surface());
4803 assert_eq!(theme.indicator_color, scheme.on_primary());
4804 assert!(!theme.apply_elevation_overlay_color);
4805 });
4806 }
4807
4808 #[test]
4809 fn theme_data_can_generate_a_dark_color_scheme_from_color_scheme_seed() {
4810 android(|app| {
4811 let theme = ThemeData::builder()
4812 .color_scheme_seed(Colors::BLUE.primary())
4813 .brightness(Brightness::Dark)
4814 .build(app);
4815 let scheme = &theme.color_scheme;
4816 let expected = [
4817 (scheme.primary(), Color::new(0xFFA0CAFD)),
4818 (scheme.on_primary(), Color::new(0xFF003258)),
4819 (scheme.primary_container(), Color::new(0xFF194975)),
4820 (scheme.on_primary_container(), Color::new(0xFFD1E4FF)),
4821 (scheme.primary_fixed(), Color::new(0xFFD1E4FF)),
4822 (scheme.primary_fixed_dim(), Color::new(0xFFA0CAFD)),
4823 (scheme.on_primary_fixed(), Color::new(0xFF001D36)),
4824 (scheme.on_primary_fixed_variant(), Color::new(0xFF194975)),
4825 (scheme.secondary(), Color::new(0xFFBBC7DB)),
4826 (scheme.on_secondary(), Color::new(0xFF253140)),
4827 (scheme.secondary_container(), Color::new(0xFF3B4858)),
4828 (scheme.on_secondary_container(), Color::new(0xFFD7E3F7)),
4829 (scheme.secondary_fixed(), Color::new(0xFFD7E3F7)),
4830 (scheme.secondary_fixed_dim(), Color::new(0xFFBBC7DB)),
4831 (scheme.on_secondary_fixed(), Color::new(0xFF101C2B)),
4832 (scheme.on_secondary_fixed_variant(), Color::new(0xFF3B4858)),
4833 (scheme.tertiary(), Color::new(0xFFD6BEE4)),
4834 (scheme.on_tertiary(), Color::new(0xFF3B2948)),
4835 (scheme.tertiary_container(), Color::new(0xFF523F5F)),
4836 (scheme.on_tertiary_container(), Color::new(0xFFF2DAFF)),
4837 (scheme.tertiary_fixed(), Color::new(0xFFF2DAFF)),
4838 (scheme.tertiary_fixed_dim(), Color::new(0xFFD6BEE4)),
4839 (scheme.on_tertiary_fixed(), Color::new(0xFF251431)),
4840 (scheme.on_tertiary_fixed_variant(), Color::new(0xFF523F5F)),
4841 (scheme.error(), Color::new(0xFFFFB4AB)),
4842 (scheme.on_error(), Color::new(0xFF690005)),
4843 (scheme.error_container(), Color::new(0xFF93000A)),
4844 (scheme.on_error_container(), Color::new(0xFFFFDAD6)),
4845 (scheme.outline(), Color::new(0xFF8D9199)),
4846 (scheme.outline_variant(), Color::new(0xFF43474E)),
4847 (scheme.background(), Color::new(0xFF111418)),
4848 (scheme.on_background(), Color::new(0xFFE1E2E8)),
4849 (scheme.surface(), Color::new(0xFF111418)),
4850 (scheme.surface_dim(), Color::new(0xFF111418)),
4851 (scheme.surface_bright(), Color::new(0xFF36393E)),
4852 (scheme.surface_container_lowest(), Color::new(0xFF0B0E13)),
4853 (scheme.surface_container_low(), Color::new(0xFF191C20)),
4854 (scheme.surface_container(), Color::new(0xFF1D2024)),
4855 (scheme.surface_container_high(), Color::new(0xFF272A2F)),
4856 (scheme.surface_container_highest(), Color::new(0xFF32353A)),
4857 (scheme.on_surface(), Color::new(0xFFE1E2E8)),
4858 (scheme.surface_variant(), Color::new(0xFF43474E)),
4859 (scheme.on_surface_variant(), Color::new(0xFFC3C7CF)),
4860 (scheme.inverse_surface(), Color::new(0xFFE1E2E8)),
4861 (scheme.on_inverse_surface(), Color::new(0xFF2E3135)),
4862 (scheme.inverse_primary(), Color::new(0xFF36618E)),
4863 (scheme.shadow(), Color::new(0xFF000000)),
4864 (scheme.scrim(), Color::new(0xFF000000)),
4865 (scheme.surface_tint(), Color::new(0xFFA0CAFD)),
4866 ];
4867 for (index, (actual, expected)) in expected.into_iter().enumerate() {
4868 assert_eq!(actual, expected, "colour {index}");
4869 }
4870 assert_eq!(scheme.brightness(), Brightness::Dark);
4871
4872 assert_eq!(theme.primary_color, scheme.surface());
4873 assert_eq!(theme.canvas_color, scheme.surface());
4874 assert_eq!(theme.scaffold_background_color, scheme.surface());
4875 assert_eq!(theme.card_color, scheme.surface());
4876 assert_eq!(theme.divider_color, scheme.outline());
4877 assert_eq!(theme.dialog_background_color, scheme.surface());
4878 assert_eq!(theme.indicator_color, scheme.on_surface());
4879 assert!(theme.apply_elevation_overlay_color);
4880 });
4881 }
4882
4883 #[test]
4884 fn theme_data_can_generate_a_default_m3_light_color_scheme_when_use_material3_is_true() {
4885 android(|app| {
4886 let constructors: [fn(&App) -> ThemeData; 2] =
4887 [ThemeData::new, |app| ThemeData::light(app, None)];
4888 for constructor in constructors {
4889 let theme = Box::new(constructor(app));
4890 let scheme = &theme.color_scheme;
4891 let expected = [
4892 (scheme.primary(), Color::new(0xFF6750A4)),
4893 (scheme.on_primary(), Color::new(0xFFFFFFFF)),
4894 (scheme.primary_container(), Color::new(0xFFEADDFF)),
4895 (scheme.on_primary_container(), Color::new(0xFF4F378B)),
4896 (scheme.primary_fixed(), Color::new(0xFFEADDFF)),
4897 (scheme.primary_fixed_dim(), Color::new(0xFFD0BCFF)),
4898 (scheme.on_primary_fixed(), Color::new(0xFF21005D)),
4899 (scheme.on_primary_fixed_variant(), Color::new(0xFF4F378B)),
4900 (scheme.secondary(), Color::new(0xFF625B71)),
4901 (scheme.on_secondary(), Color::new(0xFFFFFFFF)),
4902 (scheme.secondary_container(), Color::new(0xFFE8DEF8)),
4903 (scheme.on_secondary_container(), Color::new(0xFF4A4458)),
4904 (scheme.secondary_fixed(), Color::new(0xFFE8DEF8)),
4905 (scheme.secondary_fixed_dim(), Color::new(0xFFCCC2DC)),
4906 (scheme.on_secondary_fixed(), Color::new(0xFF1D192B)),
4907 (scheme.on_secondary_fixed_variant(), Color::new(0xFF4A4458)),
4908 (scheme.tertiary(), Color::new(0xFF7D5260)),
4909 (scheme.on_tertiary(), Color::new(0xFFFFFFFF)),
4910 (scheme.tertiary_container(), Color::new(0xFFFFD8E4)),
4911 (scheme.on_tertiary_container(), Color::new(0xFF633B48)),
4912 (scheme.tertiary_fixed(), Color::new(0xFFFFD8E4)),
4913 (scheme.tertiary_fixed_dim(), Color::new(0xFFEFB8C8)),
4914 (scheme.on_tertiary_fixed(), Color::new(0xFF31111D)),
4915 (scheme.on_tertiary_fixed_variant(), Color::new(0xFF633B48)),
4916 (scheme.error(), Color::new(0xFFB3261E)),
4917 (scheme.on_error(), Color::new(0xFFFFFFFF)),
4918 (scheme.error_container(), Color::new(0xFFF9DEDC)),
4919 (scheme.on_error_container(), Color::new(0xFF8C1D18)),
4920 (scheme.outline(), Color::new(0xFF79747E)),
4921 (scheme.background(), Color::new(0xFFFEF7FF)),
4922 (scheme.on_background(), Color::new(0xFF1D1B20)),
4923 (scheme.surface(), Color::new(0xFFFEF7FF)),
4924 (scheme.on_surface(), Color::new(0xFF1D1B20)),
4925 (scheme.surface_variant(), Color::new(0xFFE7E0EC)),
4926 (scheme.on_surface_variant(), Color::new(0xFF49454F)),
4927 (scheme.surface_bright(), Color::new(0xFFFEF7FF)),
4928 (scheme.surface_dim(), Color::new(0xFFDED8E1)),
4929 (scheme.surface_container(), Color::new(0xFFF3EDF7)),
4930 (scheme.surface_container_highest(), Color::new(0xFFE6E0E9)),
4931 (scheme.surface_container_high(), Color::new(0xFFECE6F0)),
4932 (scheme.surface_container_lowest(), Color::new(0xFFFFFFFF)),
4933 (scheme.surface_container_low(), Color::new(0xFFF7F2FA)),
4934 (scheme.inverse_surface(), Color::new(0xFF322F35)),
4935 (scheme.on_inverse_surface(), Color::new(0xFFF5EFF7)),
4936 (scheme.inverse_primary(), Color::new(0xFFD0BCFF)),
4937 (scheme.shadow(), Color::new(0xFF000000)),
4938 ];
4939 for (index, (actual, expected)) in expected.into_iter().enumerate() {
4940 assert_eq!(actual, expected, "colour {index}");
4941 }
4942 assert_eq!(scheme.brightness(), Brightness::Light);
4943
4944 assert_eq!(theme.primary_color, scheme.primary());
4945 assert_eq!(theme.canvas_color, scheme.surface());
4946 assert_eq!(theme.scaffold_background_color, scheme.surface());
4947 assert_eq!(theme.card_color, scheme.surface());
4948 assert_eq!(theme.divider_color, scheme.outline());
4949 assert_eq!(theme.dialog_background_color, scheme.surface());
4950 assert_eq!(theme.indicator_color, scheme.on_primary());
4951 assert!(!theme.apply_elevation_overlay_color);
4952 }
4953 });
4954 }
4955
4956 #[test]
4957 fn theme_data_dark_can_generate_a_default_m3_dark_color_scheme_when_use_material3_is_true() {
4958 android(|app| {
4959 let theme = ThemeData::dark(app, None);
4960 let scheme = &theme.color_scheme;
4961 let expected = [
4962 (scheme.primary(), Color::new(0xFFD0BCFF)),
4963 (scheme.on_primary(), Color::new(0xFF381E72)),
4964 (scheme.primary_container(), Color::new(0xFF4F378B)),
4965 (scheme.on_primary_container(), Color::new(0xFFEADDFF)),
4966 (scheme.primary_fixed(), Color::new(0xFFEADDFF)),
4967 (scheme.primary_fixed_dim(), Color::new(0xFFD0BCFF)),
4968 (scheme.on_primary_fixed(), Color::new(0xFF21005D)),
4969 (scheme.on_primary_fixed_variant(), Color::new(0xFF4F378B)),
4970 (scheme.secondary(), Color::new(0xFFCCC2DC)),
4971 (scheme.on_secondary(), Color::new(0xFF332D41)),
4972 (scheme.secondary_container(), Color::new(0xFF4A4458)),
4973 (scheme.on_secondary_container(), Color::new(0xFFE8DEF8)),
4974 (scheme.secondary_fixed(), Color::new(0xFFE8DEF8)),
4975 (scheme.secondary_fixed_dim(), Color::new(0xFFCCC2DC)),
4976 (scheme.on_secondary_fixed(), Color::new(0xFF1D192B)),
4977 (scheme.on_secondary_fixed_variant(), Color::new(0xFF4A4458)),
4978 (scheme.tertiary(), Color::new(0xFFEFB8C8)),
4979 (scheme.on_tertiary(), Color::new(0xFF492532)),
4980 (scheme.tertiary_container(), Color::new(0xFF633B48)),
4981 (scheme.on_tertiary_container(), Color::new(0xFFFFD8E4)),
4982 (scheme.tertiary_fixed(), Color::new(0xFFFFD8E4)),
4983 (scheme.tertiary_fixed_dim(), Color::new(0xFFEFB8C8)),
4984 (scheme.on_tertiary_fixed(), Color::new(0xFF31111D)),
4985 (scheme.on_tertiary_fixed_variant(), Color::new(0xFF633B48)),
4986 (scheme.error(), Color::new(0xFFF2B8B5)),
4987 (scheme.on_error(), Color::new(0xFF601410)),
4988 (scheme.error_container(), Color::new(0xFF8C1D18)),
4989 (scheme.on_error_container(), Color::new(0xFFF9DEDC)),
4990 (scheme.outline(), Color::new(0xFF938F99)),
4991 (scheme.background(), Color::new(0xFF141218)),
4992 (scheme.on_background(), Color::new(0xFFE6E0E9)),
4993 (scheme.surface(), Color::new(0xFF141218)),
4994 (scheme.on_surface(), Color::new(0xFFE6E0E9)),
4995 (scheme.surface_variant(), Color::new(0xFF49454F)),
4996 (scheme.on_surface_variant(), Color::new(0xFFCAC4D0)),
4997 (scheme.surface_bright(), Color::new(0xFF3B383E)),
4998 (scheme.surface_dim(), Color::new(0xFF141218)),
4999 (scheme.surface_container(), Color::new(0xFF211F26)),
5000 (scheme.surface_container_highest(), Color::new(0xFF36343B)),
5001 (scheme.surface_container_high(), Color::new(0xFF2B2930)),
5002 (scheme.surface_container_lowest(), Color::new(0xFF0F0D13)),
5003 (scheme.surface_container_low(), Color::new(0xFF1D1B20)),
5004 (scheme.inverse_surface(), Color::new(0xFFE6E0E9)),
5005 (scheme.on_inverse_surface(), Color::new(0xFF322F35)),
5006 (scheme.inverse_primary(), Color::new(0xFF6750A4)),
5007 (scheme.shadow(), Color::new(0xFF000000)),
5008 ];
5009 for (index, (actual, expected)) in expected.into_iter().enumerate() {
5010 assert_eq!(actual, expected, "colour {index}");
5011 }
5012 assert_eq!(scheme.brightness(), Brightness::Dark);
5013
5014 assert_eq!(theme.primary_color, scheme.surface());
5015 assert_eq!(theme.canvas_color, scheme.surface());
5016 assert_eq!(theme.scaffold_background_color, scheme.surface());
5017 assert_eq!(theme.card_color, scheme.surface());
5018 assert_eq!(theme.divider_color, scheme.outline());
5019 assert_eq!(theme.dialog_background_color, scheme.surface());
5020 assert_eq!(theme.indicator_color, scheme.on_surface());
5021 assert!(theme.apply_elevation_overlay_color);
5022 });
5023 }
5024
5025 #[test]
5026 fn theme_data_from_a_light_color_scheme_sets_appropriate_values() {
5027 android(|app| {
5028 let light_colors = ColorScheme::light();
5029 let theme = ThemeData::from(light_colors.clone()).build(app);
5030
5031 assert_eq!(theme.brightness(), Brightness::Light);
5032 assert_eq!(theme.primary_color, light_colors.primary());
5033 assert_eq!(theme.card_color, light_colors.surface());
5034 assert_eq!(theme.canvas_color, light_colors.surface());
5035 assert_eq!(theme.scaffold_background_color, light_colors.surface());
5036 assert_eq!(theme.dialog_background_color, light_colors.surface());
5037 assert!(!theme.apply_elevation_overlay_color);
5038 });
5039 }
5040
5041 #[test]
5042 fn theme_data_from_a_dark_color_scheme_sets_appropriate_values() {
5043 android(|app| {
5044 let dark_colors = ColorScheme::dark();
5045 let theme = ThemeData::from(dark_colors.clone()).build(app);
5046
5047 assert_eq!(theme.brightness(), Brightness::Dark);
5048 assert_eq!(theme.primary_color, dark_colors.surface());
5050 assert_eq!(theme.card_color, dark_colors.surface());
5051 assert_eq!(theme.canvas_color, dark_colors.surface());
5052 assert_eq!(theme.scaffold_background_color, dark_colors.surface());
5053 assert_eq!(theme.dialog_background_color, dark_colors.surface());
5054 assert!(theme.apply_elevation_overlay_color);
5055 });
5056 }
5057
5058 #[test]
5059 fn splash_factory_is_ink_sparkle_only_for_android_non_web_when_use_material3_is_true() {
5060 for platform in ALL_PLATFORMS {
5061 on(platform, |app| {
5062 let theme = ThemeData::new(app);
5063
5064 assert!(theme.use_material3);
5066
5067 let expected = match platform {
5068 TargetPlatform::Android if K_IS_WEB => InkRipple::splash_factory(),
5069 TargetPlatform::Android => InkSparkle::splash_factory(),
5070 _ => InkRipple::splash_factory(),
5071 };
5072 assert!(Rc::ptr_eq(&theme.splash_factory, &expected), "{platform:?}");
5073 });
5074 }
5075 }
5076
5077 #[test]
5078 fn splash_factory_is_ink_splash_for_every_platform_when_use_material3_is_false() {
5079 for platform in ALL_PLATFORMS {
5080 on(platform, |app| {
5081 let theme = ThemeData::builder().use_material3(false).build(app);
5082 assert!(
5083 Rc::ptr_eq(&theme.splash_factory, &InkSplash::splash_factory()),
5084 "{platform:?}"
5085 );
5086 });
5087 }
5088 }
5089
5090 #[test]
5091 fn visual_density_adaptive_platform_density_returns_adaptive_values() {
5092 for platform in ALL_PLATFORMS {
5093 on(platform, |app| {
5094 let expected = match platform {
5095 TargetPlatform::Android | TargetPlatform::IOS | TargetPlatform::Fuchsia => {
5096 VisualDensity::STANDARD
5097 }
5098 TargetPlatform::Linux | TargetPlatform::MacOS | TargetPlatform::Windows => {
5099 VisualDensity::COMPACT
5100 }
5101 };
5102 assert_eq!(VisualDensity::adaptive_platform_density(app), expected);
5103 assert_eq!(
5104 VisualDensity::default_density_for_platform(platform),
5105 expected
5106 );
5107 });
5108 }
5109 }
5110
5111 #[test]
5112 fn visual_density_in_theme_data_defaults_to_compact_on_desktop_and_standard_on_mobile() {
5113 for platform in ALL_PLATFORMS {
5114 on(platform, |app| {
5115 let theme_data = ThemeData::new(app);
5116 let expected = match platform {
5117 TargetPlatform::Android | TargetPlatform::IOS | TargetPlatform::Fuchsia => {
5118 VisualDensity::STANDARD
5119 }
5120 TargetPlatform::Linux | TargetPlatform::MacOS | TargetPlatform::Windows => {
5121 VisualDensity::COMPACT
5122 }
5123 };
5124 assert_eq!(theme_data.visual_density, expected, "{platform:?}");
5125 });
5126 }
5127 }
5128
5129 #[test]
5130 fn visual_density_in_theme_data_defaults_to_the_right_thing_when_a_platform_is_supplied_to_it()
5131 {
5132 for platform in ALL_PLATFORMS {
5133 on(platform, |app| {
5134 let theme_data = ThemeData::builder()
5135 .platform(if platform == TargetPlatform::Android {
5136 TargetPlatform::Linux
5137 } else {
5138 TargetPlatform::Android
5139 })
5140 .build(app);
5141 let expected = match platform {
5142 TargetPlatform::Android => VisualDensity::COMPACT,
5143 _ => VisualDensity::STANDARD,
5144 };
5145 assert_eq!(theme_data.visual_density, expected, "{platform:?}");
5146 });
5147 }
5148 }
5149
5150 #[test]
5151 fn visual_density_effective_constraints_are_clamped() {
5152 let expand = VisualDensity::new(4.0, 4.0);
5153 let contract = VisualDensity::new(-4.0, -4.0);
5154
5155 let square = BoxConstraints::tight_for(Some(35.0), Some(35.0));
5156 let expanded = expand.effective_constraints(square);
5157 assert_eq!(expanded.min_width, 35.0);
5158 assert_eq!(expanded.min_height, 35.0);
5159 assert_eq!(expanded.max_width, 35.0);
5160 assert_eq!(expanded.max_height, 35.0);
5161
5162 let contracted = contract.effective_constraints(square);
5163 assert_eq!(contracted.min_width, 19.0);
5164 assert_eq!(contracted.min_height, 19.0);
5165 assert_eq!(expanded.max_width, 35.0);
5166 assert_eq!(expanded.max_height, 35.0);
5167
5168 let small = BoxConstraints::tight_for(Some(4.0), Some(4.0));
5169 let expanded = expand.effective_constraints(small);
5170 assert_eq!(expanded.min_width, 4.0);
5171 assert_eq!(expanded.min_height, 4.0);
5172 assert_eq!(expanded.max_width, 4.0);
5173 assert_eq!(expanded.max_height, 4.0);
5174
5175 let contracted = contract.effective_constraints(small);
5176 assert_eq!(contracted.min_width, 0.0);
5177 assert_eq!(contracted.min_height, 0.0);
5178 assert_eq!(expanded.max_width, 4.0);
5179 assert_eq!(expanded.max_height, 4.0);
5180 }
5181
5182 #[test]
5183 fn visual_density_effective_constraints_expand_and_contract() {
5184 let square = BoxConstraints::new();
5185 let expanded = VisualDensity::new(4.0, 4.0).effective_constraints(square);
5186 assert_eq!(expanded.min_width, 16.0);
5187 assert_eq!(expanded.min_height, 16.0);
5188 assert_eq!(expanded.max_width, f64::INFINITY);
5189 assert_eq!(expanded.max_height, f64::INFINITY);
5190
5191 let contracted = VisualDensity::new(-4.0, -4.0).effective_constraints(square);
5192 assert_eq!(contracted.min_width, 0.0);
5193 assert_eq!(contracted.min_height, 0.0);
5194 assert_eq!(expanded.max_width, f64::INFINITY);
5195 assert_eq!(expanded.max_height, f64::INFINITY);
5196 }
5197
5198 #[test]
5199 fn theme_extensions_can_be_obtained() {
5200 android(|app| {
5201 let theme = ThemeData::builder()
5202 .extensions(vec![
5203 Rc::new(MyThemeExtensionA {
5204 color1: Some(Colors::BLACK),
5205 color2: Some(Colors::AMBER.primary()),
5206 }),
5207 Rc::new(MyThemeExtensionB {
5208 text_style: Some(TextStyle::new().font_size(50.0)),
5209 }),
5210 ])
5211 .build(app);
5212
5213 let a = theme.extension::<MyThemeExtensionA>().expect("extension A");
5214 assert_eq!(a.color1, Some(Colors::BLACK));
5215 assert_eq!(a.color2, Some(Colors::AMBER.primary()));
5216 let b = theme.extension::<MyThemeExtensionB>().expect("extension B");
5217 assert_eq!(b.text_style, Some(TextStyle::new().font_size(50.0)));
5218 });
5219 }
5220
5221 #[test]
5222 fn theme_extensions_can_use_copy_with() {
5223 android(|app| {
5224 let extension = MyThemeExtensionA {
5225 color1: Some(Colors::BLACK),
5226 color2: Some(Colors::AMBER.primary()),
5227 };
5228 let copied = MyThemeExtensionA {
5229 color1: Some(Colors::BLUE.primary()),
5230 ..extension
5231 };
5232 let theme = ThemeData::builder()
5233 .extensions(vec![copied.copy_with()])
5234 .build(app);
5235
5236 let a = theme.extension::<MyThemeExtensionA>().expect("extension A");
5237 assert_eq!(a.color1, Some(Colors::BLUE.primary()));
5238 assert_eq!(a.color2, Some(Colors::AMBER.primary()));
5239 });
5240 }
5241
5242 fn assert_same_color(actual: Option<Color>, expected: Color) {
5245 let actual = actual.expect("a color");
5246 let step = 1.0 / 255.0;
5247 assert!(
5248 (actual.a - expected.a).abs() < step
5249 && (actual.r - expected.r).abs() < step
5250 && (actual.g - expected.g).abs() < step
5251 && (actual.b - expected.b).abs() < step,
5252 "{actual:?} is not {expected:?}"
5253 );
5254 }
5255
5256 #[test]
5257 fn theme_extensions_can_lerp() {
5258 android(|app| {
5259 let extension_a1: ThemeExtensionRef = Rc::new(MyThemeExtensionA {
5260 color1: Some(Colors::BLACK),
5261 color2: Some(Colors::AMBER.primary()),
5262 });
5263 let extension_a2: ThemeExtensionRef = Rc::new(MyThemeExtensionA {
5264 color1: Some(Colors::WHITE),
5265 color2: Some(Colors::BLUE.primary()),
5266 });
5267 let extension_b1: ThemeExtensionRef = Rc::new(MyThemeExtensionB {
5268 text_style: Some(TextStyle::new().font_size(50.0)),
5269 });
5270 let extension_b2: ThemeExtensionRef = Rc::new(MyThemeExtensionB {
5271 text_style: Some(TextStyle::new().font_size(100.0)),
5272 });
5273 let themed = |extensions: &[&ThemeExtensionRef]| {
5274 ThemeData::builder()
5275 .extensions(
5276 extensions
5277 .iter()
5278 .map(|&extension| Rc::clone(extension))
5279 .collect(),
5280 )
5281 .build(app)
5282 };
5283 let a = |theme: &ThemeData| {
5284 let a = theme.extension::<MyThemeExtensionA>().expect("extension A");
5285 (a.color1, a.color2)
5286 };
5287 let font_size = |theme: &ThemeData| {
5288 theme
5289 .extension::<MyThemeExtensionB>()
5290 .expect("extension B")
5291 .text_style
5292 .clone()
5293 };
5294
5295 let lerped = ThemeData::lerp(
5297 &themed(&[&extension_a1, &extension_b1]),
5298 &themed(&[&extension_a2, &extension_b2]),
5299 0.5,
5300 );
5301 let (color1, color2) = a(&lerped);
5302 assert_same_color(color1, Color::new(0xff7f7f7f));
5303 assert_same_color(color2, Color::new(0xff90ab7d));
5304 assert_eq!(font_size(&lerped), Some(TextStyle::new().font_size(75.0)));
5305
5306 let lerped = ThemeData::lerp(
5308 &themed(&[&extension_a1, &extension_b1]),
5309 &themed(&[&extension_b2]),
5310 0.5,
5311 );
5312 let (color1, color2) = a(&lerped);
5314 assert_same_color(color1, Colors::BLACK);
5315 assert_same_color(color2, Colors::AMBER.primary());
5316 assert_eq!(font_size(&lerped), Some(TextStyle::new().font_size(75.0)));
5317
5318 let lerped = ThemeData::lerp(
5320 &themed(&[&extension_a1]),
5321 &themed(&[&extension_a2, &extension_b2]),
5322 0.5,
5323 );
5324 let (color1, color2) = a(&lerped);
5325 assert_same_color(color1, Color::new(0xff7f7f7f));
5326 assert_same_color(color2, Color::new(0xff90ab7d));
5327 assert_eq!(font_size(&lerped), Some(TextStyle::new().font_size(100.0)));
5329 });
5330 }
5331
5332 #[test]
5333 fn theme_extensions_keep_the_order_they_were_given_in_and_lerp_keeps_the_first_themes() {
5334 android(|app| {
5335 let a: ThemeExtensionRef = Rc::new(MyThemeExtensionA {
5336 color1: None,
5337 color2: None,
5338 });
5339 let b: ThemeExtensionRef = Rc::new(MyThemeExtensionB { text_style: None });
5340 let a_type = TypeId::of::<MyThemeExtensionA>();
5341 let b_type = TypeId::of::<MyThemeExtensionB>();
5342 let types = |theme: &ThemeData| theme.extensions.keys().copied().collect::<Vec<_>>();
5343 let themed = |extensions: Vec<ThemeExtensionRef>| {
5344 ThemeData::builder().extensions(extensions).build(app)
5345 };
5346
5347 assert_eq!(
5348 types(&themed(vec![Rc::clone(&a), Rc::clone(&b)])),
5349 [a_type, b_type]
5350 );
5351 assert_eq!(
5352 types(&themed(vec![Rc::clone(&b), Rc::clone(&a)])),
5353 [b_type, a_type]
5354 );
5355
5356 let last_a: ThemeExtensionRef = Rc::new(MyThemeExtensionA {
5358 color1: Some(Colors::BLACK),
5359 color2: None,
5360 });
5361 let repeated = themed(vec![Rc::clone(&a), Rc::clone(&b), last_a]);
5362 assert_eq!(types(&repeated), [a_type, b_type]);
5363 assert_eq!(
5364 repeated
5365 .extension::<MyThemeExtensionA>()
5366 .expect("extension A")
5367 .color1,
5368 Some(Colors::BLACK)
5369 );
5370
5371 let lerped = ThemeData::lerp(
5373 &themed(vec![Rc::clone(&b)]),
5374 &themed(vec![Rc::clone(&a), Rc::clone(&b)]),
5375 0.5,
5376 );
5377 assert_eq!(types(&lerped), [b_type, a_type]);
5378 });
5379 }
5380
5381 #[test]
5382 fn theme_extensions_return_none_when_not_found() {
5383 android(|app| {
5384 let theme = ThemeData::builder().extensions(Vec::new()).build(app);
5385 assert!(theme.extension::<MyThemeExtensionA>().is_none());
5386 });
5387 }
5388
5389 #[test]
5390 fn copy_with_and_equality_basics() {
5391 android(|app| {
5392 assert_eq!(ThemeData::new(app), ThemeData::new(app).copy_with().build());
5393 });
5394 }
5395
5396 #[test]
5397 fn equality_includes_focus_color_and_hover_color() {
5398 android(|app| {
5400 let button_theme = ButtonThemeData::new();
5403
5404 let focus_color_black = ThemeData::builder()
5405 .focus_color(Colors::BLACK)
5406 .button_theme(button_theme.clone())
5407 .build(app);
5408 let focus_color_white = ThemeData::builder()
5409 .focus_color(Colors::WHITE)
5410 .button_theme(button_theme.clone())
5411 .build(app);
5412 assert_ne!(focus_color_black, focus_color_white);
5413
5414 let hover_color_black = ThemeData::builder()
5415 .hover_color(Colors::BLACK)
5416 .button_theme(button_theme.clone())
5417 .build(app);
5418 let hover_color_white = ThemeData::builder()
5419 .hover_color(Colors::WHITE)
5420 .button_theme(button_theme)
5421 .build(app);
5422 assert_ne!(hover_color_black, hover_color_white);
5423 });
5424 }
5425
5426 #[test]
5427 fn text_selection_theme_defaults_to_an_empty_theme_and_takes_part_in_equality() {
5428 android(|app| {
5429 let theme = ThemeData::new(app);
5430 assert_eq!(theme.text_selection_theme, TextSelectionThemeData::new());
5431
5432 let red = ThemeData::builder()
5433 .text_selection_theme(
5434 TextSelectionThemeData::new().cursor_color(Colors::RED.primary()),
5435 )
5436 .build(app);
5437 assert_eq!(
5438 red.text_selection_theme.cursor_color,
5439 Some(Colors::RED.primary())
5440 );
5441 assert_ne!(theme, red);
5442 assert_eq!(
5443 theme
5444 .copy_with()
5445 .text_selection_theme(red.text_selection_theme.clone())
5446 .build(),
5447 red
5448 );
5449 });
5450 }
5451
5452 #[test]
5453 fn text_selection_theme_lerps_with_the_theme() {
5454 android(|app| {
5455 let theme_with = |color: Color| {
5456 ThemeData::builder()
5457 .text_selection_theme(TextSelectionThemeData::new().cursor_color(color))
5458 .build(app)
5459 };
5460 let lerped =
5461 ThemeData::lerp(&theme_with(Colors::BLACK), &theme_with(Colors::WHITE), 0.5);
5462 assert_eq!(
5463 lerped.text_selection_theme.cursor_color,
5464 Color::lerp(Some(Colors::BLACK), Some(Colors::WHITE), 0.5)
5465 );
5466 });
5467 }
5468
5469 fn black_raw_theme(dark: &ThemeData) -> Box<ThemeData> {
5471 Box::new(ThemeData {
5472 adaptation_map: IndexMap::new(),
5474 apply_elevation_overlay_color: false,
5475 cupertino_override_theme: None,
5476 extensions: IndexMap::new(),
5477 input_decoration_theme: dark
5478 .input_decoration_theme
5479 .copy_with()
5480 .border(OutlineInputBorder::new()),
5481 material_tap_target_size: MaterialTapTargetSize::ShrinkWrap,
5482 page_transitions_theme: PageTransitionsTheme::new().with_builders(IndexMap::from([
5483 (
5484 TargetPlatform::IOS,
5485 PageTransitionsBuilderRef::new(CupertinoPageTransitionsBuilder::new()),
5486 ),
5487 (
5488 TargetPlatform::MacOS,
5489 PageTransitionsBuilderRef::new(CupertinoPageTransitionsBuilder::new()),
5490 ),
5491 ])),
5492 platform: TargetPlatform::IOS,
5493 scrollbar_theme: ScrollbarThemeData::new()
5494 .radius(waft_embedder::Radius::circular(10.0)),
5495 splash_factory: InkRipple::splash_factory(),
5496 use_material3: false,
5497 visual_density: VisualDensity::STANDARD,
5498 canvas_color: Colors::BLACK,
5500 card_color: Colors::BLACK,
5501 color_scheme: ColorScheme::light(),
5502 disabled_color: Colors::BLACK,
5503 divider_color: Colors::BLACK,
5504 focus_color: Colors::BLACK,
5505 highlight_color: Colors::BLACK,
5506 hint_color: Colors::BLACK,
5507 hover_color: Colors::BLACK,
5508 primary_color: Colors::BLACK,
5509 primary_color_dark: Colors::BLACK,
5510 primary_color_light: Colors::BLACK,
5511 scaffold_background_color: Colors::BLACK,
5512 secondary_header_color: Colors::BLACK,
5513 shadow_color: Colors::BLACK,
5514 splash_color: Colors::BLACK,
5515 unselected_widget_color: Colors::BLACK,
5516 icon_theme: dark.icon_theme.clone(),
5518 primary_icon_theme: dark.icon_theme.clone(),
5519 primary_text_theme: dark.text_theme.clone(),
5520 text_theme: dark.text_theme.clone(),
5521 typography: Typography::material2018().build(),
5522 action_icon_theme: Some(ActionIconThemeData::new()),
5524 app_bar_theme: AppBarThemeData::new().elevation(1.0),
5525 badge_theme: BadgeThemeData::new().small_size(1.0),
5526 banner_theme: MaterialBannerThemeData::new().elevation(1.0),
5527 bottom_navigation_bar_theme: BottomNavigationBarThemeData::new()
5528 .r#type(BottomNavigationBarType::Fixed),
5529 bottom_sheet_theme: BottomSheetThemeData::new().elevation(1.0),
5530 button_theme: ButtonThemeData::new().with_color_scheme(ColorScheme::dark()),
5531 card_theme: CardThemeData::new().elevation(1.0),
5532 carousel_view_theme: CarouselViewThemeData::new(),
5533 checkbox_theme: CheckboxThemeData::new(),
5534 chip_theme: ChipThemeData::from_defaults(
5535 None,
5536 Some(Colors::BLACK),
5537 Colors::WHITE,
5538 TextStyle::new().color(Colors::BLACK),
5539 ),
5540 data_table_theme: DataTableThemeData::new(),
5541 date_picker_theme: DatePickerThemeData::new(),
5542 dialog_theme: DialogThemeData::new().background_color(Colors::BLACK),
5543 divider_theme: DividerThemeData::new().color(Colors::BLACK),
5544 drawer_theme: DrawerThemeData::new().elevation(1.0),
5545 dropdown_menu_theme: DropdownMenuThemeData::new(),
5546 elevated_button_theme: ElevatedButtonThemeData::new().style(
5547 ElevatedButton::style_from()
5548 .background_color(Colors::GREEN.primary())
5549 .build(),
5550 ),
5551 expansion_tile_theme: ExpansionTileThemeData::new().background_color(Colors::BLACK),
5552 filled_button_theme: FilledButtonThemeData::new().style(
5553 FilledButton::style_from()
5554 .foreground_color(Colors::GREEN.primary())
5555 .build(),
5556 ),
5557 floating_action_button_theme: FloatingActionButtonThemeData::new().elevation(1.0),
5558 icon_button_theme: IconButtonThemeData::new().style(
5559 IconButton::style_from()
5560 .foreground_color(Colors::PINK.primary())
5561 .build(),
5562 ),
5563 list_tile_theme: ListTileThemeData::new(),
5564 menu_bar_theme: MenuBarThemeData::new().style(
5565 MenuStyle::new()
5566 .background_color(Rc::new(MaterialStatePropertyAll::new(Some(Colors::BLACK)))),
5567 ),
5568 menu_button_theme: MenuButtonThemeData::new().style(
5569 MenuItemButton::style_from()
5570 .background_color(Colors::BLACK)
5571 .build(),
5572 ),
5573 menu_theme: MenuThemeData::new().style(
5574 MenuStyle::new()
5575 .background_color(Rc::new(MaterialStatePropertyAll::new(Some(Colors::BLACK)))),
5576 ),
5577 navigation_bar_theme: NavigationBarThemeData::new().background_color(Colors::BLACK),
5578 navigation_drawer_theme: NavigationDrawerThemeData::new()
5579 .background_color(Colors::BLACK),
5580 navigation_rail_theme: NavigationRailThemeData::new().background_color(Colors::BLACK),
5581 outlined_button_theme: OutlinedButtonThemeData::new().style(
5582 OutlinedButton::style_from()
5583 .foreground_color(Colors::BLUE.primary())
5584 .build(),
5585 ),
5586 popup_menu_theme: PopupMenuThemeData::new().color(Colors::BLACK),
5587 progress_indicator_theme: ProgressIndicatorThemeData::new(),
5588 radio_theme: RadioThemeData::new(),
5589 search_bar_theme: SearchBarThemeData::new(),
5590 search_view_theme: SearchViewThemeData::new(),
5591 segmented_button_theme: SegmentedButtonThemeData::new(),
5592 slider_theme: SliderThemeData::from_primary_colors(
5593 Colors::BLACK,
5594 Colors::BLACK,
5595 Colors::BLACK,
5596 TextStyle::new().color(Colors::BLACK),
5597 ),
5598 snack_bar_theme: SnackBarThemeData::new().elevation(1.0),
5599 switch_theme: SwitchThemeData::new(),
5600 tab_bar_theme: TabBarThemeData::new().label_color(Colors::BLACK),
5601 text_button_theme: TextButtonThemeData::new().style(
5602 TextButton::style_from()
5603 .foreground_color(Colors::RED.primary())
5604 .build(),
5605 ),
5606 text_selection_theme: TextSelectionThemeData::new().cursor_color(Colors::BLACK),
5607 time_picker_theme: TimePickerThemeData::new().background_color(Colors::BLACK),
5608 toggle_buttons_theme: ToggleButtonsThemeData::new()
5609 .text_style(TextStyle::new().color(Colors::BLACK)),
5610 tooltip_theme: TooltipThemeData::new().height(100.0),
5611 button_bar_theme: ButtonBarThemeData::new().with_alignment(MainAxisAlignment::Start),
5613 dialog_background_color: Colors::BLACK,
5614 indicator_color: Colors::BLACK,
5615 })
5616 }
5617
5618 fn white_raw_theme(light: &ThemeData) -> Box<ThemeData> {
5620 Box::new(ThemeData {
5621 adaptation_map: ThemeData::create_adaptation_map(vec![AdaptationRef::new(
5623 StringAdaptation,
5624 )]),
5625 apply_elevation_overlay_color: true,
5626 cupertino_override_theme: light.cupertino_override_theme.clone(),
5627 extensions: ThemeData::theme_extension_iterable_to_map(vec![Rc::new(
5628 MyThemeExtensionB {
5629 text_style: Some(TextStyle::new()),
5630 },
5631 )]),
5632 input_decoration_theme: light
5633 .input_decoration_theme
5634 .copy_with()
5635 .border(InputBorderRef::none()),
5636 material_tap_target_size: MaterialTapTargetSize::Padded,
5637 page_transitions_theme: PageTransitionsTheme::new(),
5638 platform: TargetPlatform::Android,
5639 scrollbar_theme: ScrollbarThemeData::new()
5640 .radius(waft_embedder::Radius::circular(10.0)),
5641 splash_factory: InkRipple::splash_factory(),
5642 use_material3: true,
5643 visual_density: VisualDensity::STANDARD,
5644 canvas_color: Colors::WHITE,
5646 card_color: Colors::WHITE,
5647 color_scheme: ColorScheme::light(),
5648 disabled_color: Colors::WHITE,
5649 divider_color: Colors::WHITE,
5650 focus_color: Colors::WHITE,
5651 highlight_color: Colors::WHITE,
5652 hint_color: Colors::WHITE,
5653 hover_color: Colors::WHITE,
5654 primary_color: Colors::WHITE,
5655 primary_color_dark: Colors::WHITE,
5656 primary_color_light: Colors::WHITE,
5657 scaffold_background_color: Colors::WHITE,
5658 secondary_header_color: Colors::WHITE,
5659 shadow_color: Colors::WHITE,
5660 splash_color: Colors::WHITE,
5661 unselected_widget_color: Colors::WHITE,
5662 icon_theme: light.icon_theme.clone(),
5664 primary_icon_theme: light.icon_theme.clone(),
5665 primary_text_theme: light.text_theme.clone(),
5666 text_theme: light.text_theme.clone(),
5667 typography: Typography::material2018()
5668 .platform(Some(TargetPlatform::IOS))
5669 .build(),
5670 action_icon_theme: Some(ActionIconThemeData::new()),
5672 app_bar_theme: AppBarThemeData::new().elevation(2.0),
5673 badge_theme: BadgeThemeData::new().small_size(2.0),
5674 banner_theme: MaterialBannerThemeData::new().elevation(2.0),
5675 bottom_navigation_bar_theme: BottomNavigationBarThemeData::new()
5676 .r#type(BottomNavigationBarType::Shifting),
5677 bottom_sheet_theme: BottomSheetThemeData::new().elevation(2.0),
5678 button_theme: ButtonThemeData::new().with_color_scheme(ColorScheme::light()),
5679 card_theme: CardThemeData::new().elevation(2.0),
5680 carousel_view_theme: CarouselViewThemeData::new().elevation(2.0),
5681 checkbox_theme: CheckboxThemeData::new(),
5682 chip_theme: ChipThemeData::from_defaults(
5683 None,
5684 Some(Colors::WHITE),
5685 Colors::BLACK,
5686 TextStyle::new().color(Colors::WHITE),
5687 ),
5688 data_table_theme: DataTableThemeData::new().divider_thickness(7.0),
5689 date_picker_theme: DatePickerThemeData::new().background_color(Colors::AMBER.primary()),
5690 dialog_theme: DialogThemeData::new().background_color(Colors::WHITE),
5691 divider_theme: DividerThemeData::new().color(Colors::WHITE),
5692 drawer_theme: DrawerThemeData::new().elevation(2.0),
5693 dropdown_menu_theme: DropdownMenuThemeData::new(),
5694 elevated_button_theme: ElevatedButtonThemeData::new(),
5695 expansion_tile_theme: ExpansionTileThemeData::new().background_color(Colors::BLACK),
5696 filled_button_theme: FilledButtonThemeData::new(),
5697 floating_action_button_theme: FloatingActionButtonThemeData::new().elevation(2.0),
5698 icon_button_theme: IconButtonThemeData::new(),
5699 list_tile_theme: ListTileThemeData::new(),
5700 menu_bar_theme: MenuBarThemeData::new().style(
5701 MenuStyle::new()
5702 .background_color(Rc::new(MaterialStatePropertyAll::new(Some(Colors::WHITE)))),
5703 ),
5704 menu_button_theme: MenuButtonThemeData::new().style(
5705 MenuItemButton::style_from()
5706 .background_color(Colors::BLACK)
5707 .build(),
5708 ),
5709 menu_theme: MenuThemeData::new().style(
5710 MenuStyle::new()
5711 .background_color(Rc::new(MaterialStatePropertyAll::new(Some(Colors::WHITE)))),
5712 ),
5713 navigation_bar_theme: NavigationBarThemeData::new().background_color(Colors::WHITE),
5714 navigation_drawer_theme: NavigationDrawerThemeData::new()
5715 .background_color(Colors::WHITE),
5716 navigation_rail_theme: NavigationRailThemeData::new().background_color(Colors::WHITE),
5717 outlined_button_theme: OutlinedButtonThemeData::new(),
5718 popup_menu_theme: PopupMenuThemeData::new().color(Colors::WHITE),
5719 progress_indicator_theme: ProgressIndicatorThemeData::new(),
5720 radio_theme: RadioThemeData::new(),
5721 search_bar_theme: SearchBarThemeData::new(),
5722 search_view_theme: SearchViewThemeData::new().elevation(3.0),
5723 segmented_button_theme: SegmentedButtonThemeData::new(),
5724 slider_theme: SliderThemeData::from_primary_colors(
5725 Colors::WHITE,
5726 Colors::WHITE,
5727 Colors::WHITE,
5728 TextStyle::new().color(Colors::WHITE),
5729 ),
5730 snack_bar_theme: SnackBarThemeData::new().elevation(2.0),
5731 switch_theme: SwitchThemeData::new(),
5732 tab_bar_theme: TabBarThemeData::new().label_color(Colors::WHITE),
5733 text_button_theme: TextButtonThemeData::new(),
5734 text_selection_theme: TextSelectionThemeData::new().cursor_color(Colors::WHITE),
5735 time_picker_theme: TimePickerThemeData::new().background_color(Colors::WHITE),
5736 toggle_buttons_theme: ToggleButtonsThemeData::new()
5737 .text_style(TextStyle::new().color(Colors::WHITE)),
5738 tooltip_theme: TooltipThemeData::new().height(100.0),
5739 button_bar_theme: ButtonBarThemeData::new().with_alignment(MainAxisAlignment::End),
5741 dialog_background_color: Colors::WHITE,
5742 indicator_color: Colors::WHITE,
5743 })
5744 }
5745
5746 fn copy_every_argument(theme: &ThemeData, other_theme: &ThemeData) -> Box<ThemeData> {
5748 let theme_data_copy = theme
5749 .copy_with()
5750 .adaptations(other_theme.adaptation_map.values().cloned().collect())
5752 .apply_elevation_overlay_color(other_theme.apply_elevation_overlay_color)
5753 .extensions(other_theme.extensions.values().cloned().collect())
5754 .input_decoration_theme(other_theme.input_decoration_theme.clone())
5755 .material_tap_target_size(other_theme.material_tap_target_size)
5756 .page_transitions_theme(other_theme.page_transitions_theme.clone())
5757 .platform(other_theme.platform)
5758 .scrollbar_theme(other_theme.scrollbar_theme.clone())
5759 .splash_factory(Rc::clone(&other_theme.splash_factory))
5760 .use_material3(other_theme.use_material3)
5761 .visual_density(other_theme.visual_density)
5762 .canvas_color(other_theme.canvas_color)
5764 .card_color(other_theme.card_color)
5765 .color_scheme(other_theme.color_scheme.clone())
5766 .disabled_color(other_theme.disabled_color)
5767 .divider_color(other_theme.divider_color)
5768 .focus_color(other_theme.focus_color)
5769 .highlight_color(other_theme.highlight_color)
5770 .hint_color(other_theme.hint_color)
5771 .hover_color(other_theme.hover_color)
5772 .primary_color(other_theme.primary_color)
5773 .primary_color_dark(other_theme.primary_color_dark)
5774 .primary_color_light(other_theme.primary_color_light)
5775 .scaffold_background_color(other_theme.scaffold_background_color)
5776 .secondary_header_color(other_theme.secondary_header_color)
5777 .shadow_color(other_theme.shadow_color)
5778 .splash_color(other_theme.splash_color)
5779 .unselected_widget_color(other_theme.unselected_widget_color)
5780 .icon_theme(other_theme.icon_theme.clone())
5782 .primary_icon_theme(other_theme.primary_icon_theme.clone())
5783 .primary_text_theme(other_theme.primary_text_theme.clone())
5784 .text_theme(other_theme.text_theme.clone())
5785 .typography(other_theme.typography.clone())
5786 .action_icon_theme(
5788 other_theme
5789 .action_icon_theme
5790 .clone()
5791 .expect("the other theme has an action icon theme"),
5792 )
5793 .app_bar_theme(other_theme.app_bar_theme.clone())
5794 .badge_theme(other_theme.badge_theme.clone())
5795 .banner_theme(other_theme.banner_theme.clone())
5796 .bottom_navigation_bar_theme(other_theme.bottom_navigation_bar_theme.clone())
5797 .bottom_sheet_theme(other_theme.bottom_sheet_theme.clone())
5798 .button_theme(other_theme.button_theme.clone())
5799 .card_theme(other_theme.card_theme.clone())
5800 .carousel_view_theme(other_theme.carousel_view_theme.clone())
5801 .checkbox_theme(other_theme.checkbox_theme.clone())
5802 .chip_theme(other_theme.chip_theme.clone())
5803 .data_table_theme(other_theme.data_table_theme.clone())
5804 .date_picker_theme(other_theme.date_picker_theme.clone())
5805 .dialog_theme(other_theme.dialog_theme.clone())
5806 .divider_theme(other_theme.divider_theme.clone())
5807 .drawer_theme(other_theme.drawer_theme.clone())
5808 .dropdown_menu_theme(other_theme.dropdown_menu_theme.clone())
5809 .elevated_button_theme(other_theme.elevated_button_theme.clone())
5810 .expansion_tile_theme(other_theme.expansion_tile_theme.clone())
5811 .filled_button_theme(other_theme.filled_button_theme.clone())
5812 .floating_action_button_theme(other_theme.floating_action_button_theme.clone())
5813 .icon_button_theme(other_theme.icon_button_theme.clone())
5814 .list_tile_theme(other_theme.list_tile_theme.clone())
5815 .menu_bar_theme(other_theme.menu_bar_theme.clone())
5816 .menu_button_theme(other_theme.menu_button_theme.clone())
5817 .menu_theme(other_theme.menu_theme.clone())
5818 .navigation_bar_theme(other_theme.navigation_bar_theme.clone())
5819 .navigation_drawer_theme(other_theme.navigation_drawer_theme.clone())
5820 .navigation_rail_theme(other_theme.navigation_rail_theme.clone())
5821 .outlined_button_theme(other_theme.outlined_button_theme.clone())
5822 .popup_menu_theme(other_theme.popup_menu_theme.clone())
5823 .progress_indicator_theme(other_theme.progress_indicator_theme.clone())
5824 .radio_theme(other_theme.radio_theme.clone())
5825 .search_bar_theme(other_theme.search_bar_theme.clone())
5826 .search_view_theme(other_theme.search_view_theme.clone())
5827 .segmented_button_theme(other_theme.segmented_button_theme.clone())
5828 .slider_theme(other_theme.slider_theme.clone())
5829 .snack_bar_theme(other_theme.snack_bar_theme.clone())
5830 .switch_theme(other_theme.switch_theme.clone())
5831 .tab_bar_theme(other_theme.tab_bar_theme.clone())
5832 .text_button_theme(other_theme.text_button_theme.clone())
5833 .text_selection_theme(other_theme.text_selection_theme.clone())
5834 .time_picker_theme(other_theme.time_picker_theme.clone())
5835 .toggle_buttons_theme(other_theme.toggle_buttons_theme.clone())
5836 .tooltip_theme(other_theme.tooltip_theme.clone())
5837 .button_bar_theme(other_theme.button_bar_theme.clone())
5839 .dialog_background_color(other_theme.dialog_background_color)
5840 .indicator_color(other_theme.indicator_color)
5841 .build();
5842 Box::new(theme_data_copy)
5843 }
5844
5845 #[test]
5846 fn copy_with_correctly_creates_new_theme_data_with_all_copied_arguments() {
5847 android(|app| {
5848 let dark = Box::new(ThemeData::dark(app, None));
5851 let light = Box::new(ThemeData::new(app));
5852 let theme = black_raw_theme(&dark);
5853
5854 let other_theme = white_raw_theme(&light);
5855
5856 let theme_data_copy = copy_every_argument(&theme, &other_theme);
5857
5858 assert_eq!(theme_data_copy.adaptation_map, other_theme.adaptation_map);
5860 assert_eq!(
5861 theme_data_copy.apply_elevation_overlay_color,
5862 other_theme.apply_elevation_overlay_color
5863 );
5864 assert_eq!(
5865 theme_data_copy.cupertino_override_theme,
5866 other_theme.cupertino_override_theme
5867 );
5868 assert!(extensions_equal(
5869 &theme_data_copy.extensions,
5870 &other_theme.extensions
5871 ));
5872 assert_eq!(
5873 theme_data_copy.input_decoration_theme,
5874 other_theme.input_decoration_theme
5875 );
5876 assert_eq!(
5877 theme_data_copy.material_tap_target_size,
5878 other_theme.material_tap_target_size
5879 );
5880 assert_eq!(
5881 theme_data_copy.page_transitions_theme,
5882 other_theme.page_transitions_theme
5883 );
5884 assert_eq!(theme_data_copy.platform, other_theme.platform);
5885 assert_eq!(theme_data_copy.scrollbar_theme, other_theme.scrollbar_theme);
5886 assert!(Rc::ptr_eq(
5887 &theme_data_copy.splash_factory,
5888 &other_theme.splash_factory
5889 ));
5890 assert_eq!(theme_data_copy.use_material3, other_theme.use_material3);
5891 assert_eq!(theme_data_copy.visual_density, other_theme.visual_density);
5892 assert_eq!(theme_data_copy.canvas_color, other_theme.canvas_color);
5894 assert_eq!(theme_data_copy.card_color, other_theme.card_color);
5895 assert_eq!(theme_data_copy.color_scheme, other_theme.color_scheme);
5896 assert_eq!(theme_data_copy.disabled_color, other_theme.disabled_color);
5897 assert_eq!(theme_data_copy.divider_color, other_theme.divider_color);
5898 assert_eq!(theme_data_copy.focus_color, other_theme.focus_color);
5899 assert_eq!(theme_data_copy.highlight_color, other_theme.highlight_color);
5900 assert_eq!(theme_data_copy.hint_color, other_theme.hint_color);
5901 assert_eq!(theme_data_copy.hover_color, other_theme.hover_color);
5902 assert_eq!(theme_data_copy.primary_color, other_theme.primary_color);
5903 assert_eq!(
5904 theme_data_copy.primary_color_dark,
5905 other_theme.primary_color_dark
5906 );
5907 assert_eq!(
5908 theme_data_copy.primary_color_light,
5909 other_theme.primary_color_light
5910 );
5911 assert_eq!(
5912 theme_data_copy.scaffold_background_color,
5913 other_theme.scaffold_background_color
5914 );
5915 assert_eq!(
5916 theme_data_copy.secondary_header_color,
5917 other_theme.secondary_header_color
5918 );
5919 assert_eq!(theme_data_copy.shadow_color, other_theme.shadow_color);
5920 assert_eq!(theme_data_copy.splash_color, other_theme.splash_color);
5921 assert_eq!(
5922 theme_data_copy.unselected_widget_color,
5923 other_theme.unselected_widget_color
5924 );
5925 assert_eq!(theme_data_copy.icon_theme, other_theme.icon_theme);
5927 assert_eq!(
5928 theme_data_copy.primary_icon_theme,
5929 other_theme.primary_icon_theme
5930 );
5931 assert_eq!(
5932 theme_data_copy.primary_text_theme,
5933 other_theme.primary_text_theme
5934 );
5935 assert_eq!(theme_data_copy.text_theme, other_theme.text_theme);
5936 assert_eq!(theme_data_copy.typography, other_theme.typography);
5937 assert_eq!(
5939 theme_data_copy.action_icon_theme,
5940 other_theme.action_icon_theme
5941 );
5942 assert_eq!(theme_data_copy.app_bar_theme, other_theme.app_bar_theme);
5943 assert_eq!(theme_data_copy.badge_theme, other_theme.badge_theme);
5944 assert_eq!(theme_data_copy.banner_theme, other_theme.banner_theme);
5945 assert_eq!(
5946 theme_data_copy.bottom_navigation_bar_theme,
5947 other_theme.bottom_navigation_bar_theme
5948 );
5949 assert_eq!(
5950 theme_data_copy.bottom_sheet_theme,
5951 other_theme.bottom_sheet_theme
5952 );
5953 assert_eq!(theme_data_copy.button_theme, other_theme.button_theme);
5954 assert_eq!(theme_data_copy.card_theme, other_theme.card_theme);
5955 assert_eq!(
5956 theme_data_copy.carousel_view_theme,
5957 other_theme.carousel_view_theme
5958 );
5959 assert_eq!(theme_data_copy.checkbox_theme, other_theme.checkbox_theme);
5960 assert_eq!(theme_data_copy.chip_theme, other_theme.chip_theme);
5961 assert_eq!(
5962 theme_data_copy.data_table_theme,
5963 other_theme.data_table_theme
5964 );
5965 assert_eq!(
5966 theme_data_copy.date_picker_theme,
5967 other_theme.date_picker_theme
5968 );
5969 assert_eq!(theme_data_copy.dialog_theme, other_theme.dialog_theme);
5970 assert_eq!(theme_data_copy.divider_theme, other_theme.divider_theme);
5971 assert_eq!(theme_data_copy.drawer_theme, other_theme.drawer_theme);
5972 assert_eq!(
5973 theme_data_copy.dropdown_menu_theme,
5974 other_theme.dropdown_menu_theme
5975 );
5976 assert_eq!(
5977 theme_data_copy.elevated_button_theme,
5978 other_theme.elevated_button_theme
5979 );
5980 assert_eq!(
5981 theme_data_copy.expansion_tile_theme,
5982 other_theme.expansion_tile_theme
5983 );
5984 assert_eq!(
5985 theme_data_copy.filled_button_theme,
5986 other_theme.filled_button_theme
5987 );
5988 assert_eq!(
5989 theme_data_copy.floating_action_button_theme,
5990 other_theme.floating_action_button_theme
5991 );
5992 assert_eq!(
5993 theme_data_copy.icon_button_theme,
5994 other_theme.icon_button_theme
5995 );
5996 assert_eq!(theme_data_copy.list_tile_theme, other_theme.list_tile_theme);
5997 assert_eq!(theme_data_copy.menu_bar_theme, other_theme.menu_bar_theme);
5998 assert_eq!(
5999 theme_data_copy.menu_button_theme,
6000 other_theme.menu_button_theme
6001 );
6002 assert_eq!(theme_data_copy.menu_theme, other_theme.menu_theme);
6003 assert_eq!(
6004 theme_data_copy.navigation_bar_theme,
6005 other_theme.navigation_bar_theme
6006 );
6007 assert_eq!(
6008 theme_data_copy.navigation_drawer_theme,
6009 other_theme.navigation_drawer_theme
6010 );
6011 assert_eq!(
6012 theme_data_copy.navigation_rail_theme,
6013 other_theme.navigation_rail_theme
6014 );
6015 assert_eq!(
6016 theme_data_copy.outlined_button_theme,
6017 other_theme.outlined_button_theme
6018 );
6019 assert_eq!(
6020 theme_data_copy.popup_menu_theme,
6021 other_theme.popup_menu_theme
6022 );
6023 assert_eq!(
6024 theme_data_copy.progress_indicator_theme,
6025 other_theme.progress_indicator_theme
6026 );
6027 assert_eq!(theme_data_copy.radio_theme, other_theme.radio_theme);
6028 assert_eq!(
6029 theme_data_copy.search_bar_theme,
6030 other_theme.search_bar_theme
6031 );
6032 assert_eq!(
6033 theme_data_copy.search_view_theme,
6034 other_theme.search_view_theme
6035 );
6036 assert_eq!(
6037 theme_data_copy.segmented_button_theme,
6038 other_theme.segmented_button_theme
6039 );
6040 assert_eq!(theme_data_copy.slider_theme, other_theme.slider_theme);
6041 assert_eq!(theme_data_copy.snack_bar_theme, other_theme.snack_bar_theme);
6042 assert_eq!(theme_data_copy.switch_theme, other_theme.switch_theme);
6043 assert_eq!(theme_data_copy.tab_bar_theme, other_theme.tab_bar_theme);
6044 assert_eq!(
6045 theme_data_copy.text_button_theme,
6046 other_theme.text_button_theme
6047 );
6048 assert_eq!(
6049 theme_data_copy.text_selection_theme,
6050 other_theme.text_selection_theme
6051 );
6052 assert_eq!(
6053 theme_data_copy.text_selection_theme.selection_color,
6054 other_theme.text_selection_theme.selection_color
6055 );
6056 assert_eq!(
6057 theme_data_copy.text_selection_theme.cursor_color,
6058 other_theme.text_selection_theme.cursor_color
6059 );
6060 assert_eq!(
6061 theme_data_copy.text_selection_theme.selection_handle_color,
6062 other_theme.text_selection_theme.selection_handle_color
6063 );
6064 assert_eq!(
6065 theme_data_copy.time_picker_theme,
6066 other_theme.time_picker_theme
6067 );
6068 assert_eq!(
6069 theme_data_copy.toggle_buttons_theme,
6070 other_theme.toggle_buttons_theme
6071 );
6072 assert_eq!(theme_data_copy.tooltip_theme, other_theme.tooltip_theme);
6073 assert_eq!(
6075 theme_data_copy.button_bar_theme,
6076 other_theme.button_bar_theme
6077 );
6078 assert_eq!(
6079 theme_data_copy.dialog_background_color,
6080 other_theme.dialog_background_color
6081 );
6082 assert_eq!(theme_data_copy.indicator_color, other_theme.indicator_color);
6083 });
6084 }
6085
6086 #[test]
6087 #[should_panic(expected = "ThemeData.brightness does not match ColorScheme.brightness")]
6088 fn theme_data_brightness_must_match_the_color_scheme_brightness() {
6089 android(|app| {
6090 ThemeData::builder()
6091 .color_scheme(ColorScheme::light())
6092 .brightness(Brightness::Dark)
6093 .build(app)
6094 });
6095 }
6096
6097 #[test]
6098 fn theme_data_copy_with_brightness_parameter_overrides_color_scheme_brightness() {
6099 android(|app| {
6100 let light_colors = ColorScheme::light();
6101 let theme = ThemeData::from(light_colors.clone())
6102 .build(app)
6103 .copy_with()
6104 .brightness(Brightness::Dark)
6105 .build();
6106
6107 assert_eq!(theme.brightness(), Brightness::Dark);
6109 assert_eq!(theme.color_scheme.brightness(), Brightness::Dark);
6110 assert_eq!(theme.primary_color, light_colors.primary());
6111 assert_eq!(theme.card_color, light_colors.surface());
6112 assert_eq!(theme.canvas_color, light_colors.surface());
6113 assert_eq!(theme.scaffold_background_color, light_colors.surface());
6114 assert_eq!(theme.dialog_background_color, light_colors.surface());
6115 assert!(!theme.apply_elevation_overlay_color);
6116 });
6117 }
6118
6119 #[test]
6120 fn copy_with_applies_brightness_whichever_order_it_is_given_in() {
6121 android(|app| {
6122 let theme = ThemeData::new(app);
6123 let brightness_first = theme
6124 .copy_with()
6125 .brightness(Brightness::Dark)
6126 .color_scheme(ColorScheme::light())
6127 .build();
6128 assert_eq!(brightness_first.color_scheme.brightness(), Brightness::Dark);
6129 });
6130 }
6131
6132 #[test]
6133 fn theme_adaptation_map_can_be_obtained() {
6134 android(|app| {
6135 let theme = ThemeData::builder()
6136 .adaptations(vec![AdaptationRef::new(StringAdaptation)])
6137 .build(app);
6138 let adaptive_string = theme
6139 .get_adaptation::<String>()
6140 .expect("a String adaptation")
6141 .adapt(&theme, "Default theme".to_string());
6142
6143 assert_eq!(adaptive_string, "Adaptive theme.");
6144 });
6145 }
6146
6147 #[test]
6148 fn theme_adaptation_map_returns_none_when_not_found() {
6149 android(|app| {
6150 let theme = ThemeData::builder()
6151 .adaptations(vec![AdaptationRef::new(StringAdaptation)])
6152 .build(app);
6153
6154 assert!(theme.get_adaptation::<f64>().is_none());
6155 assert!(theme.extension::<MyThemeExtensionA>().is_none());
6156 });
6157 }
6158
6159 #[test]
6162 fn theme_data_input_decoration_theme_accepts_only_a_theme_or_theme_data() {
6163 android(|app| {
6164 let from_theme = ThemeData::builder()
6165 .input_decoration_theme(crate::input_decorator::InputDecorationTheme::new())
6166 .build(app);
6167 assert_eq!(
6168 from_theme.input_decoration_theme,
6169 crate::input_decorator::InputDecorationThemeData::new()
6170 );
6171
6172 let from_data = ThemeData::builder()
6173 .input_decoration_theme(crate::input_decorator::InputDecorationThemeData::new())
6174 .build(app);
6175 assert_eq!(
6176 from_data.input_decoration_theme,
6177 crate::input_decorator::InputDecorationThemeData::new()
6178 );
6179 });
6180 }
6181
6182 #[test]
6183 fn localize_merges_the_text_geometry_under_the_theme_text_styles() {
6184 test_widgets(|tester| {
6185 let mut app = tester.cell.borrow_mut();
6186 let theme = Rc::new(ThemeData::new(&app));
6187 let geometry = Rc::clone(&theme.typography.english_like);
6188 let localized = ThemeData::localize(&mut app, &theme, &geometry);
6189 assert_eq!(
6190 localized.text_theme,
6191 geometry.merge(Some(&theme.text_theme))
6192 );
6193 assert_eq!(
6194 localized.primary_text_theme,
6195 geometry.merge(Some(&theme.primary_text_theme))
6196 );
6197 let body = localized
6198 .text_theme
6199 .body_medium
6200 .clone()
6201 .expect("a body style");
6202 assert_eq!(body.font_size, Some(14.0));
6203 assert!(!body.inherit);
6204 });
6205 }
6206 #[test]
6208 fn a_theme_describes_each_property_once_under_its_dart_name() {
6209 test_widgets(|tester| {
6210 let app = tester.cell.borrow();
6211 let theme = ThemeData::new(&app);
6212 let mut properties = waft_foundation::DiagnosticPropertiesBuilder::new();
6213 Diagnosticable::debug_fill_properties(&theme, &mut properties);
6214 let names: Vec<String> = properties
6215 .properties
6216 .iter()
6217 .filter_map(|node| node.name().map(str::to_string))
6218 .collect();
6219 let unique: std::collections::HashSet<&str> =
6220 names.iter().map(String::as_str).collect();
6221 assert_eq!(names.len(), unique.len(), "duplicate property name");
6222 let expected: std::collections::HashSet<&str> = [
6223 "adaptations",
6225 "applyElevationOverlayColor",
6226 "cupertinoOverrideTheme",
6227 "extensions",
6228 "inputDecorationTheme",
6229 "materialTapTargetSize",
6230 "pageTransitionsTheme",
6231 "platform",
6232 "scrollbarTheme",
6233 "splashFactory",
6234 "visualDensity",
6235 "useMaterial3",
6236 "colorScheme",
6238 "primaryColor",
6239 "primaryColorLight",
6240 "primaryColorDark",
6241 "focusColor",
6242 "hoverColor",
6243 "shadowColor",
6244 "canvasColor",
6245 "scaffoldBackgroundColor",
6246 "cardColor",
6247 "dividerColor",
6248 "highlightColor",
6249 "splashColor",
6250 "unselectedWidgetColor",
6251 "disabledColor",
6252 "secondaryHeaderColor",
6253 "hintColor",
6254 "typography",
6256 "textTheme",
6257 "primaryTextTheme",
6258 "iconTheme",
6259 "primaryIconTheme",
6260 "actionIconTheme",
6262 "appBarTheme",
6263 "badgeTheme",
6264 "bannerTheme",
6265 "bottomNavigationBarTheme",
6266 "bottomSheetTheme",
6267 "buttonTheme",
6268 "cardTheme",
6269 "carouselViewTheme",
6270 "checkboxTheme",
6271 "chipTheme",
6272 "dataTableTheme",
6273 "datePickerTheme",
6274 "dialogTheme",
6275 "dividerTheme",
6276 "drawerTheme",
6277 "dropdownMenuTheme",
6278 "elevatedButtonTheme",
6279 "expansionTileTheme",
6280 "filledButtonTheme",
6281 "floatingActionButtonTheme",
6282 "iconButtonTheme",
6283 "listTileTheme",
6284 "menuBarTheme",
6285 "menuButtonTheme",
6286 "menuTheme",
6287 "navigationBarTheme",
6288 "navigationDrawerTheme",
6289 "navigationRailTheme",
6290 "outlinedButtonTheme",
6291 "popupMenuTheme",
6292 "progressIndicatorTheme",
6293 "radioTheme",
6294 "searchBarTheme",
6295 "searchViewTheme",
6296 "segmentedButtonTheme",
6297 "sliderTheme",
6298 "snackBarTheme",
6299 "switchTheme",
6300 "tabBarTheme",
6301 "textButtonTheme",
6302 "textSelectionTheme",
6303 "timePickerTheme",
6304 "toggleButtonsTheme",
6305 "tooltipTheme",
6306 "buttonBarTheme",
6308 "dialogBackgroundColor",
6309 "indicatorColor",
6310 ]
6311 .into_iter()
6312 .collect();
6313 assert_eq!(unique, expected);
6314 });
6315 }
6316
6317 #[test]
6319 fn a_theme_to_string_is_short() {
6320 test_widgets(|tester| {
6321 let app = tester.cell.borrow();
6322 let theme = ThemeData::from(ColorScheme::dark()).build(&app);
6323 assert!(Diagnosticable::to_string(&theme).len() < 200);
6324 let theme = ThemeData::from(ColorScheme::light()).build(&app);
6325 assert!(Diagnosticable::to_string(&theme).len() < 200);
6326 });
6327 }
6328}