1use std::any::Any;
11use std::fmt::{self, Debug};
12use std::rc::Rc;
13use std::time::Duration;
14
15use waft_animation::{
16 Animatable, Animation, AnimationBehavior, AnimationController, AnimationRef,
17 AnimationWithParent, ColorTween, Curve, CurvedAnimation, Curves, Drive, Tween,
18};
19use waft_embedder::{
20 Brightness, Canvas, Color, Matrix4, Offset, Paint, PaintStyle, SignedDuration, Size,
21 TextBaseline, lerp_double,
22};
23use waft_foundation::{
24 App, ChangeNotifier, ChangeNotifierData, DiagnosticPropertiesBuilder, Diagnosticable,
25 DiagnosticsNode, DiagnosticsProperty, Handle, IntProperty, Listenable, Listener,
26 MergingListenable, OptionRetained, Ref,
27};
28use waft_painting::{
29 AnyBorderSide, AnyColor, AnyTextStyle, BorderSide, ColorProperty, EdgeInsets,
30 EdgeInsetsDirectional, EdgeInsetsGeometry, ShapeBorderRef, TextAlign, TextAlignVertical,
31 TextDirection, TextOverflow, TextStyle,
32};
33use waft_rendering::{
34 AnyRenderBox, AnyRenderObject, BoxConstraints, BoxHitTestResult, BoxParentData,
35 ChildLayoutHelper, ChildLayouter, ContainerLayer, CustomPainter, PaintingContext,
36 PipelineOwner, RenderBox, RenderBoxData, RenderHandle, RenderObject, RenderObjectData,
37 TransformLayer,
38};
39use waft_scheduler::{Ticker, TickerCallback, TickerProviderObject};
40use waft_services::SystemMouseCursors;
41use waft_widgets::{
42 AnimatedDefaultTextStyle, AnimatedOpacity, AnimatedSwitcher, AnimatedSwitcherTransitionBuilder,
43 BuildContext, Builder, Center, ConstrainedBox, CustomPaint, DefaultTextStyle, Directionality,
44 FadeTransition, FractionalTranslation, IconTheme, IconThemeData, IgnorePointer, InheritedTheme,
45 InheritedWidget, IntoWidget, KeyRef, MatrixTransition, MediaQuery, MouseRegion, Padding,
46 RenderObjectWidget, SingleTickerProviderStateMixin, SingleTickerProviderStateMixinData,
47 SizedBox, SlottedContainerRenderObjectData, SlottedContainerRenderObjectMixin,
48 SlottedMultiChildRenderObjectWidget, Stack, State, StateData, StatefulWidget, StatelessWidget,
49 Text, TickerProviderStateMixin, TickerProviderStateMixinData, WidgetRef, WidgetState,
50 WidgetStateColor, WidgetStateProperty, WidgetStatePropertyAll, WidgetStatePropertyRef,
51 WidgetStateTextStyle, WidgetStates,
52};
53
54use crate::button_style::ButtonStyle;
55use crate::colors::Colors;
56use crate::constants::K_MIN_INTERACTIVE_DIMENSION;
57use crate::generated::input_decorator_defaults_m3::InputDecoratorDefaultsM3;
58use crate::icon_button_theme::{IconButtonTheme, IconButtonThemeData};
59use crate::input_border::{
60 InputBorder, InputBorderRef, OutlineInputBorder, UnderlineInputBorder, input_border_of,
61};
62use crate::theme::Theme;
63use crate::theme_data::{ThemeData, VisualDensity};
64use crate::theme_data_getters::DefaultsContext;
65
66const K_TRANSITION_DURATION: Duration = Duration::from_millis(167);
69fn k_transition_curve() -> Rc<dyn Curve> {
70 Curves::fast_out_slow_in()
71}
72const K_FINAL_LABEL_SCALE: f64 = 0.75;
73
74const K_INPUT_EXTRA_PADDING: f64 = 4.0;
80
81const K_SUPPORTING_TEXT_COUNTER_PADDING: f64 = 16.0;
84
85#[derive(Clone, Copy, Debug)]
87struct SupportingTextSize {
88 ascent: f64,
89 bottom_height: f64,
90 supporting_text_height: f64,
91}
92
93type ChildBaselineGetter = fn(&mut App, AnyRenderBox, BoxConstraints) -> f64;
95
96const K_HINT_FADE_TRANSITION_DURATION: Duration = Duration::from_millis(20);
102
103struct InputBorderGap {
106 change_notifier: ChangeNotifierData,
107 start: Option<f64>,
108 extent: f64,
109}
110
111impl InputBorderGap {
112 fn new(app: &mut App) -> Ref<InputBorderGap> {
113 app.create(InputBorderGap {
114 change_notifier: ChangeNotifierData::default(),
115 start: None,
116 extent: 0.0,
117 })
118 }
119
120 fn start(self: Handle<Self>, app: &App) -> Option<f64> {
121 app.get(self).start
122 }
123
124 fn set_start(self: Handle<Self>, app: &mut App, value: Option<f64>) {
125 if value != app.get(self).start {
126 app.get_mut(self).start = value;
127 self.notify_listeners(app);
128 }
129 }
130
131 fn extent(self: Handle<Self>, app: &App) -> f64 {
132 app.get(self).extent
133 }
134
135 fn set_extent(self: Handle<Self>, app: &mut App, value: f64) {
136 if value != app.get(self).extent {
137 app.get_mut(self).extent = value;
138 self.notify_listeners(app);
139 }
140 }
141}
142
143impl ChangeNotifier for InputBorderGap {
144 fn change_notifier_data(&self) -> &ChangeNotifierData {
145 &self.change_notifier
146 }
147
148 fn change_notifier_data_mut(&mut self) -> &mut ChangeNotifierData {
149 &mut self.change_notifier
150 }
151}
152
153impl Debug for InputBorderGap {
154 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
155 f.write_str("_InputBorderGap")
156 }
157}
158
159#[derive(Clone, Debug)]
164struct InputBorderTween {
165 begin: InputBorderRef,
166 end: InputBorderRef,
167}
168
169impl InputBorderTween {
170 fn new(begin: InputBorderRef, end: InputBorderRef) -> InputBorderTween {
171 InputBorderTween { begin, end }
172 }
173
174 fn lerp(&self, t: f64) -> InputBorderRef {
176 let begin = ShapeBorderRef::from(self.begin.clone());
177 let end = ShapeBorderRef::from(self.end.clone());
178 let lerped = ShapeBorderRef::lerp(Some(&begin), Some(&end), t)
179 .expect("lerping two borders gives a border");
180 input_border_of(&*lerped).unwrap_or_else(|| {
181 if t < 0.5 {
182 self.begin.clone()
183 } else {
184 self.end.clone()
185 }
186 })
187 }
188}
189
190impl Animatable<InputBorderRef> for InputBorderTween {
192 fn transform(&self, _app: &App, t: f64) -> InputBorderRef {
193 if t == 0.0 {
194 return self.begin.clone();
195 }
196 if t == 1.0 {
197 return self.end.clone();
198 }
199 self.lerp(t)
200 }
201}
202
203struct InputBorderPainter {
205 repaint: Rc<dyn Listenable>,
206 border_animation: AnimationRef<f64>,
207 border: Rc<InputBorderTween>,
208 gap_animation: AnimationRef<f64>,
209 gap: Ref<InputBorderGap>,
210 text_direction: TextDirection,
211 fill_color: Color,
212 hover_color_tween: Ref<ColorTween>,
213 hover_animation: AnimationRef<f64>,
214}
215
216impl InputBorderPainter {
217 fn blended_color(&self, app: &App) -> Color {
218 Color::alpha_blend(
219 self.hover_color_tween
220 .evaluate(app, self.hover_animation.handle())
221 .expect("both colours are given"),
222 self.fill_color,
223 )
224 }
225}
226
227impl CustomPainter for InputBorderPainter {
228 fn repaint(&self) -> Option<&Rc<dyn Listenable>> {
229 Some(&self.repaint)
230 }
231
232 fn paint(&self, app: &mut App, canvas: &mut Canvas, size: Size) {
233 let border_value = self.border.evaluate(app, self.border_animation.handle());
234 let canvas_rect = Offset::ZERO & size;
235 let blended_fill_color = self.blended_color(app);
236 #[allow(deprecated)]
238 let alpha = blended_fill_color.alpha();
239 if alpha > 0 {
240 let paint = Paint {
241 color: blended_fill_color.into(),
242 style: PaintStyle::Fill,
243 ..Paint::default()
244 };
245 if border_value.prefer_paint_interior() {
246 border_value.paint_interior(canvas, canvas_rect, &paint, Some(self.text_direction));
247 } else {
248 let path = border_value.get_outer_path(canvas_rect, Some(self.text_direction));
249 canvas.draw_path(path.as_valo(), path.fill_type, &paint);
250 }
251 }
252
253 InputBorder::paint(
254 &*border_value,
255 canvas,
256 canvas_rect,
257 self.gap.start(app),
258 self.gap.extent(app),
259 self.gap_animation.value(app),
260 Some(self.text_direction),
261 );
262 }
263
264 fn should_repaint(&self, _app: &App, old_delegate: &dyn CustomPainter) -> bool {
265 let Some(old_painter) = old_delegate.as_any().downcast_ref::<InputBorderPainter>() else {
266 return true;
267 };
268 self.border_animation != old_painter.border_animation
269 || self.hover_animation != old_painter.hover_animation
270 || self.gap_animation != old_painter.gap_animation
271 || !Rc::ptr_eq(&self.border, &old_painter.border)
272 || self.gap != old_painter.gap
273 || self.text_direction != old_painter.text_direction
274 }
275
276 fn as_any(&self) -> &dyn Any {
277 self
278 }
279}
280
281impl Debug for InputBorderPainter {
282 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
283 f.write_str("_InputBorderPainter")
284 }
285}
286
287#[derive(Debug)]
292struct BorderContainer {
293 border: InputBorderRef,
294 gap: Ref<InputBorderGap>,
295 gap_animation: AnimationRef<f64>,
296 fill_color: Color,
297 hover_color: Color,
298 is_hovering: bool,
299}
300
301impl StatefulWidget for BorderContainer {
302 type State = BorderContainerState;
303
304 fn key(&self) -> Option<&KeyRef> {
305 None
306 }
307
308 fn create_state(&self) -> BorderContainerState {
309 BorderContainerState {
310 state: StateData::new(),
311 ticker_provider: TickerProviderStateMixinData::new(),
312 controller: None,
313 hover_color_controller: None,
314 border_animation: None,
315 border: None,
316 hover_animation: None,
317 hover_color_tween: None,
318 }
319 }
320}
321
322struct BorderContainerState {
325 state: StateData<BorderContainer>,
326 ticker_provider: TickerProviderStateMixinData,
327 controller: Option<Ref<AnimationController>>,
328 hover_color_controller: Option<Ref<AnimationController>>,
329 border_animation: Option<Ref<CurvedAnimation>>,
330 border: Option<Rc<InputBorderTween>>,
331 hover_animation: Option<Ref<CurvedAnimation>>,
332 hover_color_tween: Option<Ref<ColorTween>>,
333}
334
335impl BorderContainerState {
336 const K_HOVER_DURATION: Duration = Duration::from_millis(15);
337
338 fn controller(self: Handle<Self>, app: &App) -> Ref<AnimationController> {
339 app.get(self)
340 .controller
341 .clone()
342 .expect("created in init_state")
343 }
344
345 fn hover_color_controller(self: Handle<Self>, app: &App) -> Ref<AnimationController> {
346 app.get(self)
347 .hover_color_controller
348 .clone()
349 .expect("created in init_state")
350 }
351
352 fn border_animation(self: Handle<Self>, app: &App) -> Ref<CurvedAnimation> {
353 app.get(self)
354 .border_animation
355 .clone()
356 .expect("created in init_state")
357 }
358
359 fn hover_animation(self: Handle<Self>, app: &App) -> Ref<CurvedAnimation> {
360 app.get(self)
361 .hover_animation
362 .clone()
363 .expect("created in init_state")
364 }
365
366 fn hover_color_tween(self: Handle<Self>, app: &App) -> Ref<ColorTween> {
367 app.get(self)
368 .hover_color_tween
369 .clone()
370 .expect("created in init_state")
371 }
372
373 fn new_hover_color_tween(app: &mut App, hover_color: Color) -> Ref<ColorTween> {
376 ColorTween::new(app, Some(hover_color.with_alpha(0)), Some(hover_color))
377 }
378}
379
380impl TickerProviderStateMixin for BorderContainerState {
381 fn ticker_provider_data(self: Handle<Self>, app: &App) -> &TickerProviderStateMixinData {
382 &app.get(self).ticker_provider
383 }
384
385 fn ticker_provider_data_mut(
386 self: Handle<Self>,
387 app: &mut App,
388 ) -> &mut TickerProviderStateMixinData {
389 &mut app.get_mut(self).ticker_provider
390 }
391}
392
393impl TickerProviderObject for BorderContainerState {
395 fn create_ticker(self: Handle<Self>, app: &mut App, on_tick: TickerCallback) -> Ref<Ticker> {
396 TickerProviderStateMixin::create_ticker(self, app, on_tick)
397 }
398}
399
400impl State for BorderContainerState {
401 type Widget = BorderContainer;
402 waft_widgets::state_accessors!();
403
404 fn init_state(self: Handle<Self>, app: &mut App) {
405 let widget = self.widget(app);
406 let is_hovering = widget.is_hovering;
407 let border = widget.border.clone();
408 let hover_color = widget.hover_color;
409 let hover_color_controller = AnimationController::new(
410 app,
411 Some(if is_hovering { 1.0 } else { 0.0 }),
412 Some(BorderContainerState::K_HOVER_DURATION),
413 None,
414 0.0,
415 1.0,
416 AnimationBehavior::Normal,
417 self,
418 );
419 let controller = AnimationController::new(
420 app,
421 None,
422 Some(K_TRANSITION_DURATION),
423 None,
424 0.0,
425 1.0,
426 AnimationBehavior::Normal,
427 self,
428 );
429 let border_animation = CurvedAnimation::new(
430 app,
431 controller.erase(),
432 k_transition_curve(),
433 Some(Rc::new(k_transition_curve().flipped())),
434 );
435 let border_tween = Rc::new(InputBorderTween::new(border.clone(), border));
436 let hover_animation =
437 CurvedAnimation::new(app, hover_color_controller.erase(), Curves::linear(), None);
438
439 let hover_color_tween = BorderContainerState::new_hover_color_tween(app, hover_color);
441 let state = app.get_mut(self);
442 state.hover_color_controller = Some(hover_color_controller);
443 state.controller = Some(controller);
444 state.border_animation = Some(border_animation);
445 state.border = Some(border_tween);
446 state.hover_animation = Some(hover_animation);
447 state.hover_color_tween = Some(hover_color_tween);
448 }
449
450 fn activate(self: Handle<Self>, app: &mut App) {
451 TickerProviderStateMixin::activate(self, app);
452 }
453
454 fn dispose(self: Handle<Self>, app: &mut App) {
455 let controller = self.controller(app);
456 let hover_color_controller = self.hover_color_controller(app);
457 let border_animation = self.border_animation(app);
458 let hover_animation = self.hover_animation(app);
459 controller.dispose(app);
460 hover_color_controller.dispose(app);
461 border_animation.dispose(app);
462 hover_animation.dispose(app);
463 TickerProviderStateMixin::dispose(self, app);
464 }
465
466 fn did_update_widget(self: Handle<Self>, app: &mut App, old_widget: &BorderContainer) {
467 let widget = self.widget(app);
468 let border = widget.border.clone();
469 let hover_color = widget.hover_color;
470 let is_hovering = widget.is_hovering;
471 if *border != *old_widget.border {
472 app.get_mut(self).border = Some(Rc::new(InputBorderTween::new(
473 old_widget.border.clone(),
474 border,
475 )));
476 let controller = self.controller(app);
477 controller.set_value(app, 0.0);
478 controller.forward(app, None);
479 }
480 if hover_color != old_widget.hover_color {
481 let hover_color_tween = BorderContainerState::new_hover_color_tween(app, hover_color);
483 app.get_mut(self).hover_color_tween = Some(hover_color_tween);
484 }
485 if is_hovering != old_widget.is_hovering {
486 let hover_color_controller = self.hover_color_controller(app);
487 if is_hovering {
488 hover_color_controller.forward(app, None);
489 } else {
490 hover_color_controller.reverse(app, None);
491 }
492 }
493 }
494
495 fn build(self: Handle<Self>, app: &mut App, context: BuildContext) -> WidgetRef {
496 let border_animation: AnimationRef<f64> = self.border_animation(app).erase();
497 let hover_color_controller = self.hover_color_controller(app);
498 let widget = self.widget(app);
499 let gap = widget.gap.clone();
500 let gap_animation = widget.gap_animation.clone();
501 let fill_color = widget.fill_color;
502 let border = app.get(self).border.clone().expect("created in init_state");
503 let hover_color_tween = self.hover_color_tween(app);
504 let hover_animation: AnimationRef<f64> = self.hover_animation(app).erase();
505 let text_direction = Directionality::of(app, context);
506 let repaint: Rc<dyn Listenable> = Rc::new(MergingListenable::new(vec![
507 Some(Rc::new(border_animation.clone()) as Rc<dyn Listenable>),
508 Some(Rc::new(gap.clone()) as Rc<dyn Listenable>),
509 Some(Rc::new(hover_color_controller) as Rc<dyn Listenable>),
510 ]));
511 CustomPaint::new()
512 .foreground_painter(InputBorderPainter {
513 repaint,
514 border_animation,
515 border,
516 gap_animation,
517 gap,
518 text_direction,
519 fill_color,
520 hover_color_tween,
521 hover_animation,
522 })
523 .into_widget()
524 }
525}
526
527impl Debug for BorderContainerState {
528 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
529 f.debug_struct("_BorderContainerState")
530 .field("controller", &self.controller)
531 .finish_non_exhaustive()
532 }
533}
534
535#[derive(Debug)]
539struct HelperError {
540 text_align: Option<TextAlign>,
541 helper: Option<WidgetRef>,
542 helper_text: Option<String>,
543 helper_style: Option<TextStyle>,
544 helper_max_lines: Option<i32>,
545 error: Option<WidgetRef>,
546 error_text: Option<String>,
547 error_style: Option<TextStyle>,
548 error_max_lines: Option<i32>,
549}
550
551impl StatefulWidget for HelperError {
552 type State = HelperErrorState;
553
554 fn key(&self) -> Option<&KeyRef> {
555 None
556 }
557
558 fn create_state(&self) -> HelperErrorState {
559 HelperErrorState {
560 state: StateData::new(),
561 single_ticker_provider: SingleTickerProviderStateMixinData::default(),
562 controller: None,
563 helper: None,
564 error: None,
565 }
566 }
567}
568
569struct HelperErrorState {
571 state: StateData<HelperError>,
572 single_ticker_provider: SingleTickerProviderStateMixinData,
573 controller: Option<Ref<AnimationController>>,
575 helper: Option<WidgetRef>,
576 error: Option<WidgetRef>,
577}
578
579impl HelperErrorState {
580 fn controller(self: Handle<Self>, app: &App) -> Ref<AnimationController> {
581 app.get(self)
582 .controller
583 .clone()
584 .expect("created in init_state")
585 }
586
587 fn fade_out(self: Handle<Self>, app: &mut App) -> AnimationRef<f64> {
589 let controller = self.controller(app);
590 let tween = Tween::new(app, Some(1.0), Some(0.0));
591 controller.drive(app, tween)
592 }
593
594 fn has_helper(self: Handle<Self>, app: &App) -> bool {
595 let widget = self.widget(app);
596 widget.helper_text.is_some() || widget.helper.is_some()
597 }
598
599 fn has_error(self: Handle<Self>, app: &App) -> bool {
600 let widget = self.widget(app);
601 widget.error_text.is_some() || widget.error.is_some()
602 }
603
604 fn handle_change(self: Handle<Self>, app: &mut App) {
605 self.set_state(app, |_state| {
606 });
608 }
609
610 fn build_helper(self: Handle<Self>, app: &mut App) -> WidgetRef {
611 debug_assert!(self.widget(app).helper.is_some() || self.widget(app).helper_text.is_some());
612 let fade_out = self.fade_out(app);
613 let widget = self.widget(app);
614 let child = match &widget.helper {
615 Some(helper) => helper.clone(),
616 None => {
617 let mut text = Text::new(
618 widget
619 .helper_text
620 .clone()
621 .expect("a helper or its text is given"),
622 )
623 .overflow(TextOverflow::Ellipsis);
624 if let Some(style) = &widget.helper_style {
625 text = text.style(style.clone());
626 }
627 if let Some(text_align) = widget.text_align {
628 text = text.text_align(text_align);
629 }
630 if let Some(max_lines) = widget.helper_max_lines {
631 text = text.max_lines(max_lines);
632 }
633 text.into_widget()
634 }
635 };
636 FadeTransition::new(fade_out).child(child).into_widget()
637 }
638
639 fn build_error(self: Handle<Self>, app: &mut App) -> WidgetRef {
640 debug_assert!(self.widget(app).error.is_some() || self.widget(app).error_text.is_some());
641 let controller = self.controller(app);
642 let widget = self.widget(app);
643 let captured_error_text = widget.error_text.clone();
644 let mut captured_error = widget.error.clone();
645 if let (Some(error), Some(error_style)) = (&captured_error, &widget.error_style) {
646 captured_error =
647 Some(DefaultTextStyle::new(error_style.clone(), error.clone()).into_widget());
648 }
649 let this = self;
650 Builder::new(move |app, context| {
651 MediaQuery::supports_announce_of(app, context);
654 let child = match &captured_error {
655 Some(error) => error.clone(),
656 None => {
657 let widget = this.widget(app);
658 let error_style = widget.error_style.clone();
659 let text_align = widget.text_align;
660 let error_max_lines = widget.error_max_lines;
661 let mut text = Text::new(
662 captured_error_text
663 .clone()
664 .expect("an error or its text is given"),
665 )
666 .overflow(TextOverflow::Ellipsis);
667 if let Some(style) = &error_style {
668 text = text.style(style.clone());
669 }
670 if let Some(text_align) = text_align {
671 text = text.text_align(text_align);
672 }
673 if let Some(max_lines) = error_max_lines {
674 text = text.max_lines(max_lines);
675 }
676 text.into_widget()
677 }
678 };
679 let begin = Offset::new(0.0, -0.25);
682 let translation = begin + (Offset::ZERO - begin) * controller.value(app);
683 FadeTransition::new(controller.erase())
684 .child(FractionalTranslation::new(translation).child(child))
685 .into_widget()
686 })
687 .into_widget()
688 }
689}
690
691impl SingleTickerProviderStateMixin for HelperErrorState {
692 fn single_ticker_provider_data(
693 self: Handle<Self>,
694 app: &App,
695 ) -> &SingleTickerProviderStateMixinData {
696 &app.get(self).single_ticker_provider
697 }
698
699 fn single_ticker_provider_data_mut(
700 self: Handle<Self>,
701 app: &mut App,
702 ) -> &mut SingleTickerProviderStateMixinData {
703 &mut app.get_mut(self).single_ticker_provider
704 }
705}
706
707impl TickerProviderObject for HelperErrorState {
709 fn create_ticker(self: Handle<Self>, app: &mut App, on_tick: TickerCallback) -> Ref<Ticker> {
710 SingleTickerProviderStateMixin::create_ticker(self, app, on_tick)
711 }
712}
713
714impl State for HelperErrorState {
715 type Widget = HelperError;
716 waft_widgets::state_accessors!();
717
718 fn init_state(self: Handle<Self>, app: &mut App) {
719 let controller = AnimationController::new(
720 app,
721 None,
722 Some(K_TRANSITION_DURATION),
723 None,
724 0.0,
725 1.0,
726 AnimationBehavior::Normal,
727 self,
728 );
729 app.get_mut(self).controller = Some(controller.clone());
730 if self.has_error(app) {
731 let error = self.build_error(app);
732 app.get_mut(self).error = Some(error);
733 controller.set_value(app, 1.0);
734 } else if self.has_helper(app) {
735 let helper = self.build_helper(app);
736 app.get_mut(self).helper = Some(helper);
737 }
738 controller.add_listener(app, Listener::handle_method(self, Self::handle_change));
739 }
740
741 fn activate(self: Handle<Self>, app: &mut App) {
742 SingleTickerProviderStateMixin::activate(self, app);
743 }
744
745 fn dispose(self: Handle<Self>, app: &mut App) {
746 let controller = self.controller(app);
747 controller.dispose(app);
748 SingleTickerProviderStateMixin::dispose(self, app);
749 }
750
751 fn did_update_widget(self: Handle<Self>, app: &mut App, old: &HelperError) {
752 let widget = self.widget(app);
753 let new_error = widget.error.is_some();
754 let new_error_text = widget.error_text.is_some();
755 let new_helper = widget.helper.is_some();
756 let new_helper_text = widget.helper_text.is_some();
757 let old_error = old.error.is_some();
758 let old_error_text = old.error_text.is_some();
759 let old_helper = old.helper.is_some();
760 let old_helper_text = old.helper_text.is_some();
761
762 let error_state_changed = new_error != old_error;
763 let error_text_state_changed = new_error_text != old_error_text;
764 let helper_state_changed = new_helper != old_helper;
765 let helper_text_state_changed = !new_error_text && new_helper_text != old_helper_text;
766
767 if error_state_changed
768 || error_text_state_changed
769 || helper_state_changed
770 || helper_text_state_changed
771 {
772 let controller = self.controller(app);
773 if new_error || new_error_text {
774 let error = self.build_error(app);
775 app.get_mut(self).error = Some(error);
776 controller.forward(app, None);
777 } else if new_helper || new_helper_text {
778 let helper = self.build_helper(app);
779 app.get_mut(self).helper = Some(helper);
780 controller.reverse(app, None);
781 } else {
782 controller.reverse(app, None);
783 }
784 }
785 }
786
787 fn build(self: Handle<Self>, app: &mut App, _context: BuildContext) -> WidgetRef {
788 let empty = || SizedBox::shrink().into_widget();
791
792 let controller = self.controller(app);
793 if controller.is_dismissed(app) {
794 app.get_mut(self).error = None;
795 if self.has_helper(app) {
796 let helper = self.build_helper(app);
797 app.get_mut(self).helper = Some(helper.clone());
798 return helper;
799 } else {
800 app.get_mut(self).helper = None;
801 return empty();
802 }
803 }
804
805 if controller.is_completed(app) {
806 app.get_mut(self).helper = None;
807 if self.has_error(app) {
808 let error = self.build_error(app);
809 app.get_mut(self).error = Some(error.clone());
810 return error;
811 } else {
812 app.get_mut(self).error = None;
813 return empty();
814 }
815 }
816
817 if app.get(self).helper.is_none() && self.has_error(app) {
818 return self.build_error(app);
819 }
820
821 if app.get(self).error.is_none() && self.has_helper(app) {
822 return self.build_helper(app);
823 }
824
825 if self.has_error(app) {
826 let fade_out = self.fade_out(app);
827 let mut helper_transition = FadeTransition::new(fade_out);
828 if let Some(helper) = app.get(self).helper.clone() {
829 helper_transition = helper_transition.child(helper);
830 }
831 let error = self.build_error(app);
832 return Stack::new()
833 .children([helper_transition.into_widget(), error])
834 .into_widget();
835 }
836
837 if self.has_helper(app) {
838 let helper = self.build_helper(app);
839 let mut error_transition = FadeTransition::new(controller.erase());
840 if let Some(error) = app.get(self).error.clone() {
841 error_transition = error_transition.child(error);
842 }
843 return Stack::new()
844 .children([helper, error_transition.into_widget()])
845 .into_widget();
846 }
847
848 empty()
849 }
850}
851
852impl Debug for HelperErrorState {
853 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
854 f.debug_struct("_HelperErrorState")
855 .field("controller", &self.controller)
856 .finish_non_exhaustive()
857 }
858}
859
860#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
869pub enum FloatingLabelBehavior {
870 Never,
872
873 Auto,
875
876 Always,
878}
879
880#[derive(Clone, Copy, PartialEq)]
890pub struct FloatingLabelAlignment {
891 x: f64,
893}
894
895impl FloatingLabelAlignment {
896 const fn private(x: f64) -> FloatingLabelAlignment {
897 assert!(x >= -1.0 && x <= 1.0);
898 FloatingLabelAlignment { x }
899 }
900
901 pub const START: FloatingLabelAlignment = FloatingLabelAlignment::private(-1.0);
907
908 pub const CENTER: FloatingLabelAlignment = FloatingLabelAlignment::private(0.0);
910
911 fn stringify(x: f64) -> String {
912 if x == -1.0 {
913 "FloatingLabelAlignment.start".to_owned()
914 } else if x == 0.0 {
915 "FloatingLabelAlignment.center".to_owned()
916 } else {
917 format!("FloatingLabelAlignment(x: {x:.1})")
918 }
919 }
920}
921
922impl Debug for FloatingLabelAlignment {
923 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
924 f.write_str(&FloatingLabelAlignment::stringify(self.x))
925 }
926}
927
928#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
930enum DecorationSlot {
931 Icon,
932 Input,
933 Label,
934 Hint,
935 Prefix,
936 Suffix,
937 PrefixIcon,
938 SuffixIcon,
939 HelperError,
940 Counter,
941 Container,
942}
943
944impl DecorationSlot {
945 const VALUES: [DecorationSlot; 11] = [
947 DecorationSlot::Icon,
948 DecorationSlot::Input,
949 DecorationSlot::Label,
950 DecorationSlot::Hint,
951 DecorationSlot::Prefix,
952 DecorationSlot::Suffix,
953 DecorationSlot::PrefixIcon,
954 DecorationSlot::SuffixIcon,
955 DecorationSlot::HelperError,
956 DecorationSlot::Counter,
957 DecorationSlot::Container,
958 ];
959}
960
961fn same_widget(a: &Option<WidgetRef>, b: &Option<WidgetRef>) -> bool {
963 match (a, b) {
964 (None, None) => true,
965 (Some(a), Some(b)) => Rc::ptr_eq(a, b),
966 _ => false,
967 }
968}
969
970#[derive(Clone, Debug)]
972struct Decoration {
973 content_padding: EdgeInsetsDirectional,
974 is_collapsed: bool,
975 floating_label_height: f64,
976 floating_label_progress: f64,
977 floating_label_alignment: FloatingLabelAlignment,
978 border: InputBorderRef,
979 border_gap: Ref<InputBorderGap>,
980 align_label_with_hint: bool,
981 is_dense: Option<bool>,
982 is_empty: bool,
983 visual_density: VisualDensity,
984 input_gap: f64,
985 maintain_hint_size: bool,
986 maintain_label_size: bool,
987 icon: Option<WidgetRef>,
988 input: Option<WidgetRef>,
989 label: Option<WidgetRef>,
990 hint: Option<WidgetRef>,
991 prefix: Option<WidgetRef>,
992 suffix: Option<WidgetRef>,
993 prefix_icon: Option<WidgetRef>,
994 suffix_icon: Option<WidgetRef>,
995 helper_error: Option<WidgetRef>,
996 counter: Option<WidgetRef>,
997 container: Option<WidgetRef>,
998 supporting_text_padding: Option<EdgeInsetsDirectional>,
999}
1000
1001impl PartialEq for Decoration {
1002 fn eq(&self, other: &Decoration) -> bool {
1004 other.content_padding == self.content_padding
1005 && other.is_collapsed == self.is_collapsed
1006 && other.floating_label_height == self.floating_label_height
1007 && other.floating_label_progress == self.floating_label_progress
1008 && other.floating_label_alignment == self.floating_label_alignment
1009 && *other.border == *self.border
1010 && other.border_gap == self.border_gap
1011 && other.align_label_with_hint == self.align_label_with_hint
1012 && other.is_dense == self.is_dense
1013 && other.is_empty == self.is_empty
1014 && other.visual_density == self.visual_density
1015 && other.input_gap == self.input_gap
1016 && other.maintain_hint_size == self.maintain_hint_size
1017 && other.maintain_label_size == self.maintain_label_size
1018 && same_widget(&other.icon, &self.icon)
1019 && same_widget(&other.input, &self.input)
1020 && same_widget(&other.label, &self.label)
1021 && same_widget(&other.hint, &self.hint)
1022 && same_widget(&other.prefix, &self.prefix)
1023 && same_widget(&other.suffix, &self.suffix)
1024 && same_widget(&other.prefix_icon, &self.prefix_icon)
1025 && same_widget(&other.suffix_icon, &self.suffix_icon)
1026 && same_widget(&other.helper_error, &self.helper_error)
1027 && same_widget(&other.counter, &self.counter)
1028 && same_widget(&other.container, &self.container)
1029 && other.supporting_text_padding == self.supporting_text_padding
1030 }
1031}
1032
1033#[derive(Clone, Copy, Debug)]
1037struct RenderDecorationLayout {
1038 input_constraints: BoxConstraints,
1039 baseline: f64,
1040 container_height: f64,
1041 supporting_text_size: Option<SupportingTextSize>,
1042 size: Size,
1043}
1044
1045struct RenderDecoration {
1047 render_object: RenderObjectData,
1048 render_box: RenderBoxData,
1049 slotted: SlottedContainerRenderObjectData<DecorationSlot, AnyRenderBox>,
1050 decoration: Decoration,
1051 text_direction: TextDirection,
1052 text_baseline: TextBaseline,
1053 text_align_vertical: Option<TextAlignVertical>,
1054 is_focused: bool,
1055 expands: bool,
1056 material3: bool,
1057 label_transform: Option<Matrix4>,
1059}
1060
1061macro_rules! render_decoration_property {
1065 ($($field:ident, $setter:ident: $ty:ty;)*) => {
1066 $(
1067 #[allow(dead_code)]
1068 fn $field(self: RenderHandle<Self>, app: &App) -> $ty {
1069 self.get(app).$field
1070 }
1071
1072 #[allow(dead_code)]
1073 fn $setter(self: RenderHandle<Self>, app: &mut App, value: $ty) {
1074 if self.get(app).$field == value {
1075 return;
1076 }
1077 self.get_mut(app).$field = value;
1078 self.mark_needs_layout(app);
1079 }
1080 )*
1081 };
1082}
1083
1084impl RenderDecoration {
1085 #[allow(clippy::too_many_arguments)]
1086 fn new(
1087 app: &mut App,
1088 decoration: Decoration,
1089 text_direction: TextDirection,
1090 text_baseline: TextBaseline,
1091 is_focused: bool,
1092 expands: bool,
1093 material3: bool,
1094 text_align_vertical: Option<TextAlignVertical>,
1095 ) -> RenderHandle<RenderDecoration> {
1096 RenderHandle::new_box(
1097 app,
1098 RenderDecoration {
1099 render_object: RenderObjectData::new(),
1100 render_box: RenderBoxData::new(),
1101 slotted: SlottedContainerRenderObjectData::new(),
1102 decoration,
1103 text_direction,
1104 text_baseline,
1105 text_align_vertical,
1106 is_focused,
1107 expands,
1108 material3,
1109 label_transform: None,
1110 },
1111 )
1112 }
1113
1114 fn supporting_text_gap(self: RenderHandle<Self>, app: &App) -> f64 {
1117 if self.material3(app) { 4.0 } else { 8.0 }
1118 }
1119
1120 fn prefix_to_input_gap(self: RenderHandle<Self>, app: &App) -> f64 {
1121 if self.material3(app) { 4.0 } else { 0.0 }
1122 }
1123
1124 fn input_to_suffix_gap(self: RenderHandle<Self>, app: &App) -> f64 {
1125 if self.material3(app) { 4.0 } else { 0.0 }
1126 }
1127
1128 fn icon(self: RenderHandle<Self>, app: &App) -> Option<AnyRenderBox> {
1129 self.child_for_slot(app, &DecorationSlot::Icon)
1130 }
1131
1132 fn input(self: RenderHandle<Self>, app: &App) -> Option<AnyRenderBox> {
1133 self.child_for_slot(app, &DecorationSlot::Input)
1134 }
1135
1136 fn label(self: RenderHandle<Self>, app: &App) -> Option<AnyRenderBox> {
1137 self.child_for_slot(app, &DecorationSlot::Label)
1138 }
1139
1140 fn hint(self: RenderHandle<Self>, app: &App) -> Option<AnyRenderBox> {
1141 self.child_for_slot(app, &DecorationSlot::Hint)
1142 }
1143
1144 fn prefix(self: RenderHandle<Self>, app: &App) -> Option<AnyRenderBox> {
1145 self.child_for_slot(app, &DecorationSlot::Prefix)
1146 }
1147
1148 fn suffix(self: RenderHandle<Self>, app: &App) -> Option<AnyRenderBox> {
1149 self.child_for_slot(app, &DecorationSlot::Suffix)
1150 }
1151
1152 fn prefix_icon(self: RenderHandle<Self>, app: &App) -> Option<AnyRenderBox> {
1153 self.child_for_slot(app, &DecorationSlot::PrefixIcon)
1154 }
1155
1156 fn suffix_icon(self: RenderHandle<Self>, app: &App) -> Option<AnyRenderBox> {
1157 self.child_for_slot(app, &DecorationSlot::SuffixIcon)
1158 }
1159
1160 fn helper_error(self: RenderHandle<Self>, app: &App) -> AnyRenderBox {
1161 self.child_for_slot(app, &DecorationSlot::HelperError)
1162 .expect("an input decorator always has a helper and error child")
1163 }
1164
1165 fn counter(self: RenderHandle<Self>, app: &App) -> Option<AnyRenderBox> {
1166 self.child_for_slot(app, &DecorationSlot::Counter)
1167 }
1168
1169 fn container(self: RenderHandle<Self>, app: &App) -> Option<AnyRenderBox> {
1171 self.child_for_slot(app, &DecorationSlot::Container)
1172 }
1173
1174 fn decoration(self: RenderHandle<Self>, app: &App) -> &Decoration {
1175 &self.get(app).decoration
1176 }
1177
1178 fn set_decoration(self: RenderHandle<Self>, app: &mut App, value: Decoration) {
1179 if self.get(app).decoration == value {
1180 return;
1181 }
1182 self.get_mut(app).decoration = value;
1183 self.mark_needs_layout(app);
1184 }
1185
1186 render_decoration_property! {
1187 text_direction, set_text_direction: TextDirection;
1188 text_baseline, set_text_baseline: TextBaseline;
1189 expands, set_expands: bool;
1190 material3, set_material3: bool;
1191 }
1192
1193 fn default_text_align_vertical(self: RenderHandle<Self>, app: &App) -> TextAlignVertical {
1194 if self.is_outline_aligned(app) {
1195 TextAlignVertical::CENTER
1196 } else {
1197 TextAlignVertical::TOP
1198 }
1199 }
1200
1201 fn text_align_vertical(self: RenderHandle<Self>, app: &App) -> TextAlignVertical {
1202 self.get(app)
1203 .text_align_vertical
1204 .unwrap_or_else(|| self.default_text_align_vertical(app))
1205 }
1206
1207 fn set_text_align_vertical(
1208 self: RenderHandle<Self>,
1209 app: &mut App,
1210 value: Option<TextAlignVertical>,
1211 ) {
1212 if self.get(app).text_align_vertical.map(|value| value.y) == value.map(|value| value.y) {
1213 return;
1214 }
1215 if self.text_align_vertical(app).y
1217 == value.map_or_else(|| self.default_text_align_vertical(app).y, |value| value.y)
1218 {
1219 self.get_mut(app).text_align_vertical = value;
1220 return;
1221 }
1222 self.get_mut(app).text_align_vertical = value;
1223 self.mark_needs_layout(app);
1224 }
1225
1226 fn set_is_focused(self: RenderHandle<Self>, app: &mut App, value: bool) {
1227 if self.get(app).is_focused == value {
1228 return;
1229 }
1230 self.get_mut(app).is_focused = value;
1231 }
1232
1233 fn is_outline_aligned(self: RenderHandle<Self>, app: &App) -> bool {
1236 let decoration = self.decoration(app);
1237 !decoration.is_collapsed && decoration.border.is_outline()
1238 }
1239
1240 fn density_offset(self: RenderHandle<Self>, app: &App) -> Offset {
1241 self.decoration(app).visual_density.base_size_adjustment()
1242 }
1243
1244 fn min_width(app: &mut App, r#box: Option<AnyRenderBox>, height: f64) -> f64 {
1245 r#box.map_or(0.0, |r#box| r#box.get_min_intrinsic_width(app, height))
1246 }
1247
1248 fn max_width(app: &mut App, r#box: Option<AnyRenderBox>, height: f64) -> f64 {
1249 r#box.map_or(0.0, |r#box| r#box.get_max_intrinsic_width(app, height))
1250 }
1251
1252 fn min_height(app: &mut App, r#box: Option<AnyRenderBox>, width: f64) -> f64 {
1253 r#box.map_or(0.0, |r#box| r#box.get_min_intrinsic_height(app, width))
1254 }
1255
1256 fn box_size(app: &App, r#box: Option<AnyRenderBox>) -> Size {
1257 r#box.map_or(Size::ZERO, |r#box| r#box.size(app))
1258 }
1259
1260 fn get_baseline(app: &mut App, r#box: AnyRenderBox, box_constraints: BoxConstraints) -> f64 {
1261 ChildLayoutHelper::get_baseline(app, r#box, box_constraints, TextBaseline::Alphabetic)
1262 .unwrap_or_else(|| r#box.size(app).height())
1263 }
1264
1265 fn get_dry_baseline(
1266 app: &mut App,
1267 r#box: AnyRenderBox,
1268 box_constraints: BoxConstraints,
1269 ) -> f64 {
1270 ChildLayoutHelper::get_dry_baseline(app, r#box, box_constraints, TextBaseline::Alphabetic)
1271 .unwrap_or_else(|| {
1272 ChildLayoutHelper::dry_layout_child(app, r#box, box_constraints).height()
1273 })
1274 }
1275
1276 fn box_parent_data(app: &App, r#box: AnyRenderBox) -> &BoxParentData {
1277 r#box.box_parent_data(app)
1278 }
1279
1280 fn set_offset(app: &mut App, r#box: AnyRenderBox, offset: Offset) {
1281 r#box.parent_data_of_mut::<BoxParentData>(app).offset = offset;
1282 }
1283
1284 fn content_padding(self: RenderHandle<Self>, app: &App) -> EdgeInsetsDirectional {
1285 self.decoration(app).content_padding
1286 }
1287
1288 fn supporting_text_padding(
1289 self: RenderHandle<Self>,
1290 app: &App,
1291 ) -> Option<EdgeInsetsDirectional> {
1292 self.decoration(app).supporting_text_padding
1293 }
1294
1295 fn compute_supporting_text_sizes(
1296 self: RenderHandle<Self>,
1297 app: &mut App,
1298 constraints: BoxConstraints,
1299 layout_child: ChildLayouter,
1300 get_baseline: ChildBaselineGetter,
1301 ) -> Option<SupportingTextSize> {
1302 let counter = self.counter(app);
1303 let (counter_size, counter_ascent) = match counter {
1304 Some(r#box) => (
1305 layout_child(app, r#box, constraints),
1306 get_baseline(app, r#box, constraints),
1307 ),
1308 None => (Size::ZERO, 0.0),
1309 };
1310
1311 let counter_padding = if counter.is_some() {
1313 K_SUPPORTING_TEXT_COUNTER_PADDING
1314 } else {
1315 0.0
1316 };
1317 let helper_error_constraints = constraints.deflate(EdgeInsetsGeometry::only(
1318 counter_size.width() + counter_padding,
1319 0.0,
1320 0.0,
1321 0.0,
1322 ));
1323 let helper_error = self.helper_error(app);
1324 let helper_error_height =
1325 layout_child(app, helper_error, helper_error_constraints).height();
1326
1327 if helper_error_height == 0.0 && counter_size.height() == 0.0 {
1328 return None;
1329 }
1330
1331 let supporting_text_padding = self.supporting_text_padding(app);
1338 let top_padding = supporting_text_padding
1339 .map_or_else(|| self.supporting_text_gap(app), |padding| padding.top);
1340 let bottom_padding = supporting_text_padding.map_or(0.0, |padding| padding.bottom);
1341 let ascent = counter_ascent.max(get_baseline(app, helper_error, helper_error_constraints))
1342 + top_padding;
1343 let bottom_height =
1344 counter_ascent.max(helper_error_height) + (top_padding + bottom_padding);
1345 let supporting_text_height =
1346 counter_size.height().max(helper_error_height) + (top_padding + bottom_padding);
1347 Some(SupportingTextSize {
1348 ascent,
1349 bottom_height,
1350 supporting_text_height,
1351 })
1352 }
1353
1354 fn layout(
1358 self: RenderHandle<Self>,
1359 app: &mut App,
1360 constraints: BoxConstraints,
1361 layout_child: ChildLayouter,
1362 get_baseline: ChildBaselineGetter,
1363 ) -> RenderDecorationLayout {
1364 debug_assert!(
1365 constraints.max_width < f64::INFINITY,
1366 "An InputDecorator, which is typically created by a TextField, cannot \
1367 have an unbounded width.\n\
1368 This happens when the parent widget does not provide a finite width \
1369 constraint. For example, if the InputDecorator is contained by a Row, \
1370 then its width must be constrained. An Expanded widget or a SizedBox \
1371 can be used to constrain the width of the InputDecorator or the \
1372 TextField that contains it."
1373 );
1374
1375 let box_constraints = constraints.loosen();
1376 let decoration = self.decoration(app).clone();
1377 let content_padding = self.content_padding(app);
1378 let supporting_text_padding = decoration.supporting_text_padding;
1379
1380 let icon = self.icon(app);
1382 let icon_width = match icon {
1383 None => 0.0,
1384 Some(icon) => layout_child(app, icon, box_constraints).width(),
1385 };
1386 let container_constraints =
1387 box_constraints.deflate(EdgeInsetsGeometry::only(icon_width, 0.0, 0.0, 0.0));
1388 let content_constraints = container_constraints.deflate(EdgeInsetsGeometry::directional(
1389 content_padding.start + decoration.input_gap,
1390 0.0,
1391 content_padding.end + decoration.input_gap,
1392 0.0,
1393 ));
1394 let supporting_text_constraints =
1395 container_constraints.deflate(EdgeInsetsGeometry::directional(
1396 supporting_text_padding.map_or(content_padding.start, |padding| padding.start)
1397 + decoration.input_gap,
1398 0.0,
1399 supporting_text_padding.map_or(content_padding.end, |padding| padding.end)
1400 + decoration.input_gap,
1401 0.0,
1402 ));
1403
1404 let supporting_text_size = self.compute_supporting_text_sizes(
1407 app,
1408 supporting_text_constraints,
1409 layout_child,
1410 get_baseline,
1411 );
1412
1413 let prefix_icon = self.prefix_icon(app);
1414 let suffix_icon = self.suffix_icon(app);
1415 let prefix_icon_size = match prefix_icon {
1416 None => Size::ZERO,
1417 Some(prefix_icon) => layout_child(app, prefix_icon, container_constraints),
1418 };
1419 let suffix_icon_size = match suffix_icon {
1420 None => Size::ZERO,
1421 Some(suffix_icon) => layout_child(app, suffix_icon, container_constraints),
1422 };
1423 let prefix = self.prefix(app);
1424 let suffix = self.suffix(app);
1425 let prefix_size = match prefix {
1426 None => Size::ZERO,
1427 Some(prefix) => layout_child(app, prefix, content_constraints),
1428 };
1429 let suffix_size = match suffix {
1430 None => Size::ZERO,
1431 Some(suffix) => layout_child(app, suffix, content_constraints),
1432 };
1433
1434 let accessory_horizontal_insets = EdgeInsetsDirectional::only(
1435 icon_width
1436 + prefix_size.width()
1437 + if prefix_icon.is_none() {
1438 content_padding.start + decoration.input_gap
1439 } else {
1440 prefix_icon_size.width() + self.prefix_to_input_gap(app)
1441 },
1442 0.0,
1443 suffix_size.width()
1444 + if suffix_icon.is_none() {
1445 content_padding.end + decoration.input_gap
1446 } else {
1447 suffix_icon_size.width() + self.input_to_suffix_gap(app)
1448 },
1449 0.0,
1450 );
1451
1452 let input_width = (constraints.max_width
1453 - (accessory_horizontal_insets.start + accessory_horizontal_insets.end))
1454 .max(0.0);
1455 let label = self.label(app);
1456 let top_height = if let Some(label) = label {
1457 let suffix_icon_space = if decoration.border.is_outline() {
1458 lerp_double(
1459 Some(suffix_icon_size.width()),
1460 Some(content_padding.end),
1461 decoration.floating_label_progress,
1462 )
1463 .expect("both ends are given")
1464 } else {
1465 suffix_icon_size.width()
1466 };
1467 let label_width = (constraints.max_width
1468 - (decoration.input_gap * 2.0
1469 + icon_width
1470 + if prefix_icon.is_none() {
1471 content_padding.start
1472 } else {
1473 prefix_icon_size.width()
1474 }
1475 + if suffix_icon.is_none() {
1476 content_padding.end
1477 } else {
1478 suffix_icon_space
1479 }))
1480 .max(0.0);
1481
1482 let inverted_label_scale = lerp_double(
1484 Some(1.00),
1485 Some(1.0 / K_FINAL_LABEL_SCALE),
1486 decoration.floating_label_progress,
1487 )
1488 .expect("both ends are given");
1489 let label_constraints = BoxConstraints {
1490 max_width: label_width * inverted_label_scale,
1491 ..box_constraints
1492 };
1493 layout_child(app, label, label_constraints);
1494
1495 let label_height = decoration.floating_label_height;
1496 if decoration.border.is_outline() {
1497 (label_height - get_baseline(app, label, label_constraints)).max(0.0)
1498 } else {
1499 label_height
1500 }
1501 } else {
1502 0.0
1503 };
1504
1505 let bottom_height = supporting_text_size.map_or(0.0, |size| size.bottom_height);
1508 let density_offset = self.density_offset(app);
1509 let input_constraints = box_constraints
1510 .deflate(EdgeInsetsGeometry::only(
1511 0.0,
1512 (content_padding.top + content_padding.bottom)
1513 + top_height
1514 + bottom_height
1515 + density_offset.dy(),
1516 0.0,
1517 0.0,
1518 ))
1519 .tighten(Some(input_width), None);
1520
1521 let input = self.input(app);
1522 let hint = self.hint(app);
1523 let input_size = match input {
1524 None => Size::ZERO,
1525 Some(input) => layout_child(app, input, input_constraints),
1526 };
1527 let hint_size = match hint {
1528 None => Size::ZERO,
1529 Some(hint) => layout_child(app, hint, box_constraints.tighten(Some(input_width), None)),
1530 };
1531 let input_baseline = match input {
1532 None => 0.0,
1533 Some(input) => get_baseline(app, input, input_constraints),
1534 };
1535 let hint_baseline = match hint {
1536 None => 0.0,
1537 Some(hint) => get_baseline(app, hint, box_constraints.tighten(Some(input_width), None)),
1538 };
1539
1540 let input_height = (if decoration.is_empty || decoration.maintain_hint_size {
1542 hint_size.height()
1543 } else {
1544 0.0
1545 })
1546 .max(input_size.height());
1547 let input_internal_baseline = input_baseline.max(hint_baseline);
1548
1549 let prefix_baseline = match prefix {
1550 None => 0.0,
1551 Some(prefix) => get_baseline(app, prefix, content_constraints),
1552 };
1553 let suffix_baseline = match suffix {
1554 None => 0.0,
1555 Some(suffix) => get_baseline(app, suffix, content_constraints),
1556 };
1557
1558 let fix_height = prefix_baseline.max(suffix_baseline);
1561 let fix_above_input = (fix_height - input_internal_baseline).max(0.0);
1562 let fix_below_baseline =
1563 (prefix_size.height() - prefix_baseline).max(suffix_size.height() - suffix_baseline);
1564 let fix_below_input =
1568 (fix_below_baseline - (input_height - input_internal_baseline)).max(0.0);
1569
1570 let fix_icon_height = prefix_icon_size.height().max(suffix_icon_size.height());
1572 let content_height = fix_icon_height.max(
1573 top_height
1574 + content_padding.top
1575 + fix_above_input
1576 + input_height
1577 + fix_below_input
1578 + content_padding.bottom
1579 + density_offset.dy(),
1580 );
1581 let expands = self.expands(app);
1582 let min_container_height = if decoration.is_dense.expect("applied from the theme")
1583 || decoration.is_collapsed
1584 || expands
1585 {
1586 input_height
1587 } else {
1588 K_MIN_INTERACTIVE_DIMENSION
1589 };
1590 let max_container_height = (box_constraints.max_height - bottom_height).max(0.0);
1591 let container_height = if expands {
1592 max_container_height
1593 } else {
1594 content_height
1595 .max(min_container_height)
1596 .min(max_container_height)
1597 };
1598
1599 let interactive_adjustment = if min_container_height > content_height {
1602 (min_container_height - content_height) / 2.0
1603 } else {
1604 0.0
1605 };
1606
1607 let overflow = (content_height - max_container_height).max(0.0);
1611 let text_align_vertical = self.text_align_vertical(app);
1614 let text_align_vertical_factor = (text_align_vertical.y + 1.0) / 2.0;
1615 let baseline_adjustment = fix_above_input - overflow * (1.0 - text_align_vertical_factor);
1619
1620 let top_input_baseline = content_padding.top
1622 + top_height
1623 + input_internal_baseline
1624 + baseline_adjustment
1625 + interactive_adjustment
1626 + density_offset.dy() / 2.0;
1627 let max_content_height = container_height
1628 - (content_padding.top + content_padding.bottom)
1629 - top_height
1630 - density_offset.dy();
1631 let alignable_height = fix_above_input + input_height + fix_below_input;
1632 let max_vertical_offset = max_content_height - alignable_height;
1633
1634 let baseline = if self.is_outline_aligned(app) {
1635 let outline_center_baseline = input_internal_baseline
1646 + baseline_adjustment / 2.0
1647 + (container_height - input_height) / 2.0;
1648 let outline_top_baseline = top_input_baseline;
1649 let outline_bottom_baseline = top_input_baseline + max_vertical_offset;
1650 RenderDecoration::interpolate_three(
1651 outline_top_baseline,
1652 outline_center_baseline,
1653 outline_bottom_baseline,
1654 text_align_vertical,
1655 )
1656 } else {
1657 let text_align_vertical_offset = max_vertical_offset * text_align_vertical_factor;
1658 top_input_baseline + text_align_vertical_offset
1659 };
1660
1661 RenderDecorationLayout {
1662 input_constraints,
1663 container_height,
1664 baseline,
1665 supporting_text_size,
1666 size: Size::new(
1667 constraints.max_width,
1668 container_height
1669 + supporting_text_size.map_or(0.0, |size| size.supporting_text_height),
1670 ),
1671 }
1672 }
1673
1674 fn interpolate_three(
1682 begin: f64,
1683 middle: f64,
1684 end: f64,
1685 text_align_vertical: TextAlignVertical,
1686 ) -> f64 {
1687 let basis = if text_align_vertical.y <= 0.0 {
1690 (middle - begin).max(0.0)
1691 } else {
1692 (end - middle).max(0.0)
1693 };
1694 middle + basis * text_align_vertical.y
1695 }
1696
1697 fn line_height(app: &mut App, width: f64, boxes: &[Option<AnyRenderBox>]) -> f64 {
1698 let mut height: f64 = 0.0;
1699 for r#box in boxes {
1700 let Some(r#box) = *r#box else {
1701 continue;
1702 };
1703 height = RenderDecoration::min_height(app, Some(r#box), width).max(height);
1704 }
1705 height
1706 }
1710
1711 fn label_offset(app: &App, label: AnyRenderBox) -> Offset {
1712 RenderDecoration::box_parent_data(app, label).offset
1713 }
1714
1715 fn paint_label(
1716 self: RenderHandle<Self>,
1717 app: &mut App,
1718 context: &mut PaintingContext,
1719 offset: Offset,
1720 ) {
1721 let label = self
1722 .label(app)
1723 .expect("the label is painted only when there is one");
1724 context.paint_child(app, label.as_object(), offset);
1725 }
1726}
1727
1728impl SlottedContainerRenderObjectMixin<DecorationSlot> for RenderDecoration {
1729 type ChildType = AnyRenderBox;
1730
1731 fn slotted_container_data(
1732 self: RenderHandle<Self>,
1733 app: &App,
1734 ) -> &SlottedContainerRenderObjectData<DecorationSlot, AnyRenderBox> {
1735 &self.get(app).slotted
1736 }
1737
1738 fn slotted_container_data_mut(
1739 self: RenderHandle<Self>,
1740 app: &mut App,
1741 ) -> &mut SlottedContainerRenderObjectData<DecorationSlot, AnyRenderBox> {
1742 &mut self.get_mut(app).slotted
1743 }
1744
1745 fn children(self: RenderHandle<Self>, app: &App) -> Vec<AnyRenderBox> {
1747 let helper_error = self.child_for_slot(app, &DecorationSlot::HelperError);
1748 [
1749 self.icon(app),
1750 self.input(app),
1751 self.prefix_icon(app),
1752 self.suffix_icon(app),
1753 self.prefix(app),
1754 self.suffix(app),
1755 self.label(app),
1756 self.hint(app),
1757 helper_error,
1758 self.counter(app),
1759 self.container(app),
1760 ]
1761 .into_iter()
1762 .flatten()
1763 .collect()
1764 }
1765}
1766
1767impl RenderObject for RenderDecoration {
1768 waft_rendering::render_object_accessors!();
1769
1770 fn debug_describe_children(
1771 self: RenderHandle<Self>,
1772 app: &App,
1773 ) -> Vec<Rc<dyn DiagnosticsNode>> {
1774 SlottedContainerRenderObjectMixin::debug_describe_children(self, app)
1775 }
1776
1777 fn did_attach(self: RenderHandle<Self>, app: &mut App, owner: Handle<PipelineOwner>) {
1778 SlottedContainerRenderObjectMixin::did_attach(self, app, owner);
1779 }
1780
1781 fn did_detach(self: RenderHandle<Self>, app: &mut App) {
1782 SlottedContainerRenderObjectMixin::did_detach(self, app);
1783 }
1784
1785 fn redepth_children(self: RenderHandle<Self>, app: &mut App) {
1786 SlottedContainerRenderObjectMixin::redepth_children(self, app);
1787 }
1788
1789 fn visit_children(
1790 self: RenderHandle<Self>,
1791 app: &App,
1792 visitor: &mut dyn FnMut(AnyRenderObject),
1793 ) {
1794 SlottedContainerRenderObjectMixin::visit_children(self, app, visitor);
1795 }
1796
1797 fn sized_by_parent(self: RenderHandle<Self>, _app: &App) -> bool {
1798 false
1799 }
1800
1801 fn perform_layout(self: RenderHandle<Self>, app: &mut App) {
1802 let constraints = self.constraints(app);
1803 self.get_mut(app).label_transform = None;
1804 let layout = self.layout(
1805 app,
1806 constraints,
1807 ChildLayoutHelper::layout_child,
1808 RenderDecoration::get_baseline,
1809 );
1810 let size = constraints.constrain(layout.size);
1811 self.set_size(app, size);
1812 debug_assert!(size.width() == constraints.constrain_width(layout.size.width()));
1813 debug_assert!(size.height() == constraints.constrain_height(layout.size.height()));
1814
1815 let overall_width = layout.size.width();
1816 let text_direction = self.text_direction(app);
1817 let decoration = self.decoration(app).clone();
1818 let content_padding = self.content_padding(app);
1819 let supporting_text_padding = decoration.supporting_text_padding;
1820 let icon = self.icon(app);
1821
1822 let container = self.container(app);
1823 if let Some(container) = container {
1824 let container_constraints = BoxConstraints::tight_for(
1825 Some(overall_width - RenderDecoration::box_size(app, icon).width()),
1826 Some(layout.container_height),
1827 );
1828 container.layout(app, container_constraints, true);
1829 let x = match text_direction {
1830 TextDirection::Rtl => 0.0,
1831 TextDirection::Ltr => RenderDecoration::box_size(app, icon).width(),
1832 };
1833 RenderDecoration::set_offset(app, container, Offset::new(x, 0.0));
1834 }
1835
1836 let height = layout.container_height;
1837 let center_layout = |app: &mut App, r#box: AnyRenderBox, x: f64| -> f64 {
1838 let box_size = r#box.size(app);
1839 RenderDecoration::set_offset(
1840 app,
1841 r#box,
1842 Offset::new(x, (height - box_size.height()) / 2.0),
1843 );
1844 box_size.width()
1845 };
1846
1847 if let Some(icon) = icon {
1848 let x = match text_direction {
1849 TextDirection::Rtl => overall_width - icon.size(app).width(),
1850 TextDirection::Ltr => 0.0,
1851 };
1852 center_layout(app, icon, x);
1853 }
1854
1855 let supporting_text_baseline =
1856 layout.supporting_text_size.map_or(0.0, |size| size.ascent) + layout.container_height;
1857 let counter = self.counter(app);
1858 let helper_error = self.helper_error(app);
1859 let helper_error_baseline = helper_error
1860 .get_distance_to_baseline(app, TextBaseline::Alphabetic, false)
1861 .expect("a baseline that falls back to the height");
1862 let counter_baseline = counter.map_or(0.0, |counter| {
1863 counter
1864 .get_distance_to_baseline(app, TextBaseline::Alphabetic, false)
1865 .expect("a baseline that falls back to the height")
1866 });
1867
1868 let icon_width = RenderDecoration::box_size(app, icon).width();
1869 let mut start;
1870 let mut end;
1871 let start_supporting;
1872 let end_supporting;
1873 match text_direction {
1874 TextDirection::Ltr => {
1875 start = content_padding.start + icon_width;
1876 end = overall_width - content_padding.end;
1877 start_supporting = supporting_text_padding
1878 .map_or(content_padding.start, |padding| padding.start)
1879 + icon_width;
1880 end_supporting = overall_width
1881 - supporting_text_padding.map_or(content_padding.end, |padding| padding.end);
1882 RenderDecoration::set_offset(
1883 app,
1884 helper_error,
1885 Offset::new(
1886 start_supporting + decoration.input_gap,
1887 supporting_text_baseline - helper_error_baseline,
1888 ),
1889 );
1890 if let Some(counter) = counter {
1891 let counter_width = counter.size(app).width();
1892 RenderDecoration::set_offset(
1893 app,
1894 counter,
1895 Offset::new(
1896 end_supporting - counter_width - decoration.input_gap,
1897 supporting_text_baseline - counter_baseline,
1898 ),
1899 );
1900 }
1901 }
1902 TextDirection::Rtl => {
1903 start = overall_width - content_padding.start - icon_width;
1904 end = content_padding.end;
1905 start_supporting = overall_width
1906 - supporting_text_padding
1907 .map_or(content_padding.start, |padding| padding.start)
1908 - icon_width;
1909 end_supporting =
1910 supporting_text_padding.map_or(content_padding.end, |padding| padding.end);
1911 let helper_error_width = helper_error.size(app).width();
1912 RenderDecoration::set_offset(
1913 app,
1914 helper_error,
1915 Offset::new(
1916 start_supporting - helper_error_width - decoration.input_gap,
1917 supporting_text_baseline - helper_error_baseline,
1918 ),
1919 );
1920 if let Some(counter) = counter {
1921 RenderDecoration::set_offset(
1922 app,
1923 counter,
1924 Offset::new(
1925 end_supporting + decoration.input_gap,
1926 supporting_text_baseline - counter_baseline,
1927 ),
1928 );
1929 }
1930 }
1931 }
1932
1933 let baseline = layout.baseline;
1934 let baseline_layout = |app: &mut App, r#box: AnyRenderBox, x: f64| -> f64 {
1935 let distance = r#box
1936 .get_distance_to_baseline(app, TextBaseline::Alphabetic, false)
1937 .expect("a baseline that falls back to the height");
1938 RenderDecoration::set_offset(app, r#box, Offset::new(x, baseline - distance));
1939 r#box.size(app).width()
1940 };
1941
1942 let prefix_icon = self.prefix_icon(app);
1943 let suffix_icon = self.suffix_icon(app);
1944 let label = self.label(app);
1945 let prefix = self.prefix(app);
1946 let suffix = self.suffix(app);
1947 let input = self.input(app);
1948 let hint = self.hint(app);
1949 let prefix_to_input_gap = self.prefix_to_input_gap(app);
1950 let input_to_suffix_gap = self.input_to_suffix_gap(app);
1951 match text_direction {
1952 TextDirection::Rtl => {
1953 if let Some(prefix_icon) = prefix_icon {
1954 start += content_padding.start;
1955 let x = start - prefix_icon.size(app).width();
1956 start -= center_layout(app, prefix_icon, x);
1957 start -= prefix_to_input_gap;
1958 } else {
1959 start -= decoration.input_gap;
1960 }
1961 if let Some(label) = label {
1962 let x = start - label.size(app).width();
1963 if decoration.align_label_with_hint {
1964 baseline_layout(app, label, x);
1965 } else {
1966 center_layout(app, label, x);
1967 }
1968 }
1969 if let Some(prefix) = prefix {
1970 let x = start - prefix.size(app).width();
1971 start -= baseline_layout(app, prefix, x);
1972 }
1973 if let Some(input) = input {
1974 let x = start - input.size(app).width();
1975 baseline_layout(app, input, x);
1976 }
1977 if let Some(hint) = hint {
1978 let x = start - hint.size(app).width();
1979 baseline_layout(app, hint, x);
1980 }
1981 if let Some(suffix_icon) = suffix_icon {
1982 end -= content_padding.end;
1983 end += center_layout(app, suffix_icon, end);
1984 end += input_to_suffix_gap;
1985 } else {
1986 end += decoration.input_gap;
1987 }
1988 if let Some(suffix) = suffix {
1989 baseline_layout(app, suffix, end);
1990 }
1991 }
1992 TextDirection::Ltr => {
1993 if let Some(prefix_icon) = prefix_icon {
1994 start -= content_padding.start;
1995 start += center_layout(app, prefix_icon, start);
1996 start += prefix_to_input_gap;
1997 } else {
1998 start += decoration.input_gap;
1999 }
2000 if let Some(label) = label {
2001 if decoration.align_label_with_hint {
2002 baseline_layout(app, label, start);
2003 } else {
2004 center_layout(app, label, start);
2005 }
2006 }
2007 if let Some(prefix) = prefix {
2008 start += baseline_layout(app, prefix, start);
2009 }
2010 if let Some(input) = input {
2011 baseline_layout(app, input, start);
2012 }
2013 if let Some(hint) = hint {
2014 baseline_layout(app, hint, start);
2015 }
2016 if let Some(suffix_icon) = suffix_icon {
2017 end += content_padding.end;
2018 let x = end - suffix_icon.size(app).width();
2019 end -= center_layout(app, suffix_icon, x);
2020 end -= input_to_suffix_gap;
2021 } else {
2022 end -= decoration.input_gap;
2023 }
2024 if let Some(suffix) = suffix {
2025 let x = end - suffix.size(app).width();
2026 baseline_layout(app, suffix, x);
2027 }
2028 }
2029 }
2030
2031 let border_gap = decoration.border_gap;
2032 if let Some(label) = label {
2033 let label_x = RenderDecoration::label_offset(app, label).dx();
2034 let float_align = decoration.floating_label_alignment.x + 1.0;
2036 let label_width = RenderDecoration::box_size(app, Some(label)).width();
2037 let float_width = label_width * K_FINAL_LABEL_SCALE;
2038 let container_width = RenderDecoration::box_size(app, container).width();
2039 let prefix_icon_width = RenderDecoration::box_size(app, prefix_icon).width();
2040 let material3 = self.material3(app);
2041 match text_direction {
2044 TextDirection::Rtl => {
2045 let mut offset_to_prefix_icon = 0.0;
2046 if prefix_icon.is_some() && !decoration.align_label_with_hint {
2047 offset_to_prefix_icon = if material3 {
2048 prefix_icon_width - content_padding.end
2049 } else {
2050 0.0
2051 };
2052 }
2053 border_gap.set_start(
2054 app,
2055 lerp_double(
2056 Some(label_x + label_width + offset_to_prefix_icon),
2057 Some(container_width / 2.0 + float_width / 2.0),
2058 float_align,
2059 ),
2060 );
2061 }
2062 TextDirection::Ltr => {
2063 let mut offset_to_prefix_icon = 0.0;
2067 if prefix_icon.is_some() && !decoration.align_label_with_hint {
2068 offset_to_prefix_icon = if material3 {
2069 -prefix_icon_width + content_padding.start
2070 } else {
2071 0.0
2072 };
2073 }
2074 border_gap.set_start(
2075 app,
2076 lerp_double(
2077 Some(label_x - icon_width + offset_to_prefix_icon),
2078 Some(container_width / 2.0 - float_width / 2.0),
2079 float_align,
2080 ),
2081 );
2082 }
2083 }
2084 border_gap.set_extent(app, label.size(app).width() * K_FINAL_LABEL_SCALE);
2085 } else {
2086 border_gap.set_start(app, None);
2087 border_gap.set_extent(app, 0.0);
2088 }
2089 }
2090
2091 fn paint(
2092 self: RenderHandle<Self>,
2093 app: &mut App,
2094 context: &mut PaintingContext,
2095 offset: Offset,
2096 ) {
2097 let do_paint =
2098 |app: &mut App, context: &mut PaintingContext, child: Option<AnyRenderBox>| {
2099 if let Some(child) = child {
2100 let child_offset = RenderDecoration::box_parent_data(app, child).offset;
2101 context.paint_child(app, child.as_object(), child_offset + offset);
2102 }
2103 };
2104
2105 let container = self.container(app);
2106 do_paint(app, context, container);
2107
2108 if let Some(label) = self.label(app) {
2109 let decoration = self.decoration(app).clone();
2110 let label_offset = RenderDecoration::label_offset(app, label);
2111 let label_height = RenderDecoration::box_size(app, Some(label)).height();
2112 let label_width = RenderDecoration::box_size(app, Some(label)).width();
2113 let float_align = decoration.floating_label_alignment.x + 1.0;
2115 let float_width = label_width * K_FINAL_LABEL_SCALE;
2116 let border_side = decoration.border.border_side();
2117 let t = decoration.floating_label_progress;
2118 let is_outline_border = decoration.border.is_outline();
2121 let outlined_floating_y =
2124 (-label_height * K_FINAL_LABEL_SCALE) / 2.0 - border_side.stroke_offset() / 2.0;
2125 let floating_y = if is_outline_border {
2126 outlined_floating_y
2127 } else {
2128 decoration.content_padding.top + self.density_offset(app).dy() / 2.0
2129 };
2130 let scale =
2131 lerp_double(Some(1.0), Some(K_FINAL_LABEL_SCALE), t).expect("both ends are given");
2132 let container = container.expect("an input decorator always has a container");
2133 let centered_float_x = RenderDecoration::box_parent_data(app, container)
2134 .offset
2135 .dx()
2136 + RenderDecoration::box_size(app, Some(container)).width() / 2.0
2137 - float_width / 2.0;
2138 let prefix_icon = self.prefix_icon(app);
2139 let prefix_icon_width = RenderDecoration::box_size(app, prefix_icon).width();
2140 let material3 = self.material3(app);
2141 let start_x;
2142 let mut float_start_x;
2143 match self.text_direction(app) {
2144 TextDirection::Rtl => {
2146 start_x = label_offset.dx() + label_width * (1.0 - scale);
2147 float_start_x = start_x;
2148 if prefix_icon.is_some()
2149 && !decoration.align_label_with_hint
2150 && is_outline_border
2151 {
2152 float_start_x += if material3 {
2153 prefix_icon_width - decoration.content_padding.end
2154 } else {
2155 0.0
2156 };
2157 }
2158 }
2159 TextDirection::Ltr => {
2161 start_x = label_offset.dx();
2162 float_start_x = start_x;
2163 if prefix_icon.is_some()
2164 && !decoration.align_label_with_hint
2165 && is_outline_border
2166 {
2167 float_start_x += if material3 {
2168 -prefix_icon_width + decoration.content_padding.start
2169 } else {
2170 0.0
2171 };
2172 }
2173 }
2174 }
2175 let float_end_x = lerp_double(Some(float_start_x), Some(centered_float_x), float_align)
2176 .expect("both ends are given");
2177 let dx = lerp_double(Some(start_x), Some(float_end_x), t).expect("both ends are given");
2178 let dy = lerp_double(Some(0.0), Some(floating_y - label_offset.dy()), t)
2179 .expect("both ends are given");
2180 let (dx, dy, scale) = (dx as f32, (label_offset.dy() + dy) as f32, scale as f32);
2183 let label_transform = Matrix4::from_flutter_array(&[
2184 scale, 0.0, 0.0, 0.0, 0.0, scale, 0.0, 0.0, 0.0, 0.0, scale, 0.0, dx, dy, 0.0, 1.0,
2188 ]);
2189 self.get_mut(app).label_transform = Some(label_transform);
2190 let old_layer = self.as_object().layer_as::<TransformLayer>(app);
2191 let needs_compositing = self.as_object().needs_compositing(app);
2192 let layer = context.push_transform(
2193 app,
2194 needs_compositing,
2195 offset,
2196 label_transform,
2197 |app, context, offset| self.paint_label(app, context, offset),
2198 old_layer,
2199 );
2200 self.as_object()
2201 .set_layer(app, layer.map(|layer| layer.as_container_layer()));
2202 } else {
2203 self.as_object().set_layer(app, None);
2204 }
2205
2206 do_paint(app, context, self.icon(app));
2207 do_paint(app, context, self.prefix(app));
2208 do_paint(app, context, self.suffix(app));
2209 do_paint(app, context, self.prefix_icon(app));
2210 do_paint(app, context, self.suffix_icon(app));
2211 if self.decoration(app).is_empty {
2212 do_paint(app, context, self.hint(app));
2213 }
2214 do_paint(app, context, self.input(app));
2215 do_paint(app, context, Some(self.helper_error(app)));
2216 do_paint(app, context, self.counter(app));
2217 }
2218}
2219
2220impl RenderBox for RenderDecoration {
2221 waft_rendering::render_box_accessors!();
2222
2223 fn compute_min_intrinsic_width(self: RenderHandle<Self>, app: &mut App, height: f64) -> f64 {
2224 let decoration = self.decoration(app).clone();
2225 let input = self.input(app);
2226 let input_width = if decoration.is_empty || decoration.maintain_hint_size {
2227 let input_width = RenderDecoration::min_width(app, input, height);
2228 let hint = self.hint(app);
2229 input_width.max(RenderDecoration::min_width(app, hint, height))
2230 } else {
2231 RenderDecoration::min_width(app, input, height)
2232 };
2233 let content_width = if decoration.maintain_label_size {
2234 let label = self.label(app);
2235 input_width.max(RenderDecoration::min_width(app, label, height))
2236 } else {
2237 input_width
2238 };
2239 let prefix_icon = self.prefix_icon(app);
2240 let suffix_icon = self.suffix_icon(app);
2241 let content_padding = self.content_padding(app);
2242 let leading_gap = if prefix_icon.is_some() {
2243 self.prefix_to_input_gap(app)
2244 } else {
2245 content_padding.start + decoration.input_gap
2246 };
2247 let trailing_gap = if suffix_icon.is_some() {
2248 self.input_to_suffix_gap(app)
2249 } else {
2250 content_padding.end + decoration.input_gap
2251 };
2252 let icon = self.icon(app);
2253 let prefix = self.prefix(app);
2254 let suffix = self.suffix(app);
2255 RenderDecoration::min_width(app, icon, height)
2256 + leading_gap
2257 + RenderDecoration::min_width(app, prefix_icon, height)
2258 + RenderDecoration::min_width(app, prefix, height)
2259 + content_width
2260 + RenderDecoration::min_width(app, suffix, height)
2261 + RenderDecoration::min_width(app, suffix_icon, height)
2262 + trailing_gap
2263 }
2264
2265 fn compute_max_intrinsic_width(self: RenderHandle<Self>, app: &mut App, height: f64) -> f64 {
2266 let decoration = self.decoration(app).clone();
2267 let input = self.input(app);
2268 let input_width = if decoration.is_empty || decoration.maintain_hint_size {
2269 let input_width = RenderDecoration::max_width(app, input, height);
2270 let hint = self.hint(app);
2271 input_width.max(RenderDecoration::max_width(app, hint, height))
2272 } else {
2273 RenderDecoration::max_width(app, input, height)
2274 };
2275 let content_width = if decoration.maintain_label_size {
2276 let label = self.label(app);
2277 input_width.max(RenderDecoration::max_width(app, label, height))
2278 } else {
2279 input_width
2280 };
2281 let prefix_icon = self.prefix_icon(app);
2282 let suffix_icon = self.suffix_icon(app);
2283 let content_padding = self.content_padding(app);
2284 let leading_gap = if prefix_icon.is_some() {
2285 self.prefix_to_input_gap(app)
2286 } else {
2287 content_padding.start + decoration.input_gap
2288 };
2289 let trailing_gap = if suffix_icon.is_some() {
2290 self.input_to_suffix_gap(app)
2291 } else {
2292 content_padding.end + decoration.input_gap
2293 };
2294 let icon = self.icon(app);
2295 let prefix = self.prefix(app);
2296 let suffix = self.suffix(app);
2297 RenderDecoration::max_width(app, icon, height)
2298 + leading_gap
2299 + RenderDecoration::max_width(app, prefix_icon, height)
2300 + RenderDecoration::max_width(app, prefix, height)
2301 + content_width
2302 + RenderDecoration::max_width(app, suffix, height)
2303 + RenderDecoration::max_width(app, suffix_icon, height)
2304 + trailing_gap
2305 }
2306
2307 fn compute_min_intrinsic_height(self: RenderHandle<Self>, app: &mut App, width: f64) -> f64 {
2308 let decoration = self.decoration(app).clone();
2309 let content_padding = self.content_padding(app);
2310 let supporting_text_padding = decoration.supporting_text_padding;
2311 let icon = self.icon(app);
2312 let icon_height = RenderDecoration::min_height(app, icon, width);
2313 let icon_width = RenderDecoration::min_width(app, icon, icon_height);
2314
2315 let width = (width - icon_width).max(0.0);
2316 let supporting_text_width = (width
2317 - supporting_text_padding
2318 .map_or(content_padding.start + content_padding.end, |padding| {
2319 padding.start + padding.end
2320 })
2321 - decoration.input_gap * 2.0)
2322 .max(0.0);
2323
2324 let prefix_icon = self.prefix_icon(app);
2325 let prefix_icon_height = RenderDecoration::min_height(app, prefix_icon, width);
2326 let prefix_icon_width = RenderDecoration::min_width(app, prefix_icon, prefix_icon_height);
2327
2328 let suffix_icon = self.suffix_icon(app);
2329 let suffix_icon_height = RenderDecoration::min_height(app, suffix_icon, width);
2330 let suffix_icon_width = RenderDecoration::min_width(app, suffix_icon, suffix_icon_height);
2331
2332 let width =
2333 (width - (content_padding.start + content_padding.end) - decoration.input_gap * 2.0)
2334 .max(0.0);
2335
2336 let counter = self.counter(app);
2339 let counter_height = RenderDecoration::min_height(app, counter, supporting_text_width);
2340 let counter_width = RenderDecoration::min_width(app, counter, counter_height);
2341
2342 let counter_padding = if counter.is_some() {
2344 K_SUPPORTING_TEXT_COUNTER_PADDING
2345 } else {
2346 0.0
2347 };
2348 let helper_error_available_width =
2349 (supporting_text_width - counter_width - counter_padding).max(0.0);
2350 let helper_error = self.helper_error(app);
2351 let helper_error_height =
2352 RenderDecoration::min_height(app, Some(helper_error), helper_error_available_width);
2353 let mut supporting_text_height = counter_height.max(helper_error_height);
2354 if supporting_text_height > 0.0 {
2355 let top_padding = supporting_text_padding
2356 .map_or_else(|| self.supporting_text_gap(app), |padding| padding.top);
2357 let bottom_padding = supporting_text_padding.map_or(0.0, |padding| padding.bottom);
2358 supporting_text_height += top_padding + bottom_padding;
2359 }
2360
2361 let prefix = self.prefix(app);
2362 let prefix_height = RenderDecoration::min_height(app, prefix, width);
2363 let prefix_width = RenderDecoration::min_width(app, prefix, prefix_height);
2364
2365 let suffix = self.suffix(app);
2366 let suffix_height = RenderDecoration::min_height(app, suffix, width);
2367 let suffix_width = RenderDecoration::min_width(app, suffix, suffix_height);
2368
2369 let available_input_width =
2370 (width - prefix_width - suffix_width - prefix_icon_width - suffix_icon_width).max(0.0);
2371 let input = self.input(app);
2372 let hint = if decoration.is_empty {
2373 self.hint(app)
2374 } else {
2375 None
2376 };
2377 let input_height =
2378 RenderDecoration::line_height(app, available_input_width, &[input, hint]);
2379 let input_max_height = [input_height, prefix_height, suffix_height]
2380 .into_iter()
2381 .fold(f64::NEG_INFINITY, f64::max);
2382
2383 let label = self.label(app);
2384 let content_height = content_padding.top
2385 + if label.is_none() {
2386 0.0
2387 } else {
2388 decoration.floating_label_height
2389 }
2390 + input_max_height
2391 + content_padding.bottom
2392 + self.density_offset(app).dy();
2393 let container_height = [
2394 icon_height,
2395 content_height,
2396 prefix_icon_height,
2397 suffix_icon_height,
2398 ]
2399 .into_iter()
2400 .fold(f64::NEG_INFINITY, f64::max);
2401 let min_container_height = if decoration.is_dense.expect("applied from the theme")
2403 || decoration.is_collapsed
2404 || self.expands(app)
2405 {
2406 input_height
2407 } else {
2408 K_MIN_INTERACTIVE_DIMENSION
2409 };
2410
2411 container_height.max(min_container_height) + supporting_text_height
2412 }
2413
2414 fn compute_max_intrinsic_height(self: RenderHandle<Self>, app: &mut App, width: f64) -> f64 {
2415 self.as_box().get_min_intrinsic_height(app, width)
2416 }
2417
2418 fn compute_distance_to_actual_baseline(
2419 self: RenderHandle<Self>,
2420 app: &mut App,
2421 baseline: TextBaseline,
2422 ) -> Option<f64> {
2423 let Some(input) = self.input(app) else {
2424 return Some(0.0);
2425 };
2426 let offset_dy = RenderDecoration::box_parent_data(app, input).offset.dy();
2427 let distance = input
2428 .get_distance_to_actual_baseline(app, baseline)
2429 .unwrap_or_else(|| input.size(app).height());
2430 Some(offset_dy + distance)
2431 }
2432
2433 fn compute_dry_baseline(
2434 self: RenderHandle<Self>,
2435 app: &mut App,
2436 constraints: BoxConstraints,
2437 baseline: TextBaseline,
2438 ) -> Option<f64> {
2439 let Some(input) = self.input(app) else {
2440 return Some(0.0);
2441 };
2442 let layout = self.layout(
2443 app,
2444 constraints,
2445 ChildLayoutHelper::dry_layout_child,
2446 RenderDecoration::get_dry_baseline,
2447 );
2448 let baseline_offset = match baseline {
2449 TextBaseline::Alphabetic => 0.0,
2450 TextBaseline::Ideographic => {
2451 let ideographic = input
2452 .get_dry_baseline(app, layout.input_constraints, TextBaseline::Ideographic)
2453 .unwrap_or_else(|| {
2454 input.get_dry_layout(app, layout.input_constraints).height()
2455 });
2456 let alphabetic = input
2457 .get_dry_baseline(app, layout.input_constraints, TextBaseline::Alphabetic)
2458 .unwrap_or_else(|| {
2459 input.get_dry_layout(app, layout.input_constraints).height()
2460 });
2461 ideographic - alphabetic
2462 }
2463 };
2464 Some(baseline_offset + layout.baseline)
2465 }
2466
2467 fn compute_dry_layout(
2468 self: RenderHandle<Self>,
2469 app: &mut App,
2470 constraints: BoxConstraints,
2471 ) -> Size {
2472 let layout = self.layout(
2473 app,
2474 constraints,
2475 ChildLayoutHelper::dry_layout_child,
2476 RenderDecoration::get_dry_baseline,
2477 );
2478 constraints.constrain(layout.size)
2479 }
2480
2481 fn apply_paint_transform(
2482 self: RenderHandle<Self>,
2483 app: &App,
2484 child: AnyRenderObject,
2485 transform: &mut Matrix4,
2486 ) {
2487 if let Some(label) = self.label(app)
2488 && child == label.as_object()
2489 && let Some(label_transform) = self.get(app).label_transform
2490 {
2491 let label_offset = RenderDecoration::label_offset(app, label);
2492 *transform = transform.then(&label_transform).then(&Matrix4::translation(
2493 -label_offset.dx() as f32,
2494 -label_offset.dy() as f32,
2495 ));
2496 }
2497 let offset = child.parent_data_of::<BoxParentData>(app).offset;
2500 *transform = transform.then(&Matrix4::translation(
2501 offset.dx() as f32,
2502 offset.dy() as f32,
2503 ));
2504 }
2505
2506 fn hit_test_self(self: RenderHandle<Self>, _app: &App, _position: Offset) -> bool {
2507 true
2508 }
2509
2510 fn hit_test_children(
2511 self: RenderHandle<Self>,
2512 app: &mut App,
2513 result: &mut BoxHitTestResult<'_>,
2514 position: Offset,
2515 ) -> bool {
2516 let label = self.label(app);
2517 for child in SlottedContainerRenderObjectMixin::children(self, app) {
2518 if Some(child) == label && self.get(app).label_transform.is_some() {
2520 let mut transform = Matrix4::IDENTITY;
2521 RenderBox::apply_paint_transform(self, app, child.as_object(), &mut transform);
2522 if result.add_with_paint_transform(
2523 Some(transform),
2524 position,
2525 |result, transformed| child.hit_test(app, result, transformed),
2526 ) {
2527 return true;
2528 }
2529 continue;
2530 }
2531 let offset = RenderDecoration::box_parent_data(app, child).offset;
2532 let is_hit =
2533 result.add_with_paint_offset(Some(offset), position, |result, transformed| {
2534 debug_assert!(transformed == position - offset);
2535 child.hit_test(app, result, transformed)
2536 });
2537 if is_hit {
2538 return true;
2539 }
2540 }
2541 false
2542 }
2543}
2544
2545impl Debug for RenderDecoration {
2546 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
2547 f.debug_struct("_RenderDecoration")
2548 .field("decoration", &self.decoration)
2549 .finish_non_exhaustive()
2550 }
2551}
2552
2553#[derive(Debug)]
2555struct Decorator {
2556 decoration: Decoration,
2557 text_direction: TextDirection,
2558 text_baseline: TextBaseline,
2559 text_align_vertical: Option<TextAlignVertical>,
2560 is_focused: bool,
2561 expands: bool,
2562}
2563
2564impl SlottedMultiChildRenderObjectWidget for Decorator {
2565 type Slot = DecorationSlot;
2566
2567 fn slots(&self) -> Vec<DecorationSlot> {
2568 DecorationSlot::VALUES.to_vec()
2569 }
2570
2571 fn child_for_slot(&self, slot: &DecorationSlot) -> Option<WidgetRef> {
2572 let decoration = &self.decoration;
2573 match slot {
2574 DecorationSlot::Icon => decoration.icon.clone(),
2575 DecorationSlot::Input => decoration.input.clone(),
2576 DecorationSlot::Label => decoration.label.clone(),
2577 DecorationSlot::Hint => decoration.hint.clone(),
2578 DecorationSlot::Prefix => decoration.prefix.clone(),
2579 DecorationSlot::Suffix => decoration.suffix.clone(),
2580 DecorationSlot::PrefixIcon => decoration.prefix_icon.clone(),
2581 DecorationSlot::SuffixIcon => decoration.suffix_icon.clone(),
2582 DecorationSlot::HelperError => decoration.helper_error.clone(),
2583 DecorationSlot::Counter => decoration.counter.clone(),
2584 DecorationSlot::Container => decoration.container.clone(),
2585 }
2586 }
2587}
2588
2589impl RenderObjectWidget for Decorator {
2590 type RenderObject = RenderDecoration;
2591
2592 fn create_render_object(&self, app: &mut App, context: BuildContext) -> AnyRenderObject {
2593 let material3 = Theme::of(app, context).use_material3;
2594 RenderDecoration::new(
2595 app,
2596 self.decoration.clone(),
2597 self.text_direction,
2598 self.text_baseline,
2599 self.is_focused,
2600 self.expands,
2601 material3,
2602 self.text_align_vertical,
2603 )
2604 .as_object()
2605 }
2606
2607 fn update_render_object(
2608 &self,
2609 app: &mut App,
2610 _context: BuildContext,
2611 render_object: RenderHandle<RenderDecoration>,
2612 ) {
2613 render_object.set_decoration(app, self.decoration.clone());
2614 render_object.set_expands(app, self.expands);
2615 render_object.set_is_focused(app, self.is_focused);
2616 render_object.set_text_align_vertical(app, self.text_align_vertical);
2617 render_object.set_text_baseline(app, self.text_baseline);
2618 render_object.set_text_direction(app, self.text_direction);
2619 }
2620}
2621
2622#[derive(Debug)]
2624struct AffixText {
2625 label_is_floating: bool,
2626 text: Option<String>,
2627 style: Option<TextStyle>,
2628 child: Option<WidgetRef>,
2629}
2630
2631impl StatelessWidget for AffixText {
2632 fn key(&self) -> Option<&KeyRef> {
2633 None
2634 }
2635
2636 fn build(&self, _app: &mut App, _context: BuildContext) -> WidgetRef {
2637 let child = self.child.clone().or_else(|| {
2639 self.text.as_ref().map(|text| {
2640 let mut text = Text::new(text.clone());
2641 if let Some(style) = &self.style {
2642 text = text.style(style.clone());
2643 }
2644 text.into_widget()
2645 })
2646 });
2647 let mut opacity = AnimatedOpacity::new(
2648 if self.label_is_floating { 1.0 } else { 0.0 },
2649 K_TRANSITION_DURATION,
2650 )
2651 .curve(k_transition_curve());
2652 if let Some(child) = child {
2653 opacity = opacity.child(child);
2654 }
2655 let mut merge = DefaultTextStyle::merge(
2656 IgnorePointer::new()
2657 .ignoring(!self.label_is_floating)
2658 .child(opacity),
2659 );
2660 if let Some(style) = &self.style {
2661 merge = merge.style(style.clone());
2662 }
2663 merge.into_widget()
2664 }
2665}
2666
2667#[derive(Clone)]
2690pub struct InputDecorator {
2691 pub key: Option<KeyRef>,
2692
2693 pub decoration: InputDecoration,
2698
2699 pub base_style: Option<TextStyle>,
2708
2709 pub text_align: Option<TextAlign>,
2711
2712 pub text_align_vertical: Option<TextAlignVertical>,
2723
2724 pub is_focused: bool,
2737
2738 pub is_hovering: bool,
2746
2747 pub expands: bool,
2759
2760 pub is_empty: bool,
2767
2768 pub child: Option<WidgetRef>,
2772}
2773
2774impl InputDecorator {
2775 pub fn new(decoration: InputDecoration) -> InputDecorator {
2778 InputDecorator {
2779 key: None,
2780 decoration,
2781 base_style: None,
2782 text_align: None,
2783 text_align_vertical: None,
2784 is_focused: false,
2785 is_hovering: false,
2786 expands: false,
2787 is_empty: false,
2788 child: None,
2789 }
2790 }
2791
2792 pub fn key(mut self, key: KeyRef) -> InputDecorator {
2794 self.key = Some(key);
2795 self
2796 }
2797
2798 pub fn base_style(mut self, base_style: TextStyle) -> InputDecorator {
2800 self.base_style = Some(base_style);
2801 self
2802 }
2803
2804 pub fn text_align(mut self, text_align: TextAlign) -> InputDecorator {
2806 self.text_align = Some(text_align);
2807 self
2808 }
2809
2810 pub fn text_align_vertical(mut self, text_align_vertical: TextAlignVertical) -> InputDecorator {
2812 self.text_align_vertical = Some(text_align_vertical);
2813 self
2814 }
2815
2816 pub fn is_focused(mut self, is_focused: bool) -> InputDecorator {
2818 self.is_focused = is_focused;
2819 self
2820 }
2821
2822 pub fn is_hovering(mut self, is_hovering: bool) -> InputDecorator {
2824 self.is_hovering = is_hovering;
2825 self
2826 }
2827
2828 pub fn expands(mut self, expands: bool) -> InputDecorator {
2830 self.expands = expands;
2831 self
2832 }
2833
2834 pub fn is_empty(mut self, is_empty: bool) -> InputDecorator {
2836 self.is_empty = is_empty;
2837 self
2838 }
2839
2840 #[cfg_attr(debug_assertions, track_caller)]
2842 pub fn child<K>(mut self, child: impl IntoWidget<K>) -> InputDecorator {
2843 self.child = Some(child.into_widget());
2844 self
2845 }
2846
2847 fn label_should_withdraw(&self) -> bool {
2852 !self.is_empty || (self.is_focused && self.decoration.enabled)
2853 }
2854
2855 pub fn container_of(app: &App, context: BuildContext) -> Option<AnyRenderBox> {
2863 context
2864 .find_ancestor_render_object_of_type::<RenderDecoration>(app)
2865 .and_then(|result| result.container(app))
2866 }
2867}
2868
2869impl StatefulWidget for InputDecorator {
2870 type State = InputDecoratorState;
2871
2872 fn key(&self) -> Option<&KeyRef> {
2873 self.key.as_ref()
2874 }
2875
2876 fn debug_fill_properties(&self, _app: &App, properties: &mut DiagnosticPropertiesBuilder) {
2877 properties.add(DiagnosticsProperty::<InputDecoration>::new(
2878 "decoration",
2879 Some(self.decoration.clone()),
2880 ));
2881 properties.add(
2882 DiagnosticsProperty::<TextStyle>::new("baseStyle", self.base_style.clone())
2883 .default_value(None),
2884 );
2885 properties.add(DiagnosticsProperty::<bool>::new(
2886 "isFocused",
2887 self.is_focused,
2888 ));
2889 properties.add(
2890 DiagnosticsProperty::<bool>::new("expands", self.expands).default_value(Some(false)),
2891 );
2892 properties.add(DiagnosticsProperty::<bool>::new("isEmpty", self.is_empty));
2893 }
2894
2895 fn debug_check_constructor(&self) {
2896 self.decoration.debug_check_arguments();
2897 }
2898
2899 fn create_state(&self) -> InputDecoratorState {
2900 InputDecoratorState {
2901 state: StateData::new(),
2902 ticker_provider: TickerProviderStateMixinData::new(),
2903 floating_label_controller: None,
2904 floating_label_animation: None,
2905 shaking_label_controller: None,
2906 border_gap: None,
2907 effective_decoration: None,
2908 curved_animation: None,
2909 }
2910 }
2911}
2912
2913impl Debug for InputDecorator {
2914 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
2915 let mut debug = f.debug_struct("InputDecorator");
2916 debug.field("decoration", &self.decoration);
2917 if let Some(base_style) = &self.base_style {
2918 debug.field("baseStyle", base_style);
2919 }
2920 debug.field("isFocused", &self.is_focused);
2921 if self.expands {
2922 debug.field("expands", &self.expands);
2923 }
2924 debug.field("isEmpty", &self.is_empty);
2925 debug.finish()
2926 }
2927}
2928
2929pub struct InputDecoratorState {
2933 state: StateData<InputDecorator>,
2934 ticker_provider: TickerProviderStateMixinData,
2935 floating_label_controller: Option<Ref<AnimationController>>,
2936 floating_label_animation: Option<Ref<CurvedAnimation>>,
2937 shaking_label_controller: Option<Ref<AnimationController>>,
2938 border_gap: Option<Ref<InputBorderGap>>,
2939 effective_decoration: Option<InputDecoration>,
2940 curved_animation: Option<Ref<CurvedAnimation>>,
2941}
2942
2943impl InputDecoratorState {
2944 fn floating_label_controller(self: Handle<Self>, app: &App) -> Ref<AnimationController> {
2945 app.get(self)
2946 .floating_label_controller
2947 .clone()
2948 .expect("created in init_state")
2949 }
2950
2951 fn floating_label_animation(self: Handle<Self>, app: &App) -> Ref<CurvedAnimation> {
2952 app.get(self)
2953 .floating_label_animation
2954 .clone()
2955 .expect("created in init_state")
2956 }
2957
2958 fn shaking_label_controller(self: Handle<Self>, app: &App) -> Ref<AnimationController> {
2959 app.get(self)
2960 .shaking_label_controller
2961 .clone()
2962 .expect("created in init_state")
2963 }
2964
2965 fn border_gap(self: Handle<Self>, app: &App) -> Ref<InputBorderGap> {
2966 app.get(self)
2967 .border_gap
2968 .clone()
2969 .expect("created in init_state")
2970 }
2971
2972 fn handle_change(self: Handle<Self>, app: &mut App) {
2973 self.set_state(app, |_state| {
2974 });
2976 }
2977
2978 fn decoration(self: Handle<Self>, app: &mut App) -> &InputDecoration {
2981 if app.get(self).effective_decoration.is_none() {
2982 let context = self.context(app);
2983 let theme = InputDecorationTheme::of(app, context);
2984 let effective = self.widget(app).decoration.apply_defaults(theme);
2985 app.get_mut(self).effective_decoration = Some(effective);
2986 }
2987 app.get(self)
2988 .effective_decoration
2989 .as_ref()
2990 .expect("worked out above")
2991 }
2992
2993 fn text_align(self: Handle<Self>, app: &App) -> Option<TextAlign> {
2994 self.widget(app).text_align
2995 }
2996
2997 fn is_focused(self: Handle<Self>, app: &App) -> bool {
2998 self.widget(app).is_focused
2999 }
3000
3001 fn has_error(self: Handle<Self>, app: &mut App) -> bool {
3002 let decoration = self.decoration(app);
3003 decoration.error_text.is_some() || decoration.error.is_some()
3004 }
3005
3006 fn is_hovering(self: Handle<Self>, app: &mut App) -> bool {
3007 let enabled = self.decoration(app).enabled;
3008 self.widget(app).is_hovering && enabled
3009 }
3010
3011 fn is_empty(self: Handle<Self>, app: &App) -> bool {
3012 self.widget(app).is_empty
3013 }
3014
3015 fn floating_label_enabled(self: Handle<Self>, app: &mut App) -> bool {
3016 self.decoration(app).floating_label_behavior != Some(FloatingLabelBehavior::Never)
3017 }
3018
3019 fn label_should_withdraw(self: Handle<Self>, app: &mut App) -> bool {
3020 let always =
3021 self.decoration(app).floating_label_behavior == Some(FloatingLabelBehavior::Always);
3022 self.widget(app).label_should_withdraw() || always
3023 }
3024
3025 fn get_default_m2_border_color(
3026 self: Handle<Self>,
3027 app: &mut App,
3028 theme_data: &ThemeData,
3029 ) -> Color {
3030 let is_focused = self.is_focused(app);
3031 let has_error = self.has_error(app);
3032 let is_hovering = self.is_hovering(app);
3033 let decoration = self.decoration(app);
3034 if !decoration.enabled && !is_focused {
3035 return if decoration.filled.unwrap_or(false)
3036 && !decoration
3037 .border
3038 .as_ref()
3039 .is_some_and(|border| border.is_outline())
3040 {
3041 Colors::TRANSPARENT
3042 } else {
3043 theme_data.disabled_color
3044 };
3045 }
3046 if has_error {
3047 return theme_data.color_scheme.error();
3048 }
3049 if is_focused {
3050 return theme_data.color_scheme.primary();
3051 }
3052 if decoration.filled.expect("applied from the theme") {
3053 return theme_data.hint_color;
3054 }
3055 #[allow(deprecated)]
3058 let enabled_color = theme_data.color_scheme.on_surface().with_opacity(0.38);
3059 if is_hovering {
3060 let hover_color = decoration.hover_color.unwrap_or(theme_data.hover_color);
3061 #[allow(deprecated)]
3062 return Color::alpha_blend(hover_color.with_opacity(0.12), enabled_color);
3063 }
3064 enabled_color
3065 }
3066
3067 fn get_fill_color(
3068 self: Handle<Self>,
3069 app: &mut App,
3070 _theme_data: &ThemeData,
3071 defaults: &dyn InputDecorationThemeDataGetters,
3072 ) -> Color {
3073 let widget_state = self.widget_state(app);
3074 let decoration = self.decoration(app);
3075 if decoration.filled != Some(true) {
3076 return Colors::TRANSPARENT;
3078 }
3079 if let Some(fill_color) = &decoration.fill_color {
3080 return *<dyn WidgetStateProperty<Color>>::resolve_as(fill_color, &widget_state);
3081 }
3082 *<dyn WidgetStateProperty<Color>>::resolve_as(
3083 defaults
3084 .fill_color(app)
3085 .as_ref()
3086 .expect("the defaults give one"),
3087 &widget_state,
3088 )
3089 }
3090
3091 fn get_hover_color(self: Handle<Self>, app: &mut App, theme_data: &ThemeData) -> Color {
3092 let decoration = self.decoration(app);
3093 if decoration.filled.is_none() || !decoration.filled.unwrap_or(false) || !decoration.enabled
3094 {
3095 return Colors::TRANSPARENT;
3096 }
3097 decoration.hover_color.unwrap_or(theme_data.hover_color)
3098 }
3099
3100 fn get_icon_color(
3101 self: Handle<Self>,
3102 app: &mut App,
3103 _theme_data: &ThemeData,
3104 defaults: &dyn InputDecorationThemeDataGetters,
3105 ) -> Color {
3106 let widget_state = self.widget_state(app);
3107 let decoration = self.decoration(app);
3108 <dyn WidgetStateProperty<Color>>::resolve_as(&decoration.icon_color, &widget_state)
3109 .map(|color| *color)
3110 .unwrap_or_else(|| {
3111 *<dyn WidgetStateProperty<Color>>::resolve_as(
3112 defaults
3113 .icon_color(app)
3114 .as_ref()
3115 .expect("the defaults give one"),
3116 &widget_state,
3117 )
3118 })
3119 }
3120
3121 fn get_prefix_icon_color(
3122 self: Handle<Self>,
3123 app: &mut App,
3124 icon_button_theme: &IconButtonThemeData,
3125 defaults: &dyn InputDecorationThemeDataGetters,
3126 ) -> Color {
3127 let widget_state = self.widget_state(app);
3128 let decoration = self.decoration(app);
3129 <dyn WidgetStateProperty<Color>>::resolve_as(&decoration.prefix_icon_color, &widget_state)
3130 .map(|color| *color)
3131 .or_else(|| {
3132 icon_button_theme
3133 .style
3134 .as_ref()
3135 .and_then(|style| style.foreground_color.as_ref())
3136 .and_then(|foreground_color| foreground_color.resolve(&widget_state))
3137 })
3138 .unwrap_or_else(|| {
3139 *<dyn WidgetStateProperty<Color>>::resolve_as(
3140 defaults
3141 .prefix_icon_color(app)
3142 .as_ref()
3143 .expect("the defaults give one"),
3144 &widget_state,
3145 )
3146 })
3147 }
3148
3149 fn get_suffix_icon_color(
3150 self: Handle<Self>,
3151 app: &mut App,
3152 icon_button_theme: &IconButtonThemeData,
3153 defaults: &dyn InputDecorationThemeDataGetters,
3154 ) -> Color {
3155 let widget_state = self.widget_state(app);
3156 let decoration = self.decoration(app);
3157 <dyn WidgetStateProperty<Color>>::resolve_as(&decoration.suffix_icon_color, &widget_state)
3158 .map(|color| *color)
3159 .or_else(|| {
3160 icon_button_theme
3161 .style
3162 .as_ref()
3163 .and_then(|style| style.foreground_color.as_ref())
3164 .and_then(|foreground_color| foreground_color.resolve(&widget_state))
3165 })
3166 .unwrap_or_else(|| {
3167 *<dyn WidgetStateProperty<Color>>::resolve_as(
3168 defaults
3169 .suffix_icon_color(app)
3170 .as_ref()
3171 .expect("the defaults give one"),
3172 &widget_state,
3173 )
3174 })
3175 }
3176
3177 fn has_inline_label(self: Handle<Self>, app: &mut App) -> bool {
3182 let label_should_withdraw = self.label_should_withdraw(app);
3183 let decoration = self.decoration(app);
3184 !label_should_withdraw && (decoration.label_text.is_some() || decoration.label.is_some())
3185 }
3186
3187 fn should_show_label(self: Handle<Self>, app: &mut App) -> bool {
3189 self.has_inline_label(app) || self.floating_label_enabled(app)
3190 }
3191
3192 fn get_inline_label_style(
3195 self: Handle<Self>,
3196 app: &mut App,
3197 theme_data: &ThemeData,
3198 defaults: &dyn InputDecorationThemeDataGetters,
3199 ) -> TextStyle {
3200 let widget_state = self.widget_state(app);
3201 let default_style = <dyn WidgetStateProperty<TextStyle>>::resolve_as(
3202 defaults
3203 .label_style(app)
3204 .as_ref()
3205 .expect("the defaults give one"),
3206 &widget_state,
3207 );
3208
3209 let style = <dyn WidgetStateProperty<TextStyle>>::resolve_as(
3210 &self.decoration(app).label_style,
3211 &widget_state,
3212 );
3213
3214 theme_data
3215 .text_theme
3216 .title_medium
3217 .as_ref()
3218 .expect("the theme's text theme gives titleMedium")
3219 .merge(self.widget(app).base_style.as_ref())
3220 .merge(Some(&default_style))
3221 .merge(style.as_deref())
3222 .copy_with()
3223 .height(1.0)
3224 }
3225
3226 fn get_inline_hint_style(
3229 self: Handle<Self>,
3230 app: &mut App,
3231 theme_data: &ThemeData,
3232 defaults: &dyn InputDecorationThemeDataGetters,
3233 ) -> TextStyle {
3234 let widget_state = self.widget_state(app);
3235 let default_style = <dyn WidgetStateProperty<TextStyle>>::resolve_as(
3236 defaults
3237 .hint_style(app)
3238 .as_ref()
3239 .expect("the defaults give one"),
3240 &widget_state,
3241 );
3242
3243 let style = <dyn WidgetStateProperty<TextStyle>>::resolve_as(
3244 &self.decoration(app).hint_style,
3245 &widget_state,
3246 );
3247
3248 (if theme_data.use_material3 {
3249 theme_data.text_theme.body_large.as_ref()
3250 } else {
3251 theme_data.text_theme.title_medium.as_ref()
3252 })
3253 .expect("the theme's text theme gives the style")
3254 .merge(self.widget(app).base_style.as_ref())
3255 .merge(Some(&default_style))
3256 .merge(style.as_deref())
3257 }
3258
3259 fn get_floating_label_style(
3260 self: Handle<Self>,
3261 app: &mut App,
3262 theme_data: &ThemeData,
3263 defaults: &dyn InputDecorationThemeDataGetters,
3264 ) -> TextStyle {
3265 let widget_state = self.widget_state(app);
3266 let has_error = self.has_error(app);
3267 let mut default_text_style: TextStyle = <dyn WidgetStateProperty<TextStyle>>::resolve_as(
3268 defaults
3269 .floating_label_style(app)
3270 .as_ref()
3271 .expect("the defaults give one"),
3272 &widget_state,
3273 )
3274 .value()
3275 .clone();
3276 let decoration = self.decoration(app);
3277 if has_error
3278 && let Some(error_color) = decoration
3279 .error_style
3280 .as_ref()
3281 .and_then(|error_style| error_style.color.clone())
3282 {
3283 default_text_style = default_text_style.copy_with().color(error_color);
3284 }
3285 default_text_style = default_text_style.merge(
3286 decoration
3287 .floating_label_style
3288 .as_deref()
3289 .or(decoration.label_style.as_deref()),
3290 );
3291
3292 let style = <dyn WidgetStateProperty<TextStyle>>::resolve_as(
3293 &decoration.floating_label_style,
3294 &widget_state,
3295 );
3296
3297 theme_data
3298 .text_theme
3299 .title_medium
3300 .as_ref()
3301 .expect("the theme's text theme gives titleMedium")
3302 .merge(self.widget(app).base_style.as_ref())
3303 .merge(Some(&default_text_style))
3304 .merge(style.as_deref())
3305 .copy_with()
3306 .height(1.0)
3307 }
3308
3309 fn get_helper_style(
3310 self: Handle<Self>,
3311 app: &mut App,
3312 _theme_data: &ThemeData,
3313 defaults: &dyn InputDecorationThemeDataGetters,
3314 ) -> TextStyle {
3315 let widget_state = self.widget_state(app);
3316 let style = <dyn WidgetStateProperty<TextStyle>>::resolve_as(
3317 &self.decoration(app).helper_style,
3318 &widget_state,
3319 );
3320 <dyn WidgetStateProperty<TextStyle>>::resolve_as(
3321 defaults
3322 .helper_style(app)
3323 .as_ref()
3324 .expect("the defaults give one"),
3325 &widget_state,
3326 )
3327 .merge(style.as_deref())
3328 }
3329
3330 fn get_error_style(
3331 self: Handle<Self>,
3332 app: &mut App,
3333 _theme_data: &ThemeData,
3334 defaults: &dyn InputDecorationThemeDataGetters,
3335 ) -> TextStyle {
3336 let widget_state = self.widget_state(app);
3337 let error_style = self.decoration(app).error_style.clone();
3338 <dyn WidgetStateProperty<TextStyle>>::resolve_as(
3339 defaults
3340 .error_style(app)
3341 .as_ref()
3342 .expect("the defaults give one"),
3343 &widget_state,
3344 )
3345 .merge(error_style.as_deref())
3346 }
3347
3348 fn widget_state(self: Handle<Self>, app: &mut App) -> WidgetStates {
3349 let enabled = self.decoration(app).enabled;
3350 let is_focused = self.is_focused(app);
3351 let is_hovering = self.is_hovering(app);
3352 let has_error = self.has_error(app);
3353 let mut states = WidgetStates::new();
3354 if !enabled {
3355 states.insert(WidgetState::Disabled);
3356 }
3357 if is_focused {
3358 states.insert(WidgetState::Focused);
3359 }
3360 if is_hovering {
3361 states.insert(WidgetState::Hovered);
3362 }
3363 if has_error {
3364 states.insert(WidgetState::Error);
3365 }
3366 states
3367 }
3368
3369 fn get_default_border(
3370 self: Handle<Self>,
3371 app: &mut App,
3372 theme_data: &ThemeData,
3373 defaults: &dyn InputDecorationThemeDataGetters,
3374 ) -> InputBorderRef {
3375 let widget_state = self.widget_state(app);
3376 let decoration = self.decoration(app).clone();
3377 let border: InputBorderRef = <dyn WidgetStateProperty<InputBorderRef>>::resolve_as(
3378 &decoration.border,
3379 &widget_state,
3380 )
3381 .unwrap_or_else(|| InputBorderRef::new(UnderlineInputBorder::new()));
3382
3383 if decoration
3384 .border
3385 .as_ref()
3386 .is_some_and(|border| border.as_widget_state_property().is_some())
3387 {
3388 return border;
3389 }
3390
3391 if border.border_side() == BorderSide::NONE {
3392 return border;
3393 }
3394
3395 if theme_data.use_material3 {
3396 if decoration.filled.expect("applied from the theme") {
3397 let context = self.context(app);
3398 let decoration_theme = InputDecorationTheme::of(app, context);
3399 let active_indicator_border = decoration_theme
3400 .active_indicator_border
3401 .clone()
3402 .or_else(|| defaults.active_indicator_border(app));
3403 InputBorder::copy_with(
3404 &*border,
3405 <dyn WidgetStateProperty<Option<BorderSide>>>::resolve_as(
3406 &active_indicator_border,
3407 &widget_state,
3408 )
3409 .map(|side| *side),
3410 )
3411 } else {
3412 InputBorder::copy_with(
3413 &*border,
3414 <dyn WidgetStateProperty<Option<BorderSide>>>::resolve_as(
3415 &defaults.outline_border(app),
3416 &widget_state,
3417 )
3418 .map(|side| *side),
3419 )
3420 }
3421 } else {
3422 let is_focused = self.is_focused(app);
3423 let color = self.get_default_m2_border_color(app, theme_data);
3424 let width = if decoration.is_collapsed.expect("applied from the theme")
3425 || decoration
3426 .border
3427 .as_ref()
3428 .is_some_and(|border| **border == *InputBorderRef::none())
3429 || !decoration.enabled
3430 {
3431 0.0
3432 } else if is_focused {
3433 2.0
3434 } else {
3435 1.0
3436 };
3437 InputBorder::copy_with(
3438 &*border,
3439 Some(BorderSide {
3440 color,
3441 width,
3442 ..BorderSide::default()
3443 }),
3444 )
3445 }
3446 }
3447
3448 fn build_transition(
3449 self: Handle<Self>,
3450 app: &mut App,
3451 child: &WidgetRef,
3452 animation: AnimationRef<f64>,
3453 ) -> WidgetRef {
3454 let current = app.get(self).curved_animation.handle();
3455 if current.is_none_or(|curved| curved.parent(app) != animation) {
3456 if let Some(curved) = current {
3457 curved.dispose(app);
3458 }
3459 let curved = CurvedAnimation::new(app, animation, k_transition_curve(), None);
3460 app.get_mut(self).curved_animation = Some(curved);
3461 }
3462
3463 let curved = app.get(self).curved_animation.clone().expect("made above");
3464 FadeTransition::new(curved.erase())
3465 .child(child.clone())
3466 .into_widget()
3467 }
3468
3469 fn top_start_layout(
3470 _app: &mut App,
3471 current_child: Option<&WidgetRef>,
3472 previous_children: &[WidgetRef],
3473 ) -> WidgetRef {
3474 Stack::new()
3475 .children(
3476 previous_children
3477 .iter()
3478 .cloned()
3479 .chain(current_child.cloned()),
3480 )
3481 .into_widget()
3482 }
3483}
3484
3485impl TickerProviderStateMixin for InputDecoratorState {
3486 fn ticker_provider_data(self: Handle<Self>, app: &App) -> &TickerProviderStateMixinData {
3487 &app.get(self).ticker_provider
3488 }
3489
3490 fn ticker_provider_data_mut(
3491 self: Handle<Self>,
3492 app: &mut App,
3493 ) -> &mut TickerProviderStateMixinData {
3494 &mut app.get_mut(self).ticker_provider
3495 }
3496}
3497
3498impl TickerProviderObject for InputDecoratorState {
3500 fn create_ticker(self: Handle<Self>, app: &mut App, on_tick: TickerCallback) -> Ref<Ticker> {
3501 TickerProviderStateMixin::create_ticker(self, app, on_tick)
3502 }
3503}
3504
3505impl State for InputDecoratorState {
3506 type Widget = InputDecorator;
3507 waft_widgets::state_accessors!();
3508
3509 fn init_state(self: Handle<Self>, app: &mut App) {
3510 let border_gap = InputBorderGap::new(app);
3511 app.get_mut(self).border_gap = Some(border_gap);
3512
3513 let floating_label_controller = AnimationController::new(
3514 app,
3515 None,
3516 Some(K_TRANSITION_DURATION),
3517 None,
3518 0.0,
3519 1.0,
3520 AnimationBehavior::Normal,
3521 self,
3522 );
3523 floating_label_controller
3524 .add_listener(app, Listener::handle_method(self, Self::handle_change));
3525 let floating_label_animation = CurvedAnimation::new(
3526 app,
3527 floating_label_controller.erase(),
3528 k_transition_curve(),
3529 Some(Rc::new(k_transition_curve().flipped())),
3530 );
3531
3532 let shaking_label_controller = AnimationController::new(
3533 app,
3534 None,
3535 Some(K_TRANSITION_DURATION),
3536 None,
3537 0.0,
3538 1.0,
3539 AnimationBehavior::Normal,
3540 self,
3541 );
3542 let state = app.get_mut(self);
3543 state.floating_label_controller = Some(floating_label_controller);
3544 state.floating_label_animation = Some(floating_label_animation);
3545 state.shaking_label_controller = Some(shaking_label_controller);
3546 }
3547
3548 fn did_change_dependencies(self: Handle<Self>, app: &mut App) {
3549 app.get_mut(self).effective_decoration = None;
3550
3551 let label_is_initially_floating = self.decoration(app).floating_label_behavior
3552 != Some(FloatingLabelBehavior::Never)
3553 && self.label_should_withdraw(app);
3554 let floating_label_controller = self.floating_label_controller(app);
3555 floating_label_controller.set_value(
3556 app,
3557 if label_is_initially_floating {
3558 1.0
3559 } else {
3560 0.0
3561 },
3562 );
3563 }
3564
3565 fn activate(self: Handle<Self>, app: &mut App) {
3566 TickerProviderStateMixin::activate(self, app);
3567 }
3568
3569 fn dispose(self: Handle<Self>, app: &mut App) {
3570 let floating_label_controller = self.floating_label_controller(app);
3571 let floating_label_animation = self.floating_label_animation(app);
3572 let shaking_label_controller = self.shaking_label_controller(app);
3573 let border_gap = self.border_gap(app);
3574 let curved_animation = app.get(self).curved_animation.handle();
3575 floating_label_controller.dispose(app);
3576 floating_label_animation.dispose(app);
3577 shaking_label_controller.dispose(app);
3578 ChangeNotifierData::dispose(app, &border_gap);
3579 if let Some(curved_animation) = curved_animation {
3580 curved_animation.dispose(app);
3581 }
3582 TickerProviderStateMixin::dispose(self, app);
3583 }
3584
3585 fn did_update_widget(self: Handle<Self>, app: &mut App, old: &InputDecorator) {
3586 if self.widget(app).decoration != old.decoration {
3587 app.get_mut(self).effective_decoration = None;
3588 }
3589
3590 let widget = self.widget(app);
3591 let float_behavior_changed =
3592 widget.decoration.floating_label_behavior != old.decoration.floating_label_behavior;
3593
3594 if widget.label_should_withdraw() != old.label_should_withdraw() || float_behavior_changed {
3595 let floating_label_controller = self.floating_label_controller(app);
3596 if self.floating_label_enabled(app) && self.label_should_withdraw(app) {
3597 floating_label_controller.forward(app, None);
3598 } else {
3599 floating_label_controller.reverse(app, None);
3600 }
3601 }
3602
3603 let error_text = self.decoration(app).error_text.clone();
3604 let old_error_text = &old.decoration.error_text;
3605
3606 let floating_label_controller = self.floating_label_controller(app);
3607 if floating_label_controller.is_completed(app)
3608 && error_text.is_some()
3609 && error_text != *old_error_text
3610 {
3611 let shaking_label_controller = self.shaking_label_controller(app);
3612 shaking_label_controller.set_value(app, 0.0);
3613 shaking_label_controller.forward(app, None);
3614 }
3615 }
3616
3617 fn build(self: Handle<Self>, app: &mut App, context: BuildContext) -> WidgetRef {
3618 let theme_data = Theme::of(app, context);
3619 let visual_density = self
3620 .decoration(app)
3621 .visual_density
3622 .unwrap_or(theme_data.visual_density);
3623 let use_material3 = Theme::of(app, context).use_material3;
3624 let defaults: Box<dyn InputDecorationThemeDataGetters> = if use_material3 {
3625 Box::new(InputDecoratorDefaultsM3::new(context))
3626 } else {
3627 Box::new(InputDecoratorDefaultsM2::new(context))
3628 };
3629 let icon_button_theme = IconButtonTheme::of(app, context);
3630
3631 let label_style = self.get_inline_label_style(app, &theme_data, &*defaults);
3632 let text_baseline = label_style
3633 .text_baseline
3634 .expect("the label style gives a text baseline");
3635
3636 let hint_style = self.get_inline_hint_style(app, &theme_data, &*defaults);
3637 let is_empty = self.is_empty(app);
3638 let has_inline_label = self.has_inline_label(app);
3639 let text_align = self.text_align(app);
3640 let decoration = self.decoration(app).clone();
3641 let hint_text = decoration.hint_text.clone();
3642 let maintain_hint_size = decoration.maintain_hint_size;
3643 let mut hint: Option<WidgetRef> = None;
3644 if decoration.hint.is_some() || hint_text.is_some() {
3645 let hint_widget = decoration.hint.clone().unwrap_or_else(|| {
3646 let mut text = Text::new(hint_text.clone().expect("a hint or its text is given"))
3647 .style(hint_style.clone());
3648 if let Some(text_direction) = decoration.hint_text_direction {
3649 text = text.text_direction(text_direction);
3650 }
3651 let overflow = hint_style
3652 .overflow
3653 .or(if decoration.hint_max_lines.is_none() {
3654 None
3655 } else {
3656 Some(TextOverflow::Ellipsis)
3657 });
3658 if let Some(overflow) = overflow {
3659 text = text.overflow(overflow);
3660 }
3661 if let Some(text_align) = text_align {
3662 text = text.text_align(text_align);
3663 }
3664 if let Some(max_lines) = decoration.hint_max_lines {
3665 text = text.max_lines(max_lines);
3666 }
3667 text.into_widget()
3668 });
3669 let show_hint = is_empty && !has_inline_label;
3670 let hint_fade_duration = decoration
3671 .hint_fade_duration
3672 .unwrap_or(K_HINT_FADE_TRANSITION_DURATION);
3673 hint = Some(if maintain_hint_size {
3674 AnimatedOpacity::new(if show_hint { 1.0 } else { 0.0 }, hint_fade_duration)
3675 .curve(k_transition_curve())
3676 .child(hint_widget)
3677 .into_widget()
3678 } else {
3679 AnimatedSwitcher::new(hint_fade_duration)
3680 .transition_builder(AnimatedSwitcherTransitionBuilder::handle_method(
3681 self,
3682 Self::build_transition,
3683 ))
3684 .layout_builder(Self::top_start_layout)
3685 .child(if show_hint {
3686 hint_widget
3687 } else {
3688 SizedBox::shrink().into_widget()
3689 })
3690 .into_widget()
3691 });
3692 }
3693
3694 let is_focused = self.is_focused(app);
3695 let has_error = self.has_error(app);
3696 let mut border: Option<InputBorderRef> = if !decoration.enabled {
3697 if has_error {
3698 decoration.error_border.clone()
3699 } else {
3700 decoration.disabled_border.clone()
3701 }
3702 } else if is_focused {
3703 if has_error {
3704 decoration.focused_error_border.clone()
3705 } else {
3706 decoration.focused_border.clone()
3707 }
3708 } else if has_error {
3709 decoration.error_border.clone()
3710 } else {
3711 decoration.enabled_border.clone()
3712 };
3713 let border = border
3714 .take()
3715 .unwrap_or_else(|| self.get_default_border(app, &theme_data, &*defaults));
3716
3717 let fill_color = self.get_fill_color(app, &theme_data, &*defaults);
3718 let hover_color = self.get_hover_color(app, &theme_data);
3719 let is_hovering = self.is_hovering(app);
3720 let container = BorderContainer {
3721 border: border.clone(),
3722 gap: self.border_gap(app),
3723 gap_animation: self.floating_label_animation(app).erase(),
3724 fill_color,
3725 hover_color,
3726 is_hovering,
3727 }
3728 .into_widget();
3729
3730 let label_should_withdraw = self.label_should_withdraw(app);
3731 let mut label: Option<WidgetRef> = None;
3732 if decoration.label_text.is_some() || decoration.label.is_some() {
3733 let should_show_label = self.should_show_label(app);
3734 let style = if label_should_withdraw {
3735 self.get_floating_label_style(app, &theme_data, &*defaults)
3736 } else {
3737 label_style.clone()
3738 };
3739 let label_child = decoration.label.clone().unwrap_or_else(|| {
3740 let mut text = Text::new(
3741 decoration
3742 .label_text
3743 .clone()
3744 .expect("a label or its text is given"),
3745 )
3746 .overflow(TextOverflow::Ellipsis);
3747 if let Some(text_align) = text_align {
3748 text = text.text_align(text_align);
3749 }
3750 if let Some(text_direction) = decoration.label_text_direction {
3751 text = text.text_direction(text_direction);
3752 }
3753 text.into_widget()
3754 });
3755 let shaking_label_controller = self.shaking_label_controller(app);
3756 label = Some(
3757 MatrixTransition::new(shaking_label_controller.erase(), |value: f64| {
3758 let shake_offset = if value <= 0.25 {
3759 -value
3760 } else if value < 0.75 {
3761 value - 0.5
3762 } else {
3763 (1.0 - value) * 4.0
3764 };
3765 Matrix4::translation((shake_offset * 4.0) as f32, 0.0)
3768 })
3769 .child(
3770 AnimatedOpacity::new(
3771 if should_show_label { 1.0 } else { 0.0 },
3772 K_TRANSITION_DURATION,
3773 )
3774 .curve(k_transition_curve())
3775 .child(
3776 AnimatedDefaultTextStyle::new(label_child, style, K_TRANSITION_DURATION)
3777 .curve(k_transition_curve()),
3778 ),
3779 )
3780 .into_widget(),
3781 );
3782 }
3783
3784 let has_prefix = decoration.prefix.is_some() || decoration.prefix_text.is_some();
3785 let has_suffix = decoration.suffix.is_some() || decoration.suffix_text.is_some();
3786
3787 let input = self.widget(app).child.clone();
3790
3791 let widget_state = self.widget_state(app);
3792 let prefix = has_prefix.then(|| {
3793 AffixText {
3794 label_is_floating: label_should_withdraw,
3795 text: decoration.prefix_text.clone(),
3796 style: Some(
3797 <dyn WidgetStateProperty<TextStyle>>::resolve_as(
3798 &decoration.prefix_style,
3799 &widget_state,
3800 )
3801 .map(|style| style.value().clone())
3802 .unwrap_or_else(|| hint_style.clone()),
3803 ),
3804 child: decoration.prefix.clone(),
3805 }
3806 .into_widget()
3807 });
3808
3809 let suffix = has_suffix.then(|| {
3810 AffixText {
3811 label_is_floating: label_should_withdraw,
3812 text: decoration.suffix_text.clone(),
3813 style: Some(
3814 <dyn WidgetStateProperty<TextStyle>>::resolve_as(
3815 &decoration.suffix_style,
3816 &widget_state,
3817 )
3818 .map(|style| style.value().clone())
3819 .unwrap_or_else(|| hint_style.clone()),
3820 ),
3821 child: decoration.suffix.clone(),
3822 }
3823 .into_widget()
3824 });
3825
3826 let decoration_is_dense = decoration.is_dense.unwrap_or(false);
3827 let icon_size = if decoration_is_dense { 18.0 } else { 24.0 };
3828
3829 let icon = match &decoration.icon {
3830 None => None,
3831 Some(decoration_icon) => {
3832 let icon_color = self.get_icon_color(app, &theme_data, &*defaults);
3833 Some(
3834 MouseRegion::new()
3835 .cursor(SystemMouseCursors::BASIC.into())
3836 .child(
3837 Padding::new(EdgeInsetsGeometry::directional(0.0, 0.0, 16.0, 0.0))
3838 .child(IconTheme::merge(
3839 None,
3840 IconThemeData::new().color(icon_color).size(icon_size),
3841 decoration_icon.clone(),
3842 )),
3843 )
3844 .into_widget(),
3845 )
3846 }
3847 };
3848
3849 let prefix_icon = match &decoration.prefix_icon {
3850 None => None,
3851 Some(decoration_prefix_icon) => {
3852 let prefix_icon_color =
3853 self.get_prefix_icon_color(app, &icon_button_theme, &*defaults);
3854 Some(Self::build_affix_icon(
3855 decoration_prefix_icon.clone(),
3856 decoration.prefix_icon_constraints,
3857 visual_density,
3858 prefix_icon_color,
3859 icon_size,
3860 &icon_button_theme,
3861 ))
3862 }
3863 };
3864
3865 let suffix_icon = match &decoration.suffix_icon {
3866 None => None,
3867 Some(decoration_suffix_icon) => {
3868 let suffix_icon_color =
3869 self.get_suffix_icon_color(app, &icon_button_theme, &*defaults);
3870 Some(Self::build_affix_icon(
3871 decoration_suffix_icon.clone(),
3872 decoration.suffix_icon_constraints,
3873 visual_density,
3874 suffix_icon_color,
3875 icon_size,
3876 &icon_button_theme,
3877 ))
3878 }
3879 };
3880
3881 let helper_style = self.get_helper_style(app, &theme_data, &*defaults);
3882 let error_style = self.get_error_style(app, &theme_data, &*defaults);
3883 let helper_error = HelperError {
3884 text_align,
3885 helper: decoration.helper.clone(),
3886 helper_text: decoration.helper_text.clone(),
3887 helper_style: Some(helper_style.clone()),
3888 helper_max_lines: decoration.helper_max_lines,
3889 error: decoration.error.clone(),
3890 error_text: decoration.error_text.clone(),
3891 error_style: Some(error_style),
3892 error_max_lines: decoration.error_max_lines,
3893 }
3894 .into_widget();
3895
3896 let mut counter: Option<WidgetRef> = None;
3897 if decoration.counter.is_some() {
3898 counter = decoration.counter.clone();
3899 } else if let Some(counter_text) = decoration
3900 .counter_text
3901 .as_ref()
3902 .filter(|counter_text| !counter_text.is_empty())
3903 {
3904 let counter_style = <dyn WidgetStateProperty<TextStyle>>::resolve_as(
3907 &decoration.counter_style,
3908 &widget_state,
3909 );
3910 counter = Some(
3911 Text::new(counter_text.clone())
3912 .style(helper_style.merge(counter_style.as_deref()))
3913 .overflow(TextOverflow::Ellipsis)
3914 .into_widget(),
3915 );
3916 }
3917
3918 let text_direction = Directionality::of(app, context);
3921 let flip_horizontal = match text_direction {
3922 TextDirection::Ltr => false,
3923 TextDirection::Rtl => true,
3924 };
3925 let resolved_padding = decoration
3926 .content_padding
3927 .map(|padding| padding.resolve(Some(text_direction)));
3928 let decoration_content_padding = resolved_padding.map(|resolved_padding| {
3929 EdgeInsetsDirectional::from_steb(
3930 if flip_horizontal {
3931 resolved_padding.right
3932 } else {
3933 resolved_padding.left
3934 },
3935 resolved_padding.top,
3936 if flip_horizontal {
3937 resolved_padding.left
3938 } else {
3939 resolved_padding.right
3940 },
3941 resolved_padding.bottom,
3942 )
3943 });
3944
3945 let resolved_supporting_text_padding = decoration
3946 .supporting_text_padding
3947 .map(|padding| padding.resolve(Some(text_direction)));
3948 let decoration_supporting_text_padding =
3949 resolved_supporting_text_padding.map(|resolved_supporting_text_padding| {
3950 EdgeInsetsDirectional::from_steb(
3951 if flip_horizontal {
3952 resolved_supporting_text_padding.right
3953 } else {
3954 resolved_supporting_text_padding.left
3955 },
3956 resolved_supporting_text_padding.top,
3957 if flip_horizontal {
3958 resolved_supporting_text_padding.left
3959 } else {
3960 resolved_supporting_text_padding.right
3961 },
3962 resolved_supporting_text_padding.bottom,
3963 )
3964 });
3965
3966 let content_padding: EdgeInsetsDirectional;
3967 let floating_label_height: f64;
3968
3969 if decoration.is_collapsed.expect("applied from the theme") {
3970 floating_label_height = 0.0;
3971 content_padding = decoration_content_padding.unwrap_or(EdgeInsetsDirectional::ZERO);
3972 } else if !border.is_outline() {
3973 floating_label_height = MediaQuery::text_scaler_of(app, context).scale(
3975 4.0 + 0.75
3976 * label_style
3977 .font_size
3978 .expect("the label style gives a font size"),
3979 );
3980 if decoration.filled.unwrap_or(false) {
3981 content_padding = decoration_content_padding.unwrap_or(if use_material3 {
3982 if decoration_is_dense {
3983 EdgeInsetsDirectional::from_steb(12.0, 4.0, 12.0, 4.0)
3984 } else {
3985 EdgeInsetsDirectional::from_steb(12.0, 8.0, 12.0, 8.0)
3986 }
3987 } else if decoration_is_dense {
3988 EdgeInsetsDirectional::from_steb(12.0, 8.0, 12.0, 8.0)
3989 } else {
3990 EdgeInsetsDirectional::from_steb(12.0, 12.0, 12.0, 12.0)
3991 });
3992 } else {
3993 content_padding = decoration_content_padding.unwrap_or(if use_material3 {
3997 if decoration_is_dense {
3998 EdgeInsetsDirectional::from_steb(0.0, 4.0, 0.0, 4.0)
3999 } else {
4000 EdgeInsetsDirectional::from_steb(0.0, 8.0, 0.0, 8.0)
4001 }
4002 } else if decoration_is_dense {
4003 EdgeInsetsDirectional::from_steb(0.0, 8.0, 0.0, 8.0)
4004 } else {
4005 EdgeInsetsDirectional::from_steb(0.0, 12.0, 0.0, 12.0)
4006 });
4007 }
4008 } else {
4009 floating_label_height = 0.0;
4010 content_padding = decoration_content_padding.unwrap_or(if use_material3 {
4011 if decoration_is_dense {
4012 EdgeInsetsDirectional::from_steb(12.0, 16.0, 12.0, 8.0)
4013 } else {
4014 EdgeInsetsDirectional::from_steb(12.0, 20.0, 12.0, 12.0)
4015 }
4016 } else if decoration_is_dense {
4017 EdgeInsetsDirectional::from_steb(12.0, 20.0, 12.0, 12.0)
4018 } else {
4019 EdgeInsetsDirectional::from_steb(12.0, 24.0, 12.0, 16.0)
4020 });
4021 }
4022
4023 let mut input_gap = 0.0;
4024 if use_material3 {
4025 if let Some(outline) = border.as_any().downcast_ref::<OutlineInputBorder>() {
4026 input_gap = outline.gap_padding;
4027 } else {
4028 input_gap = if border.is_outline() || decoration.filled.unwrap_or(false) {
4029 K_INPUT_EXTRA_PADDING
4030 } else {
4031 0.0
4032 };
4033 }
4034 }
4035
4036 let floating_label_progress = self.floating_label_animation(app).value(app);
4037 let border_gap = self.border_gap(app);
4038 let decorator = Decorator {
4039 decoration: Decoration {
4040 content_padding,
4041 is_collapsed: decoration.is_collapsed.expect("applied from the theme"),
4042 input_gap,
4043 floating_label_height,
4044 floating_label_alignment: decoration
4045 .floating_label_alignment
4046 .expect("applied from the theme"),
4047 floating_label_progress,
4048 border,
4049 border_gap,
4050 align_label_with_hint: decoration.align_label_with_hint.unwrap_or(false),
4051 is_dense: decoration.is_dense,
4052 is_empty,
4053 visual_density,
4054 maintain_hint_size,
4055 maintain_label_size: decoration.maintain_label_size,
4056 icon,
4057 input,
4058 label,
4059 hint,
4060 prefix,
4061 suffix,
4062 prefix_icon,
4063 suffix_icon,
4064 helper_error: Some(helper_error),
4065 counter,
4066 container: Some(container),
4067 supporting_text_padding: decoration_supporting_text_padding,
4068 },
4069 text_direction,
4070 text_baseline,
4071 text_align_vertical: self.widget(app).text_align_vertical,
4072 is_focused,
4073 expands: self.widget(app).expands,
4074 };
4075
4076 let result = decorator.into_widget();
4079
4080 if let Some(constraints) = decoration.constraints {
4081 return ConstrainedBox::new(constraints).child(result).into_widget();
4082 }
4083 result
4084 }
4085}
4086
4087impl InputDecoratorState {
4088 fn build_affix_icon(
4091 affix_icon: WidgetRef,
4092 constraints: Option<BoxConstraints>,
4093 visual_density: VisualDensity,
4094 color: Color,
4095 icon_size: f64,
4096 icon_button_theme: &IconButtonThemeData,
4097 ) -> WidgetRef {
4098 Center::new()
4100 .width_factor(1.0)
4101 .height_factor(1.0)
4102 .child(
4103 MouseRegion::new()
4104 .cursor(SystemMouseCursors::BASIC.into())
4105 .child(
4106 ConstrainedBox::new(constraints.unwrap_or_else(|| {
4107 visual_density.effective_constraints(BoxConstraints {
4108 min_width: K_MIN_INTERACTIVE_DIMENSION,
4109 min_height: K_MIN_INTERACTIVE_DIMENSION,
4110 ..BoxConstraints::new()
4111 })
4112 }))
4113 .child(IconTheme::merge(
4114 None,
4115 IconThemeData::new().color(color).size(icon_size),
4116 IconButtonTheme::new(
4117 IconButtonThemeData::new().style(
4118 ButtonStyle::new()
4119 .foreground_color(Rc::new(WidgetStatePropertyAll::new(
4120 Some(color),
4121 ))
4122 as WidgetStatePropertyRef<Option<Color>>)
4123 .icon_size(Rc::new(WidgetStatePropertyAll::new(Some(
4124 icon_size,
4125 )))
4126 as WidgetStatePropertyRef<Option<f64>>)
4127 .merge(icon_button_theme.style.as_ref()),
4128 ),
4129 affix_icon,
4130 ),
4131 )),
4132 ),
4133 )
4134 .into_widget()
4135 }
4136}
4137
4138impl Debug for InputDecoratorState {
4139 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
4140 f.debug_struct("_InputDecoratorState")
4141 .field("floating_label_controller", &self.floating_label_controller)
4142 .finish_non_exhaustive()
4143 }
4144}
4145
4146#[derive(Clone)]
4167pub struct InputDecoration {
4168 pub icon: Option<WidgetRef>,
4184
4185 pub icon_color: Option<AnyColor>,
4191
4192 pub label: Option<WidgetRef>,
4206
4207 pub label_text: Option<String>,
4219
4220 pub label_style: Option<AnyTextStyle>,
4237
4238 pub floating_label_style: Option<AnyTextStyle>,
4257
4258 pub label_text_direction: Option<TextDirection>,
4268
4269 pub helper: Option<WidgetRef>,
4277
4278 pub helper_text: Option<String>,
4289
4290 pub helper_style: Option<AnyTextStyle>,
4296
4297 pub helper_max_lines: Option<i32>,
4316
4317 pub hint_text: Option<String>,
4324
4325 pub hint: Option<WidgetRef>,
4329
4330 pub hint_style: Option<AnyTextStyle>,
4343
4344 pub hint_text_direction: Option<TextDirection>,
4354
4355 pub hint_max_lines: Option<i32>,
4363
4364 pub hint_fade_duration: Option<Duration>,
4369
4370 #[deprecated(
4378 note = "Use maintainHintSize instead. This will maintain both hint height and hint width. This feature was deprecated after v3.28.0-2.0.pre."
4379 )]
4380 pub maintain_hint_height: bool,
4381
4382 pub maintain_hint_size: bool,
4390
4391 pub maintain_label_size: bool,
4399
4400 pub error: Option<WidgetRef>,
4406
4407 pub error_text: Option<String>,
4419
4420 pub error_style: Option<AnyTextStyle>,
4431
4432 pub error_max_lines: Option<i32>,
4451
4452 pub floating_label_behavior: Option<FloatingLabelBehavior>,
4471
4472 pub floating_label_alignment: Option<FloatingLabelAlignment>,
4489
4490 pub is_dense: Option<bool>,
4495
4496 pub content_padding: Option<EdgeInsetsGeometry>,
4535
4536 pub is_collapsed: Option<bool>,
4543
4544 pub prefix_icon: Option<WidgetRef>,
4581
4582 pub prefix_icon_constraints: Option<BoxConstraints>,
4592
4593 pub prefix: Option<WidgetRef>,
4608
4609 pub prefix_text: Option<String>,
4623
4624 pub prefix_style: Option<AnyTextStyle>,
4636
4637 pub prefix_icon_color: Option<AnyColor>,
4645
4646 pub suffix_icon: Option<WidgetRef>,
4684
4685 pub suffix: Option<WidgetRef>,
4699
4700 pub suffix_text: Option<String>,
4714
4715 pub suffix_style: Option<AnyTextStyle>,
4727
4728 pub suffix_icon_color: Option<AnyColor>,
4736
4737 pub suffix_icon_constraints: Option<BoxConstraints>,
4750
4751 pub counter_text: Option<String>,
4761
4762 pub counter: Option<WidgetRef>,
4766
4767 pub counter_style: Option<AnyTextStyle>,
4775
4776 pub filled: Option<bool>,
4789
4790 pub fill_color: Option<AnyColor>,
4802
4803 pub focus_color: Option<Color>,
4817
4818 pub hover_color: Option<Color>,
4833
4834 pub error_border: Option<InputBorderRef>,
4857
4858 pub focused_border: Option<InputBorderRef>,
4881
4882 pub focused_error_border: Option<InputBorderRef>,
4905
4906 pub disabled_border: Option<InputBorderRef>,
4928
4929 pub enabled_border: Option<InputBorderRef>,
4951
4952 pub border: Option<InputBorderRef>,
4994
4995 pub enabled: bool,
4999
5000 pub semantic_counter_text: Option<String>,
5006
5007 pub align_label_with_hint: Option<bool>,
5013
5014 pub constraints: Option<BoxConstraints>,
5026
5027 pub visual_density: Option<VisualDensity>,
5051
5052 pub supporting_text_padding: Option<EdgeInsetsGeometry>,
5084}
5085
5086#[allow(deprecated)]
5087impl InputDecoration {
5088 pub fn new() -> InputDecoration {
5100 InputDecoration {
5101 icon: None,
5102 icon_color: None,
5103 label: None,
5104 label_text: None,
5105 label_style: None,
5106 floating_label_style: None,
5107 label_text_direction: None,
5108 helper: None,
5109 helper_text: None,
5110 helper_style: None,
5111 helper_max_lines: None,
5112 hint_text: None,
5113 hint: None,
5114 hint_style: None,
5115 hint_text_direction: None,
5116 hint_max_lines: None,
5117 hint_fade_duration: None,
5118 maintain_hint_height: true,
5119 maintain_hint_size: true,
5120 maintain_label_size: false,
5121 error: None,
5122 error_text: None,
5123 error_style: None,
5124 error_max_lines: None,
5125 floating_label_behavior: None,
5126 floating_label_alignment: None,
5127 is_dense: None,
5128 content_padding: None,
5129 is_collapsed: None,
5130 prefix_icon: None,
5131 prefix_icon_constraints: None,
5132 prefix: None,
5133 prefix_text: None,
5134 prefix_style: None,
5135 prefix_icon_color: None,
5136 suffix_icon: None,
5137 suffix: None,
5138 suffix_text: None,
5139 suffix_style: None,
5140 suffix_icon_color: None,
5141 suffix_icon_constraints: None,
5142 counter_text: None,
5143 counter: None,
5144 counter_style: None,
5145 filled: None,
5146 fill_color: None,
5147 focus_color: None,
5148 hover_color: None,
5149 error_border: None,
5150 focused_border: None,
5151 focused_error_border: None,
5152 disabled_border: None,
5153 enabled_border: None,
5154 border: None,
5155 enabled: true,
5156 semantic_counter_text: None,
5157 align_label_with_hint: None,
5158 constraints: None,
5159 visual_density: None,
5160 supporting_text_padding: None,
5161 }
5162 }
5163
5164 pub fn collapsed(hint_text: Option<String>) -> InputDecoration {
5174 InputDecoration {
5175 icon: None,
5176 icon_color: None,
5177 label: None,
5178 label_text: None,
5179 label_style: None,
5180 floating_label_style: None,
5181 label_text_direction: None,
5182 helper: None,
5183 helper_text: None,
5184 helper_style: None,
5185 helper_max_lines: None,
5186 hint_text,
5187 hint: None,
5188 hint_style: None,
5189 hint_text_direction: None,
5190 hint_max_lines: None,
5191 hint_fade_duration: None,
5192 maintain_hint_height: true,
5193 maintain_hint_size: true,
5194 maintain_label_size: false,
5195 error: None,
5196 error_text: None,
5197 error_style: None,
5198 error_max_lines: None,
5199 floating_label_behavior: None,
5200 floating_label_alignment: None,
5201 is_dense: Some(false),
5202 content_padding: Some(EdgeInsets::ZERO.into()),
5203 is_collapsed: Some(true),
5204 prefix_icon: None,
5205 prefix_icon_constraints: None,
5206 prefix: None,
5207 prefix_text: None,
5208 prefix_style: None,
5209 prefix_icon_color: None,
5210 suffix_icon: None,
5211 suffix: None,
5212 suffix_text: None,
5213 suffix_style: None,
5214 suffix_icon_color: None,
5215 suffix_icon_constraints: None,
5216 counter_text: None,
5217 counter: None,
5218 counter_style: None,
5219 filled: Some(false),
5220 fill_color: None,
5221 focus_color: None,
5222 hover_color: None,
5223 error_border: None,
5224 focused_border: None,
5225 focused_error_border: None,
5226 disabled_border: None,
5227 enabled_border: None,
5228 border: Some(InputBorderRef::none()),
5229 enabled: true,
5230 semantic_counter_text: None,
5231 align_label_with_hint: Some(false),
5232 constraints: None,
5233 visual_density: None,
5234 supporting_text_padding: None,
5235 }
5236 }
5237
5238 #[cfg_attr(debug_assertions, track_caller)]
5240 pub fn icon<K>(mut self, icon: impl IntoWidget<K>) -> InputDecoration {
5241 self.icon = Some(icon.into_widget());
5242 self
5243 }
5244
5245 pub fn icon_color(mut self, icon_color: impl Into<AnyColor>) -> InputDecoration {
5247 self.icon_color = Some(icon_color.into());
5248 self
5249 }
5250
5251 #[cfg_attr(debug_assertions, track_caller)]
5253 pub fn label<K>(mut self, label: impl IntoWidget<K>) -> InputDecoration {
5254 self.label = Some(label.into_widget());
5255 self
5256 }
5257
5258 pub fn label_text(mut self, label_text: impl Into<String>) -> InputDecoration {
5260 self.label_text = Some(label_text.into());
5261 self
5262 }
5263
5264 pub fn label_style(mut self, label_style: impl Into<AnyTextStyle>) -> InputDecoration {
5266 self.label_style = Some(label_style.into());
5267 self
5268 }
5269
5270 pub fn floating_label_style(
5272 mut self,
5273 floating_label_style: impl Into<AnyTextStyle>,
5274 ) -> InputDecoration {
5275 self.floating_label_style = Some(floating_label_style.into());
5276 self
5277 }
5278
5279 pub fn label_text_direction(mut self, label_text_direction: TextDirection) -> InputDecoration {
5281 self.label_text_direction = Some(label_text_direction);
5282 self
5283 }
5284
5285 #[cfg_attr(debug_assertions, track_caller)]
5287 pub fn helper<K>(mut self, helper: impl IntoWidget<K>) -> InputDecoration {
5288 self.helper = Some(helper.into_widget());
5289 self
5290 }
5291
5292 pub fn helper_text(mut self, helper_text: impl Into<String>) -> InputDecoration {
5294 self.helper_text = Some(helper_text.into());
5295 self
5296 }
5297
5298 pub fn helper_style(mut self, helper_style: impl Into<AnyTextStyle>) -> InputDecoration {
5300 self.helper_style = Some(helper_style.into());
5301 self
5302 }
5303
5304 pub fn helper_max_lines(mut self, helper_max_lines: i32) -> InputDecoration {
5306 self.helper_max_lines = Some(helper_max_lines);
5307 self
5308 }
5309
5310 pub fn hint_text(mut self, hint_text: impl Into<String>) -> InputDecoration {
5312 self.hint_text = Some(hint_text.into());
5313 self
5314 }
5315
5316 #[cfg_attr(debug_assertions, track_caller)]
5318 pub fn hint<K>(mut self, hint: impl IntoWidget<K>) -> InputDecoration {
5319 self.hint = Some(hint.into_widget());
5320 self
5321 }
5322
5323 pub fn hint_style(mut self, hint_style: impl Into<AnyTextStyle>) -> InputDecoration {
5325 self.hint_style = Some(hint_style.into());
5326 self
5327 }
5328
5329 pub fn hint_text_direction(mut self, hint_text_direction: TextDirection) -> InputDecoration {
5331 self.hint_text_direction = Some(hint_text_direction);
5332 self
5333 }
5334
5335 pub fn hint_max_lines(mut self, hint_max_lines: i32) -> InputDecoration {
5337 self.hint_max_lines = Some(hint_max_lines);
5338 self
5339 }
5340
5341 pub fn hint_fade_duration(mut self, hint_fade_duration: Duration) -> InputDecoration {
5343 self.hint_fade_duration = Some(hint_fade_duration);
5344 self
5345 }
5346
5347 #[deprecated(
5349 note = "Use maintainHintSize instead. This will maintain both hint height and hint width. This feature was deprecated after v3.28.0-2.0.pre."
5350 )]
5351 pub fn maintain_hint_height(mut self, maintain_hint_height: bool) -> InputDecoration {
5352 self.maintain_hint_height = maintain_hint_height;
5353 self
5354 }
5355
5356 pub fn maintain_hint_size(mut self, maintain_hint_size: bool) -> InputDecoration {
5358 self.maintain_hint_size = maintain_hint_size;
5359 self
5360 }
5361
5362 pub fn maintain_label_size(mut self, maintain_label_size: bool) -> InputDecoration {
5364 self.maintain_label_size = maintain_label_size;
5365 self
5366 }
5367
5368 #[cfg_attr(debug_assertions, track_caller)]
5370 pub fn error<K>(mut self, error: impl IntoWidget<K>) -> InputDecoration {
5371 self.error = Some(error.into_widget());
5372 self
5373 }
5374
5375 pub fn error_text(mut self, error_text: impl Into<String>) -> InputDecoration {
5377 self.error_text = Some(error_text.into());
5378 self
5379 }
5380
5381 pub fn error_style(mut self, error_style: impl Into<AnyTextStyle>) -> InputDecoration {
5383 self.error_style = Some(error_style.into());
5384 self
5385 }
5386
5387 pub fn error_max_lines(mut self, error_max_lines: i32) -> InputDecoration {
5389 self.error_max_lines = Some(error_max_lines);
5390 self
5391 }
5392
5393 pub fn floating_label_behavior(
5395 mut self,
5396 floating_label_behavior: FloatingLabelBehavior,
5397 ) -> InputDecoration {
5398 self.floating_label_behavior = Some(floating_label_behavior);
5399 self
5400 }
5401
5402 pub fn floating_label_alignment(
5404 mut self,
5405 floating_label_alignment: FloatingLabelAlignment,
5406 ) -> InputDecoration {
5407 self.floating_label_alignment = Some(floating_label_alignment);
5408 self
5409 }
5410
5411 pub fn is_collapsed(mut self, is_collapsed: bool) -> InputDecoration {
5413 self.is_collapsed = Some(is_collapsed);
5414 self
5415 }
5416
5417 pub fn is_dense(mut self, is_dense: bool) -> InputDecoration {
5419 self.is_dense = Some(is_dense);
5420 self
5421 }
5422
5423 pub fn content_padding(
5425 mut self,
5426 content_padding: impl Into<EdgeInsetsGeometry>,
5427 ) -> InputDecoration {
5428 let content_padding = content_padding.into();
5429 self.content_padding = Some(content_padding);
5430 self
5431 }
5432
5433 #[cfg_attr(debug_assertions, track_caller)]
5435 pub fn prefix_icon<K>(mut self, prefix_icon: impl IntoWidget<K>) -> InputDecoration {
5436 self.prefix_icon = Some(prefix_icon.into_widget());
5437 self
5438 }
5439
5440 pub fn prefix_icon_constraints(
5442 mut self,
5443 prefix_icon_constraints: BoxConstraints,
5444 ) -> InputDecoration {
5445 self.prefix_icon_constraints = Some(prefix_icon_constraints);
5446 self
5447 }
5448
5449 #[cfg_attr(debug_assertions, track_caller)]
5451 pub fn prefix<K>(mut self, prefix: impl IntoWidget<K>) -> InputDecoration {
5452 self.prefix = Some(prefix.into_widget());
5453 self
5454 }
5455
5456 pub fn prefix_text(mut self, prefix_text: impl Into<String>) -> InputDecoration {
5458 self.prefix_text = Some(prefix_text.into());
5459 self
5460 }
5461
5462 pub fn prefix_style(mut self, prefix_style: impl Into<AnyTextStyle>) -> InputDecoration {
5464 self.prefix_style = Some(prefix_style.into());
5465 self
5466 }
5467
5468 pub fn prefix_icon_color(mut self, prefix_icon_color: impl Into<AnyColor>) -> InputDecoration {
5470 self.prefix_icon_color = Some(prefix_icon_color.into());
5471 self
5472 }
5473
5474 #[cfg_attr(debug_assertions, track_caller)]
5476 pub fn suffix_icon<K>(mut self, suffix_icon: impl IntoWidget<K>) -> InputDecoration {
5477 self.suffix_icon = Some(suffix_icon.into_widget());
5478 self
5479 }
5480
5481 #[cfg_attr(debug_assertions, track_caller)]
5483 pub fn suffix<K>(mut self, suffix: impl IntoWidget<K>) -> InputDecoration {
5484 self.suffix = Some(suffix.into_widget());
5485 self
5486 }
5487
5488 pub fn suffix_text(mut self, suffix_text: impl Into<String>) -> InputDecoration {
5490 self.suffix_text = Some(suffix_text.into());
5491 self
5492 }
5493
5494 pub fn suffix_style(mut self, suffix_style: impl Into<AnyTextStyle>) -> InputDecoration {
5496 self.suffix_style = Some(suffix_style.into());
5497 self
5498 }
5499
5500 pub fn suffix_icon_color(mut self, suffix_icon_color: impl Into<AnyColor>) -> InputDecoration {
5502 self.suffix_icon_color = Some(suffix_icon_color.into());
5503 self
5504 }
5505
5506 pub fn suffix_icon_constraints(
5508 mut self,
5509 suffix_icon_constraints: BoxConstraints,
5510 ) -> InputDecoration {
5511 self.suffix_icon_constraints = Some(suffix_icon_constraints);
5512 self
5513 }
5514
5515 #[cfg_attr(debug_assertions, track_caller)]
5517 pub fn counter<K>(mut self, counter: impl IntoWidget<K>) -> InputDecoration {
5518 self.counter = Some(counter.into_widget());
5519 self
5520 }
5521
5522 pub fn counter_text(mut self, counter_text: impl Into<String>) -> InputDecoration {
5524 self.counter_text = Some(counter_text.into());
5525 self
5526 }
5527
5528 pub fn counter_style(mut self, counter_style: impl Into<AnyTextStyle>) -> InputDecoration {
5530 self.counter_style = Some(counter_style.into());
5531 self
5532 }
5533
5534 pub fn filled(mut self, filled: bool) -> InputDecoration {
5536 self.filled = Some(filled);
5537 self
5538 }
5539
5540 pub fn fill_color(mut self, fill_color: impl Into<AnyColor>) -> InputDecoration {
5542 self.fill_color = Some(fill_color.into());
5543 self
5544 }
5545
5546 pub fn focus_color(mut self, focus_color: Color) -> InputDecoration {
5548 self.focus_color = Some(focus_color);
5549 self
5550 }
5551
5552 pub fn hover_color(mut self, hover_color: Color) -> InputDecoration {
5554 self.hover_color = Some(hover_color);
5555 self
5556 }
5557
5558 pub fn error_border(mut self, error_border: impl Into<InputBorderRef>) -> InputDecoration {
5560 self.error_border = Some(error_border.into());
5561 self
5562 }
5563
5564 pub fn focused_border(mut self, focused_border: impl Into<InputBorderRef>) -> InputDecoration {
5566 self.focused_border = Some(focused_border.into());
5567 self
5568 }
5569
5570 pub fn focused_error_border(
5572 mut self,
5573 focused_error_border: impl Into<InputBorderRef>,
5574 ) -> InputDecoration {
5575 self.focused_error_border = Some(focused_error_border.into());
5576 self
5577 }
5578
5579 pub fn disabled_border(
5581 mut self,
5582 disabled_border: impl Into<InputBorderRef>,
5583 ) -> InputDecoration {
5584 self.disabled_border = Some(disabled_border.into());
5585 self
5586 }
5587
5588 pub fn enabled_border(mut self, enabled_border: impl Into<InputBorderRef>) -> InputDecoration {
5590 self.enabled_border = Some(enabled_border.into());
5591 self
5592 }
5593
5594 pub fn border(mut self, border: impl Into<InputBorderRef>) -> InputDecoration {
5596 self.border = Some(border.into());
5597 self
5598 }
5599
5600 pub fn enabled(mut self, enabled: bool) -> InputDecoration {
5602 self.enabled = enabled;
5603 self
5604 }
5605
5606 pub fn semantic_counter_text(
5608 mut self,
5609 semantic_counter_text: impl Into<String>,
5610 ) -> InputDecoration {
5611 self.semantic_counter_text = Some(semantic_counter_text.into());
5612 self
5613 }
5614
5615 pub fn align_label_with_hint(mut self, align_label_with_hint: bool) -> InputDecoration {
5617 self.align_label_with_hint = Some(align_label_with_hint);
5618 self
5619 }
5620
5621 pub fn constraints(mut self, constraints: BoxConstraints) -> InputDecoration {
5623 self.constraints = Some(constraints);
5624 self
5625 }
5626
5627 pub fn visual_density(mut self, visual_density: VisualDensity) -> InputDecoration {
5629 self.visual_density = Some(visual_density);
5630 self
5631 }
5632
5633 pub fn supporting_text_padding(
5635 mut self,
5636 supporting_text_padding: impl Into<EdgeInsetsGeometry>,
5637 ) -> InputDecoration {
5638 let supporting_text_padding = supporting_text_padding.into();
5639 self.supporting_text_padding = Some(supporting_text_padding);
5640 self
5641 }
5642
5643 pub(crate) fn debug_check_arguments(&self) {
5647 debug_assert!(
5648 !(self.label.is_some() && self.label_text.is_some()),
5649 "Declaring both label and labelText is not supported."
5650 );
5651 debug_assert!(
5652 self.hint.is_none() || self.hint_text.is_none(),
5653 "Declaring both hint and hintText is not supported."
5654 );
5655 debug_assert!(
5656 !(self.helper.is_some() && self.helper_text.is_some()),
5657 "Declaring both helper and helperText is not supported."
5658 );
5659 debug_assert!(
5660 !(self.prefix.is_some() && self.prefix_text.is_some()),
5661 "Declaring both prefix and prefixText is not supported."
5662 );
5663 debug_assert!(
5664 !(self.suffix.is_some() && self.suffix_text.is_some()),
5665 "Declaring both suffix and suffixText is not supported."
5666 );
5667 debug_assert!(
5668 !(self.error.is_some() && self.error_text.is_some()),
5669 "Declaring both error and errorText is not supported."
5670 );
5671 }
5672
5673 pub fn copy_with(&self) -> InputDecoration {
5679 self.clone()
5680 }
5681
5682 pub fn apply_defaults(
5691 &self,
5692 input_decoration_theme: impl Into<InputDecorationThemeData>,
5693 ) -> InputDecoration {
5694 let theme: InputDecorationThemeData = input_decoration_theme.into();
5695 let mut decoration = self.copy_with();
5696 decoration.label_style = self
5697 .label_style
5698 .clone()
5699 .or_else(|| theme.label_style.clone());
5700 decoration.floating_label_style = self
5701 .floating_label_style
5702 .clone()
5703 .or_else(|| theme.floating_label_style.clone());
5704 decoration.helper_style = self
5705 .helper_style
5706 .clone()
5707 .or_else(|| theme.helper_style.clone());
5708 decoration.helper_max_lines = self.helper_max_lines.or(theme.helper_max_lines);
5709 decoration.hint_style = self.hint_style.clone().or_else(|| theme.hint_style.clone());
5710 decoration.hint_fade_duration = self.hint_fade_duration.or(theme.hint_fade_duration);
5711 decoration.hint_max_lines = self.hint_max_lines.or(theme.hint_max_lines);
5712 decoration.error_style = self
5713 .error_style
5714 .clone()
5715 .or_else(|| theme.error_style.clone());
5716 decoration.error_max_lines = self.error_max_lines.or(theme.error_max_lines);
5717 decoration.floating_label_behavior = self
5718 .floating_label_behavior
5719 .or(Some(theme.floating_label_behavior));
5720 decoration.floating_label_alignment = self
5721 .floating_label_alignment
5722 .or(Some(theme.floating_label_alignment));
5723 decoration.is_dense = self.is_dense.or(Some(theme.is_dense));
5724 decoration.content_padding = self.content_padding.or(theme.content_padding);
5725 decoration.is_collapsed = self.is_collapsed.or(Some(theme.is_collapsed));
5726 decoration.icon_color = self.icon_color.clone().or_else(|| theme.icon_color.clone());
5727 decoration.prefix_style = self
5728 .prefix_style
5729 .clone()
5730 .or_else(|| theme.prefix_style.clone());
5731 decoration.prefix_icon_color = self
5732 .prefix_icon_color
5733 .clone()
5734 .or_else(|| theme.prefix_icon_color.clone());
5735 decoration.prefix_icon_constraints = self
5736 .prefix_icon_constraints
5737 .or(theme.prefix_icon_constraints);
5738 decoration.suffix_style = self
5739 .suffix_style
5740 .clone()
5741 .or_else(|| theme.suffix_style.clone());
5742 decoration.suffix_icon_color = self
5743 .suffix_icon_color
5744 .clone()
5745 .or_else(|| theme.suffix_icon_color.clone());
5746 decoration.suffix_icon_constraints = self
5747 .suffix_icon_constraints
5748 .or(theme.suffix_icon_constraints);
5749 decoration.counter_style = self
5750 .counter_style
5751 .clone()
5752 .or_else(|| theme.counter_style.clone());
5753 decoration.filled = self.filled.or(Some(theme.filled));
5754 decoration.fill_color = self.fill_color.clone().or_else(|| theme.fill_color.clone());
5755 decoration.focus_color = self.focus_color.or(theme.focus_color);
5756 decoration.hover_color = self.hover_color.or(theme.hover_color);
5757 decoration.error_border = self
5758 .error_border
5759 .clone()
5760 .or_else(|| theme.error_border.clone());
5761 decoration.focused_border = self
5762 .focused_border
5763 .clone()
5764 .or_else(|| theme.focused_border.clone());
5765 decoration.focused_error_border = self
5766 .focused_error_border
5767 .clone()
5768 .or_else(|| theme.focused_error_border.clone());
5769 decoration.disabled_border = self
5770 .disabled_border
5771 .clone()
5772 .or_else(|| theme.disabled_border.clone());
5773 decoration.enabled_border = self
5774 .enabled_border
5775 .clone()
5776 .or_else(|| theme.enabled_border.clone());
5777 decoration.border = self.border.clone().or_else(|| theme.border.clone());
5778 decoration.align_label_with_hint = self
5779 .align_label_with_hint
5780 .or(Some(theme.align_label_with_hint));
5781 decoration.constraints = self.constraints.or(theme.constraints);
5782 decoration.visual_density = self.visual_density.or(theme.visual_density);
5783 decoration.supporting_text_padding = self
5784 .supporting_text_padding
5785 .or(theme.supporting_text_padding);
5786 decoration
5787 }
5788}
5789
5790impl Default for InputDecoration {
5791 fn default() -> InputDecoration {
5792 InputDecoration::new()
5793 }
5794}
5795
5796#[allow(deprecated)]
5797impl PartialEq for InputDecoration {
5798 fn eq(&self, other: &InputDecoration) -> bool {
5800 same_widget(&other.icon, &self.icon)
5801 && other.icon_color == self.icon_color
5802 && same_widget(&other.label, &self.label)
5803 && other.label_text == self.label_text
5804 && other.label_style == self.label_style
5805 && other.floating_label_style == self.floating_label_style
5806 && other.label_text_direction == self.label_text_direction
5807 && same_widget(&other.helper, &self.helper)
5808 && other.helper_text == self.helper_text
5809 && other.helper_style == self.helper_style
5810 && other.helper_max_lines == self.helper_max_lines
5811 && other.hint_text == self.hint_text
5812 && same_widget(&other.hint, &self.hint)
5813 && other.hint_style == self.hint_style
5814 && other.hint_text_direction == self.hint_text_direction
5815 && other.hint_max_lines == self.hint_max_lines
5816 && other.hint_fade_duration == self.hint_fade_duration
5817 && other.maintain_hint_height == self.maintain_hint_height
5818 && other.maintain_hint_size == self.maintain_hint_size
5819 && other.maintain_label_size == self.maintain_label_size
5820 && same_widget(&other.error, &self.error)
5821 && other.error_text == self.error_text
5822 && other.error_style == self.error_style
5823 && other.error_max_lines == self.error_max_lines
5824 && other.floating_label_behavior == self.floating_label_behavior
5825 && other.floating_label_alignment == self.floating_label_alignment
5826 && other.is_dense == self.is_dense
5827 && other.content_padding == self.content_padding
5828 && other.is_collapsed == self.is_collapsed
5829 && same_widget(&other.prefix_icon, &self.prefix_icon)
5830 && other.prefix_icon_color == self.prefix_icon_color
5831 && same_widget(&other.prefix, &self.prefix)
5832 && other.prefix_text == self.prefix_text
5833 && other.prefix_style == self.prefix_style
5834 && other.prefix_icon_constraints == self.prefix_icon_constraints
5835 && same_widget(&other.suffix_icon, &self.suffix_icon)
5836 && other.suffix_icon_color == self.suffix_icon_color
5837 && same_widget(&other.suffix, &self.suffix)
5838 && other.suffix_text == self.suffix_text
5839 && other.suffix_style == self.suffix_style
5840 && other.suffix_icon_constraints == self.suffix_icon_constraints
5841 && same_widget(&other.counter, &self.counter)
5842 && other.counter_text == self.counter_text
5843 && other.counter_style == self.counter_style
5844 && other.filled == self.filled
5845 && other.fill_color == self.fill_color
5846 && other.focus_color == self.focus_color
5847 && other.hover_color == self.hover_color
5848 && other.error_border == self.error_border
5849 && other.focused_border == self.focused_border
5850 && other.focused_error_border == self.focused_error_border
5851 && other.disabled_border == self.disabled_border
5852 && other.enabled_border == self.enabled_border
5853 && other.border == self.border
5854 && other.enabled == self.enabled
5855 && other.semantic_counter_text == self.semantic_counter_text
5856 && other.align_label_with_hint == self.align_label_with_hint
5857 && other.constraints == self.constraints
5858 && other.visual_density == self.visual_density
5859 && other.supporting_text_padding == self.supporting_text_padding
5860 }
5861}
5862
5863#[allow(deprecated)]
5865impl Debug for InputDecoration {
5866 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
5867 let mut description: Vec<String> = Vec::new();
5868 if let Some(icon) = &self.icon {
5869 description.push(format!("icon: {:?}", icon));
5870 }
5871 if let Some(icon_color) = &self.icon_color {
5872 description.push(format!("iconColor: {:?}", icon_color));
5873 }
5874 if let Some(label) = &self.label {
5875 description.push(format!("label: {:?}", label));
5876 }
5877 if let Some(label_text) = &self.label_text {
5878 description.push(format!("labelText: \"{}\"", label_text));
5879 }
5880 if let Some(floating_label_style) = &self.floating_label_style {
5881 description.push(format!(
5882 "floatingLabelStyle: \"{:?}\"",
5883 floating_label_style
5884 ));
5885 }
5886 if let Some(helper) = &self.helper {
5887 description.push(format!("helper: \"{:?}\"", helper));
5888 }
5889 if let Some(helper_text) = &self.helper_text {
5890 description.push(format!("helperText: \"{}\"", helper_text));
5891 }
5892 if let Some(helper_max_lines) = &self.helper_max_lines {
5893 description.push(format!("helperMaxLines: \"{:?}\"", helper_max_lines));
5894 }
5895 if let Some(hint_text) = &self.hint_text {
5896 description.push(format!("hintText: \"{}\"", hint_text));
5897 }
5898 if let Some(hint) = &self.hint {
5899 description.push(format!("hint: {:?}", hint));
5900 }
5901 if let Some(hint_max_lines) = &self.hint_max_lines {
5902 description.push(format!("hintMaxLines: \"{:?}\"", hint_max_lines));
5903 }
5904 if let Some(hint_fade_duration) = &self.hint_fade_duration {
5905 description.push(format!("hintFadeDuration: \"{:?}\"", hint_fade_duration));
5906 }
5907 if !self.maintain_hint_height {
5908 description.push("maintainHintHeight: false".to_owned());
5909 }
5910 if !self.maintain_hint_size {
5911 description.push("maintainHintSize: false".to_owned());
5912 }
5913 if self.maintain_label_size {
5914 description.push("maintainLabelSize: true".to_owned());
5915 }
5916 if let Some(error) = &self.error {
5917 description.push(format!("error: \"{:?}\"", error));
5918 }
5919 if let Some(error_text) = &self.error_text {
5920 description.push(format!("errorText: \"{}\"", error_text));
5921 }
5922 if let Some(error_style) = &self.error_style {
5923 description.push(format!("errorStyle: \"{:?}\"", error_style));
5924 }
5925 if let Some(error_max_lines) = &self.error_max_lines {
5926 description.push(format!("errorMaxLines: \"{:?}\"", error_max_lines));
5927 }
5928 if let Some(floating_label_behavior) = &self.floating_label_behavior {
5929 description.push(format!(
5930 "floatingLabelBehavior: {:?}",
5931 floating_label_behavior
5932 ));
5933 }
5934 if let Some(floating_label_alignment) = &self.floating_label_alignment {
5935 description.push(format!(
5936 "floatingLabelAlignment: {:?}",
5937 floating_label_alignment
5938 ));
5939 }
5940 if self.is_dense.unwrap_or(false) {
5941 description.push(format!("isDense: {:?}", self.is_dense.expect("checked")));
5942 }
5943 if let Some(content_padding) = &self.content_padding {
5944 description.push(format!("contentPadding: {:?}", content_padding));
5945 }
5946 if self.is_collapsed.unwrap_or(false) {
5947 description.push(format!(
5948 "isCollapsed: {:?}",
5949 self.is_collapsed.expect("checked")
5950 ));
5951 }
5952 if let Some(prefix_icon) = &self.prefix_icon {
5953 description.push(format!("prefixIcon: {:?}", prefix_icon));
5954 }
5955 if let Some(prefix_icon_color) = &self.prefix_icon_color {
5956 description.push(format!("prefixIconColor: {:?}", prefix_icon_color));
5957 }
5958 if let Some(prefix) = &self.prefix {
5959 description.push(format!("prefix: {:?}", prefix));
5960 }
5961 if let Some(prefix_text) = &self.prefix_text {
5962 description.push(format!("prefixText: {}", prefix_text));
5963 }
5964 if let Some(prefix_style) = &self.prefix_style {
5965 description.push(format!("prefixStyle: {:?}", prefix_style));
5966 }
5967 if let Some(prefix_icon_constraints) = &self.prefix_icon_constraints {
5968 description.push(format!(
5969 "prefixIconConstraints: {:?}",
5970 prefix_icon_constraints
5971 ));
5972 }
5973 if let Some(suffix_icon) = &self.suffix_icon {
5974 description.push(format!("suffixIcon: {:?}", suffix_icon));
5975 }
5976 if let Some(suffix_icon_color) = &self.suffix_icon_color {
5977 description.push(format!("suffixIconColor: {:?}", suffix_icon_color));
5978 }
5979 if let Some(suffix) = &self.suffix {
5980 description.push(format!("suffix: {:?}", suffix));
5981 }
5982 if let Some(suffix_text) = &self.suffix_text {
5983 description.push(format!("suffixText: {}", suffix_text));
5984 }
5985 if let Some(suffix_style) = &self.suffix_style {
5986 description.push(format!("suffixStyle: {:?}", suffix_style));
5987 }
5988 if let Some(suffix_icon_constraints) = &self.suffix_icon_constraints {
5989 description.push(format!(
5990 "suffixIconConstraints: {:?}",
5991 suffix_icon_constraints
5992 ));
5993 }
5994 if let Some(counter) = &self.counter {
5995 description.push(format!("counter: {:?}", counter));
5996 }
5997 if let Some(counter_text) = &self.counter_text {
5998 description.push(format!("counterText: {}", counter_text));
5999 }
6000 if let Some(counter_style) = &self.counter_style {
6001 description.push(format!("counterStyle: {:?}", counter_style));
6002 }
6003 if self.filled.unwrap_or(false) {
6004 description.push("filled: true".to_owned());
6005 }
6006 if let Some(fill_color) = &self.fill_color {
6007 description.push(format!("fillColor: {:?}", fill_color));
6008 }
6009 if let Some(focus_color) = &self.focus_color {
6010 description.push(format!("focusColor: {:?}", focus_color));
6011 }
6012 if let Some(hover_color) = &self.hover_color {
6013 description.push(format!("hoverColor: {:?}", hover_color));
6014 }
6015 if let Some(error_border) = &self.error_border {
6016 description.push(format!("errorBorder: {:?}", error_border));
6017 }
6018 if let Some(focused_border) = &self.focused_border {
6019 description.push(format!("focusedBorder: {:?}", focused_border));
6020 }
6021 if let Some(focused_error_border) = &self.focused_error_border {
6022 description.push(format!("focusedErrorBorder: {:?}", focused_error_border));
6023 }
6024 if let Some(disabled_border) = &self.disabled_border {
6025 description.push(format!("disabledBorder: {:?}", disabled_border));
6026 }
6027 if let Some(enabled_border) = &self.enabled_border {
6028 description.push(format!("enabledBorder: {:?}", enabled_border));
6029 }
6030 if let Some(border) = &self.border {
6031 description.push(format!("border: {:?}", border));
6032 }
6033 if !self.enabled {
6034 description.push("enabled: false".to_owned());
6035 }
6036 if let Some(semantic_counter_text) = &self.semantic_counter_text {
6037 description.push(format!("semanticCounterText: {}", semantic_counter_text));
6038 }
6039 if let Some(align_label_with_hint) = &self.align_label_with_hint {
6040 description.push(format!("alignLabelWithHint: {:?}", align_label_with_hint));
6041 }
6042 if let Some(constraints) = &self.constraints {
6043 description.push(format!("constraints: {:?}", constraints));
6044 }
6045 if let Some(visual_density) = &self.visual_density {
6046 description.push(format!("visualDensity: {visual_density}"));
6047 }
6048 if let Some(supporting_text_padding) = &self.supporting_text_padding {
6049 description.push(format!(
6050 "supportingTextPadding: {:?}",
6051 supporting_text_padding
6052 ));
6053 }
6054 write!(f, "InputDecoration({})", description.join(", "))
6055 }
6056}
6057
6058impl fmt::Display for InputDecoration {
6060 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
6061 fmt::Debug::fmt(self, f)
6062 }
6063}
6064
6065#[derive(Clone, Debug)]
6083pub struct InputDecorationTheme {
6084 pub key: Option<KeyRef>,
6085 data: Option<InputDecorationThemeData>,
6086 label_style: Option<AnyTextStyle>,
6087 floating_label_style: Option<AnyTextStyle>,
6088 helper_style: Option<AnyTextStyle>,
6089 helper_max_lines: Option<i32>,
6090 hint_style: Option<AnyTextStyle>,
6091 hint_fade_duration: Option<Duration>,
6092 hint_max_lines: Option<i32>,
6093 error_style: Option<AnyTextStyle>,
6094 error_max_lines: Option<i32>,
6095 floating_label_behavior: Option<FloatingLabelBehavior>,
6096 floating_label_alignment: Option<FloatingLabelAlignment>,
6097 is_dense: Option<bool>,
6098 content_padding: Option<EdgeInsetsGeometry>,
6099 is_collapsed: Option<bool>,
6100 icon_color: Option<AnyColor>,
6101 prefix_style: Option<AnyTextStyle>,
6102 prefix_icon_color: Option<AnyColor>,
6103 prefix_icon_constraints: Option<BoxConstraints>,
6104 suffix_style: Option<AnyTextStyle>,
6105 suffix_icon_color: Option<AnyColor>,
6106 suffix_icon_constraints: Option<BoxConstraints>,
6107 counter_style: Option<AnyTextStyle>,
6108 filled: Option<bool>,
6109 fill_color: Option<AnyColor>,
6110 active_indicator_border: Option<AnyBorderSide>,
6111 outline_border: Option<AnyBorderSide>,
6112 focus_color: Option<Color>,
6113 hover_color: Option<Color>,
6114 error_border: Option<InputBorderRef>,
6115 focused_border: Option<InputBorderRef>,
6116 focused_error_border: Option<InputBorderRef>,
6117 disabled_border: Option<InputBorderRef>,
6118 enabled_border: Option<InputBorderRef>,
6119 border: Option<InputBorderRef>,
6120 align_label_with_hint: Option<bool>,
6121 constraints: Option<BoxConstraints>,
6122 visual_density: Option<VisualDensity>,
6123 child: WidgetRef,
6124}
6125
6126impl InputDecorationTheme {
6127 pub fn new() -> InputDecorationTheme {
6130 InputDecorationTheme {
6131 key: None,
6132 data: None,
6133 label_style: None,
6134 floating_label_style: None,
6135 helper_style: None,
6136 helper_max_lines: None,
6137 hint_style: None,
6138 hint_fade_duration: None,
6139 hint_max_lines: None,
6140 error_style: None,
6141 error_max_lines: None,
6142 floating_label_behavior: None,
6143 floating_label_alignment: None,
6144 is_dense: None,
6145 content_padding: None,
6146 is_collapsed: None,
6147 icon_color: None,
6148 prefix_style: None,
6149 prefix_icon_color: None,
6150 prefix_icon_constraints: None,
6151 suffix_style: None,
6152 suffix_icon_color: None,
6153 suffix_icon_constraints: None,
6154 counter_style: None,
6155 filled: None,
6156 fill_color: None,
6157 active_indicator_border: None,
6158 outline_border: None,
6159 focus_color: None,
6160 hover_color: None,
6161 error_border: None,
6162 focused_border: None,
6163 focused_error_border: None,
6164 disabled_border: None,
6165 enabled_border: None,
6166 border: None,
6167 align_label_with_hint: None,
6168 constraints: None,
6169 visual_density: None,
6170 child: SizedBox::shrink().into_widget(),
6171 }
6172 }
6173
6174 pub fn key(mut self, key: KeyRef) -> InputDecorationTheme {
6176 self.key = Some(key);
6177 self
6178 }
6179
6180 #[cfg_attr(debug_assertions, track_caller)]
6182 pub fn child<K>(mut self, child: impl IntoWidget<K>) -> InputDecorationTheme {
6183 self.child = child.into_widget();
6184 self
6185 }
6186
6187 pub fn with_data(mut self, data: InputDecorationThemeData) -> InputDecorationTheme {
6189 debug_assert!(self.has_no_property());
6190 self.data = Some(data);
6191 self
6192 }
6193
6194 pub fn with_label_style(
6196 mut self,
6197 label_style: impl Into<AnyTextStyle>,
6198 ) -> InputDecorationTheme {
6199 debug_assert!(self.data.is_none());
6200 self.label_style = Some(label_style.into());
6201 self
6202 }
6203
6204 pub fn with_floating_label_style(
6206 mut self,
6207 floating_label_style: impl Into<AnyTextStyle>,
6208 ) -> InputDecorationTheme {
6209 debug_assert!(self.data.is_none());
6210 self.floating_label_style = Some(floating_label_style.into());
6211 self
6212 }
6213
6214 pub fn with_helper_style(
6216 mut self,
6217 helper_style: impl Into<AnyTextStyle>,
6218 ) -> InputDecorationTheme {
6219 debug_assert!(self.data.is_none());
6220 self.helper_style = Some(helper_style.into());
6221 self
6222 }
6223
6224 pub fn with_helper_max_lines(mut self, helper_max_lines: i32) -> InputDecorationTheme {
6226 debug_assert!(self.data.is_none());
6227 self.helper_max_lines = Some(helper_max_lines);
6228 self
6229 }
6230
6231 pub fn with_hint_style(mut self, hint_style: impl Into<AnyTextStyle>) -> InputDecorationTheme {
6233 debug_assert!(self.data.is_none());
6234 self.hint_style = Some(hint_style.into());
6235 self
6236 }
6237
6238 pub fn with_hint_fade_duration(mut self, hint_fade_duration: Duration) -> InputDecorationTheme {
6240 debug_assert!(self.data.is_none());
6241 self.hint_fade_duration = Some(hint_fade_duration);
6242 self
6243 }
6244
6245 pub fn with_hint_max_lines(mut self, hint_max_lines: i32) -> InputDecorationTheme {
6247 debug_assert!(self.data.is_none());
6248 self.hint_max_lines = Some(hint_max_lines);
6249 self
6250 }
6251
6252 pub fn with_error_style(
6254 mut self,
6255 error_style: impl Into<AnyTextStyle>,
6256 ) -> InputDecorationTheme {
6257 debug_assert!(self.data.is_none());
6258 self.error_style = Some(error_style.into());
6259 self
6260 }
6261
6262 pub fn with_error_max_lines(mut self, error_max_lines: i32) -> InputDecorationTheme {
6264 debug_assert!(self.data.is_none());
6265 self.error_max_lines = Some(error_max_lines);
6266 self
6267 }
6268
6269 pub fn with_floating_label_behavior(
6271 mut self,
6272 floating_label_behavior: FloatingLabelBehavior,
6273 ) -> InputDecorationTheme {
6274 debug_assert!(self.data.is_none());
6275 self.floating_label_behavior = Some(floating_label_behavior);
6276 self
6277 }
6278
6279 pub fn with_floating_label_alignment(
6281 mut self,
6282 floating_label_alignment: FloatingLabelAlignment,
6283 ) -> InputDecorationTheme {
6284 debug_assert!(self.data.is_none());
6285 self.floating_label_alignment = Some(floating_label_alignment);
6286 self
6287 }
6288
6289 pub fn with_is_dense(mut self, is_dense: bool) -> InputDecorationTheme {
6291 debug_assert!(self.data.is_none());
6292 self.is_dense = Some(is_dense);
6293 self
6294 }
6295
6296 pub fn with_content_padding(
6298 mut self,
6299 content_padding: impl Into<EdgeInsetsGeometry>,
6300 ) -> InputDecorationTheme {
6301 let content_padding = content_padding.into();
6302 debug_assert!(self.data.is_none());
6303 self.content_padding = Some(content_padding);
6304 self
6305 }
6306
6307 pub fn with_is_collapsed(mut self, is_collapsed: bool) -> InputDecorationTheme {
6309 debug_assert!(self.data.is_none());
6310 self.is_collapsed = Some(is_collapsed);
6311 self
6312 }
6313
6314 pub fn with_icon_color(mut self, icon_color: impl Into<AnyColor>) -> InputDecorationTheme {
6316 debug_assert!(self.data.is_none());
6317 self.icon_color = Some(icon_color.into());
6318 self
6319 }
6320
6321 pub fn with_prefix_style(
6323 mut self,
6324 prefix_style: impl Into<AnyTextStyle>,
6325 ) -> InputDecorationTheme {
6326 debug_assert!(self.data.is_none());
6327 self.prefix_style = Some(prefix_style.into());
6328 self
6329 }
6330
6331 pub fn with_prefix_icon_color(
6333 mut self,
6334 prefix_icon_color: impl Into<AnyColor>,
6335 ) -> InputDecorationTheme {
6336 debug_assert!(self.data.is_none());
6337 self.prefix_icon_color = Some(prefix_icon_color.into());
6338 self
6339 }
6340
6341 pub fn with_prefix_icon_constraints(
6343 mut self,
6344 prefix_icon_constraints: BoxConstraints,
6345 ) -> InputDecorationTheme {
6346 debug_assert!(self.data.is_none());
6347 self.prefix_icon_constraints = Some(prefix_icon_constraints);
6348 self
6349 }
6350
6351 pub fn with_suffix_style(
6353 mut self,
6354 suffix_style: impl Into<AnyTextStyle>,
6355 ) -> InputDecorationTheme {
6356 debug_assert!(self.data.is_none());
6357 self.suffix_style = Some(suffix_style.into());
6358 self
6359 }
6360
6361 pub fn with_suffix_icon_color(
6363 mut self,
6364 suffix_icon_color: impl Into<AnyColor>,
6365 ) -> InputDecorationTheme {
6366 debug_assert!(self.data.is_none());
6367 self.suffix_icon_color = Some(suffix_icon_color.into());
6368 self
6369 }
6370
6371 pub fn with_suffix_icon_constraints(
6373 mut self,
6374 suffix_icon_constraints: BoxConstraints,
6375 ) -> InputDecorationTheme {
6376 debug_assert!(self.data.is_none());
6377 self.suffix_icon_constraints = Some(suffix_icon_constraints);
6378 self
6379 }
6380
6381 pub fn with_counter_style(
6383 mut self,
6384 counter_style: impl Into<AnyTextStyle>,
6385 ) -> InputDecorationTheme {
6386 debug_assert!(self.data.is_none());
6387 self.counter_style = Some(counter_style.into());
6388 self
6389 }
6390
6391 pub fn with_filled(mut self, filled: bool) -> InputDecorationTheme {
6393 debug_assert!(self.data.is_none());
6394 self.filled = Some(filled);
6395 self
6396 }
6397
6398 pub fn with_fill_color(mut self, fill_color: impl Into<AnyColor>) -> InputDecorationTheme {
6400 debug_assert!(self.data.is_none());
6401 self.fill_color = Some(fill_color.into());
6402 self
6403 }
6404
6405 pub fn with_active_indicator_border(
6407 mut self,
6408 active_indicator_border: impl Into<AnyBorderSide>,
6409 ) -> InputDecorationTheme {
6410 debug_assert!(self.data.is_none());
6411 self.active_indicator_border = Some(active_indicator_border.into());
6412 self
6413 }
6414
6415 pub fn with_outline_border(
6417 mut self,
6418 outline_border: impl Into<AnyBorderSide>,
6419 ) -> InputDecorationTheme {
6420 debug_assert!(self.data.is_none());
6421 self.outline_border = Some(outline_border.into());
6422 self
6423 }
6424
6425 pub fn with_focus_color(mut self, focus_color: Color) -> InputDecorationTheme {
6427 debug_assert!(self.data.is_none());
6428 self.focus_color = Some(focus_color);
6429 self
6430 }
6431
6432 pub fn with_hover_color(mut self, hover_color: Color) -> InputDecorationTheme {
6434 debug_assert!(self.data.is_none());
6435 self.hover_color = Some(hover_color);
6436 self
6437 }
6438
6439 pub fn with_error_border(
6441 mut self,
6442 error_border: impl Into<InputBorderRef>,
6443 ) -> InputDecorationTheme {
6444 debug_assert!(self.data.is_none());
6445 self.error_border = Some(error_border.into());
6446 self
6447 }
6448
6449 pub fn with_focused_border(
6451 mut self,
6452 focused_border: impl Into<InputBorderRef>,
6453 ) -> InputDecorationTheme {
6454 debug_assert!(self.data.is_none());
6455 self.focused_border = Some(focused_border.into());
6456 self
6457 }
6458
6459 pub fn with_focused_error_border(
6461 mut self,
6462 focused_error_border: impl Into<InputBorderRef>,
6463 ) -> InputDecorationTheme {
6464 debug_assert!(self.data.is_none());
6465 self.focused_error_border = Some(focused_error_border.into());
6466 self
6467 }
6468
6469 pub fn with_disabled_border(
6471 mut self,
6472 disabled_border: impl Into<InputBorderRef>,
6473 ) -> InputDecorationTheme {
6474 debug_assert!(self.data.is_none());
6475 self.disabled_border = Some(disabled_border.into());
6476 self
6477 }
6478
6479 pub fn with_enabled_border(
6481 mut self,
6482 enabled_border: impl Into<InputBorderRef>,
6483 ) -> InputDecorationTheme {
6484 debug_assert!(self.data.is_none());
6485 self.enabled_border = Some(enabled_border.into());
6486 self
6487 }
6488
6489 pub fn with_border(mut self, border: impl Into<InputBorderRef>) -> InputDecorationTheme {
6491 debug_assert!(self.data.is_none());
6492 self.border = Some(border.into());
6493 self
6494 }
6495
6496 pub fn with_align_label_with_hint(
6498 mut self,
6499 align_label_with_hint: bool,
6500 ) -> InputDecorationTheme {
6501 debug_assert!(self.data.is_none());
6502 self.align_label_with_hint = Some(align_label_with_hint);
6503 self
6504 }
6505
6506 pub fn with_constraints(mut self, constraints: BoxConstraints) -> InputDecorationTheme {
6508 debug_assert!(self.data.is_none());
6509 self.constraints = Some(constraints);
6510 self
6511 }
6512
6513 pub fn with_visual_density(mut self, visual_density: VisualDensity) -> InputDecorationTheme {
6515 debug_assert!(self.data.is_none());
6516 self.visual_density = Some(visual_density);
6517 self
6518 }
6519
6520 pub fn label_style(&self) -> Option<AnyTextStyle> {
6525 match &self.data {
6526 Some(data) => data.label_style.clone(),
6527 None => self.label_style.clone(),
6528 }
6529 }
6530
6531 pub fn floating_label_style(&self) -> Option<AnyTextStyle> {
6536 match &self.data {
6537 Some(data) => data.floating_label_style.clone(),
6538 None => self.floating_label_style.clone(),
6539 }
6540 }
6541
6542 pub fn helper_style(&self) -> Option<AnyTextStyle> {
6547 match &self.data {
6548 Some(data) => data.helper_style.clone(),
6549 None => self.helper_style.clone(),
6550 }
6551 }
6552
6553 pub fn helper_max_lines(&self) -> Option<i32> {
6558 match &self.data {
6559 Some(data) => data.helper_max_lines,
6560 None => self.helper_max_lines,
6561 }
6562 }
6563
6564 pub fn hint_style(&self) -> Option<AnyTextStyle> {
6569 match &self.data {
6570 Some(data) => data.hint_style.clone(),
6571 None => self.hint_style.clone(),
6572 }
6573 }
6574
6575 pub fn hint_fade_duration(&self) -> Option<Duration> {
6580 match &self.data {
6581 Some(data) => data.hint_fade_duration,
6582 None => self.hint_fade_duration,
6583 }
6584 }
6585
6586 pub fn hint_max_lines(&self) -> Option<i32> {
6591 match &self.data {
6592 Some(data) => data.hint_max_lines,
6593 None => self.hint_max_lines,
6594 }
6595 }
6596
6597 pub fn error_style(&self) -> Option<AnyTextStyle> {
6602 match &self.data {
6603 Some(data) => data.error_style.clone(),
6604 None => self.error_style.clone(),
6605 }
6606 }
6607
6608 pub fn error_max_lines(&self) -> Option<i32> {
6613 match &self.data {
6614 Some(data) => data.error_max_lines,
6615 None => self.error_max_lines,
6616 }
6617 }
6618
6619 pub fn floating_label_behavior(&self) -> FloatingLabelBehavior {
6624 match &self.data {
6625 Some(data) => data.floating_label_behavior,
6626 None => self
6627 .floating_label_behavior
6628 .unwrap_or(FloatingLabelBehavior::Auto),
6629 }
6630 }
6631
6632 pub fn floating_label_alignment(&self) -> FloatingLabelAlignment {
6637 match &self.data {
6638 Some(data) => data.floating_label_alignment,
6639 None => self
6640 .floating_label_alignment
6641 .unwrap_or(FloatingLabelAlignment::START),
6642 }
6643 }
6644
6645 pub fn is_dense(&self) -> bool {
6650 match &self.data {
6651 Some(data) => data.is_dense,
6652 None => self.is_dense.unwrap_or(false),
6653 }
6654 }
6655
6656 pub fn content_padding(&self) -> Option<EdgeInsetsGeometry> {
6661 match &self.data {
6662 Some(data) => data.content_padding,
6663 None => self.content_padding,
6664 }
6665 }
6666
6667 pub fn is_collapsed(&self) -> bool {
6672 match &self.data {
6673 Some(data) => data.is_collapsed,
6674 None => self.is_collapsed.unwrap_or(false),
6675 }
6676 }
6677
6678 pub fn icon_color(&self) -> Option<AnyColor> {
6683 match &self.data {
6684 Some(data) => data.icon_color.clone(),
6685 None => self.icon_color.clone(),
6686 }
6687 }
6688
6689 pub fn prefix_style(&self) -> Option<AnyTextStyle> {
6694 match &self.data {
6695 Some(data) => data.prefix_style.clone(),
6696 None => self.prefix_style.clone(),
6697 }
6698 }
6699
6700 pub fn prefix_icon_color(&self) -> Option<AnyColor> {
6705 match &self.data {
6706 Some(data) => data.prefix_icon_color.clone(),
6707 None => self.prefix_icon_color.clone(),
6708 }
6709 }
6710
6711 pub fn prefix_icon_constraints(&self) -> Option<BoxConstraints> {
6716 match &self.data {
6717 Some(data) => data.prefix_icon_constraints,
6718 None => self.prefix_icon_constraints,
6719 }
6720 }
6721
6722 pub fn suffix_style(&self) -> Option<AnyTextStyle> {
6727 match &self.data {
6728 Some(data) => data.suffix_style.clone(),
6729 None => self.suffix_style.clone(),
6730 }
6731 }
6732
6733 pub fn suffix_icon_color(&self) -> Option<AnyColor> {
6738 match &self.data {
6739 Some(data) => data.suffix_icon_color.clone(),
6740 None => self.suffix_icon_color.clone(),
6741 }
6742 }
6743
6744 pub fn suffix_icon_constraints(&self) -> Option<BoxConstraints> {
6749 match &self.data {
6750 Some(data) => data.suffix_icon_constraints,
6751 None => self.suffix_icon_constraints,
6752 }
6753 }
6754
6755 pub fn counter_style(&self) -> Option<AnyTextStyle> {
6760 match &self.data {
6761 Some(data) => data.counter_style.clone(),
6762 None => self.counter_style.clone(),
6763 }
6764 }
6765
6766 pub fn filled(&self) -> bool {
6771 match &self.data {
6772 Some(data) => data.filled,
6773 None => self.filled.unwrap_or(false),
6774 }
6775 }
6776
6777 pub fn fill_color(&self) -> Option<AnyColor> {
6782 match &self.data {
6783 Some(data) => data.fill_color.clone(),
6784 None => self.fill_color.clone(),
6785 }
6786 }
6787
6788 pub fn active_indicator_border(&self) -> Option<AnyBorderSide> {
6793 match &self.data {
6794 Some(data) => data.active_indicator_border.clone(),
6795 None => self.active_indicator_border.clone(),
6796 }
6797 }
6798
6799 pub fn outline_border(&self) -> Option<AnyBorderSide> {
6804 match &self.data {
6805 Some(data) => data.outline_border.clone(),
6806 None => self.outline_border.clone(),
6807 }
6808 }
6809
6810 pub fn focus_color(&self) -> Option<Color> {
6815 match &self.data {
6816 Some(data) => data.focus_color,
6817 None => self.focus_color,
6818 }
6819 }
6820
6821 pub fn hover_color(&self) -> Option<Color> {
6826 match &self.data {
6827 Some(data) => data.hover_color,
6828 None => self.hover_color,
6829 }
6830 }
6831
6832 pub fn error_border(&self) -> Option<&InputBorderRef> {
6837 match &self.data {
6838 Some(data) => data.error_border.as_ref(),
6839 None => self.error_border.as_ref(),
6840 }
6841 }
6842
6843 pub fn focused_border(&self) -> Option<&InputBorderRef> {
6848 match &self.data {
6849 Some(data) => data.focused_border.as_ref(),
6850 None => self.focused_border.as_ref(),
6851 }
6852 }
6853
6854 pub fn focused_error_border(&self) -> Option<&InputBorderRef> {
6859 match &self.data {
6860 Some(data) => data.focused_error_border.as_ref(),
6861 None => self.focused_error_border.as_ref(),
6862 }
6863 }
6864
6865 pub fn disabled_border(&self) -> Option<&InputBorderRef> {
6870 match &self.data {
6871 Some(data) => data.disabled_border.as_ref(),
6872 None => self.disabled_border.as_ref(),
6873 }
6874 }
6875
6876 pub fn enabled_border(&self) -> Option<&InputBorderRef> {
6881 match &self.data {
6882 Some(data) => data.enabled_border.as_ref(),
6883 None => self.enabled_border.as_ref(),
6884 }
6885 }
6886
6887 pub fn border(&self) -> Option<&InputBorderRef> {
6892 match &self.data {
6893 Some(data) => data.border.as_ref(),
6894 None => self.border.as_ref(),
6895 }
6896 }
6897
6898 pub fn align_label_with_hint(&self) -> bool {
6903 match &self.data {
6904 Some(data) => data.align_label_with_hint,
6905 None => self.align_label_with_hint.unwrap_or(false),
6906 }
6907 }
6908
6909 pub fn constraints(&self) -> Option<BoxConstraints> {
6914 match &self.data {
6915 Some(data) => data.constraints,
6916 None => self.constraints,
6917 }
6918 }
6919
6920 pub fn visual_density(&self) -> Option<VisualDensity> {
6925 match &self.data {
6926 Some(data) => data.visual_density,
6927 None => self.visual_density,
6928 }
6929 }
6930
6931 pub fn data(&self) -> InputDecorationThemeData {
6933 self.data
6934 .clone()
6935 .unwrap_or_else(|| InputDecorationThemeData {
6936 label_style: self.label_style.clone(),
6937 floating_label_style: self.floating_label_style.clone(),
6938 helper_style: self.helper_style.clone(),
6939 helper_max_lines: self.helper_max_lines,
6940 hint_style: self.hint_style.clone(),
6941 hint_fade_duration: self.hint_fade_duration,
6942 hint_max_lines: self.hint_max_lines,
6943 error_style: self.error_style.clone(),
6944 error_max_lines: self.error_max_lines,
6945 floating_label_behavior: self
6946 .floating_label_behavior
6947 .unwrap_or(FloatingLabelBehavior::Auto),
6948 floating_label_alignment: self
6949 .floating_label_alignment
6950 .unwrap_or(FloatingLabelAlignment::START),
6951 is_dense: self.is_dense.unwrap_or(false),
6952 content_padding: self.content_padding,
6953 is_collapsed: self.is_collapsed.unwrap_or(false),
6954 icon_color: self.icon_color.clone(),
6955 prefix_style: self.prefix_style.clone(),
6956 prefix_icon_color: self.prefix_icon_color.clone(),
6957 prefix_icon_constraints: self.prefix_icon_constraints,
6958 suffix_style: self.suffix_style.clone(),
6959 suffix_icon_color: self.suffix_icon_color.clone(),
6960 suffix_icon_constraints: self.suffix_icon_constraints,
6961 counter_style: self.counter_style.clone(),
6962 filled: self.filled.unwrap_or(false),
6963 fill_color: self.fill_color.clone(),
6964 active_indicator_border: self.active_indicator_border.clone(),
6965 outline_border: self.outline_border.clone(),
6966 focus_color: self.focus_color,
6967 hover_color: self.hover_color,
6968 error_border: self.error_border.clone(),
6969 focused_border: self.focused_border.clone(),
6970 focused_error_border: self.focused_error_border.clone(),
6971 disabled_border: self.disabled_border.clone(),
6972 enabled_border: self.enabled_border.clone(),
6973 border: self.border.clone(),
6974 align_label_with_hint: self.align_label_with_hint.unwrap_or(false),
6975 constraints: self.constraints,
6976 visual_density: self.visual_density,
6977 supporting_text_padding: None,
6978 })
6979 }
6980
6981 pub fn of(app: &mut App, context: BuildContext) -> InputDecorationThemeData {
6992 match context.depend_on_inherited_widget_of_exact_type::<InputDecorationTheme>(app) {
6993 Some(input_decoration_theme) => input_decoration_theme.data(),
6994 None => Theme::of(app, context).input_decoration_theme.clone(),
6995 }
6996 }
6997
6998 pub fn copy_with(&self) -> InputDecorationTheme {
7008 InputDecorationTheme {
7009 key: None,
7010 data: None,
7011 label_style: self.label_style(),
7012 floating_label_style: self.floating_label_style(),
7013 helper_style: self.helper_style(),
7014 helper_max_lines: self.helper_max_lines(),
7015 hint_style: self.hint_style(),
7016 hint_fade_duration: self.hint_fade_duration(),
7017 hint_max_lines: self.hint_max_lines(),
7018 error_style: self.error_style(),
7019 error_max_lines: self.error_max_lines(),
7020 floating_label_behavior: Some(self.floating_label_behavior()),
7021 floating_label_alignment: Some(self.floating_label_alignment()),
7022 is_dense: Some(self.is_dense()),
7023 content_padding: self.content_padding(),
7024 is_collapsed: Some(self.is_collapsed()),
7025 icon_color: self.icon_color(),
7026 prefix_style: self.prefix_style(),
7027 prefix_icon_color: self.prefix_icon_color(),
7028 prefix_icon_constraints: self.prefix_icon_constraints(),
7029 suffix_style: self.suffix_style(),
7030 suffix_icon_color: self.suffix_icon_color(),
7031 suffix_icon_constraints: self.suffix_icon_constraints(),
7032 counter_style: self.counter_style(),
7033 filled: Some(self.filled()),
7034 fill_color: self.fill_color(),
7035 active_indicator_border: self.active_indicator_border(),
7036 outline_border: self.outline_border(),
7037 focus_color: self.focus_color(),
7038 hover_color: self.hover_color(),
7039 error_border: self.error_border().cloned(),
7040 focused_border: self.focused_border().cloned(),
7041 focused_error_border: self.focused_error_border().cloned(),
7042 disabled_border: self.disabled_border().cloned(),
7043 enabled_border: self.enabled_border().cloned(),
7044 border: self.border().cloned(),
7045 align_label_with_hint: Some(self.align_label_with_hint()),
7046 constraints: self.constraints(),
7047 visual_density: self.visual_density(),
7048 child: SizedBox::shrink().into_widget(),
7049 }
7050 }
7051
7052 pub fn merge(&self, other: Option<&InputDecorationTheme>) -> InputDecorationTheme {
7062 let Some(other) = other else {
7063 return self.clone();
7064 };
7065 let mut merged = self.copy_with();
7066 merged.label_style = self.label_style().or_else(|| other.label_style());
7067 merged.floating_label_style = self
7068 .floating_label_style()
7069 .or_else(|| other.floating_label_style());
7070 merged.helper_style = self.helper_style().or_else(|| other.helper_style());
7071 merged.helper_max_lines = self.helper_max_lines().or_else(|| other.helper_max_lines());
7072 merged.hint_style = self.hint_style().or_else(|| other.hint_style());
7073 merged.hint_fade_duration = self
7074 .hint_fade_duration()
7075 .or_else(|| other.hint_fade_duration());
7076 merged.hint_max_lines = self.hint_max_lines().or_else(|| other.hint_max_lines());
7077 merged.error_style = self.error_style().or_else(|| other.error_style());
7078 merged.error_max_lines = self.error_max_lines().or_else(|| other.error_max_lines());
7079 merged.content_padding = self.content_padding().or_else(|| other.content_padding());
7080 merged.icon_color = self.icon_color().or_else(|| other.icon_color());
7081 merged.prefix_style = self.prefix_style().or_else(|| other.prefix_style());
7082 merged.prefix_icon_color = self
7083 .prefix_icon_color()
7084 .or_else(|| other.prefix_icon_color());
7085 merged.prefix_icon_constraints = self
7086 .prefix_icon_constraints()
7087 .or_else(|| other.prefix_icon_constraints());
7088 merged.suffix_style = self.suffix_style().or_else(|| other.suffix_style());
7089 merged.suffix_icon_color = self
7090 .suffix_icon_color()
7091 .or_else(|| other.suffix_icon_color());
7092 merged.suffix_icon_constraints = self
7093 .suffix_icon_constraints()
7094 .or_else(|| other.suffix_icon_constraints());
7095 merged.counter_style = self.counter_style().or_else(|| other.counter_style());
7096 merged.fill_color = self.fill_color().or_else(|| other.fill_color());
7097 merged.active_indicator_border = self
7098 .active_indicator_border()
7099 .or_else(|| other.active_indicator_border());
7100 merged.outline_border = self.outline_border().or_else(|| other.outline_border());
7101 merged.focus_color = self.focus_color().or_else(|| other.focus_color());
7102 merged.hover_color = self.hover_color().or_else(|| other.hover_color());
7103 merged.error_border = self.error_border().or(other.error_border()).cloned();
7104 merged.focused_border = self.focused_border().or(other.focused_border()).cloned();
7105 merged.focused_error_border = self
7106 .focused_error_border()
7107 .or(other.focused_error_border())
7108 .cloned();
7109 merged.disabled_border = self.disabled_border().or(other.disabled_border()).cloned();
7110 merged.enabled_border = self.enabled_border().or(other.enabled_border()).cloned();
7111 merged.border = self.border().or(other.border()).cloned();
7112 merged.constraints = self.constraints().or_else(|| other.constraints());
7113 merged.visual_density = self.visual_density().or_else(|| other.visual_density());
7114 merged
7115 }
7116
7117 fn has_no_property(&self) -> bool {
7119 self.label_style.is_none()
7120 && self.floating_label_style.is_none()
7121 && self.helper_style.is_none()
7122 && self.helper_max_lines.is_none()
7123 && self.hint_style.is_none()
7124 && self.hint_fade_duration.is_none()
7125 && self.hint_max_lines.is_none()
7126 && self.error_style.is_none()
7127 && self.error_max_lines.is_none()
7128 && self.floating_label_behavior.is_none()
7129 && self.floating_label_alignment.is_none()
7130 && self.is_dense.is_none()
7131 && self.content_padding.is_none()
7132 && self.is_collapsed.is_none()
7133 && self.icon_color.is_none()
7134 && self.prefix_style.is_none()
7135 && self.prefix_icon_color.is_none()
7136 && self.prefix_icon_constraints.is_none()
7137 && self.suffix_style.is_none()
7138 && self.suffix_icon_color.is_none()
7139 && self.suffix_icon_constraints.is_none()
7140 && self.counter_style.is_none()
7141 && self.filled.is_none()
7142 && self.fill_color.is_none()
7143 && self.active_indicator_border.is_none()
7144 && self.outline_border.is_none()
7145 && self.focus_color.is_none()
7146 && self.hover_color.is_none()
7147 && self.error_border.is_none()
7148 && self.focused_border.is_none()
7149 && self.focused_error_border.is_none()
7150 && self.disabled_border.is_none()
7151 && self.enabled_border.is_none()
7152 && self.border.is_none()
7153 && self.align_label_with_hint.is_none()
7154 && self.constraints.is_none()
7155 && self.visual_density.is_none()
7156 }
7157}
7158
7159impl Default for InputDecorationTheme {
7160 fn default() -> InputDecorationTheme {
7161 InputDecorationTheme::new()
7162 }
7163}
7164
7165impl From<InputDecorationTheme> for InputDecorationThemeData {
7166 fn from(theme: InputDecorationTheme) -> InputDecorationThemeData {
7168 theme.data()
7169 }
7170}
7171
7172impl InheritedTheme for InputDecorationTheme {
7173 fn wrap(&self, _app: &mut App, _context: BuildContext, child: WidgetRef) -> WidgetRef {
7174 InputDecorationTheme::new()
7175 .with_data(self.data())
7176 .child(child)
7177 .into_widget()
7178 }
7179}
7180
7181impl InheritedWidget for InputDecorationTheme {
7182 fn key(&self) -> Option<&KeyRef> {
7183 self.key.as_ref()
7184 }
7185
7186 fn child(&self) -> &WidgetRef {
7187 &self.child
7188 }
7189
7190 fn update_should_notify(&self, old_widget: &InputDecorationTheme) -> bool {
7191 self.data() != old_widget.data()
7192 }
7193
7194 fn as_inherited_theme(&self) -> Option<&dyn InheritedTheme> {
7195 Some(self)
7196 }
7197}
7198
7199#[derive(Clone, PartialEq)]
7213pub struct InputDecorationThemeData {
7214 pub label_style: Option<AnyTextStyle>,
7231
7232 pub floating_label_style: Option<AnyTextStyle>,
7251
7252 pub helper_style: Option<AnyTextStyle>,
7258
7259 pub helper_max_lines: Option<i32>,
7271
7272 pub hint_style: Option<AnyTextStyle>,
7286
7287 pub hint_fade_duration: Option<Duration>,
7289
7290 pub hint_max_lines: Option<i32>,
7298
7299 pub error_style: Option<AnyTextStyle>,
7310
7311 pub error_max_lines: Option<i32>,
7323
7324 pub floating_label_behavior: FloatingLabelBehavior,
7338
7339 pub floating_label_alignment: FloatingLabelAlignment,
7351
7352 pub is_dense: bool,
7357
7358 pub content_padding: Option<EdgeInsetsGeometry>,
7371
7372 pub is_collapsed: bool,
7377
7378 pub icon_color: Option<AnyColor>,
7386
7387 pub prefix_style: Option<AnyTextStyle>,
7395
7396 pub prefix_icon_color: Option<AnyColor>,
7404
7405 pub prefix_icon_constraints: Option<BoxConstraints>,
7419
7420 pub suffix_style: Option<AnyTextStyle>,
7428
7429 pub suffix_icon_color: Option<AnyColor>,
7437
7438 pub suffix_icon_constraints: Option<BoxConstraints>,
7452
7453 pub counter_style: Option<AnyTextStyle>,
7461
7462 pub filled: bool,
7473
7474 pub fill_color: Option<AnyColor>,
7482
7483 pub outline_border: Option<AnyBorderSide>,
7485
7486 pub active_indicator_border: Option<AnyBorderSide>,
7488
7489 pub focus_color: Option<Color>,
7498
7499 pub hover_color: Option<Color>,
7510
7511 pub error_border: Option<InputBorderRef>,
7534
7535 pub focused_border: Option<InputBorderRef>,
7558
7559 pub focused_error_border: Option<InputBorderRef>,
7582
7583 pub disabled_border: Option<InputBorderRef>,
7605
7606 pub enabled_border: Option<InputBorderRef>,
7628
7629 pub border: Option<InputBorderRef>,
7662
7663 pub align_label_with_hint: bool,
7667
7668 pub constraints: Option<BoxConstraints>,
7684
7685 pub visual_density: Option<VisualDensity>,
7708
7709 pub supporting_text_padding: Option<EdgeInsetsGeometry>,
7737}
7738
7739crate::theme_data_getters::theme_data_getters! {
7740 pub trait InputDecorationThemeDataGetters for InputDecorationThemeData, fields: input_decoration_theme_data {
7744 label_style: Option<AnyTextStyle>,
7745 floating_label_style: Option<AnyTextStyle>,
7746 helper_style: Option<AnyTextStyle>,
7747 helper_max_lines: Option<i32>,
7748 hint_style: Option<AnyTextStyle>,
7749 hint_fade_duration: Option<Duration>,
7750 hint_max_lines: Option<i32>,
7751 error_style: Option<AnyTextStyle>,
7752 error_max_lines: Option<i32>,
7753 floating_label_behavior: FloatingLabelBehavior,
7754 floating_label_alignment: FloatingLabelAlignment,
7755 is_dense: bool,
7756 content_padding: Option<EdgeInsetsGeometry>,
7757 is_collapsed: bool,
7758 icon_color: Option<AnyColor>,
7759 prefix_style: Option<AnyTextStyle>,
7760 prefix_icon_color: Option<AnyColor>,
7761 prefix_icon_constraints: Option<BoxConstraints>,
7762 suffix_style: Option<AnyTextStyle>,
7763 suffix_icon_color: Option<AnyColor>,
7764 suffix_icon_constraints: Option<BoxConstraints>,
7765 counter_style: Option<AnyTextStyle>,
7766 filled: bool,
7767 fill_color: Option<AnyColor>,
7768 outline_border: Option<AnyBorderSide>,
7769 active_indicator_border: Option<AnyBorderSide>,
7770 focus_color: Option<Color>,
7771 hover_color: Option<Color>,
7772 error_border: Option<InputBorderRef>,
7773 focused_border: Option<InputBorderRef>,
7774 focused_error_border: Option<InputBorderRef>,
7775 disabled_border: Option<InputBorderRef>,
7776 enabled_border: Option<InputBorderRef>,
7777 border: Option<InputBorderRef>,
7778 align_label_with_hint: bool,
7779 constraints: Option<BoxConstraints>,
7780 visual_density: Option<VisualDensity>,
7781 supporting_text_padding: Option<EdgeInsetsGeometry>,
7782 }
7783}
7784
7785impl InputDecorationThemeData {
7786 pub fn new() -> InputDecorationThemeData {
7789 InputDecorationThemeData {
7790 label_style: None,
7791 floating_label_style: None,
7792 helper_style: None,
7793 helper_max_lines: None,
7794 hint_style: None,
7795 hint_fade_duration: None,
7796 hint_max_lines: None,
7797 error_style: None,
7798 error_max_lines: None,
7799 floating_label_behavior: FloatingLabelBehavior::Auto,
7800 floating_label_alignment: FloatingLabelAlignment::START,
7801 is_dense: false,
7802 content_padding: None,
7803 is_collapsed: false,
7804 icon_color: None,
7805 prefix_style: None,
7806 prefix_icon_color: None,
7807 prefix_icon_constraints: None,
7808 suffix_style: None,
7809 suffix_icon_color: None,
7810 suffix_icon_constraints: None,
7811 counter_style: None,
7812 filled: false,
7813 fill_color: None,
7814 outline_border: None,
7815 active_indicator_border: None,
7816 focus_color: None,
7817 hover_color: None,
7818 error_border: None,
7819 focused_border: None,
7820 focused_error_border: None,
7821 disabled_border: None,
7822 enabled_border: None,
7823 border: None,
7824 align_label_with_hint: false,
7825 constraints: None,
7826 visual_density: None,
7827 supporting_text_padding: None,
7828 }
7829 }
7830
7831 pub fn label_style(mut self, label_style: impl Into<AnyTextStyle>) -> InputDecorationThemeData {
7833 self.label_style = Some(label_style.into());
7834 self
7835 }
7836
7837 pub fn floating_label_style(
7839 mut self,
7840 floating_label_style: impl Into<AnyTextStyle>,
7841 ) -> InputDecorationThemeData {
7842 self.floating_label_style = Some(floating_label_style.into());
7843 self
7844 }
7845
7846 pub fn helper_style(
7848 mut self,
7849 helper_style: impl Into<AnyTextStyle>,
7850 ) -> InputDecorationThemeData {
7851 self.helper_style = Some(helper_style.into());
7852 self
7853 }
7854
7855 pub fn helper_max_lines(mut self, helper_max_lines: i32) -> InputDecorationThemeData {
7857 self.helper_max_lines = Some(helper_max_lines);
7858 self
7859 }
7860
7861 pub fn hint_style(mut self, hint_style: impl Into<AnyTextStyle>) -> InputDecorationThemeData {
7863 self.hint_style = Some(hint_style.into());
7864 self
7865 }
7866
7867 pub fn hint_fade_duration(mut self, hint_fade_duration: Duration) -> InputDecorationThemeData {
7869 self.hint_fade_duration = Some(hint_fade_duration);
7870 self
7871 }
7872
7873 pub fn hint_max_lines(mut self, hint_max_lines: i32) -> InputDecorationThemeData {
7875 self.hint_max_lines = Some(hint_max_lines);
7876 self
7877 }
7878
7879 pub fn error_style(mut self, error_style: impl Into<AnyTextStyle>) -> InputDecorationThemeData {
7881 self.error_style = Some(error_style.into());
7882 self
7883 }
7884
7885 pub fn error_max_lines(mut self, error_max_lines: i32) -> InputDecorationThemeData {
7887 self.error_max_lines = Some(error_max_lines);
7888 self
7889 }
7890
7891 pub fn floating_label_behavior(
7893 mut self,
7894 floating_label_behavior: FloatingLabelBehavior,
7895 ) -> InputDecorationThemeData {
7896 self.floating_label_behavior = floating_label_behavior;
7897 self
7898 }
7899
7900 pub fn floating_label_alignment(
7902 mut self,
7903 floating_label_alignment: FloatingLabelAlignment,
7904 ) -> InputDecorationThemeData {
7905 self.floating_label_alignment = floating_label_alignment;
7906 self
7907 }
7908
7909 pub fn is_dense(mut self, is_dense: bool) -> InputDecorationThemeData {
7911 self.is_dense = is_dense;
7912 self
7913 }
7914
7915 pub fn content_padding(
7917 mut self,
7918 content_padding: impl Into<EdgeInsetsGeometry>,
7919 ) -> InputDecorationThemeData {
7920 let content_padding = content_padding.into();
7921 self.content_padding = Some(content_padding);
7922 self
7923 }
7924
7925 pub fn is_collapsed(mut self, is_collapsed: bool) -> InputDecorationThemeData {
7927 self.is_collapsed = is_collapsed;
7928 self
7929 }
7930
7931 pub fn icon_color(mut self, icon_color: impl Into<AnyColor>) -> InputDecorationThemeData {
7933 self.icon_color = Some(icon_color.into());
7934 self
7935 }
7936
7937 pub fn prefix_style(
7939 mut self,
7940 prefix_style: impl Into<AnyTextStyle>,
7941 ) -> InputDecorationThemeData {
7942 self.prefix_style = Some(prefix_style.into());
7943 self
7944 }
7945
7946 pub fn prefix_icon_color(
7948 mut self,
7949 prefix_icon_color: impl Into<AnyColor>,
7950 ) -> InputDecorationThemeData {
7951 self.prefix_icon_color = Some(prefix_icon_color.into());
7952 self
7953 }
7954
7955 pub fn prefix_icon_constraints(
7957 mut self,
7958 prefix_icon_constraints: BoxConstraints,
7959 ) -> InputDecorationThemeData {
7960 self.prefix_icon_constraints = Some(prefix_icon_constraints);
7961 self
7962 }
7963
7964 pub fn suffix_style(
7966 mut self,
7967 suffix_style: impl Into<AnyTextStyle>,
7968 ) -> InputDecorationThemeData {
7969 self.suffix_style = Some(suffix_style.into());
7970 self
7971 }
7972
7973 pub fn suffix_icon_color(
7975 mut self,
7976 suffix_icon_color: impl Into<AnyColor>,
7977 ) -> InputDecorationThemeData {
7978 self.suffix_icon_color = Some(suffix_icon_color.into());
7979 self
7980 }
7981
7982 pub fn suffix_icon_constraints(
7984 mut self,
7985 suffix_icon_constraints: BoxConstraints,
7986 ) -> InputDecorationThemeData {
7987 self.suffix_icon_constraints = Some(suffix_icon_constraints);
7988 self
7989 }
7990
7991 pub fn counter_style(
7993 mut self,
7994 counter_style: impl Into<AnyTextStyle>,
7995 ) -> InputDecorationThemeData {
7996 self.counter_style = Some(counter_style.into());
7997 self
7998 }
7999
8000 pub fn filled(mut self, filled: bool) -> InputDecorationThemeData {
8002 self.filled = filled;
8003 self
8004 }
8005
8006 pub fn fill_color(mut self, fill_color: impl Into<AnyColor>) -> InputDecorationThemeData {
8008 self.fill_color = Some(fill_color.into());
8009 self
8010 }
8011
8012 pub fn active_indicator_border(
8014 mut self,
8015 active_indicator_border: impl Into<AnyBorderSide>,
8016 ) -> InputDecorationThemeData {
8017 self.active_indicator_border = Some(active_indicator_border.into());
8018 self
8019 }
8020
8021 pub fn outline_border(
8023 mut self,
8024 outline_border: impl Into<AnyBorderSide>,
8025 ) -> InputDecorationThemeData {
8026 self.outline_border = Some(outline_border.into());
8027 self
8028 }
8029
8030 pub fn focus_color(mut self, focus_color: Color) -> InputDecorationThemeData {
8032 self.focus_color = Some(focus_color);
8033 self
8034 }
8035
8036 pub fn hover_color(mut self, hover_color: Color) -> InputDecorationThemeData {
8038 self.hover_color = Some(hover_color);
8039 self
8040 }
8041
8042 pub fn error_border(
8044 mut self,
8045 error_border: impl Into<InputBorderRef>,
8046 ) -> InputDecorationThemeData {
8047 self.error_border = Some(error_border.into());
8048 self
8049 }
8050
8051 pub fn focused_border(
8053 mut self,
8054 focused_border: impl Into<InputBorderRef>,
8055 ) -> InputDecorationThemeData {
8056 self.focused_border = Some(focused_border.into());
8057 self
8058 }
8059
8060 pub fn focused_error_border(
8062 mut self,
8063 focused_error_border: impl Into<InputBorderRef>,
8064 ) -> InputDecorationThemeData {
8065 self.focused_error_border = Some(focused_error_border.into());
8066 self
8067 }
8068
8069 pub fn disabled_border(
8071 mut self,
8072 disabled_border: impl Into<InputBorderRef>,
8073 ) -> InputDecorationThemeData {
8074 self.disabled_border = Some(disabled_border.into());
8075 self
8076 }
8077
8078 pub fn enabled_border(
8080 mut self,
8081 enabled_border: impl Into<InputBorderRef>,
8082 ) -> InputDecorationThemeData {
8083 self.enabled_border = Some(enabled_border.into());
8084 self
8085 }
8086
8087 pub fn border(mut self, border: impl Into<InputBorderRef>) -> InputDecorationThemeData {
8089 self.border = Some(border.into());
8090 self
8091 }
8092
8093 pub fn align_label_with_hint(
8095 mut self,
8096 align_label_with_hint: bool,
8097 ) -> InputDecorationThemeData {
8098 self.align_label_with_hint = align_label_with_hint;
8099 self
8100 }
8101
8102 pub fn constraints(mut self, constraints: BoxConstraints) -> InputDecorationThemeData {
8104 self.constraints = Some(constraints);
8105 self
8106 }
8107
8108 pub fn visual_density(mut self, visual_density: VisualDensity) -> InputDecorationThemeData {
8110 self.visual_density = Some(visual_density);
8111 self
8112 }
8113
8114 pub fn supporting_text_padding(
8116 mut self,
8117 supporting_text_padding: impl Into<EdgeInsetsGeometry>,
8118 ) -> InputDecorationThemeData {
8119 let supporting_text_padding = supporting_text_padding.into();
8120 self.supporting_text_padding = Some(supporting_text_padding);
8121 self
8122 }
8123
8124 pub fn copy_with(&self) -> InputDecorationThemeData {
8130 self.clone()
8131 }
8132
8133 pub fn merge(&self, other: Option<&InputDecorationThemeData>) -> InputDecorationThemeData {
8145 let Some(other) = other else {
8146 return self.clone();
8147 };
8148 let mut merged = self.copy_with();
8149 merged.label_style = self
8150 .label_style
8151 .clone()
8152 .or_else(|| other.label_style.clone());
8153 merged.floating_label_style = self
8154 .floating_label_style
8155 .clone()
8156 .or_else(|| other.floating_label_style.clone());
8157 merged.helper_style = self
8158 .helper_style
8159 .clone()
8160 .or_else(|| other.helper_style.clone());
8161 merged.helper_max_lines = self.helper_max_lines.or(other.helper_max_lines);
8162 merged.hint_style = self.hint_style.clone().or_else(|| other.hint_style.clone());
8163 merged.hint_fade_duration = self.hint_fade_duration.or(other.hint_fade_duration);
8164 merged.hint_max_lines = self.hint_max_lines.or(other.hint_max_lines);
8165 merged.error_style = self
8166 .error_style
8167 .clone()
8168 .or_else(|| other.error_style.clone());
8169 merged.error_max_lines = self.error_max_lines.or(other.error_max_lines);
8170 merged.content_padding = self.content_padding.or(other.content_padding);
8171 merged.icon_color = self.icon_color.clone().or_else(|| other.icon_color.clone());
8172 merged.prefix_style = self
8173 .prefix_style
8174 .clone()
8175 .or_else(|| other.prefix_style.clone());
8176 merged.prefix_icon_color = self
8177 .prefix_icon_color
8178 .clone()
8179 .or_else(|| other.prefix_icon_color.clone());
8180 merged.prefix_icon_constraints = self
8181 .prefix_icon_constraints
8182 .or(other.prefix_icon_constraints);
8183 merged.suffix_style = self
8184 .suffix_style
8185 .clone()
8186 .or_else(|| other.suffix_style.clone());
8187 merged.suffix_icon_color = self
8188 .suffix_icon_color
8189 .clone()
8190 .or_else(|| other.suffix_icon_color.clone());
8191 merged.suffix_icon_constraints = self
8192 .suffix_icon_constraints
8193 .or(other.suffix_icon_constraints);
8194 merged.counter_style = self
8195 .counter_style
8196 .clone()
8197 .or_else(|| other.counter_style.clone());
8198 merged.fill_color = self.fill_color.clone().or_else(|| other.fill_color.clone());
8199 merged.active_indicator_border = self
8200 .active_indicator_border
8201 .clone()
8202 .or_else(|| other.active_indicator_border.clone());
8203 merged.outline_border = self
8204 .outline_border
8205 .clone()
8206 .or_else(|| other.outline_border.clone());
8207 merged.focus_color = self.focus_color.or(other.focus_color);
8208 merged.hover_color = self.hover_color.or(other.hover_color);
8209 merged.error_border = self
8210 .error_border
8211 .clone()
8212 .or_else(|| other.error_border.clone());
8213 merged.focused_border = self
8214 .focused_border
8215 .clone()
8216 .or_else(|| other.focused_border.clone());
8217 merged.focused_error_border = self
8218 .focused_error_border
8219 .clone()
8220 .or_else(|| other.focused_error_border.clone());
8221 merged.disabled_border = self
8222 .disabled_border
8223 .clone()
8224 .or_else(|| other.disabled_border.clone());
8225 merged.enabled_border = self
8226 .enabled_border
8227 .clone()
8228 .or_else(|| other.enabled_border.clone());
8229 merged.border = self.border.clone().or_else(|| other.border.clone());
8230 merged.constraints = self.constraints.or(other.constraints);
8231 merged.visual_density = self.visual_density.or(other.visual_density);
8232 merged.supporting_text_padding = self
8233 .supporting_text_padding
8234 .or(other.supporting_text_padding);
8235 merged
8236 }
8237}
8238
8239impl Default for InputDecorationThemeData {
8240 fn default() -> InputDecorationThemeData {
8241 InputDecorationThemeData::new()
8242 }
8243}
8244
8245impl Debug for InputDecorationThemeData {
8248 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
8249 let default_theme = InputDecorationThemeData::new();
8250 let mut properties: Vec<String> = Vec::new();
8251 if let Some(label_style) = &self.label_style {
8252 properties.push(format!("labelStyle: {:?}", label_style));
8253 }
8254 if let Some(floating_label_style) = &self.floating_label_style {
8255 properties.push(format!("floatingLabelStyle: {:?}", floating_label_style));
8256 }
8257 if let Some(helper_style) = &self.helper_style {
8258 properties.push(format!("helperStyle: {:?}", helper_style));
8259 }
8260 if let Some(helper_max_lines) = &self.helper_max_lines {
8261 properties.push(format!("helperMaxLines: {:?}", helper_max_lines));
8262 }
8263 if let Some(hint_style) = &self.hint_style {
8264 properties.push(format!("hintStyle: {:?}", hint_style));
8265 }
8266 if let Some(hint_fade_duration) = &self.hint_fade_duration {
8267 properties.push(format!("hintFadeDuration: {:?}", hint_fade_duration));
8268 }
8269 if let Some(hint_max_lines) = &self.hint_max_lines {
8270 properties.push(format!("hintMaxLines: {:?}", hint_max_lines));
8271 }
8272 if let Some(error_style) = &self.error_style {
8273 properties.push(format!("errorStyle: {:?}", error_style));
8274 }
8275 if let Some(error_max_lines) = &self.error_max_lines {
8276 properties.push(format!("errorMaxLines: {:?}", error_max_lines));
8277 }
8278 if self.floating_label_behavior != default_theme.floating_label_behavior {
8279 properties.push(format!(
8280 "floatingLabelBehavior: {:?}",
8281 self.floating_label_behavior
8282 ));
8283 }
8284 if self.floating_label_alignment != default_theme.floating_label_alignment {
8285 properties.push(format!(
8286 "floatingLabelAlignment: {:?}",
8287 self.floating_label_alignment
8288 ));
8289 }
8290 if self.is_dense != default_theme.is_dense {
8291 properties.push(format!("isDense: {:?}", self.is_dense));
8292 }
8293 if let Some(content_padding) = &self.content_padding {
8294 properties.push(format!("contentPadding: {:?}", content_padding));
8295 }
8296 if let Some(supporting_text_padding) = &self.supporting_text_padding {
8297 properties.push(format!(
8298 "supportingTextPadding: {:?}",
8299 supporting_text_padding
8300 ));
8301 }
8302 if self.is_collapsed != default_theme.is_collapsed {
8303 properties.push(format!("isCollapsed: {:?}", self.is_collapsed));
8304 }
8305 if let Some(icon_color) = &self.icon_color {
8306 properties.push(format!("iconColor: {:?}", icon_color));
8307 }
8308 if let Some(prefix_icon_color) = &self.prefix_icon_color {
8309 properties.push(format!("prefixIconColor: {:?}", prefix_icon_color));
8310 }
8311 if let Some(prefix_icon_constraints) = &self.prefix_icon_constraints {
8312 properties.push(format!(
8313 "prefixIconConstraints: {:?}",
8314 prefix_icon_constraints
8315 ));
8316 }
8317 if let Some(prefix_style) = &self.prefix_style {
8318 properties.push(format!("prefixStyle: {:?}", prefix_style));
8319 }
8320 if let Some(suffix_icon_color) = &self.suffix_icon_color {
8321 properties.push(format!("suffixIconColor: {:?}", suffix_icon_color));
8322 }
8323 if let Some(suffix_icon_constraints) = &self.suffix_icon_constraints {
8324 properties.push(format!(
8325 "suffixIconConstraints: {:?}",
8326 suffix_icon_constraints
8327 ));
8328 }
8329 if let Some(suffix_style) = &self.suffix_style {
8330 properties.push(format!("suffixStyle: {:?}", suffix_style));
8331 }
8332 if let Some(counter_style) = &self.counter_style {
8333 properties.push(format!("counterStyle: {:?}", counter_style));
8334 }
8335 if self.filled != default_theme.filled {
8336 properties.push(format!("filled: {:?}", self.filled));
8337 }
8338 if let Some(fill_color) = &self.fill_color {
8339 properties.push(format!("fillColor: {:?}", fill_color));
8340 }
8341 if let Some(active_indicator_border) = &self.active_indicator_border {
8342 properties.push(format!(
8343 "activeIndicatorBorder: {:?}",
8344 active_indicator_border
8345 ));
8346 }
8347 if let Some(outline_border) = &self.outline_border {
8348 properties.push(format!("outlineBorder: {:?}", outline_border));
8349 }
8350 if let Some(focus_color) = &self.focus_color {
8351 properties.push(format!("focusColor: {:?}", focus_color));
8352 }
8353 if let Some(hover_color) = &self.hover_color {
8354 properties.push(format!("hoverColor: {:?}", hover_color));
8355 }
8356 if let Some(error_border) = &self.error_border {
8357 properties.push(format!("errorBorder: {:?}", error_border));
8358 }
8359 if let Some(focused_border) = &self.focused_border {
8360 properties.push(format!("focusedBorder: {:?}", focused_border));
8361 }
8362 if let Some(focused_error_border) = &self.focused_error_border {
8363 properties.push(format!("focusedErrorBorder: {:?}", focused_error_border));
8364 }
8365 if let Some(disabled_border) = &self.disabled_border {
8366 properties.push(format!("disabledBorder: {:?}", disabled_border));
8367 }
8368 if let Some(enabled_border) = &self.enabled_border {
8369 properties.push(format!("enabledBorder: {:?}", enabled_border));
8370 }
8371 if let Some(border) = &self.border {
8372 properties.push(format!("border: {:?}", border));
8373 }
8374 if self.align_label_with_hint != default_theme.align_label_with_hint {
8375 properties.push(format!(
8376 "alignLabelWithHint: {:?}",
8377 self.align_label_with_hint
8378 ));
8379 }
8380 if let Some(constraints) = &self.constraints {
8381 properties.push(format!("constraints: {:?}", constraints));
8382 }
8383 if let Some(visual_density) = &self.visual_density {
8384 properties.push(format!("visualDensity: {:?}", visual_density));
8385 }
8386 write!(f, "InputDecorationThemeData({})", properties.join(", "))
8387 }
8388}
8389
8390impl Diagnosticable for InputDecorationThemeData {
8391 fn debug_fill_properties(&self, properties: &mut DiagnosticPropertiesBuilder) {
8392 let default_theme = InputDecorationThemeData::new();
8393 properties.add(
8394 DiagnosticsProperty::<AnyTextStyle>::new("labelStyle", self.label_style.clone())
8395 .default_value(default_theme.label_style.clone()),
8396 );
8397 properties.add(
8398 DiagnosticsProperty::<AnyTextStyle>::new(
8399 "floatingLabelStyle",
8400 self.floating_label_style.clone(),
8401 )
8402 .default_value(default_theme.floating_label_style.clone()),
8403 );
8404 properties.add(
8405 DiagnosticsProperty::<AnyTextStyle>::new("helperStyle", self.helper_style.clone())
8406 .default_value(default_theme.helper_style.clone()),
8407 );
8408 properties.add(
8409 IntProperty::<i32>::new("helperMaxLines", self.helper_max_lines)
8410 .default_value(default_theme.helper_max_lines),
8411 );
8412 properties.add(
8413 DiagnosticsProperty::<AnyTextStyle>::new("hintStyle", self.hint_style.clone())
8414 .default_value(default_theme.hint_style.clone()),
8415 );
8416 properties.add(
8417 DiagnosticsProperty::<SignedDuration>::new(
8418 "hintFadeDuration",
8419 self.hint_fade_duration.map(SignedDuration::from),
8420 )
8421 .default_value(default_theme.hint_fade_duration.map(SignedDuration::from)),
8422 );
8423 properties.add(
8424 IntProperty::<i32>::new("hintMaxLines", self.hint_max_lines)
8425 .default_value(default_theme.hint_max_lines),
8426 );
8427 properties.add(
8428 DiagnosticsProperty::<AnyTextStyle>::new("errorStyle", self.error_style.clone())
8429 .default_value(default_theme.error_style.clone()),
8430 );
8431 properties.add(
8432 IntProperty::<i32>::new("errorMaxLines", self.error_max_lines)
8433 .default_value(default_theme.error_max_lines),
8434 );
8435 properties.add(
8436 DiagnosticsProperty::<waft_foundation::DartEnum<FloatingLabelBehavior>>::new(
8437 "floatingLabelBehavior",
8438 Some(waft_foundation::DartEnum(self.floating_label_behavior)),
8439 )
8440 .default_value(Some(waft_foundation::DartEnum(
8441 default_theme.floating_label_behavior,
8442 ))),
8443 );
8444 properties.add(
8445 DiagnosticsProperty::<String>::new(
8446 "floatingLabelAlignment",
8447 Some(format!("{:?}", self.floating_label_alignment)),
8448 )
8449 .default_value(Some(format!(
8450 "{:?}",
8451 default_theme.floating_label_alignment
8452 ))),
8453 );
8454 properties.add(
8455 DiagnosticsProperty::<bool>::new("isDense", Some(self.is_dense))
8456 .default_value(Some(default_theme.is_dense)),
8457 );
8458 properties.add(
8459 DiagnosticsProperty::<EdgeInsetsGeometry>::new("contentPadding", self.content_padding)
8460 .default_value(default_theme.content_padding),
8461 );
8462 properties.add(
8463 DiagnosticsProperty::<bool>::new("isCollapsed", Some(self.is_collapsed))
8464 .default_value(Some(default_theme.is_collapsed)),
8465 );
8466 properties.add(
8467 DiagnosticsProperty::<AnyColor>::new("iconColor", self.icon_color.clone())
8468 .default_value(default_theme.icon_color.clone()),
8469 );
8470 properties.add(
8471 DiagnosticsProperty::<AnyColor>::new("prefixIconColor", self.prefix_icon_color.clone())
8472 .default_value(default_theme.prefix_icon_color.clone()),
8473 );
8474 properties.add(
8475 DiagnosticsProperty::<BoxConstraints>::new(
8476 "prefixIconConstraints",
8477 self.prefix_icon_constraints,
8478 )
8479 .default_value(default_theme.prefix_icon_constraints),
8480 );
8481 properties.add(
8482 DiagnosticsProperty::<AnyTextStyle>::new("prefixStyle", self.prefix_style.clone())
8483 .default_value(default_theme.prefix_style.clone()),
8484 );
8485 properties.add(
8486 DiagnosticsProperty::<AnyColor>::new("suffixIconColor", self.suffix_icon_color.clone())
8487 .default_value(default_theme.suffix_icon_color.clone()),
8488 );
8489 properties.add(
8490 DiagnosticsProperty::<BoxConstraints>::new(
8491 "suffixIconConstraints",
8492 self.suffix_icon_constraints,
8493 )
8494 .default_value(default_theme.suffix_icon_constraints),
8495 );
8496 properties.add(
8497 DiagnosticsProperty::<AnyTextStyle>::new("suffixStyle", self.suffix_style.clone())
8498 .default_value(default_theme.suffix_style.clone()),
8499 );
8500 properties.add(
8501 DiagnosticsProperty::<AnyTextStyle>::new("counterStyle", self.counter_style.clone())
8502 .default_value(default_theme.counter_style.clone()),
8503 );
8504 properties.add(
8505 DiagnosticsProperty::<bool>::new("filled", Some(self.filled))
8506 .default_value(Some(default_theme.filled)),
8507 );
8508 properties.add(
8509 DiagnosticsProperty::<AnyColor>::new("fillColor", self.fill_color.clone())
8510 .default_value(default_theme.fill_color.clone()),
8511 );
8512 properties.add(
8513 DiagnosticsProperty::<String>::new(
8514 "activeIndicatorBorder",
8515 self.active_indicator_border
8516 .as_ref()
8517 .map(|border| format!("{border:?}")),
8518 )
8519 .default_value(
8520 default_theme
8521 .active_indicator_border
8522 .as_ref()
8523 .map(|border| format!("{border:?}")),
8524 ),
8525 );
8526 properties.add(
8527 DiagnosticsProperty::<String>::new(
8528 "outlineBorder",
8529 self.outline_border
8530 .as_ref()
8531 .map(|border| format!("{border:?}")),
8532 )
8533 .default_value(
8534 default_theme
8535 .outline_border
8536 .as_ref()
8537 .map(|border| format!("{border:?}")),
8538 ),
8539 );
8540 properties.add(
8541 ColorProperty::new("focusColor", self.focus_color)
8542 .default_value(default_theme.focus_color),
8543 );
8544 properties.add(
8545 ColorProperty::new("hoverColor", self.hover_color)
8546 .default_value(default_theme.hover_color),
8547 );
8548 properties.add(
8549 DiagnosticsProperty::<String>::new(
8550 "errorBorder",
8551 self.error_border
8552 .as_ref()
8553 .map(|border| format!("{border:?}")),
8554 )
8555 .default_value(
8556 default_theme
8557 .error_border
8558 .as_ref()
8559 .map(|border| format!("{border:?}")),
8560 ),
8561 );
8562 properties.add(
8563 DiagnosticsProperty::<String>::new(
8564 "focusedBorder",
8565 self.focused_border
8566 .as_ref()
8567 .map(|border| format!("{border:?}")),
8568 )
8569 .default_value(
8570 default_theme
8571 .focused_error_border
8572 .as_ref()
8573 .map(|border| format!("{border:?}")),
8574 ),
8575 );
8576 properties.add(
8577 DiagnosticsProperty::<String>::new(
8578 "focusedErrorBorder",
8579 self.focused_error_border
8580 .as_ref()
8581 .map(|border| format!("{border:?}")),
8582 )
8583 .default_value(
8584 default_theme
8585 .focused_error_border
8586 .as_ref()
8587 .map(|border| format!("{border:?}")),
8588 ),
8589 );
8590 properties.add(
8591 DiagnosticsProperty::<String>::new(
8592 "disabledBorder",
8593 self.disabled_border
8594 .as_ref()
8595 .map(|border| format!("{border:?}")),
8596 )
8597 .default_value(
8598 default_theme
8599 .disabled_border
8600 .as_ref()
8601 .map(|border| format!("{border:?}")),
8602 ),
8603 );
8604 properties.add(
8605 DiagnosticsProperty::<String>::new(
8606 "enabledBorder",
8607 self.enabled_border
8608 .as_ref()
8609 .map(|border| format!("{border:?}")),
8610 )
8611 .default_value(
8612 default_theme
8613 .enabled_border
8614 .as_ref()
8615 .map(|border| format!("{border:?}")),
8616 ),
8617 );
8618 properties.add(
8619 DiagnosticsProperty::<String>::new(
8620 "border",
8621 self.border.as_ref().map(|border| format!("{border:?}")),
8622 )
8623 .default_value(
8624 default_theme
8625 .border
8626 .as_ref()
8627 .map(|border| format!("{border:?}")),
8628 ),
8629 );
8630 properties.add(
8631 DiagnosticsProperty::<bool>::new(
8632 "alignLabelWithHint",
8633 Some(self.align_label_with_hint),
8634 )
8635 .default_value(Some(default_theme.align_label_with_hint)),
8636 );
8637 properties.add(
8638 DiagnosticsProperty::<BoxConstraints>::new("constraints", self.constraints)
8639 .default_value(default_theme.constraints),
8640 );
8641 properties.add(
8642 DiagnosticsProperty::<crate::theme_data::VisualDensity>::new(
8643 "visualDensity",
8644 self.visual_density,
8645 )
8646 .default_value(default_theme.visual_density),
8647 );
8648 }
8649}
8650
8651impl fmt::Display for InputDecorationThemeData {
8653 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
8654 f.write_str(&Diagnosticable::to_string(self))
8655 }
8656}
8657
8658struct InputDecoratorDefaultsM2 {
8660 input_decoration_theme_data: InputDecorationThemeData,
8662 context: DefaultsContext,
8663}
8664
8665impl InputDecoratorDefaultsM2 {
8666 fn new(context: BuildContext) -> InputDecoratorDefaultsM2 {
8667 InputDecoratorDefaultsM2 {
8668 input_decoration_theme_data: InputDecorationThemeData::new(),
8669 context: DefaultsContext::new(context),
8670 }
8671 }
8672}
8673
8674impl InputDecorationThemeDataGetters for InputDecoratorDefaultsM2 {
8675 fn input_decoration_theme_data(&self) -> &InputDecorationThemeData {
8676 &self.input_decoration_theme_data
8677 }
8678
8679 fn hint_style(&self, app: &mut App) -> Option<AnyTextStyle> {
8680 let theme = self.context.theme(app);
8681 let disabled_color = theme.disabled_color;
8682 let hint_color = theme.hint_color;
8683 let hint_style = WidgetStateTextStyle::resolve_with(move |states: &WidgetStates| {
8684 if states.contains(&WidgetState::Disabled) {
8685 return TextStyle::new().color(disabled_color);
8686 }
8687 TextStyle::new().color(hint_color)
8688 });
8689 Some(hint_style.into())
8690 }
8691
8692 fn label_style(&self, app: &mut App) -> Option<AnyTextStyle> {
8693 let theme = self.context.theme(app);
8694 let disabled_color = theme.disabled_color;
8695 let hint_color = theme.hint_color;
8696 let label_style = WidgetStateTextStyle::resolve_with(move |states: &WidgetStates| {
8697 if states.contains(&WidgetState::Disabled) {
8698 return TextStyle::new().color(disabled_color);
8699 }
8700 TextStyle::new().color(hint_color)
8701 });
8702 Some(label_style.into())
8703 }
8704
8705 fn floating_label_style(&self, app: &mut App) -> Option<AnyTextStyle> {
8706 let theme = self.context.theme(app);
8707 let disabled_color = theme.disabled_color;
8708 let hint_color = theme.hint_color;
8709 let error = theme.color_scheme.error();
8710 let primary = theme.color_scheme.primary();
8711 let floating_label_style =
8712 WidgetStateTextStyle::resolve_with(move |states: &WidgetStates| {
8713 if states.contains(&WidgetState::Disabled) {
8714 return TextStyle::new().color(disabled_color);
8715 }
8716 if states.contains(&WidgetState::Error) {
8717 return TextStyle::new().color(error);
8718 }
8719 if states.contains(&WidgetState::Focused) {
8720 return TextStyle::new().color(primary);
8721 }
8722 TextStyle::new().color(hint_color)
8723 });
8724 Some(floating_label_style.into())
8725 }
8726
8727 fn helper_style(&self, app: &mut App) -> Option<AnyTextStyle> {
8728 let theme = self.context.theme(app);
8729 let hint_color = theme.hint_color;
8730 let body_small = theme.text_theme.body_small.clone();
8731 let helper_body_small = body_small.clone();
8732 let helper_style = WidgetStateTextStyle::resolve_with(move |states: &WidgetStates| {
8733 let body_small = helper_body_small
8734 .clone()
8735 .expect("ThemeData::text_theme has a body_small");
8736 if states.contains(&WidgetState::Disabled) {
8737 return body_small.copy_with().color(Colors::TRANSPARENT);
8738 }
8739
8740 body_small.copy_with().color(hint_color)
8741 });
8742 Some(helper_style.into())
8743 }
8744
8745 fn error_style(&self, app: &mut App) -> Option<AnyTextStyle> {
8746 let theme = self.context.theme(app);
8747 let error = theme.color_scheme.error();
8748 let body_small = theme.text_theme.body_small.clone();
8749 let error_style = WidgetStateTextStyle::resolve_with(move |states: &WidgetStates| {
8750 let body_small = body_small
8751 .clone()
8752 .expect("ThemeData::text_theme has a body_small");
8753 if states.contains(&WidgetState::Disabled) {
8754 return body_small.copy_with().color(Colors::TRANSPARENT);
8755 }
8756 body_small.copy_with().color(error)
8757 });
8758 Some(error_style.into())
8759 }
8760
8761 fn fill_color(&self, app: &mut App) -> Option<AnyColor> {
8762 let brightness = Theme::brightness_of(app, self.context.context());
8763 let fill_color = WidgetStateColor::resolve_with(move |states: &WidgetStates| {
8764 match (brightness, states.contains(&WidgetState::Disabled)) {
8765 (Brightness::Dark, true) => Color::new(0x0DFFFFFF), (Brightness::Dark, false) => Color::new(0x1AFFFFFF), (Brightness::Light, true) => Color::new(0x05000000), (Brightness::Light, false) => Color::new(0x0A000000), }
8770 });
8771 Some(fill_color.into())
8772 }
8773
8774 fn icon_color(&self, app: &mut App) -> Option<AnyColor> {
8775 let theme = self.context.theme(app);
8776 let brightness = Theme::brightness_of(app, self.context.context());
8777 let disabled_color = theme.disabled_color;
8778 let primary = theme.color_scheme.primary();
8779 let icon_color = WidgetStateColor::resolve_with(move |states: &WidgetStates| {
8780 if states.contains(&WidgetState::Disabled) && !states.contains(&WidgetState::Focused) {
8781 return disabled_color;
8782 }
8783 if states.contains(&WidgetState::Focused) {
8784 return primary;
8785 }
8786 match brightness {
8787 Brightness::Dark => Colors::WHITE70,
8788 Brightness::Light => Colors::BLACK45,
8789 }
8790 });
8791 Some(icon_color.into())
8792 }
8793
8794 fn prefix_icon_color(&self, app: &mut App) -> Option<AnyColor> {
8795 let theme = self.context.theme(app);
8796 let brightness = Theme::brightness_of(app, self.context.context());
8797 let disabled_color = theme.disabled_color;
8798 let primary = theme.color_scheme.primary();
8799 let prefix_icon_color = WidgetStateColor::resolve_with(move |states: &WidgetStates| {
8800 if states.contains(&WidgetState::Disabled) && !states.contains(&WidgetState::Focused) {
8801 return disabled_color;
8802 }
8803 if states.contains(&WidgetState::Focused) {
8804 return primary;
8805 }
8806 match brightness {
8807 Brightness::Dark => Colors::WHITE70,
8808 Brightness::Light => Colors::BLACK45,
8809 }
8810 });
8811 Some(prefix_icon_color.into())
8812 }
8813
8814 fn suffix_icon_color(&self, app: &mut App) -> Option<AnyColor> {
8815 let theme = self.context.theme(app);
8816 let brightness = Theme::brightness_of(app, self.context.context());
8817 let disabled_color = theme.disabled_color;
8818 let error = theme.color_scheme.error();
8819 let primary = theme.color_scheme.primary();
8820 let suffix_icon_color = WidgetStateColor::resolve_with(move |states: &WidgetStates| {
8821 if states.contains(&WidgetState::Disabled) && !states.contains(&WidgetState::Focused) {
8822 return disabled_color;
8823 }
8824 if states.contains(&WidgetState::Error) {
8825 return error;
8826 }
8827 if states.contains(&WidgetState::Focused) {
8828 return primary;
8829 }
8830 match brightness {
8831 Brightness::Dark => Colors::WHITE70,
8832 Brightness::Light => Colors::BLACK45,
8833 }
8834 });
8835 Some(suffix_icon_color.into())
8836 }
8837}
8838
8839#[cfg(test)]
8840pub(crate) mod tests {
8841 pub(crate) mod common {
8842 use waft_widgets::testing::WidgetTester;
8845
8846 use std::time::Duration;
8847
8848 use waft_embedder::{Color, Offset, Rect};
8849 use waft_painting::{
8850 Alignment, AlignmentGeometry, BorderRadius, BorderSide, TextAlignVertical,
8851 TextDirection, TextStyle,
8852 };
8853 use waft_rendering::AnyRenderBox;
8854 use waft_widgets::{
8855 Align, AnyElement, Builder, CustomPaint, Directionality, IntoWidget, IntrinsicWidth,
8856 MatrixTransition, WidgetRef, downcast_widget,
8857 };
8858
8859 use crate::test_support::PaintInspection;
8860 use waft_embedder::valo::Op;
8861 pub(super) use waft_rendering::testing::{PaintCall, PaintedStyle, assert_paints, paints};
8862 pub(super) use waft_widgets::testing::{
8863 assert_f32_equals, assert_more_or_less_equals_within, expect, find, finds_nothing,
8864 finds_one_widget,
8865 };
8866
8867 use super::super::*;
8868 use crate::app::MaterialApp;
8869 use crate::icon_button_theme::IconButtonThemeData;
8870 use crate::input_border::{InputBorderRef, OutlineInputBorder, UnderlineInputBorder};
8871 use crate::material::Material;
8872 use crate::theme::Theme;
8873 use crate::theme_data::{ThemeData, VisualDensity};
8874
8875 pub(super) const K_TRANSITION_DURATION: Duration = Duration::from_millis(167);
8876
8877 pub(super) const HINT_TEXT: &str = "hint";
8878 pub(super) const INPUT_TEXT: &str = "text";
8879 pub(super) const LABEL_TEXT: &str = "label";
8880 pub(super) const ERROR_TEXT: &str = "error";
8881 pub(super) const HELPER_TEXT: &str = "helper";
8882 pub(super) const COUNTER_TEXT: &str = "counter";
8883
8884 pub(super) const TWO_LINES: &str = "line1\nline2";
8885 pub(super) const THREE_LINES: &str = "line1\nline2\nline3";
8886
8887 pub(super) fn m3(app: &App) -> ThemeData {
8889 ThemeData::builder().build(app)
8890 }
8891
8892 pub(super) fn m2(app: &App) -> ThemeData {
8894 ThemeData::builder().use_material3(false).build(app)
8895 }
8896
8897 pub(super) fn input_text() -> WidgetRef {
8901 Text::new(INPUT_TEXT)
8902 .style(
8903 TextStyle::new()
8904 .font_size(16.0)
8905 .font_weight(waft_painting::FontWeight::W400)
8906 .letter_spacing(0.5)
8907 .height(1.50),
8908 )
8909 .into_widget()
8910 }
8911
8912 pub(super) struct BuildInputDecorator {
8914 decoration: InputDecoration,
8915 theme: Option<fn(&App) -> ThemeData>,
8916 input_decoration_theme: Option<InputDecorationThemeData>,
8917 local_input_decoration_theme: Option<InputDecorationThemeData>,
8918 icon_button_theme: Option<IconButtonThemeData>,
8919 text_direction: TextDirection,
8920 expands: bool,
8921 is_empty: bool,
8922 is_focused: bool,
8923 is_hovering: bool,
8924 use_intrinsic_width: bool,
8925 base_style: Option<TextStyle>,
8926 text_align_vertical: Option<TextAlignVertical>,
8927 visual_density: Option<VisualDensity>,
8928 child: Option<WidgetRef>,
8929 }
8930
8931 #[allow(dead_code, clippy::wrong_self_convention)]
8934 impl BuildInputDecorator {
8935 pub(super) fn new(decoration: InputDecoration) -> BuildInputDecorator {
8936 BuildInputDecorator {
8937 decoration,
8938 theme: None,
8939 input_decoration_theme: None,
8940 local_input_decoration_theme: None,
8941 icon_button_theme: None,
8942 text_direction: TextDirection::Ltr,
8943 expands: false,
8944 is_empty: false,
8945 is_focused: false,
8946 is_hovering: false,
8947 use_intrinsic_width: false,
8948 base_style: None,
8949 text_align_vertical: None,
8950 visual_density: None,
8951 child: None,
8952 }
8953 }
8954
8955 pub(super) fn theme(mut self, theme: fn(&App) -> ThemeData) -> BuildInputDecorator {
8956 self.theme = Some(theme);
8957 self
8958 }
8959
8960 pub(super) fn input_decoration_theme(
8961 mut self,
8962 theme: InputDecorationThemeData,
8963 ) -> BuildInputDecorator {
8964 self.input_decoration_theme = Some(theme);
8965 self
8966 }
8967
8968 pub(super) fn local_input_decoration_theme(
8969 mut self,
8970 theme: InputDecorationThemeData,
8971 ) -> BuildInputDecorator {
8972 self.local_input_decoration_theme = Some(theme);
8973 self
8974 }
8975
8976 pub(super) fn icon_button_theme(
8977 mut self,
8978 theme: IconButtonThemeData,
8979 ) -> BuildInputDecorator {
8980 self.icon_button_theme = Some(theme);
8981 self
8982 }
8983
8984 pub(super) fn text_direction(
8985 mut self,
8986 text_direction: TextDirection,
8987 ) -> BuildInputDecorator {
8988 self.text_direction = text_direction;
8989 self
8990 }
8991
8992 pub(super) fn expands(mut self, expands: bool) -> BuildInputDecorator {
8993 self.expands = expands;
8994 self
8995 }
8996
8997 pub(super) fn is_empty(mut self, is_empty: bool) -> BuildInputDecorator {
8998 self.is_empty = is_empty;
8999 self
9000 }
9001
9002 pub(super) fn is_focused(mut self, is_focused: bool) -> BuildInputDecorator {
9003 self.is_focused = is_focused;
9004 self
9005 }
9006
9007 pub(super) fn is_hovering(mut self, is_hovering: bool) -> BuildInputDecorator {
9008 self.is_hovering = is_hovering;
9009 self
9010 }
9011
9012 pub(super) fn use_intrinsic_width(
9013 mut self,
9014 use_intrinsic_width: bool,
9015 ) -> BuildInputDecorator {
9016 self.use_intrinsic_width = use_intrinsic_width;
9017 self
9018 }
9019
9020 pub(super) fn base_style(mut self, base_style: TextStyle) -> BuildInputDecorator {
9021 self.base_style = Some(base_style);
9022 self
9023 }
9024
9025 pub(super) fn text_align_vertical(
9026 mut self,
9027 text_align_vertical: TextAlignVertical,
9028 ) -> BuildInputDecorator {
9029 self.text_align_vertical = Some(text_align_vertical);
9030 self
9031 }
9032
9033 pub(super) fn visual_density(
9034 mut self,
9035 visual_density: VisualDensity,
9036 ) -> BuildInputDecorator {
9037 self.visual_density = Some(visual_density);
9038 self
9039 }
9040
9041 pub(super) fn child<K>(mut self, child: impl IntoWidget<K>) -> BuildInputDecorator {
9042 self.child = Some(child.into_widget());
9043 self
9044 }
9045
9046 pub(super) fn build(self) -> WidgetRef {
9047 let mut decorator = InputDecorator::new(self.decoration)
9048 .expands(self.expands)
9049 .is_empty(self.is_empty)
9050 .is_focused(self.is_focused)
9051 .is_hovering(self.is_hovering)
9052 .child(self.child.unwrap_or_else(input_text));
9053 if let Some(base_style) = self.base_style {
9054 decorator = decorator.base_style(base_style);
9055 }
9056 if let Some(text_align_vertical) = self.text_align_vertical {
9057 decorator = decorator.text_align_vertical(text_align_vertical);
9058 }
9059 let mut widget = decorator.into_widget();
9060
9061 if self.use_intrinsic_width {
9062 widget = IntrinsicWidth::new().child(widget).into_widget();
9063 }
9064
9065 if let Some(local) = self.local_input_decoration_theme {
9066 widget = InputDecorationTheme::new()
9067 .with_data(local)
9068 .child(widget)
9069 .into_widget();
9070 }
9071
9072 let theme = self.theme;
9073 let input_decoration_theme = self.input_decoration_theme;
9074 let icon_button_theme = self.icon_button_theme;
9075 let visual_density = self.visual_density;
9076 let text_direction = self.text_direction;
9077 MaterialApp::new()
9078 .home(Material::new().child(Builder::new(move |app, context| {
9079 let base = match theme {
9080 Some(theme) => Rc::new(theme(app)),
9081 None => Theme::of(app, context),
9082 };
9083 let mut copy = base.copy_with();
9084 if let Some(input_decoration_theme) = &input_decoration_theme {
9085 copy = copy.input_decoration_theme(input_decoration_theme.clone());
9086 }
9087 if let Some(icon_button_theme) = &icon_button_theme {
9088 copy = copy.icon_button_theme(icon_button_theme.clone());
9089 }
9090 if let Some(visual_density) = visual_density {
9091 copy = copy.visual_density(visual_density);
9092 }
9093 Theme::new(
9094 copy.build(),
9095 Align::new()
9096 .alignment(AlignmentGeometry::Alignment(Alignment::TOP_LEFT))
9097 .child(Directionality::new(text_direction, widget.clone())),
9098 )
9099 .into_widget()
9100 })))
9101 .into_widget()
9102 }
9103 }
9104
9105 pub(super) fn get_decorator_rect(tester: &WidgetTester) -> Rect {
9107 tester.get_rect(find::by_type::<InputDecorator>())
9108 }
9109
9110 pub(super) fn ops_of(tester: &WidgetTester, element: AnyElement) -> Vec<Op> {
9112 tester.painted_ops(tester.render_object(element))
9113 }
9114
9115 pub(super) fn border_painter_ops(tester: &WidgetTester) -> Vec<Op> {
9117 ops_of(
9118 tester,
9119 find_border_painter(tester).expect("a border painter"),
9120 )
9121 }
9122
9123 pub(super) fn decorator_ops(tester: &WidgetTester) -> Vec<Op> {
9125 ops_of(tester, tester.element(find::by_type::<InputDecorator>()))
9126 }
9127
9128 pub(super) fn find_border_painter(tester: &WidgetTester) -> Option<AnyElement> {
9130 find::descendant(
9131 find::by_type::<BorderContainer>().first(),
9132 find::by_type::<CustomPaint>(),
9133 )
9134 .first()
9135 .evaluate(tester)
9136 .into_iter()
9137 .next()
9138 }
9139
9140 pub(super) fn with_border_painter<R>(
9142 tester: &WidgetTester,
9143 read: impl FnOnce(&App, &InputBorderPainter) -> R,
9144 ) -> R {
9145 let element = find_border_painter(tester).expect("a border painter");
9146 let app = tester.cell.borrow();
9147 let custom_paint =
9148 downcast_widget::<CustomPaint>(&**element.widget(&app)).expect("a CustomPaint");
9149 let painter = custom_paint
9150 .foreground_painter
9151 .as_ref()
9152 .expect("a foreground painter");
9153 let painter = painter
9154 .as_any()
9155 .downcast_ref::<InputBorderPainter>()
9156 .expect("an _InputBorderPainter");
9157 read(&app, painter)
9158 }
9159
9160 pub(super) fn get_border(tester: &WidgetTester) -> Option<InputBorderRef> {
9162 find_border_painter(tester)?;
9163 Some(with_border_painter(tester, |app, painter| {
9164 painter
9165 .border
9166 .evaluate(app, painter.border_animation.handle())
9167 }))
9168 }
9169
9170 pub(super) fn get_border_side(tester: &WidgetTester) -> BorderSide {
9172 get_border(tester).expect("a border").border_side()
9173 }
9174
9175 pub(super) fn get_border_radius(tester: &WidgetTester) -> Option<BorderRadius> {
9177 let border = get_border(tester).expect("a border");
9178 if let Some(underline) = border.as_any().downcast_ref::<UnderlineInputBorder>() {
9179 return Some(underline.border_radius);
9180 }
9181 border
9182 .as_any()
9183 .downcast_ref::<OutlineInputBorder>()
9184 .map(|outline| outline.border_radius)
9185 }
9186
9187 pub(super) fn get_border_weight(tester: &WidgetTester) -> f64 {
9189 get_border_side(tester).width
9190 }
9191
9192 pub(super) fn get_border_color(tester: &WidgetTester) -> Color {
9194 get_border_side(tester).color
9195 }
9196
9197 pub(crate) fn borders_and_container_colors(
9200 tester: &WidgetTester,
9201 ) -> Vec<(InputBorderRef, Color)> {
9202 (0..find::by_type::<BorderContainer>().evaluate(tester).len())
9203 .filter_map(|index| {
9204 find::descendant(
9205 find::by_type::<BorderContainer>().at(index),
9206 find::by_type::<CustomPaint>(),
9207 )
9208 .first()
9209 .evaluate(tester)
9210 .into_iter()
9211 .next()
9212 })
9213 .map(|element| {
9214 let app = tester.cell.borrow();
9215 let custom_paint = downcast_widget::<CustomPaint>(&**element.widget(&app))
9216 .expect("a CustomPaint");
9217 let painter = custom_paint
9218 .foreground_painter
9219 .as_ref()
9220 .and_then(|painter| painter.as_any().downcast_ref::<InputBorderPainter>())
9221 .expect("an _InputBorderPainter");
9222 (
9223 painter
9224 .border
9225 .evaluate(&app, painter.border_animation.handle()),
9226 painter.blended_color(&app),
9227 )
9228 })
9229 .collect()
9230 }
9231
9232 pub(super) fn get_container_color(tester: &WidgetTester) -> Color {
9234 with_border_painter(tester, |app, painter| painter.blended_color(app))
9235 }
9236
9237 pub(super) fn get_border_bottom(tester: &WidgetTester) -> f64 {
9240 get_container_rect(tester).height()
9241 }
9242
9243 pub(super) fn get_container_rect(tester: &WidgetTester) -> Rect {
9245 let element = find_border_painter(tester).expect("a border painter");
9246 let app = tester.cell.borrow();
9247 let container: AnyRenderBox =
9248 InputDecorator::container_of(&app, element).expect("a decorator container");
9249 Offset::ZERO & container.size(&app)
9250 }
9251
9252 pub(super) fn find_label(tester: &WidgetTester) -> AnyElement {
9255 let transitions = find::by_type::<waft_widgets::Animated<MatrixTransition>>();
9256 for index in 0..transitions.evaluate(tester).len() {
9257 if let Some(text) = find::descendant(
9258 transitions.clone().at(index),
9259 find::by_type::<waft_widgets::Text>(),
9260 )
9261 .first()
9262 .evaluate(tester)
9263 .into_iter()
9264 .next()
9265 {
9266 return text;
9267 }
9268 }
9269 panic!("no label")
9270 }
9271
9272 pub(super) fn get_label_rect(tester: &WidgetTester) -> Rect {
9274 tester.get_rect(find_label(tester))
9275 }
9276
9277 pub(super) fn get_label_style(tester: &WidgetTester) -> TextStyle {
9279 let label = find_label(tester);
9280 let ancestor = tester
9281 .ancestors(label)
9282 .into_iter()
9283 .find(|element| {
9284 let app = tester.cell.borrow();
9285 downcast_widget::<waft_widgets::AnimatedDefaultTextStyle>(
9286 &**element.widget(&app),
9287 )
9288 .is_some()
9289 })
9290 .expect("an AnimatedDefaultTextStyle above the label");
9291 tester.widget::<waft_widgets::AnimatedDefaultTextStyle, _>(ancestor, |style| {
9292 style.style.clone()
9293 })
9294 }
9295 }
9296 mod data {
9298 use waft_widgets::testing::test_widgets;
9299
9300 use waft_embedder::{Color, Radius};
9301 use waft_painting::{
9302 AnyColor, AnyTextStyle, BorderRadius, BorderSide, EdgeInsets, EdgeInsetsDirectional,
9303 EdgeInsetsGeometry, TextStyle,
9304 };
9305 use waft_rendering::BoxConstraints;
9306
9307 use super::super::*;
9308 use crate::colors::Colors;
9309 use crate::input_border::{InputBorderRef, OutlineInputBorder, UnderlineInputBorder};
9310 use crate::theme_data::ThemeData;
9311
9312 fn constraints(
9313 min_width: f64,
9314 max_width: f64,
9315 min_height: f64,
9316 max_height: f64,
9317 ) -> BoxConstraints {
9318 BoxConstraints::new()
9319 .min_width(min_width)
9320 .max_width(max_width)
9321 .min_height(min_height)
9322 .max_height(max_height)
9323 }
9324
9325 fn side(color: u32) -> BorderSide {
9326 BorderSide {
9327 color: Color::new(color),
9328 ..BorderSide::default()
9329 }
9330 }
9331
9332 fn border(border: impl Into<InputBorderRef>) -> Option<InputBorderRef> {
9333 Some(border.into())
9334 }
9335
9336 fn style(color: u32) -> Option<AnyTextStyle> {
9337 Some(TextStyle::new().color(Color::new(color)).into())
9338 }
9339
9340 fn color(color: u32) -> Option<AnyColor> {
9341 Some(Color::new(color).into())
9342 }
9343
9344 #[test]
9346 fn a_theme_is_collapsed_is_applied() {
9347 let decoration = InputDecoration::new()
9348 .hint_text("Hello, Flutter!")
9349 .apply_defaults(InputDecorationThemeData::new().is_collapsed(true));
9350
9351 assert_eq!(decoration.is_collapsed, Some(true));
9352 }
9353
9354 #[test]
9356 fn a_theme_is_collapsed_defaults_to_false() {
9357 let decoration = InputDecoration::new()
9358 .hint_text("Hello, Flutter!")
9359 .apply_defaults(InputDecorationThemeData::new());
9360
9361 assert_eq!(decoration.is_collapsed, Some(false));
9362 }
9363
9364 #[test]
9366 fn a_theme_is_collapsed_can_be_overridden() {
9367 let decoration = InputDecoration::new()
9368 .is_collapsed(true)
9369 .hint_text("Hello, Flutter!")
9370 .apply_defaults(InputDecorationThemeData::new());
9371
9372 assert_eq!(decoration.is_collapsed, Some(true));
9373 }
9374
9375 #[test]
9377 fn floating_label_alignment_prints_its_name() {
9378 assert_eq!(
9379 format!("{:?}", FloatingLabelAlignment::START),
9380 "FloatingLabelAlignment.start"
9381 );
9382 assert_eq!(
9383 format!("{:?}", FloatingLabelAlignment::CENTER),
9384 "FloatingLabelAlignment.center"
9385 );
9386 }
9387
9388 #[test]
9390 fn apply_defaults_initializes_empty_fields() {
9391 let theme_style = style(0xFF00FFFF);
9392 let theme_color = Color::new(0xFF00FF00);
9393 let theme_input_border = OutlineInputBorder::new().border_side(side(0xFF0000FF));
9394
9395 let decoration = InputDecoration::new().apply_defaults(
9396 InputDecorationThemeData::new()
9397 .label_style(theme_style.clone().unwrap())
9398 .floating_label_style(theme_style.clone().unwrap())
9399 .helper_style(theme_style.clone().unwrap())
9400 .helper_max_lines(2)
9401 .hint_style(theme_style.clone().unwrap())
9402 .error_style(theme_style.clone().unwrap())
9403 .error_max_lines(2)
9404 .floating_label_behavior(FloatingLabelBehavior::Never)
9405 .floating_label_alignment(FloatingLabelAlignment::CENTER)
9406 .is_dense(true)
9407 .content_padding(EdgeInsetsGeometry::all(1.0))
9408 .icon_color(theme_color)
9409 .prefix_style(theme_style.clone().unwrap())
9410 .prefix_icon_color(theme_color)
9411 .prefix_icon_constraints(constraints(10.0, 10.0, 30.0, 30.0))
9412 .suffix_style(theme_style.clone().unwrap())
9413 .suffix_icon_color(theme_color)
9414 .suffix_icon_constraints(constraints(20.0, 20.0, 40.0, 40.0))
9415 .counter_style(theme_style.clone().unwrap())
9416 .filled(true)
9417 .fill_color(theme_color)
9418 .focus_color(theme_color)
9419 .hover_color(theme_color)
9420 .error_border(theme_input_border)
9421 .focused_border(theme_input_border)
9422 .focused_error_border(theme_input_border)
9423 .disabled_border(theme_input_border)
9424 .enabled_border(theme_input_border)
9425 .border(InputBorderRef::none())
9426 .align_label_with_hint(true)
9427 .constraints(constraints(10.0, 20.0, 30.0, 40.0)),
9428 );
9429
9430 assert_eq!(decoration.label_style, theme_style);
9431 assert_eq!(decoration.floating_label_style, theme_style);
9432 assert_eq!(decoration.helper_style, theme_style);
9433 assert_eq!(decoration.helper_max_lines, Some(2));
9434 assert_eq!(decoration.hint_style, theme_style);
9435 assert_eq!(decoration.error_style, theme_style);
9436 assert_eq!(decoration.error_max_lines, Some(2));
9437 assert_eq!(
9438 decoration.floating_label_behavior,
9439 Some(FloatingLabelBehavior::Never)
9440 );
9441 assert_eq!(
9442 decoration.floating_label_alignment,
9443 Some(FloatingLabelAlignment::CENTER)
9444 );
9445 assert_eq!(decoration.is_dense, Some(true));
9446 assert_eq!(
9447 decoration.content_padding,
9448 Some(EdgeInsetsGeometry::all(1.0))
9449 );
9450 assert_eq!(decoration.icon_color, Some(theme_color.into()));
9451 assert_eq!(decoration.prefix_style, theme_style);
9452 assert_eq!(decoration.prefix_icon_color, Some(theme_color.into()));
9453 assert_eq!(
9454 decoration.prefix_icon_constraints,
9455 Some(constraints(10.0, 10.0, 30.0, 30.0))
9456 );
9457 assert_eq!(decoration.suffix_style, theme_style);
9458 assert_eq!(decoration.suffix_icon_color, Some(theme_color.into()));
9459 assert_eq!(
9460 decoration.suffix_icon_constraints,
9461 Some(constraints(20.0, 20.0, 40.0, 40.0))
9462 );
9463 assert_eq!(decoration.counter_style, theme_style);
9464 assert_eq!(decoration.filled, Some(true));
9465 assert_eq!(decoration.fill_color, Some(theme_color.into()));
9466 assert_eq!(decoration.focus_color, Some(theme_color));
9467 assert_eq!(decoration.hover_color, Some(theme_color));
9468 assert_eq!(decoration.error_border, border(theme_input_border));
9469 assert_eq!(decoration.focused_border, border(theme_input_border));
9470 assert_eq!(decoration.focused_error_border, border(theme_input_border));
9471 assert_eq!(decoration.disabled_border, border(theme_input_border));
9472 assert_eq!(decoration.enabled_border, border(theme_input_border));
9473 assert_eq!(decoration.border, Some(InputBorderRef::none()));
9474 assert_eq!(decoration.align_label_with_hint, Some(true));
9475 assert_eq!(
9476 decoration.constraints,
9477 Some(constraints(10.0, 20.0, 30.0, 40.0))
9478 );
9479 }
9480
9481 #[test]
9483 fn apply_defaults_does_not_override_non_null_fields() {
9484 let theme_style = style(0xFF00FFFF);
9485 let theme_color = Color::new(0xFF00FF00);
9486 let theme_input_border = OutlineInputBorder::new().border_side(side(0xFF0000FF));
9487 let decoration_style = style(0xFFFFFF00);
9488 let decoration_color = Color::new(0xFF0000FF);
9489 let decoration_input_border = OutlineInputBorder::new().border_side(side(0xFFFF00FF));
9490 let decoration_constraints = constraints(40.0, 50.0, 60.0, 70.0);
9491
9492 let decoration = InputDecoration::new()
9493 .label_style(decoration_style.clone().unwrap())
9494 .floating_label_style(decoration_style.clone().unwrap())
9495 .helper_style(decoration_style.clone().unwrap())
9496 .helper_max_lines(3)
9497 .hint_style(decoration_style.clone().unwrap())
9498 .error_style(decoration_style.clone().unwrap())
9499 .error_max_lines(3)
9500 .floating_label_behavior(FloatingLabelBehavior::Always)
9501 .floating_label_alignment(FloatingLabelAlignment::START)
9502 .is_dense(false)
9503 .content_padding(EdgeInsetsGeometry::all(4.0))
9504 .icon_color(decoration_color)
9505 .prefix_style(decoration_style.clone().unwrap())
9506 .prefix_icon_color(decoration_color)
9507 .prefix_icon_constraints(decoration_constraints)
9508 .suffix_style(decoration_style.clone().unwrap())
9509 .suffix_icon_color(decoration_color)
9510 .suffix_icon_constraints(decoration_constraints)
9511 .counter_style(decoration_style.clone().unwrap())
9512 .filled(false)
9513 .fill_color(decoration_color)
9514 .focus_color(decoration_color)
9515 .hover_color(decoration_color)
9516 .error_border(decoration_input_border)
9517 .focused_border(decoration_input_border)
9518 .focused_error_border(decoration_input_border)
9519 .disabled_border(decoration_input_border)
9520 .enabled_border(decoration_input_border)
9521 .border(OutlineInputBorder::new())
9522 .align_label_with_hint(false)
9523 .constraints(decoration_constraints)
9524 .apply_defaults(
9525 InputDecorationThemeData::new()
9526 .label_style(theme_style.clone().unwrap())
9527 .floating_label_style(theme_style.clone().unwrap())
9528 .helper_style(theme_style.clone().unwrap())
9529 .helper_max_lines(2)
9530 .hint_style(theme_style.clone().unwrap())
9531 .error_style(theme_style.clone().unwrap())
9532 .error_max_lines(2)
9533 .floating_label_behavior(FloatingLabelBehavior::Never)
9534 .floating_label_alignment(FloatingLabelAlignment::CENTER)
9535 .is_dense(true)
9536 .content_padding(EdgeInsetsGeometry::all(1.0))
9537 .icon_color(theme_color)
9538 .prefix_style(theme_style.clone().unwrap())
9539 .prefix_icon_color(theme_color)
9540 .suffix_style(theme_style.clone().unwrap())
9541 .suffix_icon_color(theme_color)
9542 .counter_style(theme_style.clone().unwrap())
9543 .filled(true)
9544 .fill_color(theme_color)
9545 .focus_color(theme_color)
9546 .hover_color(theme_color)
9547 .error_border(theme_input_border)
9548 .focused_border(theme_input_border)
9549 .focused_error_border(theme_input_border)
9550 .disabled_border(theme_input_border)
9551 .enabled_border(theme_input_border)
9552 .border(InputBorderRef::none())
9553 .align_label_with_hint(true)
9554 .constraints(constraints(10.0, 20.0, 30.0, 40.0)),
9555 );
9556
9557 assert_eq!(decoration.label_style, decoration_style);
9558 assert_eq!(decoration.floating_label_style, decoration_style);
9559 assert_eq!(decoration.helper_style, decoration_style);
9560 assert_eq!(decoration.helper_max_lines, Some(3));
9561 assert_eq!(decoration.hint_style, decoration_style);
9562 assert_eq!(decoration.error_style, decoration_style);
9563 assert_eq!(decoration.error_max_lines, Some(3));
9564 assert_eq!(
9565 decoration.floating_label_behavior,
9566 Some(FloatingLabelBehavior::Always)
9567 );
9568 assert_eq!(
9569 decoration.floating_label_alignment,
9570 Some(FloatingLabelAlignment::START)
9571 );
9572 assert_eq!(decoration.is_dense, Some(false));
9573 assert_eq!(
9574 decoration.content_padding,
9575 Some(EdgeInsetsGeometry::all(4.0))
9576 );
9577 assert_eq!(decoration.icon_color, Some(decoration_color.into()));
9578 assert_eq!(decoration.prefix_style, decoration_style);
9579 assert_eq!(decoration.prefix_icon_color, Some(decoration_color.into()));
9580 assert_eq!(
9581 decoration.prefix_icon_constraints,
9582 Some(decoration_constraints)
9583 );
9584 assert_eq!(decoration.suffix_style, decoration_style);
9585 assert_eq!(decoration.suffix_icon_color, Some(decoration_color.into()));
9586 assert_eq!(
9587 decoration.suffix_icon_constraints,
9588 Some(decoration_constraints)
9589 );
9590 assert_eq!(decoration.counter_style, decoration_style);
9591 assert_eq!(decoration.filled, Some(false));
9592 assert_eq!(decoration.fill_color, Some(decoration_color.into()));
9593 assert_eq!(decoration.focus_color, Some(decoration_color));
9594 assert_eq!(decoration.hover_color, Some(decoration_color));
9595 assert_eq!(decoration.error_border, border(decoration_input_border));
9596 assert_eq!(decoration.focused_border, border(decoration_input_border));
9597 assert_eq!(
9598 decoration.focused_error_border,
9599 border(decoration_input_border)
9600 );
9601 assert_eq!(decoration.disabled_border, border(decoration_input_border));
9602 assert_eq!(decoration.enabled_border, border(decoration_input_border));
9603 assert_eq!(decoration.border, border(OutlineInputBorder::new()));
9604 assert_eq!(decoration.align_label_with_hint, Some(false));
9605 assert_eq!(decoration.constraints, Some(decoration_constraints));
9606 }
9607
9608 #[test]
9612 fn apply_defaults_accepts_a_theme_or_its_data() {
9613 let decoration = InputDecoration::new();
9614 assert_eq!(
9615 decoration.apply_defaults(InputDecorationTheme::new()),
9616 decoration.apply_defaults(InputDecorationThemeData::new())
9617 );
9618 }
9619
9620 #[test]
9624 fn theme_data_prints_its_non_default_properties() {
9625 assert!(
9626 format!(
9627 "{:?}",
9628 InputDecorationThemeData::new()
9629 .content_padding(EdgeInsetsDirectional::only(5.0, 0.0, 0.0, 0.0))
9630 )
9631 .contains("contentPadding: EdgeInsetsDirectional(5.0, 0.0, 0.0, 0.0)")
9632 );
9633
9634 assert!(
9635 format!(
9636 "{:?}",
9637 InputDecorationThemeData::new()
9638 .content_padding(EdgeInsets::only(5.0, 0.0, 0.0, 0.0))
9639 )
9640 .contains("contentPadding: EdgeInsets(5.0, 0.0, 0.0, 0.0)")
9641 );
9642
9643 let debug_string = format!(
9644 "{:?}",
9645 InputDecorationThemeData::new()
9646 .label_style(TextStyle::new().height(1.0))
9647 .helper_style(TextStyle::new().height(2.0))
9648 .helper_max_lines(5)
9649 .hint_style(TextStyle::new().height(3.0))
9650 .error_style(TextStyle::new().height(4.0))
9651 .error_max_lines(5)
9652 .is_dense(true)
9653 .content_padding(EdgeInsets::only(0.0, 0.0, 6.0, 0.0))
9654 .is_collapsed(true)
9655 .prefix_style(TextStyle::new().height(7.0))
9656 .suffix_style(TextStyle::new().height(8.0))
9657 .counter_style(TextStyle::new().height(9.0))
9658 .filled(true)
9659 .fill_color(Color::new(0x00000010))
9660 .focus_color(Color::new(0x00000020))
9661 .error_border(UnderlineInputBorder::new())
9662 .focused_border(OutlineInputBorder::new())
9663 .focused_error_border(UnderlineInputBorder::new())
9664 .disabled_border(OutlineInputBorder::new())
9665 .enabled_border(UnderlineInputBorder::new())
9666 .border(OutlineInputBorder::new())
9667 );
9668
9669 assert!(debug_string.contains("isDense: true"));
9670 assert!(debug_string.contains(&format!("fillColor: {:?}", Color::new(0x00000010))));
9671 assert!(debug_string.contains(&format!("focusColor: {:?}", Color::new(0x00000020))));
9672 assert!(debug_string.contains("errorBorder: UnderlineInputBorder()"));
9673 assert!(debug_string.contains("focusedBorder: OutlineInputBorder()"));
9674 }
9675
9676 #[test]
9678 fn theme_data_copy_with_keeps_the_original_icon_color() {
9679 let original = InputDecorationThemeData::new().icon_color(Color::new(0xDEADBEEF));
9680 assert_eq!(original.icon_color, color(0xDEADBEEF));
9681 assert_ne!(original.fill_color, color(0xDEADCAFE));
9682 let copy1 = original.copy_with().fill_color(Color::new(0xDEADCAFE));
9683 assert_eq!(copy1.icon_color, color(0xDEADBEEF));
9684 assert_eq!(copy1.fill_color, color(0xDEADCAFE));
9685 let copy2 = original.copy_with().icon_color(Color::new(0xDEADCAFE));
9686 assert_eq!(copy2.icon_color, color(0xDEADCAFE));
9687 assert_ne!(copy2.fill_color, color(0xDEADCAFE));
9688 }
9689
9690 #[test]
9693 fn theme_data_equals_its_copy() {
9694 assert_eq!(
9695 InputDecorationThemeData::new(),
9696 InputDecorationThemeData::new().copy_with()
9697 );
9698 }
9699
9700 #[test]
9703 fn theme_data_copy_with_copies_and_replaces_values() {
9704 let original = InputDecorationThemeData::new()
9705 .focus_color(Colors::ORANGE.primary())
9706 .fill_color(Colors::GREEN.primary());
9707 let copy = original
9708 .copy_with()
9709 .focus_color(Colors::YELLOW.primary())
9710 .fill_color(Colors::BLUE.primary());
9711
9712 assert_eq!(original.focus_color, Some(Colors::ORANGE.primary()));
9713 assert_eq!(original.fill_color, Some(Colors::GREEN.primary().into()));
9714 assert_eq!(copy.focus_color, Some(Colors::YELLOW.primary()));
9715 assert_eq!(copy.fill_color, Some(Colors::BLUE.primary().into()));
9716 }
9717
9718 #[test]
9720 fn theme_data_merge_fills_only_the_nullable_fields() {
9721 test_widgets(|tester| {
9722 let app = tester.cell.borrow();
9723 let override_theme = InputDecorationThemeData::new()
9724 .label_style(TextStyle::new().color(Color::new(0x000000f0)))
9725 .floating_label_style(TextStyle::new().color(Color::new(0x000000f1)))
9726 .helper_style(TextStyle::new().color(Color::new(0x000000f2)))
9727 .helper_max_lines(1)
9728 .hint_style(TextStyle::new().color(Color::new(0x000000f3)))
9729 .error_style(TextStyle::new().color(Color::new(0x000000f4)))
9730 .error_max_lines(1)
9731 .floating_label_behavior(FloatingLabelBehavior::Never)
9732 .floating_label_alignment(FloatingLabelAlignment::CENTER)
9733 .is_dense(true)
9734 .content_padding(EdgeInsetsGeometry::all(1.0))
9735 .is_collapsed(true)
9736 .icon_color(Color::new(0x000000f5))
9737 .prefix_style(TextStyle::new().color(Color::new(0x000000f6)))
9738 .prefix_icon_color(Color::new(0x000000f7))
9739 .suffix_style(TextStyle::new().color(Color::new(0x000000f8)))
9740 .suffix_icon_color(Color::new(0x000000f9))
9741 .counter_style(TextStyle::new().color(Color::new(0x00000f10)))
9742 .filled(true)
9743 .fill_color(Color::new(0x00000f11))
9744 .active_indicator_border(side(0x00000f12))
9745 .outline_border(side(0x00000f13))
9746 .focus_color(Color::new(0x00000f14))
9747 .hover_color(Color::new(0x00000f15))
9748 .error_border(
9749 OutlineInputBorder::new()
9750 .border_radius(BorderRadius::all(Radius::circular(2.0))),
9751 )
9752 .focused_border(OutlineInputBorder::new().border_side(side(0x00000f16)))
9753 .focused_error_border(OutlineInputBorder::new().border_side(side(0x00000f17)))
9754 .disabled_border(OutlineInputBorder::new().border_side(side(0x00000f18)))
9755 .enabled_border(OutlineInputBorder::new().border_side(side(0x00000f19)))
9756 .border(OutlineInputBorder::new().border_side(side(0x00000f20)))
9757 .align_label_with_hint(true)
9758 .constraints(BoxConstraints::new().min_height(1.0).min_width(1.0));
9759
9760 let input_decoration_theme = ThemeData::new(&app).input_decoration_theme;
9761 let merged = input_decoration_theme.merge(Some(&override_theme));
9762
9763 assert_eq!(merged.label_style, override_theme.label_style);
9764 assert_eq!(
9765 merged.floating_label_style,
9766 override_theme.floating_label_style
9767 );
9768 assert_eq!(merged.helper_style, override_theme.helper_style);
9769 assert_eq!(merged.helper_max_lines, override_theme.helper_max_lines);
9770 assert_eq!(merged.hint_style, override_theme.hint_style);
9771 assert_eq!(merged.error_style, override_theme.error_style);
9772 assert_eq!(merged.error_max_lines, override_theme.error_max_lines);
9773 assert_ne!(
9774 merged.floating_label_behavior,
9775 override_theme.floating_label_behavior
9776 );
9777 assert_ne!(
9778 merged.floating_label_alignment,
9779 override_theme.floating_label_alignment
9780 );
9781 assert_ne!(merged.is_dense, override_theme.is_dense);
9782 assert_eq!(merged.content_padding, override_theme.content_padding);
9783 assert_ne!(merged.is_collapsed, override_theme.is_collapsed);
9784 assert_eq!(merged.icon_color, override_theme.icon_color);
9785 assert_eq!(merged.prefix_style, override_theme.prefix_style);
9786 assert_eq!(merged.prefix_icon_color, override_theme.prefix_icon_color);
9787 assert_eq!(merged.suffix_style, override_theme.suffix_style);
9788 assert_eq!(merged.suffix_icon_color, override_theme.suffix_icon_color);
9789 assert_eq!(merged.counter_style, override_theme.counter_style);
9790 assert_ne!(merged.filled, override_theme.filled);
9791 assert_eq!(merged.fill_color, override_theme.fill_color);
9792 assert_eq!(
9793 merged.active_indicator_border,
9794 override_theme.active_indicator_border
9795 );
9796 assert_eq!(merged.outline_border, override_theme.outline_border);
9797 assert_eq!(merged.focus_color, override_theme.focus_color);
9798 assert_eq!(merged.hover_color, override_theme.hover_color);
9799 assert_eq!(merged.error_border, override_theme.error_border);
9800 assert_eq!(merged.focused_border, override_theme.focused_border);
9801 assert_eq!(
9802 merged.focused_error_border,
9803 override_theme.focused_error_border
9804 );
9805 assert_eq!(merged.disabled_border, override_theme.disabled_border);
9806 assert_eq!(merged.enabled_border, override_theme.enabled_border);
9807 assert_eq!(merged.border, override_theme.border);
9808 assert_ne!(
9809 merged.align_label_with_hint,
9810 override_theme.align_label_with_hint
9811 );
9812 assert_eq!(merged.constraints, override_theme.constraints);
9813 });
9814 }
9815
9816 #[test]
9819 fn a_theme_widget_answers_its_properties_or_its_data() {
9820 let theme = InputDecorationTheme::new().with_is_dense(true);
9821 let data = theme.data();
9822 assert!(data.is_dense);
9823 assert_eq!(data.floating_label_behavior, FloatingLabelBehavior::Auto);
9824 assert_eq!(data.floating_label_alignment, FloatingLabelAlignment::START);
9825 assert!(!data.filled);
9826
9827 let given = InputDecorationThemeData::new().hint_max_lines(3);
9828 let theme = InputDecorationTheme::new().with_data(given.clone());
9829 assert_eq!(theme.hint_max_lines(), Some(3));
9830 assert_eq!(theme.data(), given);
9831 }
9832
9833 #[test]
9835 #[should_panic(expected = "Declaring both hint and hintText is not supported.")]
9836 fn a_decoration_with_both_hint_and_hint_text_fails_its_check() {
9837 InputDecoration::new()
9838 .hint_text("hint")
9839 .hint(Text::new("hint"))
9840 .debug_check_arguments();
9841 }
9842
9843 #[test]
9845 fn decorations_compare_widgets_by_identity_and_values_by_value() {
9846 let icon: WidgetRef = SizedBox::shrink().into_widget();
9847 let a = InputDecoration::new()
9848 .icon(icon.clone())
9849 .label_text("label");
9850 assert_eq!(a, a.copy_with());
9851 assert_eq!(a, InputDecoration::new().icon(icon).label_text("label"));
9852 assert_ne!(
9853 a,
9854 InputDecoration::new()
9855 .icon(SizedBox::shrink())
9856 .label_text("label")
9857 );
9858 }
9859
9860 #[test]
9862 fn a_decoration_prints_its_set_fields() {
9863 assert_eq!(
9864 format!(
9865 "{:?}",
9866 InputDecoration::new()
9867 .label_text("label")
9868 .helper_max_lines(2)
9869 .enabled(false)
9870 ),
9871 "InputDecoration(labelText: \"label\", helperMaxLines: \"2\", enabled: false)"
9872 );
9873 assert_eq!(
9874 format!("{:?}", InputDecoration::collapsed(Some("hint".to_owned()))),
9875 "InputDecoration(hintText: \"hint\", contentPadding: EdgeInsets.zero, \
9876 isCollapsed: true, border: _NoInputBorder(), alignLabelWithHint: false)"
9877 );
9878 }
9879 }
9880
9881 #[allow(deprecated)]
9884 mod container_and_borders {
9885 use waft_widgets::testing::{
9886 TargetPlatformVariant, WidgetTester, test_widgets, test_widgets_variant,
9887 };
9888
9889 use waft_embedder::valo::Op;
9890 use waft_embedder::{Canvas, Color, Offset, RRect, Radius, Rect, TargetPlatform};
9891 use waft_painting::{
9892 Alignment, AlignmentGeometry, BorderRadius, BorderSide, EdgeInsetsGeometry,
9893 ShapeBorder, TextDirection,
9894 };
9895 use waft_widgets::{Container, Icon, IntoWidget, RepaintBoundary, WidgetRef};
9896
9897 use super::super::*;
9898 use super::common::K_TRANSITION_DURATION;
9899 use super::common::*;
9900 use crate::app::MaterialApp;
9901 use crate::colors::Colors;
9902 use crate::icons::Icons;
9903 use crate::input_border::{InputBorderRef, OutlineInputBorder, UnderlineInputBorder};
9904 use crate::scaffold::Scaffold;
9905 use crate::theme::Theme;
9906 use crate::theme_data::{ThemeData, VisualDensity};
9907
9908 const CONTAINER_HEIGHT: f64 = 56.0;
9912
9913 const DESKTOP_CONTAINER_HEIGHT: f64 = CONTAINER_HEIGHT - 2.0 * 4.0; const ALL_PLATFORMS: [TargetPlatform; 6] = [
9921 TargetPlatform::Android,
9922 TargetPlatform::Fuchsia,
9923 TargetPlatform::IOS,
9924 TargetPlatform::Linux,
9925 TargetPlatform::MacOS,
9926 TargetPlatform::Windows,
9927 ];
9928
9929 const DESKTOP_PLATFORMS: [TargetPlatform; 3] = [
9931 TargetPlatform::Linux,
9932 TargetPlatform::MacOS,
9933 TargetPlatform::Windows,
9934 ];
9935
9936 fn pump(tester: &mut WidgetTester, decorator: BuildInputDecorator) {
9937 tester.pump_widget(decorator.build());
9938 }
9939
9940 fn theme_of(tester: &WidgetTester) -> Rc<ThemeData> {
9942 let element = tester.element(find::by_type::<InputDecorator>());
9943 Theme::of(&mut tester.cell.borrow_mut(), element)
9944 }
9945
9946 fn filled(error: bool) -> InputDecoration {
9949 let decoration = InputDecoration::new().filled(true).label_text(LABEL_TEXT);
9950 if error {
9951 decoration.error_text(ERROR_TEXT)
9952 } else {
9953 decoration.helper_text(HELPER_TEXT)
9954 }
9955 }
9956
9957 fn outlined(error: bool) -> InputDecoration {
9960 let decoration = InputDecoration::new()
9961 .border(OutlineInputBorder::new())
9962 .label_text(LABEL_TEXT);
9963 if error {
9964 decoration.error_text(ERROR_TEXT)
9965 } else {
9966 decoration.helper_text(HELPER_TEXT)
9967 }
9968 }
9969
9970 #[test]
9973 fn a_filled_enabled_container_has_the_m3_height_and_an_underline_with_rounded_top() {
9974 test_widgets(|tester| {
9975 check_filled_shape(tester, BuildInputDecorator::new(filled(false)));
9976 });
9977 }
9978
9979 fn check_filled_shape(tester: &mut WidgetTester, decorator: BuildInputDecorator) {
9980 pump(tester, decorator);
9981 assert_eq!(get_container_rect(tester).height(), CONTAINER_HEIGHT);
9982 assert!(
9983 get_border(tester)
9984 .expect("a border")
9985 .as_any()
9986 .is::<UnderlineInputBorder>()
9987 );
9988 assert_eq!(
9989 get_border_radius(tester),
9990 Some(BorderRadius::only(
9991 Radius::circular(4.0),
9992 Radius::circular(4.0),
9993 Radius::ZERO,
9994 Radius::ZERO,
9995 ))
9996 );
9997 }
9998
9999 fn check_outlined_shape(tester: &mut WidgetTester, decorator: BuildInputDecorator) {
10000 pump(tester, decorator);
10001 assert_eq!(get_container_rect(tester).height(), CONTAINER_HEIGHT);
10002 assert!(
10003 get_border(tester)
10004 .expect("a border")
10005 .as_any()
10006 .is::<OutlineInputBorder>()
10007 );
10008 assert_eq!(
10009 get_border_radius(tester),
10010 Some(BorderRadius::all(Radius::circular(4.0)))
10011 );
10012 }
10013
10014 fn check_filled_color(
10016 tester: &mut WidgetTester,
10017 decorator: BuildInputDecorator,
10018 color: fn(&ThemeData) -> Color,
10019 ) {
10020 pump(tester, decorator);
10021 let color = color(&theme_of(tester));
10022 assert_paints(
10023 &border_painter_ops(tester),
10024 &[PaintCall::RRect {
10025 rrect: None,
10026 color: Some(color),
10027 style: Some(PaintedStyle::Fill),
10028 has_mask_filter: None,
10029 }],
10030 );
10031 }
10032
10033 fn check_outlined_painted(tester: &mut WidgetTester, decorator: BuildInputDecorator) {
10035 pump(tester, decorator);
10036 assert_paints(
10037 &border_painter_ops(tester),
10038 &[PaintCall::Path {
10039 color: None,
10040 style: Some(PaintedStyle::Stroke(None)),
10041 includes: Vec::new(),
10042 excludes: Vec::new(),
10043 }],
10044 );
10045 }
10046
10047 fn check_border(
10048 tester: &mut WidgetTester,
10049 decorator: BuildInputDecorator,
10050 color: fn(&ThemeData) -> Color,
10051 width: f64,
10052 ) {
10053 pump(tester, decorator);
10054 let color = color(&theme_of(tester));
10055 assert_eq!(get_border_color(tester), color);
10056 assert_eq!(get_border_weight(tester), width);
10057 }
10058
10059 #[test]
10062 fn a_filled_enabled_container_is_filled_with_surface_container_highest() {
10063 test_widgets(|tester| {
10064 check_filled_color(tester, BuildInputDecorator::new(filled(false)), |theme| {
10065 theme.color_scheme.surface_container_highest()
10066 });
10067 });
10068 }
10069
10070 #[test]
10073 fn a_filled_enabled_active_indicator_is_on_surface_variant_and_1_wide() {
10074 test_widgets(|tester| {
10075 check_border(
10076 tester,
10077 BuildInputDecorator::new(filled(false)),
10078 |theme| theme.color_scheme.on_surface_variant(),
10079 1.0,
10080 );
10081 });
10082 }
10083
10084 #[test]
10087 fn a_filled_disabled_container_has_the_m3_height_and_an_underline_with_rounded_top() {
10088 test_widgets(|tester| {
10089 check_filled_shape(
10090 tester,
10091 BuildInputDecorator::new(filled(false).enabled(false)),
10092 );
10093 });
10094 }
10095
10096 #[test]
10099 fn a_filled_disabled_container_is_filled_with_4_percent_on_surface() {
10100 test_widgets(|tester| {
10101 check_filled_color(
10102 tester,
10103 BuildInputDecorator::new(filled(false).enabled(false)),
10104 |theme| theme.color_scheme.on_surface().with_opacity(0.04),
10105 );
10106 });
10107 }
10108
10109 #[test]
10112 fn a_filled_disabled_active_indicator_is_38_percent_on_surface_and_1_wide() {
10113 test_widgets(|tester| {
10114 check_border(
10115 tester,
10116 BuildInputDecorator::new(filled(false).enabled(false)),
10117 |theme| theme.color_scheme.on_surface().with_opacity(0.38),
10118 1.0,
10119 );
10120 });
10121 }
10122
10123 #[test]
10126 fn a_filled_hovered_container_has_the_m3_height_and_an_underline_with_rounded_top() {
10127 test_widgets(|tester| {
10128 check_filled_shape(
10129 tester,
10130 BuildInputDecorator::new(filled(false)).is_hovering(true),
10131 );
10132 });
10133 }
10134
10135 #[test]
10138 fn a_filled_hovered_container_blends_the_hover_color_over_surface_container_highest() {
10139 test_widgets(|tester| {
10140 pump(
10141 tester,
10142 BuildInputDecorator::new(filled(false)).is_hovering(true),
10143 );
10144 let theme = theme_of(tester);
10145 assert_eq!(theme.hover_color, Colors::BLACK.with_opacity(0.04));
10146 assert_paints(
10147 &border_painter_ops(tester),
10148 &[PaintCall::RRect {
10149 rrect: None,
10150 color: Some(Color::alpha_blend(
10151 theme.hover_color,
10152 theme.color_scheme.surface_container_highest(),
10153 )),
10154 style: Some(PaintedStyle::Fill),
10155 has_mask_filter: None,
10156 }],
10157 );
10158 });
10159 }
10160
10161 #[test]
10164 fn a_filled_hovered_active_indicator_is_on_surface_and_1_wide() {
10165 test_widgets(|tester| {
10166 check_border(
10167 tester,
10168 BuildInputDecorator::new(filled(false)).is_hovering(true),
10169 |theme| theme.color_scheme.on_surface(),
10170 1.0,
10171 );
10172 });
10173 }
10174
10175 #[test]
10178 fn a_filled_focused_container_has_the_m3_height_and_an_underline_with_rounded_top() {
10179 test_widgets(|tester| {
10180 check_filled_shape(
10181 tester,
10182 BuildInputDecorator::new(filled(false)).is_focused(true),
10183 );
10184 });
10185 }
10186
10187 #[test]
10190 fn a_filled_focused_container_is_filled_with_surface_container_highest() {
10191 test_widgets(|tester| {
10192 check_filled_color(
10193 tester,
10194 BuildInputDecorator::new(filled(false)).is_focused(true),
10195 |theme| theme.color_scheme.surface_container_highest(),
10196 );
10197 });
10198 }
10199
10200 #[test]
10203 fn a_filled_focused_and_hovered_container_blends_the_hover_color_over_the_fill() {
10204 test_widgets(|tester| {
10205 check_filled_color(
10206 tester,
10207 BuildInputDecorator::new(filled(false))
10208 .is_focused(true)
10209 .is_hovering(true),
10210 |theme| {
10211 let focus_color = theme.color_scheme.surface_container_highest();
10212 let hover_color = theme.hover_color;
10213 Color::alpha_blend(hover_color, focus_color)
10214 },
10215 );
10216 });
10217 }
10218
10219 #[test]
10222 fn a_filled_focused_active_indicator_is_primary_and_2_wide() {
10223 test_widgets(|tester| {
10224 check_border(
10225 tester,
10226 BuildInputDecorator::new(filled(false)).is_focused(true),
10227 |theme| theme.color_scheme.primary(),
10228 2.0,
10229 );
10230 });
10231 }
10232
10233 #[test]
10237 fn a_filled_focused_and_hovered_active_indicator_is_primary_and_2_wide() {
10238 test_widgets(|tester| {
10239 check_border(
10240 tester,
10241 BuildInputDecorator::new(filled(false))
10242 .is_focused(true)
10243 .is_hovering(true),
10244 |theme| theme.color_scheme.primary(),
10245 2.0,
10246 );
10247 });
10248 }
10249
10250 #[test]
10253 fn a_filled_error_container_has_the_m3_height_and_an_underline_with_rounded_top() {
10254 test_widgets(|tester| {
10255 check_filled_shape(tester, BuildInputDecorator::new(filled(true)));
10256 });
10257 }
10258
10259 #[test]
10262 fn a_filled_error_container_is_filled_with_surface_container_highest() {
10263 test_widgets(|tester| {
10264 check_filled_color(tester, BuildInputDecorator::new(filled(true)), |theme| {
10265 theme.color_scheme.surface_container_highest()
10266 });
10267 });
10268 }
10269
10270 #[test]
10273 fn a_filled_error_active_indicator_is_error_and_1_wide() {
10274 test_widgets(|tester| {
10275 check_border(
10276 tester,
10277 BuildInputDecorator::new(filled(true)),
10278 |theme| theme.color_scheme.error(),
10279 1.0,
10280 );
10281 });
10282 }
10283
10284 #[test]
10287 fn a_filled_focused_error_active_indicator_is_error_and_2_wide() {
10288 test_widgets(|tester| {
10289 check_border(
10290 tester,
10291 BuildInputDecorator::new(filled(true)).is_focused(true),
10292 |theme| theme.color_scheme.error(),
10293 2.0,
10294 );
10295 });
10296 }
10297
10298 #[test]
10301 fn a_filled_hovered_error_active_indicator_is_on_error_container_and_1_wide() {
10302 test_widgets(|tester| {
10303 check_border(
10304 tester,
10305 BuildInputDecorator::new(filled(true)).is_hovering(true),
10306 |theme| theme.color_scheme.on_error_container(),
10307 1.0,
10308 );
10309 });
10310 }
10311
10312 #[test]
10316 fn a_filled_focused_and_hovered_error_active_indicator_is_error_and_2_wide() {
10317 test_widgets(|tester| {
10318 check_border(
10319 tester,
10320 BuildInputDecorator::new(filled(true))
10321 .is_focused(true)
10322 .is_hovering(true),
10323 |theme| theme.color_scheme.error(),
10324 2.0,
10325 );
10326 });
10327 }
10328
10329 fn check_height_on(
10331 platforms: &[TargetPlatform],
10332 decorator: impl Fn() -> BuildInputDecorator + Sync,
10333 height: f64,
10334 ) {
10335 for platform in platforms {
10336 test_widgets_variant(TargetPlatformVariant::only(*platform), |tester| {
10337 tester.pump_widget(decorator().build());
10338 assert_eq!(
10339 get_container_rect(tester).height(),
10340 height,
10341 "on {platform:?}"
10342 );
10343 });
10344 }
10345 }
10346
10347 fn compact_decoration_theme(app: &App) -> ThemeData {
10348 ThemeData::builder()
10349 .input_decoration_theme(
10350 InputDecorationThemeData::new().visual_density(VisualDensity::COMPACT),
10351 )
10352 .build(app)
10353 }
10354
10355 fn compact_theme(app: &App) -> ThemeData {
10356 ThemeData::builder()
10357 .visual_density(VisualDensity::COMPACT)
10358 .build(app)
10359 }
10360
10361 fn standard_decoration_theme(app: &App) -> ThemeData {
10362 ThemeData::builder()
10363 .input_decoration_theme(
10364 InputDecorationThemeData::new().visual_density(VisualDensity::STANDARD),
10365 )
10366 .build(app)
10367 }
10368
10369 fn standard_theme(app: &App) -> ThemeData {
10370 ThemeData::builder()
10371 .visual_density(VisualDensity::STANDARD)
10372 .build(app)
10373 }
10374
10375 fn compact_theme_with_standard_decoration_theme(app: &App) -> ThemeData {
10377 ThemeData::builder()
10378 .visual_density(VisualDensity::COMPACT)
10379 .input_decoration_theme(
10380 InputDecorationThemeData::new().visual_density(VisualDensity::STANDARD),
10381 )
10382 .build(app)
10383 }
10384
10385 fn compact_theme_with_comfortable_decoration_theme(app: &App) -> ThemeData {
10387 ThemeData::builder()
10388 .visual_density(VisualDensity::COMPACT)
10389 .input_decoration_theme(
10390 InputDecorationThemeData::new().visual_density(VisualDensity::COMFORTABLE),
10391 )
10392 .build(app)
10393 }
10394
10395 type ThemeFn = fn(&App) -> ThemeData;
10396
10397 fn check_density_levels(
10399 decoration: fn() -> InputDecoration,
10400 density: VisualDensity,
10401 height: f64,
10402 ) {
10403 let (decoration_theme, theme): (ThemeFn, ThemeFn) = if density == VisualDensity::COMPACT
10404 {
10405 (compact_decoration_theme, compact_theme)
10406 } else {
10407 (standard_decoration_theme, standard_theme)
10408 };
10409 check_height_on(
10411 &ALL_PLATFORMS,
10412 || BuildInputDecorator::new(decoration().visual_density(density)),
10413 height,
10414 );
10415 check_height_on(
10417 &ALL_PLATFORMS,
10418 || BuildInputDecorator::new(decoration()).theme(decoration_theme),
10419 height,
10420 );
10421 check_height_on(
10423 &ALL_PLATFORMS,
10424 || BuildInputDecorator::new(decoration()).theme(theme),
10425 height,
10426 );
10427 }
10428
10429 #[test]
10432 fn a_filled_container_is_48_high_on_desktop() {
10433 check_height_on(
10434 &DESKTOP_PLATFORMS,
10435 || BuildInputDecorator::new(filled(false)),
10436 DESKTOP_CONTAINER_HEIGHT,
10437 );
10438 }
10439
10440 #[test]
10444 fn a_filled_container_is_48_high_on_every_platform_at_compact_density() {
10445 check_density_levels(|| filled(false), VisualDensity::COMPACT, 48.0);
10446 }
10447
10448 #[test]
10452 fn a_filled_container_is_56_high_on_every_platform_at_standard_density() {
10453 check_density_levels(|| filled(false), VisualDensity::STANDARD, 56.0);
10454 }
10455
10456 #[test]
10459 fn a_filled_container_takes_the_decoration_density_over_the_themes() {
10460 test_widgets(|tester| {
10461 pump(
10462 tester,
10463 BuildInputDecorator::new(
10464 filled(false).visual_density(VisualDensity::COMFORTABLE),
10465 )
10466 .theme(compact_theme_with_standard_decoration_theme),
10467 );
10468 assert_eq!(get_container_rect(tester).height(), 52.0);
10469 });
10470 }
10471
10472 #[test]
10475 fn a_filled_container_takes_the_decoration_theme_density_over_the_themes() {
10476 test_widgets(|tester| {
10477 pump(
10478 tester,
10479 BuildInputDecorator::new(filled(false))
10480 .theme(compact_theme_with_comfortable_decoration_theme),
10481 );
10482 assert_eq!(get_container_rect(tester).height(), 52.0);
10483 });
10484 }
10485
10486 #[test]
10489 fn a_filled_container_uses_the_ambient_active_indicator_border() {
10490 test_widgets(|tester| {
10491 let active_indicator_border = BorderSide {
10492 color: Colors::AMBER.primary(),
10493 width: 2.0,
10494 ..BorderSide::default()
10495 };
10496 pump(
10497 tester,
10498 BuildInputDecorator::new(InputDecoration::new().label_text(LABEL_TEXT))
10499 .input_decoration_theme(
10500 InputDecorationThemeData::new()
10501 .filled(true)
10502 .active_indicator_border(active_indicator_border),
10503 ),
10504 );
10505 assert_eq!(get_border_color(tester), active_indicator_border.color);
10506 assert_eq!(get_border_weight(tester), active_indicator_border.width);
10507 });
10508 }
10509
10510 #[test]
10513 fn an_outlined_enabled_container_has_the_m3_height_and_a_rounded_outline() {
10514 test_widgets(|tester| {
10515 check_outlined_shape(tester, BuildInputDecorator::new(outlined(false)));
10516 });
10517 }
10518
10519 #[test]
10522 fn an_outlined_enabled_container_is_stroked_and_not_filled() {
10523 test_widgets(|tester| {
10524 check_outlined_painted(tester, BuildInputDecorator::new(outlined(false)));
10525 });
10526 }
10527
10528 #[test]
10531 fn an_outlined_enabled_outline_is_outline_and_1_wide() {
10532 test_widgets(|tester| {
10533 check_border(
10534 tester,
10535 BuildInputDecorator::new(outlined(false)),
10536 |theme| theme.color_scheme.outline(),
10537 1.0,
10538 );
10539 });
10540 }
10541
10542 #[test]
10545 fn an_outlined_disabled_container_has_the_m3_height_and_a_rounded_outline() {
10546 test_widgets(|tester| {
10547 check_outlined_shape(
10548 tester,
10549 BuildInputDecorator::new(outlined(false).enabled(false)),
10550 );
10551 });
10552 }
10553
10554 #[test]
10557 fn an_outlined_disabled_container_is_stroked_and_not_filled() {
10558 test_widgets(|tester| {
10559 check_outlined_painted(
10560 tester,
10561 BuildInputDecorator::new(outlined(false).enabled(false)),
10562 );
10563 });
10564 }
10565
10566 #[test]
10569 fn an_outlined_disabled_outline_is_12_percent_on_surface_and_1_wide() {
10570 test_widgets(|tester| {
10571 check_border(
10572 tester,
10573 BuildInputDecorator::new(outlined(false).enabled(false)),
10574 |theme| theme.color_scheme.on_surface().with_opacity(0.12),
10575 1.0,
10576 );
10577 });
10578 }
10579
10580 #[test]
10583 fn an_outlined_hovered_container_has_the_m3_height_and_a_rounded_outline() {
10584 test_widgets(|tester| {
10585 check_outlined_shape(
10586 tester,
10587 BuildInputDecorator::new(outlined(false)).is_hovering(true),
10588 );
10589 });
10590 }
10591
10592 #[test]
10595 fn an_outlined_hovered_container_is_stroked_and_not_filled() {
10596 test_widgets(|tester| {
10597 check_outlined_painted(
10598 tester,
10599 BuildInputDecorator::new(outlined(false)).is_hovering(true),
10600 );
10601 });
10602 }
10603
10604 #[test]
10607 fn an_outlined_hovered_outline_is_on_surface_and_1_wide() {
10608 test_widgets(|tester| {
10609 check_border(
10610 tester,
10611 BuildInputDecorator::new(outlined(false)).is_hovering(true),
10612 |theme| theme.color_scheme.on_surface(),
10613 1.0,
10614 );
10615 });
10616 }
10617
10618 #[test]
10621 fn an_outlined_focused_container_has_the_m3_height_and_a_rounded_outline() {
10622 test_widgets(|tester| {
10623 check_outlined_shape(
10624 tester,
10625 BuildInputDecorator::new(outlined(false)).is_focused(true),
10626 );
10627 });
10628 }
10629
10630 #[test]
10633 fn an_outlined_focused_container_is_stroked_and_not_filled() {
10634 test_widgets(|tester| {
10635 check_outlined_painted(
10636 tester,
10637 BuildInputDecorator::new(outlined(false)).is_focused(true),
10638 );
10639 });
10640 }
10641
10642 #[test]
10645 fn an_outlined_focused_outline_is_primary_and_2_wide() {
10646 test_widgets(|tester| {
10647 check_border(
10648 tester,
10649 BuildInputDecorator::new(outlined(false)).is_focused(true),
10650 |theme| theme.color_scheme.primary(),
10651 2.0,
10652 );
10653 });
10654 }
10655
10656 #[test]
10660 fn an_outlined_focused_and_hovered_outline_is_primary_and_2_wide() {
10661 test_widgets(|tester| {
10662 check_border(
10663 tester,
10664 BuildInputDecorator::new(outlined(false))
10665 .is_focused(true)
10666 .is_hovering(true),
10667 |theme| theme.color_scheme.primary(),
10668 2.0,
10669 );
10670 });
10671 }
10672
10673 #[test]
10676 fn an_outlined_error_container_has_the_m3_height_and_a_rounded_outline() {
10677 test_widgets(|tester| {
10678 check_outlined_shape(tester, BuildInputDecorator::new(outlined(true)));
10679 });
10680 }
10681
10682 #[test]
10685 fn an_outlined_error_container_is_stroked_and_not_filled() {
10686 test_widgets(|tester| {
10687 check_outlined_painted(tester, BuildInputDecorator::new(outlined(true)));
10688 });
10689 }
10690
10691 #[test]
10694 fn an_outlined_error_outline_is_error_and_1_wide() {
10695 test_widgets(|tester| {
10696 check_border(
10697 tester,
10698 BuildInputDecorator::new(outlined(true)),
10699 |theme| theme.color_scheme.error(),
10700 1.0,
10701 );
10702 });
10703 }
10704
10705 #[test]
10708 fn an_outlined_focused_error_outline_is_error_and_2_wide() {
10709 test_widgets(|tester| {
10710 check_border(
10711 tester,
10712 BuildInputDecorator::new(outlined(true)).is_focused(true),
10713 |theme| theme.color_scheme.error(),
10714 2.0,
10715 );
10716 });
10717 }
10718
10719 #[test]
10722 fn an_outlined_hovered_error_outline_is_on_error_container_and_1_wide() {
10723 test_widgets(|tester| {
10724 check_border(
10725 tester,
10726 BuildInputDecorator::new(outlined(true)).is_hovering(true),
10727 |theme| theme.color_scheme.on_error_container(),
10728 1.0,
10729 );
10730 });
10731 }
10732
10733 #[test]
10737 fn an_outlined_focused_and_hovered_error_outline_is_error_and_2_wide() {
10738 test_widgets(|tester| {
10739 check_border(
10740 tester,
10741 BuildInputDecorator::new(outlined(true))
10742 .is_focused(true)
10743 .is_hovering(true),
10744 |theme| theme.color_scheme.error(),
10745 2.0,
10746 );
10747 });
10748 }
10749
10750 #[test]
10753 fn an_outlined_container_is_48_high_on_desktop() {
10754 check_height_on(
10755 &DESKTOP_PLATFORMS,
10756 || BuildInputDecorator::new(outlined(false)),
10757 DESKTOP_CONTAINER_HEIGHT,
10758 );
10759 }
10760
10761 #[test]
10765 fn an_outlined_container_is_48_high_on_every_platform_at_compact_density() {
10766 check_density_levels(|| outlined(false), VisualDensity::COMPACT, 48.0);
10767 }
10768
10769 #[test]
10773 fn an_outlined_container_is_56_high_on_every_platform_at_standard_density() {
10774 check_density_levels(|| outlined(false), VisualDensity::STANDARD, 56.0);
10775 }
10776
10777 #[test]
10780 fn an_outlined_container_takes_the_decoration_density_over_the_themes() {
10781 test_widgets(|tester| {
10782 pump(
10783 tester,
10784 BuildInputDecorator::new(
10785 outlined(false).visual_density(VisualDensity::COMFORTABLE),
10786 )
10787 .theme(compact_theme_with_standard_decoration_theme),
10788 );
10789 assert_eq!(get_container_rect(tester).height(), 52.0);
10790 });
10791 }
10792
10793 #[test]
10796 fn an_outlined_container_takes_the_decoration_theme_density_over_the_themes() {
10797 test_widgets(|tester| {
10798 pump(
10799 tester,
10800 BuildInputDecorator::new(outlined(false))
10801 .theme(compact_theme_with_comfortable_decoration_theme),
10802 );
10803 assert_eq!(get_container_rect(tester).height(), 52.0);
10804 });
10805 }
10806
10807 #[test]
10810 fn a_decorator_with_no_input_border_has_a_zero_border_weight() {
10811 test_widgets(|tester| {
10812 pump(
10813 tester,
10814 BuildInputDecorator::new(InputDecoration::new().border(InputBorderRef::none()))
10815 .is_empty(true),
10816 );
10817 assert_eq!(get_border_weight(tester), 0.0);
10818 });
10819 }
10820
10821 #[test]
10826 fn an_enabled_border_that_comes_and_goes_mid_animation_lerps_without_failing() {
10827 test_widgets(|tester| {
10828 let decorator = |wait_is_over: bool| {
10829 let mut decoration = InputDecoration::new().label_text("Test");
10830 if wait_is_over {
10831 decoration = decoration.enabled_border(
10832 OutlineInputBorder::new().border_side(BorderSide {
10833 color: Colors::BLUE.primary(),
10834 ..BorderSide::default()
10835 }),
10836 );
10837 }
10838 MaterialApp::new()
10839 .home(InputDecorator::new(decoration))
10840 .into_widget()
10841 };
10842 tester.pump_widget(decorator(false));
10843 tester.pump_widget(decorator(true));
10844 tester.pump_by(K_TRANSITION_DURATION);
10845 tester.pump_widget(decorator(false));
10846 tester.pump_by(K_TRANSITION_DURATION);
10847 assert!(get_border(tester).is_some());
10848 });
10849 }
10850
10851 fn blue_side() -> BorderSide {
10852 BorderSide {
10853 color: Colors::BLUE.primary(),
10854 ..BorderSide::default()
10855 }
10856 }
10857
10858 fn red_side() -> BorderSide {
10859 BorderSide {
10860 color: Colors::RED.primary(),
10861 ..BorderSide::default()
10862 }
10863 }
10864
10865 #[test]
10868 fn input_borders_are_equal_when_their_sides_radii_and_gap_paddings_are() {
10869 let outline_input_border = OutlineInputBorder::new()
10871 .border_radius(BorderRadius::all(Radius::circular(9.0)))
10872 .border_side(blue_side())
10873 .gap_padding(32.0);
10874 assert!(
10875 outline_input_border.eq_shape(
10876 &OutlineInputBorder::new()
10877 .border_side(blue_side())
10878 .border_radius(BorderRadius::all(Radius::circular(9.0)))
10879 .gap_padding(32.0)
10880 )
10881 );
10882 assert!(!outline_input_border.eq_shape(&OutlineInputBorder::new()));
10883 assert!(
10884 !outline_input_border.eq_shape(
10885 &OutlineInputBorder::new()
10886 .border_side(red_side())
10887 .border_radius(BorderRadius::all(Radius::circular(9.0)))
10888 .gap_padding(32.0)
10889 )
10890 );
10891 assert!(
10892 !outline_input_border.eq_shape(
10893 &OutlineInputBorder::new()
10894 .border_side(blue_side())
10895 .border_radius(BorderRadius::all(Radius::circular(10.0)))
10896 .gap_padding(32.0)
10897 )
10898 );
10899 assert!(
10900 !outline_input_border.eq_shape(
10901 &OutlineInputBorder::new()
10902 .border_side(blue_side())
10903 .border_radius(BorderRadius::all(Radius::circular(9.0)))
10904 .gap_padding(33.0)
10905 )
10906 );
10907
10908 let top = |radius: f64| {
10910 BorderRadius::only(
10911 Radius::circular(radius),
10912 Radius::circular(radius),
10913 Radius::ZERO,
10914 Radius::ZERO,
10915 )
10916 };
10917 let underline_input_border = UnderlineInputBorder::new()
10918 .border_side(blue_side())
10919 .border_radius(top(5.0));
10920 assert!(
10921 underline_input_border.eq_shape(
10922 &UnderlineInputBorder::new()
10923 .border_side(blue_side())
10924 .border_radius(top(5.0))
10925 )
10926 );
10927 assert!(!underline_input_border.eq_shape(&UnderlineInputBorder::new()));
10928 assert!(
10929 !underline_input_border.eq_shape(
10930 &UnderlineInputBorder::new()
10931 .border_side(red_side())
10932 .border_radius(top(5.0))
10933 )
10934 );
10935 assert!(
10936 !underline_input_border.eq_shape(
10937 &UnderlineInputBorder::new()
10938 .border_side(blue_side())
10939 .border_radius(top(6.0))
10940 )
10941 );
10942 }
10943
10944 #[test]
10947 fn an_outline_with_radii_larger_than_the_decorator_fills_its_rrect() {
10948 test_widgets(|tester| {
10949 const LARGER_BORDER_RADIUS: f64 = 200.0;
10950 const SMALLER_BORDER_RADIUS: f64 = 100.0;
10951 const INPUT_DECORATOR_HEIGHT: f64 = 56.0;
10952 const INPUT_DECORATOR_WIDTH: f64 = 800.0;
10953 let border_radius = BorderRadius::only(
10954 Radius::circular(SMALLER_BORDER_RADIUS),
10956 Radius::circular(LARGER_BORDER_RADIUS),
10957 Radius::circular(LARGER_BORDER_RADIUS),
10958 Radius::circular(SMALLER_BORDER_RADIUS),
10959 );
10960 pump(
10961 tester,
10962 BuildInputDecorator::new(
10963 InputDecoration::new()
10964 .filled(true)
10965 .fill_color(Color::new(0xFF00FF00))
10966 .label_text("label text")
10967 .border(OutlineInputBorder::new().border_radius(border_radius)),
10968 ),
10969 );
10970 assert_paints(
10971 &border_painter_ops(tester),
10972 &[
10973 PaintCall::Save,
10974 PaintCall::RRect {
10975 rrect: Some(border_radius.to_rrect(Rect::from_ltwh(
10976 0.0,
10977 0.0,
10978 INPUT_DECORATOR_WIDTH,
10979 INPUT_DECORATOR_HEIGHT,
10980 ))),
10981 color: Some(Color::new(0xFF00FF00)),
10982 style: Some(PaintedStyle::Fill),
10983 has_mask_filter: None,
10984 },
10985 PaintCall::Restore,
10986 ],
10987 );
10988 });
10989 }
10990
10991 #[test]
10994 fn a_rounded_outline_with_zero_gap_padding_fills_its_rrect() {
10995 test_widgets(|tester| {
10996 const BORDER_RADIUS: f64 = 30.0;
10997 const LABEL: &str = "label text";
10998 const INPUT_DECORATOR_HEIGHT: f64 = 56.0;
10999 const INPUT_DECORATOR_WIDTH: f64 = 800.0;
11000 pump(
11001 tester,
11002 BuildInputDecorator::new(
11003 InputDecoration::new()
11004 .filled(true)
11005 .fill_color(Color::new(0xFF00FF00))
11006 .label_text(LABEL)
11007 .border(
11008 OutlineInputBorder::new()
11009 .border_radius(BorderRadius::all(Radius::circular(
11010 BORDER_RADIUS,
11011 )))
11012 .gap_padding(0.0),
11013 ),
11014 ),
11015 );
11016 expect(tester, &find::text(LABEL), finds_one_widget());
11017 assert_paints(
11018 &border_painter_ops(tester),
11019 &[
11020 PaintCall::Save,
11021 PaintCall::RRect {
11022 rrect: Some(
11023 BorderRadius::all(Radius::circular(BORDER_RADIUS)).to_rrect(
11024 Rect::from_ltwh(
11025 0.0,
11026 0.0,
11027 INPUT_DECORATOR_WIDTH,
11028 INPUT_DECORATOR_HEIGHT,
11029 ),
11030 ),
11031 ),
11032 color: Some(Color::new(0xFF00FF00)),
11033 style: Some(PaintedStyle::Fill),
11034 has_mask_filter: None,
11035 },
11036 PaintCall::Restore,
11037 ],
11038 );
11039 });
11040 }
11041
11042 #[test]
11045 fn an_outline_with_a_zero_radius_draws_a_rectangle() {
11046 test_widgets(|tester| {
11047 const LABEL: &str = "Flutter";
11048 const INPUT_DECORATOR_HEIGHT: f64 = 56.0;
11049 const INPUT_DECORATOR_WIDTH: f64 = 800.0;
11050 const BORDER_WIDTH: f64 = 4.0;
11051 pump(
11052 tester,
11053 BuildInputDecorator::new(
11054 InputDecoration::new()
11055 .filled(false)
11056 .label_text(LABEL)
11057 .focused_border(
11058 OutlineInputBorder::new()
11059 .border_radius(BorderRadius::ZERO)
11060 .border_side(BorderSide {
11061 width: BORDER_WIDTH,
11062 color: Colors::RED.primary(),
11063 ..BorderSide::default()
11064 }),
11065 ),
11066 )
11067 .is_focused(true),
11068 );
11069 expect(tester, &find::text(LABEL), finds_one_widget());
11070 assert_paints(
11071 &border_painter_ops(tester),
11072 &[
11073 PaintCall::Save,
11074 PaintCall::Path {
11075 color: None,
11076 style: None,
11077 includes: vec![
11078 Offset::new(BORDER_WIDTH / 2.0, BORDER_WIDTH / 2.0),
11084 Offset::new(
11086 INPUT_DECORATOR_WIDTH - 1.0 - BORDER_WIDTH / 2.0,
11087 BORDER_WIDTH / 2.0,
11088 ),
11089 Offset::new(
11091 BORDER_WIDTH / 2.0,
11092 INPUT_DECORATOR_HEIGHT - 1.0 - BORDER_WIDTH / 2.0,
11093 ),
11094 Offset::new(
11096 INPUT_DECORATOR_WIDTH - 1.0 - BORDER_WIDTH / 2.0,
11097 INPUT_DECORATOR_HEIGHT - 1.0 - BORDER_WIDTH / 2.0,
11098 ),
11099 ],
11100 excludes: vec![
11101 Offset::ZERO,
11106 Offset::new(INPUT_DECORATOR_WIDTH - 1.0, 0.0),
11108 Offset::new(0.0, INPUT_DECORATOR_HEIGHT - 1.0),
11110 Offset::new(
11112 INPUT_DECORATOR_WIDTH - 1.0,
11113 INPUT_DECORATOR_HEIGHT - 1.0,
11114 ),
11115 ],
11116 },
11117 PaintCall::Restore,
11118 ],
11119 );
11120 });
11121 }
11122
11123 #[test]
11126 fn an_outline_fill_is_its_rounded_rect_under_the_stroked_outline() {
11127 test_widgets(|tester| {
11128 pump(
11129 tester,
11130 BuildInputDecorator::new(
11131 InputDecoration::new()
11132 .filled(true)
11133 .fill_color(Color::new(0xFF00FF00))
11134 .border(
11135 OutlineInputBorder::new()
11136 .border_radius(BorderRadius::all(Radius::circular(12.0))),
11137 ),
11138 ),
11139 );
11140 assert_paints(
11141 &decorator_ops(tester),
11142 &[
11143 PaintCall::RRect {
11145 rrect: Some(
11146 BorderRadius::all(Radius::circular(12.0))
11147 .to_rrect(Rect::from_ltwh(0.0, 0.0, 800.0, 56.0)),
11148 ),
11149 color: Some(Color::new(0xFF00FF00)),
11150 style: Some(PaintedStyle::Fill),
11151 has_mask_filter: None,
11152 },
11153 PaintCall::RRect {
11155 rrect: Some(RRect::from_ltrbr(
11156 0.5,
11157 0.5,
11158 799.5,
11159 55.5,
11160 Radius::circular(11.5),
11161 )),
11162 color: None,
11163 style: Some(PaintedStyle::Stroke(Some(1.0))),
11164 has_mask_filter: None,
11165 },
11166 ],
11167 );
11168 });
11169 }
11170
11171 const STROKE_BORDER_WIDTH: f64 = 4.0;
11172 const STROKE_BORDER_RADIUS: f64 = 12.0;
11173 const STROKE_DECORATOR_WIDTH: f64 = 800.0;
11174 const STROKE_DECORATOR_HEIGHT: f64 = 56.0;
11175
11176 fn check_stroke_align(tester: &mut WidgetTester, stroke_align: f64, expected_rrect: RRect) {
11178 pump(
11179 tester,
11180 BuildInputDecorator::new(
11181 InputDecoration::new()
11182 .filled(true)
11183 .fill_color(Color::new(0xFF00FF00))
11184 .enabled_border(
11185 OutlineInputBorder::new()
11186 .border_radius(BorderRadius::all(Radius::circular(
11187 STROKE_BORDER_RADIUS,
11188 )))
11189 .border_side(BorderSide {
11190 width: STROKE_BORDER_WIDTH,
11191 stroke_align,
11192 ..BorderSide::default()
11193 }),
11194 ),
11195 ),
11196 );
11197 assert_paints(
11198 &decorator_ops(tester),
11199 &[
11200 PaintCall::RRect {
11202 rrect: Some(RRect::from_ltrbr(
11203 0.0,
11204 0.0,
11205 STROKE_DECORATOR_WIDTH,
11206 STROKE_DECORATOR_HEIGHT,
11207 Radius::circular(STROKE_BORDER_RADIUS),
11208 )),
11209 color: Some(Color::new(0xFF00FF00)),
11210 style: Some(PaintedStyle::Fill),
11211 has_mask_filter: None,
11212 },
11213 PaintCall::RRect {
11215 rrect: Some(expected_rrect),
11216 color: None,
11217 style: Some(PaintedStyle::Stroke(Some(STROKE_BORDER_WIDTH))),
11218 has_mask_filter: None,
11219 },
11220 ],
11221 );
11222 }
11223
11224 #[test]
11227 fn an_outline_aligned_outside_strokes_outside_the_decorator() {
11228 test_widgets(|tester| {
11229 check_stroke_align(
11230 tester,
11231 BorderSide::STROKE_ALIGN_OUTSIDE,
11232 RRect::from_ltrbr(
11233 -STROKE_BORDER_WIDTH / 2.0,
11234 -STROKE_BORDER_WIDTH / 2.0,
11235 STROKE_DECORATOR_WIDTH + STROKE_BORDER_WIDTH / 2.0,
11236 STROKE_DECORATOR_HEIGHT + STROKE_BORDER_WIDTH / 2.0,
11237 Radius::circular(STROKE_BORDER_RADIUS + STROKE_BORDER_WIDTH / 2.0),
11238 ),
11239 );
11240 });
11241 }
11242
11243 #[test]
11246 fn an_outline_aligned_center_strokes_on_the_decorator_edge() {
11247 test_widgets(|tester| {
11248 check_stroke_align(
11249 tester,
11250 BorderSide::STROKE_ALIGN_CENTER,
11251 RRect::from_ltrbr(
11252 0.0,
11253 0.0,
11254 STROKE_DECORATOR_WIDTH,
11255 STROKE_DECORATOR_HEIGHT,
11256 Radius::circular(STROKE_BORDER_RADIUS),
11257 ),
11258 );
11259 });
11260 }
11261
11262 #[test]
11265 fn an_outline_aligned_inside_strokes_inside_the_decorator() {
11266 test_widgets(|tester| {
11267 check_stroke_align(
11268 tester,
11269 BorderSide::STROKE_ALIGN_INSIDE,
11270 RRect::from_ltrbr(
11271 STROKE_BORDER_WIDTH / 2.0,
11272 STROKE_BORDER_WIDTH / 2.0,
11273 STROKE_DECORATOR_WIDTH - STROKE_BORDER_WIDTH / 2.0,
11274 STROKE_DECORATOR_HEIGHT - STROKE_BORDER_WIDTH / 2.0,
11275 Radius::circular(STROKE_BORDER_RADIUS - STROKE_BORDER_WIDTH / 2.0),
11276 ),
11277 );
11278 });
11279 }
11280
11281 #[test]
11284 fn an_underline_with_rounded_bottom_corners_paints_its_bottom_as_a_ring() {
11285 test_widgets(|tester| {
11286 pump(
11287 tester,
11288 BuildInputDecorator::new(
11289 InputDecoration::new()
11290 .filled(true)
11291 .fill_color(Color::new(0xFF00FF00))
11292 .border(
11293 UnderlineInputBorder::new().border_radius(BorderRadius::only(
11294 Radius::ZERO,
11295 Radius::ZERO,
11296 Radius::circular(12.0),
11297 Radius::circular(12.0),
11298 )),
11299 ),
11300 ),
11301 );
11302 assert_paints(
11304 &decorator_ops(tester),
11305 &[PaintCall::DRRect {
11306 outer: Some(RRect::from_ltrb_and_corners(
11307 0.0,
11308 0.0,
11309 800.0,
11310 48.0,
11311 Radius::ZERO,
11312 Radius::ZERO,
11313 Radius::elliptical(12.0, 12.0),
11314 Radius::elliptical(12.0, 12.0),
11315 )),
11316 inner: Some(RRect::from_ltrb_and_corners(
11317 0.0,
11318 0.0,
11319 800.0,
11320 47.0,
11321 Radius::ZERO,
11322 Radius::ZERO,
11323 Radius::elliptical(12.0, 11.0),
11324 Radius::elliptical(12.0, 11.0),
11325 )),
11326 color: None,
11327 style: Some(PaintedStyle::Fill),
11328 }],
11329 );
11330 });
11331 }
11332
11333 fn ops_of_border(border: &dyn ShapeBorder, rect: Rect) -> Vec<Op> {
11335 let mut canvas = Canvas::new();
11336 border.paint(&mut canvas, rect, None);
11337 canvas.build().ops().to_vec()
11338 }
11339
11340 #[test]
11343 fn an_underline_paints_only_its_bottom_side_with_bottom_radii_clamped_to_half_its_height() {
11344 let canvas_rect = Rect::from_ltwh(0.0, 0.0, 100.0, 100.0);
11345 let border = UnderlineInputBorder::new()
11346 .border_radius(BorderRadius::all(Radius::circular(12.0)));
11347 assert_paints(
11348 &ops_of_border(&border, canvas_rect),
11349 &[PaintCall::DRRect {
11350 outer: Some(RRect::from_ltrb_and_corners(
11351 0.0,
11352 0.0,
11353 100.0,
11354 100.0,
11355 Radius::ZERO,
11356 Radius::ZERO,
11357 Radius::elliptical(12.0, 12.0),
11358 Radius::elliptical(12.0, 12.0),
11359 )),
11360 inner: Some(RRect::from_ltrb_and_corners(
11361 0.0,
11362 0.0,
11363 100.0,
11364 99.0,
11365 Radius::ZERO,
11366 Radius::ZERO,
11367 Radius::elliptical(12.0, 11.0),
11368 Radius::elliptical(12.0, 11.0),
11369 )),
11370 color: None,
11371 style: None,
11372 }],
11373 );
11374
11375 let border2 = UnderlineInputBorder::new()
11376 .border_radius(BorderRadius::all(Radius::circular(60.0)));
11377 assert!(
11378 paints(
11379 &ops_of_border(&border2, canvas_rect),
11380 &[PaintCall::DRRect {
11381 outer: Some(RRect::from_ltrb_and_corners(
11382 0.0,
11383 0.0,
11384 100.0,
11385 100.0,
11386 Radius::ZERO,
11387 Radius::ZERO,
11388 Radius::elliptical(50.0, 50.0),
11389 Radius::elliptical(50.0, 50.0),
11390 )),
11391 inner: Some(RRect::from_ltrb_and_corners(
11392 0.0,
11393 0.0,
11394 100.0,
11395 99.0,
11396 Radius::ZERO,
11397 Radius::ZERO,
11398 Radius::elliptical(50.0, 49.0),
11399 Radius::elliptical(50.0, 49.0),
11400 )),
11401 color: None,
11402 style: None
11403 }]
11404 )
11405 .is_ok(),
11406 "clamp is expected"
11407 );
11408 }
11409
11410 #[test]
11413 fn an_underline_draws_its_line_inside_the_bottom_of_the_rect() {
11414 let canvas_rect = Rect::from_ltwh(0.0, 0.0, 100.0, 100.0);
11415 const BORDER_WIDTH: f64 = 2.0;
11416 let border = UnderlineInputBorder::new().border_side(BorderSide {
11417 width: BORDER_WIDTH,
11418 ..BorderSide::default()
11419 });
11420 assert_paints(
11421 &ops_of_border(&border, canvas_rect),
11422 &[PaintCall::line(
11423 Offset::new(0.0, canvas_rect.height() - BORDER_WIDTH / 2.0),
11424 Offset::new(100.0, canvas_rect.height() - BORDER_WIDTH / 2.0),
11425 Some(BORDER_WIDTH),
11426 )],
11427 );
11428 }
11429
11430 fn gap_frame(text_direction: TextDirection, decorator: InputDecorator) -> WidgetRef {
11433 MaterialApp::new()
11434 .home(
11435 Scaffold::new().body(
11436 Container::new()
11437 .padding(EdgeInsetsGeometry::all(16.0))
11438 .alignment(AlignmentGeometry::Alignment(Alignment::CENTER))
11439 .child(waft_widgets::Directionality::new(
11440 text_direction,
11441 RepaintBoundary::new().child(decorator),
11442 )),
11443 ),
11444 )
11445 .into_widget()
11446 }
11447
11448 fn border_local(tester: &WidgetTester, point: Offset) -> Offset {
11450 let element = find_border_painter(tester).expect("a border painter");
11451 let app = tester.cell.borrow();
11452 let border_box =
11453 InputDecorator::container_of(&app, element).expect("a decorator container");
11454 border_box.global_to_local(&app, point, None)
11455 }
11456
11457 #[test]
11460 fn an_outline_with_a_tall_radius_starts_its_gap_at_the_label() {
11461 test_widgets(|tester| {
11462 let decorator = || {
11463 InputDecorator::new(
11464 InputDecoration::new().label_text(LABEL_TEXT).border(
11465 OutlineInputBorder::new()
11466 .border_radius(BorderRadius::all(Radius::circular(30.0)))
11467 .gap_padding(0.0),
11468 ),
11469 )
11470 .is_focused(true)
11471 .is_empty(true)
11472 };
11473 tester.pump_widget(gap_frame(TextDirection::Ltr, decorator()));
11474 let label_bottom_left_local_to_border =
11476 border_local(tester, get_label_rect(tester).bottom_left());
11477 assert_paints(
11478 &border_painter_ops(tester),
11479 &[
11480 PaintCall::Save,
11481 PaintCall::Path { color: None, style: None, includes: vec![
11483 label_bottom_left_local_to_border + Offset::new(-1.0, 0.0),
11484 label_bottom_left_local_to_border + Offset::new(-1.0, -1.0),
11485 ], excludes: vec![label_bottom_left_local_to_border] },
11487 PaintCall::Restore,
11488 ],
11489 );
11490
11491 tester.pump_widget(gap_frame(TextDirection::Rtl, decorator()));
11492 let label_bottom_right_local_to_border =
11494 border_local(tester, get_label_rect(tester).bottom_right());
11495 assert_paints(
11496 &border_painter_ops(tester),
11497 &[
11498 PaintCall::Save,
11499 PaintCall::Path { color: None, style: None, includes: vec![
11501 label_bottom_right_local_to_border + Offset::new(1.0, 0.0),
11502 label_bottom_right_local_to_border + Offset::new(1.0, -1.0),
11503 ], excludes: vec![label_bottom_right_local_to_border] },
11505 PaintCall::Restore,
11506 ],
11507 );
11508 });
11509 }
11510
11511 #[test]
11514 fn an_outline_at_the_default_radius_leaves_a_gap_exactly_under_the_label() {
11515 test_widgets(|tester| {
11516 let decorator = || {
11517 InputDecorator::new(
11518 InputDecoration::new()
11519 .label_text(LABEL_TEXT)
11520 .border(OutlineInputBorder::new().gap_padding(0.0)),
11521 )
11522 .is_focused(true)
11523 };
11524 for text_direction in [TextDirection::Ltr, TextDirection::Rtl] {
11525 tester.pump_widget(gap_frame(text_direction, decorator()));
11526 let label_rect = get_label_rect(tester);
11527 let center_left = border_local(tester, label_rect.center_left());
11528 let center_right = border_local(tester, label_rect.center_right());
11529 assert!(
11530 paints(
11531 &border_painter_ops(tester),
11532 &[
11533 PaintCall::Save,
11534 PaintCall::Path { color: None, style: None, includes: vec![center_left, center_right], excludes: vec![
11537 center_left + Offset::new(1.0, 0.0),
11538 center_right + Offset::new(-1.0, 0.0),
11539 ] },
11540 PaintCall::Restore,
11541 ]
11542 )
11543 .is_ok(),
11544 "{text_direction:?}"
11545 );
11546 }
11547 });
11548 }
11549
11550 #[test]
11553 fn an_outline_with_an_icon_leaves_its_gap_under_the_label() {
11554 test_widgets(|tester| {
11555 let decorator = || {
11556 InputDecorator::new(
11557 InputDecoration::new()
11558 .icon(Icon::new(Some(Icons::insert_link())))
11559 .label_text(LABEL_TEXT)
11560 .border(OutlineInputBorder::new()),
11561 )
11562 .is_focused(true)
11563 .is_empty(true)
11564 };
11565 for text_direction in [TextDirection::Ltr, TextDirection::Rtl] {
11566 tester.pump_widget(gap_frame(text_direction, decorator()));
11567 let label_rect = get_label_rect(tester);
11568 let center_left = border_local(tester, label_rect.center_left());
11569 let center_right = border_local(tester, label_rect.center_right());
11570 assert!(
11571 paints(
11572 &border_painter_ops(tester),
11573 &[
11574 PaintCall::Save,
11575 PaintCall::Path {
11576 color: None,
11577 style: None,
11578 includes: Vec::new(),
11579 excludes: vec![center_left, center_right]
11580 },
11581 PaintCall::Restore,
11582 ]
11583 )
11584 .is_ok(),
11585 "{text_direction:?}"
11586 );
11587 }
11588 });
11589 }
11590 }
11591
11592 mod label_and_hint {
11594 #![allow(deprecated)]
11595 use waft_widgets::testing::{WidgetTester, test_widgets};
11596
11597 use std::time::Duration;
11598
11599 use waft_embedder::{Color, Offset, Rect, Size};
11600 use waft_painting::{AnyColor, BorderSide, TextDirection, TextOverflow, TextStyle};
11601 use waft_widgets::{
11602 AnyElement, Center, FadeTransition, Icon, IntoWidget, RichText, SizedBox, Text,
11603 WidgetRef, downcast_widget,
11604 };
11605
11606 use super::super::*;
11607 use super::common::K_TRANSITION_DURATION;
11608 use super::common::*;
11609 use crate::colors::Colors;
11610 use crate::icons::Icons;
11611 use crate::input_border::OutlineInputBorder;
11612 use crate::theme::Theme;
11613 use crate::theme_data::{ThemeData, VisualDensity};
11614
11615 fn ms(milliseconds: u64) -> Duration {
11616 Duration::from_millis(milliseconds)
11617 }
11618
11619 fn decorator_theme(tester: &WidgetTester) -> Rc<ThemeData> {
11621 let element = tester.element(find::by_type::<InputDecorator>());
11622 let mut app = tester.cell.borrow_mut();
11623 Theme::of(&mut app, element)
11624 }
11625
11626 fn color_of(style: &TextStyle) -> Option<Color> {
11627 style.color.as_ref().map(AnyColor::color)
11628 }
11629
11630 fn get_input_rect(tester: &WidgetTester) -> Rect {
11632 tester.get_rect(find::text(INPUT_TEXT))
11633 }
11634
11635 fn get_hint_rect(tester: &WidgetTester) -> Rect {
11637 tester.get_rect(find::text(HINT_TEXT))
11638 }
11639
11640 fn find_hint(tester: &WidgetTester) -> AnyElement {
11642 tester.element(find::text(HINT_TEXT))
11643 }
11644
11645 fn get_hint_opacity(tester: &WidgetTester) -> f64 {
11648 let hint = find_hint(tester);
11649 let fade = tester
11650 .ancestors(hint)
11651 .into_iter()
11652 .find(|element| {
11653 let app = tester.cell.borrow();
11654 downcast_widget::<FadeTransition>(&**element.widget(&app)).is_some()
11655 })
11656 .expect("a FadeTransition above the hint");
11657 let opacity = tester.widget(fade, |fade: &FadeTransition| fade.opacity.clone());
11658 let app = tester.cell.borrow();
11659 opacity.value(&app)
11660 }
11661
11662 fn get_hint_style(tester: &WidgetTester) -> TextStyle {
11664 let rich_text = tester.element(find::descendant(
11665 find::text(HINT_TEXT),
11666 find::by_type::<RichText>(),
11667 ));
11668 tester.widget(rich_text, |rich_text: &RichText| {
11669 rich_text.text.style().cloned().expect("a style")
11670 })
11671 }
11672
11673 fn pump(tester: &mut WidgetTester, decorator: BuildInputDecorator) {
11674 tester.pump_widget(decorator.build());
11675 }
11676
11677 fn expected_label_style(theme: &ThemeData, color: Color) -> TextStyle {
11682 theme
11683 .text_theme
11684 .body_large
11685 .clone()
11686 .expect("bodyLarge")
11687 .copy_with()
11688 .color(color)
11689 .height(1.0)
11690 }
11691
11692 fn filled_label() -> InputDecoration {
11693 InputDecoration::new().filled(true).label_text(LABEL_TEXT)
11694 }
11695
11696 fn outlined_label() -> InputDecoration {
11697 InputDecoration::new()
11698 .border(OutlineInputBorder::new())
11699 .label_text(LABEL_TEXT)
11700 .helper_text(HELPER_TEXT)
11701 }
11702
11703 fn check_label_color(
11704 tester: &mut WidgetTester,
11705 decorator: BuildInputDecorator,
11706 expected: fn(&ThemeData) -> Color,
11707 ) {
11708 pump(tester, decorator);
11709 let theme = decorator_theme(tester);
11710 assert_eq!(color_of(&get_label_style(tester)), Some(expected(&theme)));
11711 }
11712
11713 #[test]
11716 fn filled_label_and_input_start_at_the_padding_and_the_input_gap_in_ltr() {
11717 test_widgets(|tester| {
11718 pump(
11719 tester,
11720 BuildInputDecorator::new(
11721 InputDecoration::new().filled(true).label_text(LABEL_TEXT),
11722 ),
11723 );
11724 assert_eq!(get_decorator_rect(tester).size(), Size::new(800.0, 56.0));
11725 let label_and_input_start = 12.0 + 4.0; assert_eq!(get_label_rect(tester).left, label_and_input_start);
11727 assert_eq!(get_input_rect(tester).left, label_and_input_start);
11728 });
11729 }
11730
11731 #[test]
11734 fn filled_label_and_input_start_at_the_padding_and_the_input_gap_in_rtl() {
11735 test_widgets(|tester| {
11736 pump(
11737 tester,
11738 BuildInputDecorator::new(
11739 InputDecoration::new().filled(true).label_text(LABEL_TEXT),
11740 )
11741 .text_direction(TextDirection::Rtl),
11742 );
11743 assert_eq!(get_decorator_rect(tester).size(), Size::new(800.0, 56.0));
11744 let label_and_input_start = 12.0 + 4.0; assert_eq!(get_label_rect(tester).right, 800.0 - label_and_input_start);
11746 assert_eq!(get_input_rect(tester).right, 800.0 - label_and_input_start);
11747 });
11748 }
11749
11750 #[test]
11753 fn filled_enabled_label_text_has_body_large_in_on_surface_variant() {
11754 test_widgets(|tester| {
11755 pump(tester, BuildInputDecorator::new(filled_label()));
11756 let theme = decorator_theme(tester);
11757 let expected =
11760 expected_label_style(&theme, theme.color_scheme.on_surface_variant());
11761 assert_eq!(get_label_style(tester), expected);
11762 });
11763 }
11764
11765 #[test]
11768 fn filled_disabled_label_text_is_on_surface_at_38_percent() {
11769 test_widgets(|tester| {
11770 check_label_color(
11771 tester,
11772 BuildInputDecorator::new(filled_label().enabled(false)),
11773 |theme| theme.color_scheme.on_surface().with_opacity(0.38),
11774 );
11775 });
11776 }
11777
11778 #[test]
11781 fn filled_hovered_label_text_is_on_surface_variant() {
11782 test_widgets(|tester| {
11783 check_label_color(
11784 tester,
11785 BuildInputDecorator::new(filled_label()).is_hovering(true),
11786 |theme| theme.color_scheme.on_surface_variant(),
11787 );
11788 });
11789 }
11790
11791 #[test]
11794 fn filled_focused_label_text_is_primary() {
11795 test_widgets(|tester| {
11796 check_label_color(
11797 tester,
11798 BuildInputDecorator::new(filled_label()).is_focused(true),
11799 |theme| theme.color_scheme.primary(),
11800 );
11801 });
11802 }
11803
11804 #[test]
11807 fn filled_focused_and_hovered_label_text_is_primary() {
11808 test_widgets(|tester| {
11809 check_label_color(
11810 tester,
11811 BuildInputDecorator::new(filled_label())
11812 .is_focused(true)
11813 .is_hovering(true),
11814 |theme| theme.color_scheme.primary(),
11815 );
11816 });
11817 }
11818
11819 #[test]
11822 fn filled_error_label_text_is_error() {
11823 test_widgets(|tester| {
11824 check_label_color(
11825 tester,
11826 BuildInputDecorator::new(filled_label().error_text(ERROR_TEXT)),
11827 |theme| theme.color_scheme.error(),
11828 );
11829 });
11830 }
11831
11832 #[test]
11835 fn filled_focused_error_label_text_is_error() {
11836 test_widgets(|tester| {
11837 check_label_color(
11838 tester,
11839 BuildInputDecorator::new(filled_label().error_text(ERROR_TEXT))
11840 .is_focused(true),
11841 |theme| theme.color_scheme.error(),
11842 );
11843 });
11844 }
11845
11846 #[test]
11849 fn filled_hovered_error_label_text_is_on_error_container() {
11850 test_widgets(|tester| {
11851 check_label_color(
11852 tester,
11853 BuildInputDecorator::new(filled_label().error_text(ERROR_TEXT))
11854 .is_hovering(true),
11855 |theme| theme.color_scheme.on_error_container(),
11856 );
11857 });
11858 }
11859
11860 #[test]
11863 fn filled_focused_and_hovered_error_label_text_is_error() {
11864 test_widgets(|tester| {
11865 check_label_color(
11866 tester,
11867 BuildInputDecorator::new(filled_label().error_text(ERROR_TEXT))
11868 .is_focused(true)
11869 .is_hovering(true),
11870 |theme| theme.color_scheme.error(),
11871 );
11872 });
11873 }
11874
11875 #[test]
11878 fn outlined_label_and_input_start_at_the_padding_and_the_input_gap_in_ltr() {
11879 test_widgets(|tester| {
11880 pump(
11881 tester,
11882 BuildInputDecorator::new(
11883 InputDecoration::new()
11884 .border(OutlineInputBorder::new())
11885 .label_text(LABEL_TEXT),
11886 ),
11887 );
11888 assert_eq!(get_decorator_rect(tester).size(), Size::new(800.0, 56.0));
11889 let label_and_input_start = 12.0 + 4.0; assert_eq!(get_label_rect(tester).left, label_and_input_start);
11891 assert_eq!(get_input_rect(tester).left, label_and_input_start);
11892 });
11893 }
11894
11895 #[test]
11898 fn outlined_label_and_input_start_at_the_padding_and_the_input_gap_in_rtl() {
11899 test_widgets(|tester| {
11900 pump(
11901 tester,
11902 BuildInputDecorator::new(
11903 InputDecoration::new()
11904 .border(OutlineInputBorder::new())
11905 .label_text(LABEL_TEXT),
11906 )
11907 .text_direction(TextDirection::Rtl),
11908 );
11909 assert_eq!(get_decorator_rect(tester).size(), Size::new(800.0, 56.0));
11910 let label_and_input_start = 12.0 + 4.0; assert_eq!(get_label_rect(tester).right, 800.0 - label_and_input_start);
11912 assert_eq!(get_input_rect(tester).right, 800.0 - label_and_input_start);
11913 });
11914 }
11915
11916 #[test]
11919 fn outlined_label_and_input_start_at_a_custom_gap_padding_in_ltr() {
11920 test_widgets(|tester| {
11921 let custom_gap = 6.0;
11922 pump(
11923 tester,
11924 BuildInputDecorator::new(
11925 InputDecoration::new()
11926 .border(OutlineInputBorder::new().gap_padding(custom_gap))
11927 .label_text(LABEL_TEXT),
11928 ),
11929 );
11930 assert_eq!(get_decorator_rect(tester).size(), Size::new(800.0, 56.0));
11931 let label_and_input_start = 12.0 + custom_gap; assert_eq!(get_label_rect(tester).left, label_and_input_start);
11933 assert_eq!(get_input_rect(tester).left, label_and_input_start);
11934 });
11935 }
11936
11937 #[test]
11940 fn outlined_label_and_input_start_at_a_custom_gap_padding_in_rtl() {
11941 test_widgets(|tester| {
11942 let custom_gap = 6.0;
11943 pump(
11944 tester,
11945 BuildInputDecorator::new(
11946 InputDecoration::new()
11947 .border(OutlineInputBorder::new().gap_padding(custom_gap))
11948 .label_text(LABEL_TEXT),
11949 )
11950 .text_direction(TextDirection::Rtl),
11951 );
11952 assert_eq!(get_decorator_rect(tester).size(), Size::new(800.0, 56.0));
11953 let label_and_input_start = 12.0 + custom_gap; assert_eq!(get_label_rect(tester).right, 800.0 - label_and_input_start);
11955 assert_eq!(get_input_rect(tester).right, 800.0 - label_and_input_start);
11956 });
11957 }
11958
11959 #[test]
11962 fn outlined_enabled_label_text_has_body_large_in_on_surface_variant() {
11963 test_widgets(|tester| {
11964 pump(tester, BuildInputDecorator::new(outlined_label()));
11965 let theme = decorator_theme(tester);
11966 let expected =
11969 expected_label_style(&theme, theme.color_scheme.on_surface_variant());
11970 assert_eq!(get_label_style(tester), expected);
11971 });
11972 }
11973
11974 #[test]
11977 fn outlined_disabled_label_text_is_on_surface_at_38_percent() {
11978 test_widgets(|tester| {
11979 check_label_color(
11980 tester,
11981 BuildInputDecorator::new(outlined_label().enabled(false)),
11982 |theme| theme.color_scheme.on_surface().with_opacity(0.38),
11983 );
11984 });
11985 }
11986
11987 #[test]
11990 fn outlined_hovered_label_text_is_on_surface_variant() {
11991 test_widgets(|tester| {
11992 check_label_color(
11993 tester,
11994 BuildInputDecorator::new(outlined_label()).is_hovering(true),
11995 |theme| theme.color_scheme.on_surface_variant(),
11996 );
11997 });
11998 }
11999
12000 #[test]
12003 fn outlined_focused_label_text_is_primary() {
12004 test_widgets(|tester| {
12005 check_label_color(
12006 tester,
12007 BuildInputDecorator::new(outlined_label()).is_focused(true),
12008 |theme| theme.color_scheme.primary(),
12009 );
12010 });
12011 }
12012
12013 #[test]
12016 fn outlined_focused_and_hovered_label_text_is_primary() {
12017 test_widgets(|tester| {
12018 check_label_color(
12019 tester,
12020 BuildInputDecorator::new(outlined_label())
12021 .is_focused(true)
12022 .is_hovering(true),
12023 |theme| theme.color_scheme.primary(),
12024 );
12025 });
12026 }
12027
12028 fn outlined_error_label() -> InputDecoration {
12029 InputDecoration::new()
12030 .border(OutlineInputBorder::new())
12031 .label_text(LABEL_TEXT)
12032 .error_text(ERROR_TEXT)
12033 }
12034
12035 #[test]
12038 fn outlined_error_label_text_is_error() {
12039 test_widgets(|tester| {
12040 check_label_color(
12041 tester,
12042 BuildInputDecorator::new(outlined_error_label()),
12043 |theme| theme.color_scheme.error(),
12044 );
12045 });
12046 }
12047
12048 #[test]
12051 fn outlined_focused_error_label_text_is_error() {
12052 test_widgets(|tester| {
12053 check_label_color(
12054 tester,
12055 BuildInputDecorator::new(outlined_error_label()).is_focused(true),
12056 |theme| theme.color_scheme.error(),
12057 );
12058 });
12059 }
12060
12061 #[test]
12064 fn outlined_hovered_error_label_text_is_on_error_container() {
12065 test_widgets(|tester| {
12066 check_label_color(
12067 tester,
12068 BuildInputDecorator::new(outlined_error_label()).is_hovering(true),
12069 |theme| theme.color_scheme.on_error_container(),
12070 );
12071 });
12072 }
12073
12074 #[test]
12077 fn outlined_focused_and_hovered_error_label_text_is_error() {
12078 test_widgets(|tester| {
12079 check_label_color(
12080 tester,
12081 BuildInputDecorator::new(outlined_error_label())
12082 .is_focused(true)
12083 .is_hovering(true),
12084 |theme| theme.color_scheme.error(),
12085 );
12086 });
12087 }
12088
12089 #[test]
12092 fn underlined_label_and_input_have_no_horizontal_padding_in_ltr() {
12093 test_widgets(|tester| {
12094 pump(
12095 tester,
12096 BuildInputDecorator::new(InputDecoration::new().label_text(LABEL_TEXT)),
12097 );
12098 assert_eq!(get_decorator_rect(tester).size(), Size::new(800.0, 56.0));
12099 assert_eq!(get_label_rect(tester).left, 0.0);
12100 assert_eq!(get_input_rect(tester).left, 0.0);
12101 });
12102 }
12103
12104 #[test]
12107 fn underlined_label_and_input_have_no_horizontal_padding_in_rtl() {
12108 test_widgets(|tester| {
12109 pump(
12110 tester,
12111 BuildInputDecorator::new(InputDecoration::new().label_text(LABEL_TEXT))
12112 .text_direction(TextDirection::Rtl),
12113 );
12114 assert_eq!(get_decorator_rect(tester).size(), Size::new(800.0, 56.0));
12115 assert_eq!(get_label_rect(tester).right, 800.0);
12116 assert_eq!(get_input_rect(tester).right, 800.0);
12117 });
12118 }
12119
12120 fn style(color: Color) -> TextStyle {
12121 TextStyle::new().color(color).font_size(16.0)
12122 }
12123
12124 fn check_label_color_and_size(tester: &WidgetTester, expected: &TextStyle) {
12125 let label_style = get_label_style(tester);
12126 assert_eq!(color_of(&label_style), color_of(expected));
12127 assert_eq!(label_style.font_size, expected.font_size);
12128 }
12129
12130 #[test]
12133 fn floating_label_style_overrides_the_default_style() {
12134 test_widgets(|tester| {
12135 let floating_label_style = style(Colors::INDIGO.primary());
12136 pump(
12137 tester,
12138 BuildInputDecorator::new(
12139 InputDecoration::new()
12140 .label_text(LABEL_TEXT)
12141 .floating_label_style(floating_label_style.clone()),
12142 )
12143 .is_empty(true)
12144 .is_focused(true),
12146 );
12147 check_label_color_and_size(tester, &floating_label_style);
12148 });
12149 }
12150
12151 #[test]
12154 fn floating_label_style_defaults_to_label_style() {
12155 test_widgets(|tester| {
12156 let label_style = style(Colors::AMBER.primary());
12157 pump(
12158 tester,
12159 BuildInputDecorator::new(
12160 InputDecoration::new()
12161 .label_text(LABEL_TEXT)
12162 .label_style(label_style.clone()),
12163 )
12164 .is_empty(true)
12165 .is_focused(true),
12167 );
12168 check_label_color_and_size(tester, &label_style);
12169 });
12170 }
12171
12172 #[test]
12175 fn floating_label_style_takes_precedence_over_label_style() {
12176 test_widgets(|tester| {
12177 let label_style = style(Colors::AMBER.primary());
12178 let floating_label_style = style(Colors::INDIGO.primary());
12179 pump(
12180 tester,
12181 BuildInputDecorator::new(
12182 InputDecoration::new()
12183 .label_text(LABEL_TEXT)
12184 .label_style(label_style)
12185 .floating_label_style(floating_label_style.clone()),
12186 )
12187 .is_empty(true)
12188 .is_focused(true),
12190 );
12191 check_label_color_and_size(tester, &floating_label_style);
12192 });
12193 }
12194
12195 #[test]
12198 fn the_theme_label_style_overrides_the_default_style() {
12199 test_widgets(|tester| {
12200 let label_style = style(Colors::AMBER.primary());
12201 pump(
12202 tester,
12203 BuildInputDecorator::new(InputDecoration::new().label_text(LABEL_TEXT))
12205 .is_empty(true)
12206 .input_decoration_theme(
12207 InputDecorationThemeData::new().label_style(label_style.clone()),
12208 ),
12209 );
12210 assert_eq!(color_of(&get_label_style(tester)), color_of(&label_style));
12211 });
12212 }
12213
12214 #[test]
12217 fn the_theme_floating_label_style_overrides_the_default_style() {
12218 test_widgets(|tester| {
12219 let floating_label_style = style(Colors::INDIGO.primary());
12220 pump(
12221 tester,
12222 BuildInputDecorator::new(InputDecoration::new().label_text(LABEL_TEXT))
12223 .is_empty(true)
12224 .is_focused(true)
12226 .input_decoration_theme(
12227 InputDecorationThemeData::new()
12228 .floating_label_style(floating_label_style.clone()),
12229 ),
12230 );
12231 assert_eq!(
12232 color_of(&get_label_style(tester)),
12233 color_of(&floating_label_style)
12234 );
12235 });
12236 }
12237
12238 #[test]
12241 fn floating_label_style_is_always_used_when_the_label_always_floats() {
12242 test_widgets(|tester| {
12243 let label_style = style(Colors::AMBER.primary());
12244 let floating_label_style = style(Colors::INDIGO.primary());
12245 let decoration = InputDecoration::new()
12246 .label_text(LABEL_TEXT)
12247 .label_style(label_style)
12248 .floating_label_style(floating_label_style.clone())
12249 .floating_label_behavior(FloatingLabelBehavior::Always);
12250
12251 pump(
12252 tester,
12253 BuildInputDecorator::new(decoration.clone()).is_empty(true),
12254 );
12255 check_label_color_and_size(tester, &floating_label_style);
12256
12257 tester.pump_widget(
12259 BuildInputDecorator::new(decoration)
12260 .is_empty(true)
12261 .is_focused(true)
12262 .build(),
12263 );
12264 check_label_color_and_size(tester, &floating_label_style);
12265 });
12266 }
12267
12268 #[test]
12277 fn the_label_appears_above_the_input() {
12278 test_widgets(|tester| {
12279 pump(
12280 tester,
12281 BuildInputDecorator::new(InputDecoration::new().label_text(LABEL_TEXT)),
12282 );
12283
12284 assert_eq!(get_decorator_rect(tester).size(), Size::new(800.0, 56.0));
12291 assert_eq!(get_label_rect(tester).top, 8.0);
12292 assert_eq!(get_label_rect(tester).bottom, 20.0);
12293 assert_eq!(get_input_rect(tester).top, 24.0);
12294 assert_eq!(get_input_rect(tester).bottom, 48.0);
12295 });
12296 }
12297
12298 #[test]
12301 fn the_label_appears_within_the_input_when_there_is_no_text_content() {
12302 test_widgets(|tester| {
12303 pump(
12304 tester,
12305 BuildInputDecorator::new(InputDecoration::new().label_text(LABEL_TEXT))
12306 .is_empty(true),
12307 );
12308 assert_eq!(get_decorator_rect(tester).size(), Size::new(800.0, 56.0));
12309 assert_eq!(get_label_rect(tester).top, 20.0);
12312 assert_eq!(get_label_rect(tester).bottom, 36.0);
12313 });
12314 }
12315
12316 fn large_label_rect(tester: &mut WidgetTester, decoration: InputDecoration) -> Rect {
12317 pump(tester, BuildInputDecorator::new(decoration).is_empty(true));
12318 let large = tester.element(find::by_widget_predicate(
12319 "a SizedBox 1000 wide",
12320 |_app, widget| {
12321 downcast_widget::<SizedBox>(widget)
12322 .is_some_and(|sized| sized.width == Some(1000.0))
12323 },
12324 ));
12325 tester.get_rect(large)
12326 }
12327
12328 #[test]
12331 fn a_label_within_the_input_fills_the_space_between_the_paddings() {
12332 test_widgets(|tester| {
12333 let large_label = || SizedBox::new().width(1000.0).height(16.0).into_widget();
12336
12337 let horizontal_padding = 16.0;
12339 let rect = large_label_rect(
12340 tester,
12341 InputDecoration::new().filled(true).label(large_label()),
12342 );
12343 assert_eq!(rect.left, horizontal_padding);
12344 assert_eq!(rect.right, 800.0 - horizontal_padding);
12345
12346 let rect = large_label_rect(
12348 tester,
12349 InputDecoration::new()
12350 .border(OutlineInputBorder::new())
12351 .label(large_label()),
12352 );
12353 assert_eq!(rect.left, horizontal_padding);
12354 assert_eq!(rect.right, 800.0 - horizontal_padding);
12355
12356 let rect = large_label_rect(
12358 tester,
12359 InputDecoration::new()
12360 .filled(true)
12361 .label(large_label())
12362 .suffix_icon(Icon::new(Some(Icons::align_horizontal_left_sharp())))
12363 .prefix_icon(Icon::new(Some(Icons::align_horizontal_right_sharp()))),
12364 );
12365
12366 let affixes_horizontal_padding = 52.0;
12369 assert_eq!(rect.left, affixes_horizontal_padding);
12370 assert_eq!(rect.right, 800.0 - affixes_horizontal_padding);
12371
12372 let rect = large_label_rect(
12374 tester,
12375 InputDecoration::new()
12376 .border(OutlineInputBorder::new())
12377 .label(large_label())
12378 .suffix_icon(Icon::new(Some(Icons::align_horizontal_left_sharp())))
12379 .prefix_icon(Icon::new(Some(Icons::align_horizontal_right_sharp()))),
12380 );
12381 assert_eq!(rect.left, affixes_horizontal_padding);
12382 assert_eq!(rect.right, 800.0 - affixes_horizontal_padding);
12383 });
12384 }
12385
12386 #[test]
12390 fn an_always_floating_label_appears_above_an_empty_input() {
12391 test_widgets(|tester| {
12392 pump(
12393 tester,
12394 BuildInputDecorator::new(
12395 InputDecoration::new()
12396 .label_text(LABEL_TEXT)
12397 .floating_label_behavior(FloatingLabelBehavior::Always),
12398 )
12399 .is_empty(true),
12400 );
12401 assert_eq!(get_decorator_rect(tester).size(), Size::new(800.0, 56.0));
12402 assert_eq!(get_label_rect(tester).top, 8.0);
12403 assert_eq!(get_label_rect(tester).bottom, 20.0);
12404 });
12405 }
12406
12407 #[test]
12411 fn a_never_floating_label_appears_within_an_input_with_content() {
12412 test_widgets(|tester| {
12413 pump(
12414 tester,
12415 BuildInputDecorator::new(
12416 InputDecoration::new()
12417 .label_text(LABEL_TEXT)
12418 .floating_label_behavior(FloatingLabelBehavior::Never),
12419 ),
12420 );
12421 assert_eq!(get_decorator_rect(tester).size(), Size::new(800.0, 56.0));
12422 assert_eq!(get_label_rect(tester).top, 20.0);
12425 assert_eq!(get_label_rect(tester).bottom, 36.0);
12426 });
12427 }
12428
12429 #[test]
12432 fn the_ambient_theme_floating_label_behavior_is_used() {
12433 test_widgets(|tester| {
12434 pump(
12435 tester,
12436 BuildInputDecorator::new(InputDecoration::new().label_text(LABEL_TEXT))
12437 .input_decoration_theme(
12438 InputDecorationThemeData::new()
12439 .floating_label_behavior(FloatingLabelBehavior::Never),
12440 ),
12441 );
12442 assert_eq!(get_decorator_rect(tester).size(), Size::new(800.0, 56.0));
12443 assert_eq!(get_label_rect(tester).top, 20.0);
12446 assert_eq!(get_label_rect(tester).bottom, 36.0);
12447 });
12448 }
12449
12450 #[test]
12453 fn the_floating_label_animates_for_167_ms_along_fast_out_slow_in() {
12454 test_widgets(|tester| {
12455 let build = |is_focused: bool| {
12456 BuildInputDecorator::new(
12457 InputDecoration::new()
12458 .label_text(LABEL_TEXT)
12459 .floating_label_behavior(FloatingLabelBehavior::Auto),
12460 )
12461 .is_empty(true)
12462 .is_focused(is_focused)
12463 .build()
12464 };
12465
12466 tester.pump_widget(build(false));
12467 assert_eq!(get_label_rect(tester).top, 20.0);
12468
12469 tester.pump_widget(build(true));
12472 tester.pump_by(ms(42));
12473 assert_more_or_less_equals_within(get_label_rect(tester).top, 17.09, 0.5);
12474 tester.pump_by(ms(42));
12475 assert_more_or_less_equals_within(get_label_rect(tester).top, 10.66, 0.5);
12476 tester.pump_by(ms(42));
12477 assert_more_or_less_equals_within(get_label_rect(tester).top, 8.47, 0.5);
12478 tester.pump_by(ms(41));
12479 assert_eq!(get_label_rect(tester).top, 8.0);
12480
12481 tester.pump_and_settle();
12486
12487 tester.pump_widget(build(false));
12490 tester.pump_by(ms(42));
12491 assert_more_or_less_equals_within(get_label_rect(tester).top, 10.90, 0.5);
12492 tester.pump_by(ms(42));
12493 assert_more_or_less_equals_within(get_label_rect(tester).top, 17.34, 0.5);
12494 tester.pump_by(ms(42));
12495 assert_more_or_less_equals_within(get_label_rect(tester).top, 19.69, 0.5);
12496 tester.pump_by(ms(41));
12497 assert_eq!(get_label_rect(tester).top, 20.0);
12498 });
12499 }
12500
12501 #[test]
12504 fn the_label_withdraws_when_not_empty_or_focused_while_enabled() {
12505 test_widgets(|tester| {
12506 let mut pump_decorator = |focused: bool, enabled: bool, empty: bool| {
12507 tester.pump_widget(
12508 BuildInputDecorator::new(
12509 InputDecoration::new()
12510 .label_text("Label")
12511 .enabled(enabled)
12512 .filled(false)
12513 .focused_border(OutlineInputBorder::new())
12514 .disabled_border(OutlineInputBorder::new())
12515 .border(OutlineInputBorder::new()),
12516 )
12517 .is_empty(empty)
12518 .is_focused(focused)
12519 .build(),
12520 );
12521 tester.pump_by(K_TRANSITION_DURATION);
12522 let rect = tester.get_rect(find::text("Label"));
12523 (rect.top_left(), rect.size())
12524 };
12525
12526 let label_size = Size::new(82.5, 16.0);
12527 let floating = (Offset::new(16.0, -5.5), label_size * 0.75);
12528 let inline = (Offset::new(16.0, 20.0), label_size);
12529 assert_eq!(pump_decorator(false, true, true), inline);
12530 assert_eq!(pump_decorator(false, true, false), floating);
12531 assert_eq!(pump_decorator(true, true, true), floating);
12532 assert_eq!(pump_decorator(true, true, false), floating);
12533 assert_eq!(pump_decorator(false, false, true), inline);
12534 assert_eq!(pump_decorator(false, false, false), floating);
12535 assert_eq!(pump_decorator(true, false, true), inline);
12537 assert_eq!(pump_decorator(true, false, false), floating);
12538 });
12539 }
12540
12541 #[test]
12545 fn the_floating_label_width_scales_up_when_focused() {
12546 test_widgets(|tester| {
12547 let long_string_a = "A".repeat(200);
12548 let long_string_b = "B".repeat(200);
12549 let build = |text: &str, is_focused: bool| {
12550 Center::new()
12551 .child(
12552 SizedBox::square(Some(100.0)).child(
12553 BuildInputDecorator::new(InputDecoration::new().label_text(text))
12554 .is_empty(true)
12555 .is_focused(is_focused)
12556 .build(),
12557 ),
12558 )
12559 .into_widget()
12560 };
12561 let label_size =
12562 |tester: &WidgetTester, text: &str| tester.get_size(find::text(text));
12563
12564 tester.pump_widget(build(&long_string_a, false));
12565 assert_eq!(label_size(tester, &long_string_a), Size::new(100.0, 16.0));
12566
12567 tester.pump_widget(build(&long_string_b, true));
12568 tester.pump_by(K_TRANSITION_DURATION);
12569 let size = label_size(tester, &long_string_b);
12570 assert!((size.width() - 100.0 / 0.75).abs() < 1e-5, "{size:?}");
12572 assert_eq!(size.height(), 16.0);
12573 });
12574 }
12575
12576 #[test]
12579 fn the_floating_label_is_centred_on_a_wide_outline() {
12580 test_widgets(|tester| {
12581 pump(
12582 tester,
12583 BuildInputDecorator::new(
12584 InputDecoration::new()
12585 .label_text(LABEL_TEXT)
12586 .enabled_border(OutlineInputBorder::new().border_side(BorderSide {
12587 width: 4.0,
12588 ..BorderSide::default()
12589 }))
12590 .floating_label_behavior(FloatingLabelBehavior::Always),
12591 )
12592 .is_empty(true),
12593 );
12594 tester.pump_by(K_TRANSITION_DURATION);
12595
12596 assert_eq!(get_label_rect(tester).top, -4.0);
12599 });
12600 }
12601
12602 fn density_frames(
12603 tester: &mut WidgetTester,
12604 visual_density: VisualDensity,
12605 height: f64,
12606 inline_label: (f64, f64),
12607 floating_label: (f64, f64),
12608 content: (f64, f64),
12609 ) {
12610 let decorator = |is_empty: bool, is_focused: bool| {
12611 BuildInputDecorator::new(
12612 InputDecoration::new()
12613 .label_text(LABEL_TEXT)
12614 .hint_text(HINT_TEXT),
12615 )
12616 .is_empty(is_empty)
12617 .is_focused(is_focused)
12618 .visual_density(visual_density)
12619 .build()
12620 };
12621 tester.pump_widget(decorator(true, false));
12622
12623 assert_eq!(get_decorator_rect(tester).size(), Size::new(800.0, height));
12624
12625 assert_eq!(get_label_rect(tester).top, inline_label.0);
12627 assert_eq!(get_label_rect(tester).bottom, inline_label.1);
12628 assert_eq!(get_hint_opacity(tester), 0.0);
12629 assert_eq!(get_border_bottom(tester), height);
12630 assert_eq!(get_border_weight(tester), 1.0);
12631
12632 tester.pump_widget(decorator(true, true));
12634 tester.pump_by(K_TRANSITION_DURATION);
12635
12636 assert_eq!(get_decorator_rect(tester).size(), Size::new(800.0, height));
12638 assert_eq!(get_label_rect(tester).top, floating_label.0);
12639 assert_eq!(get_label_rect(tester).bottom, floating_label.1);
12640 assert_eq!(get_hint_rect(tester).top, content.0);
12641 assert_eq!(get_hint_rect(tester).bottom, content.1);
12642 assert_eq!(get_hint_opacity(tester), 1.0);
12643 assert_eq!(get_border_bottom(tester), height);
12644 assert_eq!(get_border_weight(tester), 2.0);
12645
12646 tester.pump_widget(decorator(false, true));
12647 tester.pump_by(K_TRANSITION_DURATION);
12648
12649 assert_eq!(get_decorator_rect(tester).size(), Size::new(800.0, height));
12651 assert_eq!(get_label_rect(tester).top, floating_label.0);
12652 assert_eq!(get_label_rect(tester).bottom, floating_label.1);
12653 assert_eq!(get_input_rect(tester).top, content.0);
12654 assert_eq!(get_input_rect(tester).bottom, content.1);
12655 assert_eq!(get_hint_opacity(tester), 0.0);
12656 assert_eq!(get_border_bottom(tester), height);
12657 assert_eq!(get_border_weight(tester), 2.0);
12658 }
12659
12660 #[test]
12663 fn the_decorator_respects_a_reduced_theme_visual_density() {
12664 test_widgets(|tester| {
12665 density_frames(
12672 tester,
12673 VisualDensity::COMPACT,
12674 48.0,
12675 (16.0, 32.0),
12676 (4.0, 16.0),
12677 (20.0, 44.0),
12678 );
12679 });
12680 }
12681
12682 #[test]
12685 fn the_decorator_respects_an_increased_theme_visual_density() {
12686 test_widgets(|tester| {
12687 density_frames(
12694 tester,
12695 VisualDensity::new(2.0, 2.0),
12696 64.0,
12697 (24.0, 40.0),
12698 (12.0, 24.0),
12699 (28.0, 52.0),
12700 );
12701 });
12702 }
12703
12704 #[test]
12708 fn the_theme_floating_label_style_reaches_a_text_label_widget() {
12709 test_widgets(|tester| {
12710 let floating_label_style = TextStyle::new()
12711 .font_size(16.0)
12712 .merge(Some(&TextStyle::new().color(Colors::INDIGO.primary())));
12713 pump(
12714 tester,
12715 BuildInputDecorator::new(InputDecoration::new().label(Text::new("label")))
12716 .is_empty(true)
12717 .is_focused(true)
12719 .input_decoration_theme(
12720 InputDecorationThemeData::new()
12721 .floating_label_style(floating_label_style.clone()),
12722 ),
12723 );
12724 assert_eq!(
12726 color_of(&get_label_style(tester)),
12727 color_of(&floating_label_style)
12728 );
12729 });
12730 }
12731
12732 #[test]
12736 fn the_theme_label_style_reaches_a_text_label_widget() {
12737 test_widgets(|tester| {
12738 let label_style = TextStyle::new()
12739 .font_size(16.0)
12740 .merge(Some(&TextStyle::new().color(Colors::PURPLE.primary())));
12741 pump(
12742 tester,
12743 BuildInputDecorator::new(InputDecoration::new().label(Text::new("label")))
12745 .is_empty(true)
12746 .input_decoration_theme(
12747 InputDecorationThemeData::new().label_style(label_style.clone()),
12748 ),
12749 );
12750 assert_eq!(color_of(&get_label_style(tester)), color_of(&label_style));
12752 });
12753 }
12754
12755 fn hint_decoration(outlined: bool, with_label: bool) -> InputDecoration {
12760 let mut decoration = InputDecoration::new().hint_text(HINT_TEXT);
12761 decoration = if outlined {
12762 decoration.border(OutlineInputBorder::new())
12763 } else {
12764 decoration.filled(true)
12765 };
12766 if with_label {
12767 decoration = decoration.label_text(LABEL_TEXT);
12768 }
12769 decoration
12770 }
12771
12772 fn hint_style_decoration(outlined: bool, with_label: bool) -> InputDecoration {
12774 let mut decoration = InputDecoration::new().hint_text(HINT_TEXT);
12775 decoration = if outlined {
12776 decoration.border(OutlineInputBorder::new())
12777 } else {
12778 decoration.filled(true)
12779 };
12780 if with_label {
12781 decoration = decoration.label_text(LABEL_TEXT);
12782 }
12783 decoration
12784 }
12785
12786 fn check_hint_alignment(
12788 tester: &mut WidgetTester,
12789 decorator: BuildInputDecorator,
12790 container_height: f64,
12791 input: (f64, f64),
12792 hint_opacity: f64,
12793 ) {
12794 pump(tester, decorator);
12795 assert_eq!(get_container_rect(tester).height(), container_height);
12796 assert_eq!(get_input_rect(tester).top, input.0);
12797 assert_eq!(get_input_rect(tester).bottom, input.1);
12798 assert_eq!(get_hint_rect(tester).top, get_input_rect(tester).top);
12799 assert_eq!(get_hint_rect(tester).bottom, get_input_rect(tester).bottom);
12800 assert_eq!(get_hint_opacity(tester), hint_opacity);
12801 }
12802
12803 fn check_hint_style(
12805 tester: &mut WidgetTester,
12806 decorator: BuildInputDecorator,
12807 expected_color: fn(&ThemeData) -> Color,
12808 ) {
12809 pump(tester, decorator);
12810 assert_eq!(get_hint_opacity(tester), 1.0);
12812 let theme = decorator_theme(tester);
12813 let expected_style = theme
12814 .text_theme
12815 .body_large
12816 .clone()
12817 .expect("bodyLarge")
12818 .copy_with()
12819 .color(expected_color(&theme));
12820 assert_eq!(get_hint_style(tester), expected_style);
12821 }
12822
12823 fn on_surface_variant(theme: &ThemeData) -> Color {
12824 theme.color_scheme.on_surface_variant()
12825 }
12826
12827 fn disabled_on_surface(theme: &ThemeData) -> Color {
12828 theme.color_scheme.on_surface().with_opacity(0.38)
12829 }
12830
12831 macro_rules! hint_alignment_cases {
12833 ($outlined:expr, $with_label:expr, $height:expr, $input:expr,
12834 $($name:ident: $dart:literal, |$d:ident| $decoration:expr, |$b:ident| $builder:expr,
12835 $opacity:expr;)*) => {
12836 $(
12837 #[doc = concat!("input_decorator_test.dart '", $dart, "'.")]
12838 #[test]
12839 fn $name() {
12840 test_widgets(|tester| {
12841 let $d = hint_decoration($outlined, $with_label);
12842 let $b = BuildInputDecorator::new($decoration);
12843 check_hint_alignment(tester, $builder, $height, $input, $opacity);
12844 });
12845 }
12846 )*
12847 };
12848 }
12849
12850 hint_alignment_cases! { false, false, 48.0, (12.0, 36.0),
12855 filled_enabled_empty_hint_aligns_with_the_input_and_shows:
12856 "hint - for filled text field without label - when field is enabled - hint and input align vertically when decorator is empty",
12857 |d| d, |b| b.is_empty(true), 1.0;
12858 filled_enabled_hint_aligns_with_a_non_empty_input_and_hides:
12859 "hint - for filled text field without label - when field is enabled - hint and input align vertically when decorator is not empty",
12860 |d| d, |b| b, 0.0;
12861 filled_disabled_empty_hint_aligns_with_the_input_and_shows:
12862 "hint - for filled text field without label - when field is disabled - hint and input align vertically when decorator is empty",
12863 |d| d.enabled(false), |b| b.is_empty(true), 1.0;
12864 filled_disabled_hint_aligns_with_a_non_empty_input_and_hides:
12865 "hint - for filled text field without label - when field is disabled - hint and input align vertically when decorator is not empty",
12866 |d| d.enabled(false), |b| b, 0.0;
12867 filled_hovered_empty_hint_aligns_with_the_input_and_shows:
12868 "hint - for filled text field without label - when field is hovered - hint and input align vertically when decorator is empty",
12869 |d| d, |b| b.is_hovering(true).is_empty(true), 1.0;
12870 filled_hovered_hint_aligns_with_a_non_empty_input_and_hides:
12871 "hint - for filled text field without label - when field is hovered - hint and input align vertically when decorator is not empty",
12872 |d| d, |b| b.is_hovering(true), 0.0;
12873 filled_focused_empty_hint_aligns_with_the_input_and_shows:
12874 "hint - for filled text field without label - when field is focused - hint and input align vertically when decorator is empty",
12875 |d| d, |b| b.is_focused(true).is_empty(true), 1.0;
12876 filled_focused_hint_aligns_with_a_non_empty_input_and_hides:
12877 "hint - for filled text field without label - when field is focused - hint and input align vertically when decorator is not empty",
12878 |d| d, |b| b.is_focused(true), 0.0;
12879 filled_error_empty_hint_aligns_with_the_input_and_shows:
12880 "hint - for filled text field without label - when field is in error - hint and input align vertically when decorator is empty",
12881 |d| d.error_text(ERROR_TEXT), |b| b.is_empty(true), 1.0;
12882 filled_error_hint_aligns_with_a_non_empty_input_and_hides:
12883 "hint - for filled text field without label - when field is in error - hint and input align vertically when decorator is not empty",
12884 |d| d.error_text(ERROR_TEXT), |b| b, 0.0;
12885 }
12886
12887 hint_alignment_cases! { false, true, 56.0, (24.0, 48.0),
12894 filled_labelled_enabled_empty_hint_aligns_with_the_input_and_hides:
12895 "hint - for filled text field with label - when field is enabled - hint and input align vertically when decorator is empty",
12896 |d| d, |b| b.is_empty(true), 0.0;
12897 filled_labelled_enabled_hint_aligns_with_a_non_empty_input_and_hides:
12898 "hint - for filled text field with label - when field is enabled - hint and input align vertically when decorator is not empty",
12899 |d| d, |b| b, 0.0;
12900 filled_labelled_disabled_empty_hint_aligns_with_the_input_and_hides:
12901 "hint - for filled text field with label - when field is disabled - hint and input align vertically when decorator is empty",
12902 |d| d.enabled(false), |b| b.is_empty(true), 0.0;
12903 filled_labelled_disabled_hint_aligns_with_a_non_empty_input_and_hides:
12904 "hint - for filled text field with label - when field is disabled - hint and input align vertically when decorator is not empty",
12905 |d| d.enabled(false), |b| b, 0.0;
12906 filled_labelled_hovered_empty_hint_aligns_with_the_input_and_hides:
12907 "hint - for filled text field with label - when field is hovered - hint and input align vertically when decorator is empty",
12908 |d| d, |b| b.is_hovering(true).is_empty(true), 0.0;
12909 filled_labelled_hovered_hint_aligns_with_a_non_empty_input_and_hides:
12910 "hint - for filled text field with label - when field is hovered - hint and input align vertically when decorator is not empty",
12911 |d| d, |b| b.is_hovering(true), 0.0;
12912 filled_labelled_focused_empty_hint_aligns_with_the_input_and_shows:
12913 "hint - for filled text field with label - when field is focused - hint and input align vertically when decorator is empty",
12914 |d| d, |b| b.is_focused(true).is_empty(true), 1.0;
12915 filled_labelled_focused_hint_aligns_with_a_non_empty_input_and_hides:
12916 "hint - for filled text field with label - when field is focused - hint and input align vertically when decorator is not empty",
12917 |d| d, |b| b.is_focused(true), 0.0;
12918 filled_labelled_error_empty_hint_aligns_with_the_input_and_hides:
12919 "hint - for filled text field with label - when field is in error - hint and input align vertically when decorator is empty",
12920 |d| d.error_text(ERROR_TEXT), |b| b.is_empty(true), 0.0;
12921 filled_labelled_error_hint_aligns_with_a_non_empty_input_and_hides:
12922 "hint - for filled text field with label - when field is in error - hint and input align vertically when decorator is not empty",
12923 |d| d.error_text(ERROR_TEXT), |b| b, 0.0;
12924 }
12925
12926 hint_alignment_cases! { true, false, 56.0, (16.0, 40.0),
12931 outlined_enabled_empty_hint_aligns_with_the_input_and_shows:
12932 "hint - for outlined text field without label - when field is enabled - hint and input align vertically when decorator is empty",
12933 |d| d, |b| b.is_empty(true), 1.0;
12934 outlined_enabled_hint_aligns_with_a_non_empty_input_and_hides:
12935 "hint - for outlined text field without label - when field is enabled - hint and input align vertically when decorator is not empty",
12936 |d| d, |b| b, 0.0;
12937 outlined_disabled_empty_hint_aligns_with_the_input_and_shows:
12938 "hint - for outlined text field without label - when field is disabled - hint and input align vertically when decorator is empty",
12939 |d| d.enabled(false), |b| b.is_empty(true), 1.0;
12940 outlined_disabled_hint_aligns_with_a_non_empty_input_and_hides:
12941 "hint - for outlined text field without label - when field is disabled - hint and input align vertically when decorator is not empty",
12942 |d| d.enabled(false), |b| b, 0.0;
12943 outlined_hovered_empty_hint_aligns_with_the_input_and_shows:
12944 "hint - for outlined text field without label - when field is hovered - hint and input align vertically when decorator is empty",
12945 |d| d, |b| b.is_hovering(true).is_empty(true), 1.0;
12946 outlined_hovered_hint_aligns_with_a_non_empty_input_and_hides:
12947 "hint - for outlined text field without label - when field is hovered - hint and input align vertically when decorator is not empty",
12948 |d| d, |b| b.is_hovering(true), 0.0;
12949 outlined_focused_empty_hint_aligns_with_the_input_and_shows:
12950 "hint - for outlined text field without label - when field is focused - hint and input align vertically when decorator is empty",
12951 |d| d, |b| b.is_focused(true).is_empty(true), 1.0;
12952 outlined_focused_hint_aligns_with_a_non_empty_input_and_hides:
12953 "hint - for outlined text field without label - when field is focused - hint and input align vertically when decorator is not empty",
12954 |d| d, |b| b.is_focused(true), 0.0;
12955 outlined_error_empty_hint_aligns_with_the_input_and_shows:
12956 "hint - for outlined text field without label - when field is in error - hint and input align vertically when decorator is empty",
12957 |d| d.error_text(ERROR_TEXT), |b| b.is_empty(true), 1.0;
12958 outlined_error_hint_aligns_with_a_non_empty_input_and_hides:
12959 "hint - for outlined text field without label - when field is in error - hint and input align vertically when decorator is not empty",
12960 |d| d.error_text(ERROR_TEXT), |b| b, 0.0;
12961 }
12962
12963 hint_alignment_cases! { true, true, 56.0, (16.0, 40.0),
12968 outlined_labelled_enabled_empty_hint_aligns_with_the_input_and_hides:
12969 "hint - for outlined text field with label - when field is enabled - hint and input align vertically when decorator is empty",
12970 |d| d, |b| b.is_empty(true), 0.0;
12971 outlined_labelled_enabled_hint_aligns_with_a_non_empty_input_and_hides:
12972 "hint - for outlined text field with label - when field is enabled - hint and input align vertically when decorator is not empty",
12973 |d| d, |b| b, 0.0;
12974 outlined_labelled_disabled_empty_hint_aligns_with_the_input_and_hides:
12975 "hint - for outlined text field with label - when field is disabled - hint and input align vertically when decorator is empty",
12976 |d| d.enabled(false), |b| b.is_empty(true), 0.0;
12977 outlined_labelled_disabled_hint_aligns_with_a_non_empty_input_and_hides:
12978 "hint - for outlined text field with label - when field is disabled - hint and input align vertically when decorator is not empty",
12979 |d| d.enabled(false), |b| b, 0.0;
12980 outlined_labelled_hovered_empty_hint_aligns_with_the_input_and_hides:
12981 "hint - for outlined text field with label - when field is hovered - hint and input align vertically when decorator is empty",
12982 |d| d, |b| b.is_hovering(true).is_empty(true), 0.0;
12983 outlined_labelled_hovered_hint_aligns_with_a_non_empty_input_and_hides:
12984 "hint - for outlined text field with label - when field is hovered - hint and input align vertically when decorator is not empty",
12985 |d| d, |b| b.is_hovering(true), 0.0;
12986 outlined_labelled_focused_empty_hint_aligns_with_the_input_and_shows:
12987 "hint - for outlined text field with label - when field is focused - hint and input align vertically when decorator is empty",
12988 |d| d, |b| b.is_focused(true).is_empty(true), 1.0;
12989 outlined_labelled_focused_hint_aligns_with_a_non_empty_input_and_hides:
12990 "hint - for outlined text field with label - when field is focused - hint and input align vertically when decorator is not empty",
12991 |d| d, |b| b.is_focused(true), 0.0;
12992 outlined_labelled_error_empty_hint_aligns_with_the_input_and_hides:
12993 "hint - for outlined text field with label - when field is in error - hint and input align vertically when decorator is empty",
12994 |d| d.error_text(ERROR_TEXT), |b| b.is_empty(true), 0.0;
12995 outlined_labelled_error_hint_aligns_with_a_non_empty_input_and_hides:
12996 "hint - for outlined text field with label - when field is in error - hint and input align vertically when decorator is not empty",
12997 |d| d.error_text(ERROR_TEXT), |b| b, 0.0;
12998 }
12999
13000 macro_rules! hint_style_cases {
13002 ($($name:ident: $dart:literal, $outlined:expr, $with_label:expr,
13003 |$d:ident| $decoration:expr, |$b:ident| $builder:expr, $color:expr;)*) => {
13004 $(
13005 #[doc = concat!("input_decorator_test.dart '", $dart, "'.")]
13006 #[test]
13007 fn $name() {
13008 test_widgets(|tester| {
13009 let $d = hint_style_decoration($outlined, $with_label);
13010 let $b = BuildInputDecorator::new($decoration);
13011 check_hint_style(tester, $builder, $color);
13012 });
13013 }
13014 )*
13015 };
13016 }
13017
13018 hint_style_cases! {
13019 filled_enabled_hint_is_body_large_in_on_surface_variant:
13020 "hint - for filled text field without label - when field is enabled - hint has correct style when visible",
13021 false, false, |d| d, |b| b.is_empty(true), on_surface_variant;
13022 filled_disabled_hint_is_body_large_in_on_surface_at_38_percent:
13023 "hint - for filled text field without label - when field is disabled - hint has correct style when visible",
13024 false, false, |d| d.enabled(false), |b| b.is_empty(true), disabled_on_surface;
13025 filled_hovered_hint_is_body_large_in_on_surface_variant:
13026 "hint - for filled text field without label - when field is hovered - hint has correct style when visible",
13027 false, false, |d| d, |b| b.is_hovering(true).is_empty(true), on_surface_variant;
13028 filled_focused_hint_is_body_large_in_on_surface_variant:
13029 "hint - for filled text field without label - when field is focused - hint has correct style when visible",
13030 false, false, |d| d, |b| b.is_focused(true).is_empty(true), on_surface_variant;
13031 filled_error_hint_is_body_large_in_on_surface_variant:
13032 "hint - for filled text field without label - when field is in error - hint has correct style when visible",
13033 false, false, |d| d.error_text(ERROR_TEXT), |b| b.is_empty(true), on_surface_variant;
13034 filled_labelled_focused_hint_is_body_large_in_on_surface_variant:
13035 "hint - for filled text field with label - when field is focused - hint has correct style when visible",
13036 false, true, |d| d, |b| b.is_focused(true).is_empty(true), on_surface_variant;
13037 outlined_enabled_hint_is_body_large_in_on_surface_variant:
13038 "hint - for outlined text field without label - when field is enabled - hint has correct style when visible",
13039 true, false, |d| d, |b| b.is_empty(true), on_surface_variant;
13040 outlined_disabled_hint_is_body_large_in_on_surface_at_38_percent:
13041 "hint - for outlined text field without label - when field is disabled - hint has correct style when visible",
13042 true, false, |d| d.enabled(false), |b| b.is_empty(true), disabled_on_surface;
13043 outlined_hovered_hint_is_body_large_in_on_surface_variant:
13044 "hint - for outlined text field without label - when field is hovered - hint has correct style when visible",
13045 true, false, |d| d, |b| b.is_hovering(true).is_empty(true), on_surface_variant;
13046 outlined_focused_hint_is_body_large_in_on_surface_variant:
13047 "hint - for outlined text field without label - when field is focused - hint has correct style when visible",
13048 true, false, |d| d, |b| b.is_focused(true).is_empty(true), on_surface_variant;
13049 outlined_error_hint_is_body_large_in_on_surface_variant:
13050 "hint - for outlined text field without label - when field is in error - hint has correct style when visible",
13051 true, false, |d| d.error_text(ERROR_TEXT), |b| b.is_empty(true), on_surface_variant;
13052 outlined_labelled_focused_hint_is_body_large_in_on_surface_variant:
13053 "hint - for outlined text field with label - when field is focused - hint has correct style when visible",
13054 true, true, |d| d, |b| b.is_focused(true).is_empty(true), on_surface_variant;
13055 }
13056
13057 #[test]
13063 fn align_label_with_hint_centres_the_label_on_the_hint() {
13064 test_widgets(|tester| {
13065 pump(
13066 tester,
13067 BuildInputDecorator::new(
13068 InputDecoration::new()
13069 .label_text(LABEL_TEXT)
13070 .align_label_with_hint(true)
13071 .hint_text(HINT_TEXT),
13072 )
13073 .is_empty(true),
13074 );
13075 assert_eq!(
13077 get_label_rect(tester).center().dy(),
13078 get_hint_rect(tester).center().dy()
13079 );
13080 });
13081 }
13082
13083 fn ac_unit() -> WidgetRef {
13084 Icon::new(Some(Icons::ac_unit())).into_widget()
13085 }
13086
13087 #[test]
13090 fn an_outlined_label_aligns_with_the_prefix_icon_by_default() {
13091 test_widgets(|tester| {
13092 pump(
13093 tester,
13094 BuildInputDecorator::new(
13095 InputDecoration::new()
13096 .prefix_icon(ac_unit())
13097 .label_text(LABEL_TEXT)
13098 .border(OutlineInputBorder::new()),
13099 )
13100 .is_focused(true),
13101 );
13102 assert_eq!(get_label_rect(tester).left, 16.0);
13105 assert_eq!(get_input_rect(tester).left, 52.0);
13108 });
13109 }
13110
13111 #[test]
13115 fn an_outlined_label_aligned_with_the_hint_aligns_with_the_input() {
13116 test_widgets(|tester| {
13117 pump(
13118 tester,
13119 BuildInputDecorator::new(
13120 InputDecoration::new()
13121 .prefix_icon(ac_unit())
13122 .label_text(LABEL_TEXT)
13123 .border(OutlineInputBorder::new())
13124 .align_label_with_hint(true),
13125 )
13126 .is_focused(true),
13127 );
13128 assert_eq!(get_label_rect(tester).left, get_input_rect(tester).left);
13129 });
13130 }
13131
13132 #[test]
13135 fn a_filled_label_aligns_with_the_input_by_default() {
13136 test_widgets(|tester| {
13137 pump(
13138 tester,
13139 BuildInputDecorator::new(
13140 InputDecoration::new()
13141 .prefix_icon(ac_unit())
13142 .label_text(LABEL_TEXT)
13143 .filled(true),
13144 )
13145 .is_focused(true),
13146 );
13147 assert_eq!(get_label_rect(tester).left, get_input_rect(tester).left);
13150 });
13151 }
13152
13153 fn assert_in_exclusive_range(value: f64, low: f64, high: f64) {
13156 assert!(
13157 low < value && value < high,
13158 "{value} not in ({low}, {high})"
13159 );
13160 }
13161
13162 fn check_hint_fade(
13165 tester: &mut WidgetTester,
13166 decoration: InputDecoration,
13167 theme: Option<InputDecorationThemeData>,
13168 step: u64,
13169 settle: Option<u64>,
13170 ) {
13171 let build = |is_focused: bool| {
13172 let mut builder = BuildInputDecorator::new(decoration.clone())
13173 .is_empty(true)
13174 .is_focused(is_focused);
13175 if let Some(theme) = &theme {
13176 builder = builder.input_decoration_theme(theme.clone());
13177 }
13178 builder.build()
13179 };
13180 tester.pump_widget(build(false));
13182 assert_eq!(get_hint_opacity(tester), 0.0);
13184
13185 tester.pump_widget(build(true));
13187 tester.pump_by(ms(step));
13188 let first = get_hint_opacity(tester);
13189 assert_in_exclusive_range(first, 0.0, 1.0);
13190 tester.pump_by(ms(step));
13191 let second = get_hint_opacity(tester);
13192 assert_in_exclusive_range(second, first, 1.0);
13193 tester.pump_by(settle.map_or(K_TRANSITION_DURATION, ms));
13194 assert_eq!(get_hint_opacity(tester), 1.0);
13196
13197 tester.pump_widget(build(false));
13199 tester.pump_by(ms(step));
13200 let first = get_hint_opacity(tester);
13201 assert_in_exclusive_range(first, 0.0, 1.0);
13202 tester.pump_by(ms(step));
13203 let second = get_hint_opacity(tester);
13204 assert_in_exclusive_range(second, 0.0, first);
13205 if let Some(settle) = settle {
13206 tester.pump_by(ms(settle));
13207 assert_eq!(get_hint_opacity(tester), 0.0);
13208 }
13209 }
13210
13211 #[test]
13213 fn the_hint_fades_in_and_out_in_20_ms_by_default() {
13214 test_widgets(|tester| {
13215 check_hint_fade(
13217 tester,
13218 InputDecoration::new()
13219 .label_text(LABEL_TEXT)
13220 .hint_text(HINT_TEXT),
13221 None,
13222 9,
13223 None,
13224 );
13225 });
13226 }
13227
13228 #[test]
13230 fn the_hint_fades_for_the_decoration_hint_fade_duration() {
13231 test_widgets(|tester| {
13232 check_hint_fade(
13234 tester,
13235 InputDecoration::new()
13236 .label_text(LABEL_TEXT)
13237 .hint_text(HINT_TEXT)
13238 .hint_fade_duration(ms(120)),
13239 None,
13240 50,
13241 Some(50),
13242 );
13243 });
13244 }
13245
13246 #[test]
13248 fn the_hint_fades_for_the_theme_hint_fade_duration() {
13249 test_widgets(|tester| {
13250 check_hint_fade(
13252 tester,
13253 InputDecoration::new()
13254 .label_text(LABEL_TEXT)
13255 .hint_text(HINT_TEXT),
13256 Some(InputDecorationThemeData::new().hint_fade_duration(ms(120))),
13257 50,
13258 Some(50),
13259 );
13260 });
13261 }
13262
13263 #[test]
13266 fn the_decorator_shows_a_hint_widget() {
13267 test_widgets(|tester| {
13268 pump(
13269 tester,
13270 BuildInputDecorator::new(InputDecoration::new().hint(Text::new("hint"))),
13271 );
13272 expect(tester, &find::text("hint"), finds_one_widget());
13273 });
13274 }
13275
13276 #[test]
13278 fn the_hint_style_overflow_reaches_the_hint_text() {
13279 test_widgets(|tester| {
13280 let hint_text = "hint text".repeat(20);
13281 let hint_style = TextStyle::new()
13282 .font_size(14.0)
13283 .overflow(TextOverflow::Fade);
13284 let decoration = InputDecoration::new()
13285 .hint_text(hint_text.clone())
13286 .hint_style(hint_style);
13287 pump(tester, BuildInputDecorator::new(decoration.clone()));
13288 tester.pump_by(K_TRANSITION_DURATION);
13289
13290 let hint = tester.element(find::text(&hint_text));
13291 let overflow = tester.widget(hint, |text: &Text| {
13292 text.style.as_ref().and_then(|style| style.overflow)
13293 });
13294 assert_eq!(
13295 overflow,
13296 decoration
13297 .hint_style
13298 .as_ref()
13299 .and_then(|style| style.overflow)
13300 );
13301 });
13302 }
13303
13304 #[test]
13307 fn the_height_collapses_from_the_hint_height_without_maintain_hint_size() {
13308 test_widgets(|tester| {
13309 let decoration = InputDecoration::new()
13310 .hint_text("hint".repeat(20))
13311 .hint_max_lines(3)
13312 .maintain_hint_size(false);
13313 pump(tester, BuildInputDecorator::new(decoration));
13314 assert_eq!(get_decorator_rect(tester).height(), 48.0);
13315 });
13316 }
13317
13318 #[test]
13321 fn the_height_stays_at_the_hint_height_by_default() {
13322 test_widgets(|tester| {
13323 let hint_text = format!("{}\n{}", "hint".repeat(10), "hint".repeat(10));
13327 let decoration = InputDecoration::new()
13328 .hint_max_lines(3)
13329 .hint_text(hint_text.clone());
13330 pump(tester, BuildInputDecorator::new(decoration));
13331 let hint_height = tester.get_rect(find::text(&hint_text)).height();
13332 let input_height = get_decorator_rect(tester).height();
13333 assert_eq!(input_height, hint_height + 16.0);
13334 });
13335 }
13336
13337 fn hint_count(tester: &WidgetTester) -> usize {
13339 find::text(HINT_TEXT).evaluate(tester).len()
13340 }
13341
13342 fn fading_hint_decoration() -> InputDecoration {
13343 InputDecoration::new()
13344 .hint_text(HINT_TEXT)
13345 .hint_max_lines(3)
13346 .hint_fade_duration(ms(120))
13347 .maintain_hint_size(false)
13348 }
13349
13350 #[test]
13353 fn the_hint_fade_duration_applies_to_the_fade_in_without_maintain_hint_size() {
13354 test_widgets(|tester| {
13355 pump(tester, BuildInputDecorator::new(fading_hint_decoration()));
13357
13358 assert_eq!(hint_count(tester), 0);
13360
13361 tester.pump_widget(
13363 BuildInputDecorator::new(fading_hint_decoration())
13364 .is_empty(true)
13365 .build(),
13366 );
13367
13368 tester.pump_by(ms(50));
13370 let hint_opacity_50ms = get_hint_opacity(tester);
13371 assert_in_exclusive_range(hint_opacity_50ms, 0.0, 1.0);
13372 tester.pump_by(ms(50));
13373 let hint_opacity_100ms = get_hint_opacity(tester);
13374 assert_in_exclusive_range(hint_opacity_100ms, hint_opacity_50ms, 1.0);
13375 tester.pump_by(ms(20));
13376 assert_eq!(get_hint_opacity(tester), 1.0);
13377 });
13378 }
13379
13380 #[test]
13383 fn the_hint_fade_duration_applies_to_the_fade_out_without_maintain_hint_size() {
13384 test_widgets(|tester| {
13385 pump(
13387 tester,
13388 BuildInputDecorator::new(fading_hint_decoration()).is_empty(true),
13389 );
13390
13391 assert_eq!(get_hint_opacity(tester), 1.0);
13393
13394 tester.pump_widget(BuildInputDecorator::new(fading_hint_decoration()).build());
13396
13397 tester.pump_by(ms(50));
13399 let hint_opacity_50ms = get_hint_opacity(tester);
13400 assert_in_exclusive_range(hint_opacity_50ms, 0.0, 1.0);
13401 tester.pump_by(ms(50));
13402 let hint_opacity_100ms = get_hint_opacity(tester);
13403 assert_in_exclusive_range(hint_opacity_100ms, 0.0, hint_opacity_50ms);
13404 tester.pump_by(ms(20));
13405 assert_eq!(get_hint_opacity(tester), 0.0);
13406 tester.pump_by(ms(1));
13407 assert_eq!(hint_count(tester), 0);
13409 });
13410 }
13411
13412 fn hint_edge(
13413 tester: &mut WidgetTester,
13414 maintain_hint_size: bool,
13415 text_direction: TextDirection,
13416 ) -> Rect {
13417 pump(
13418 tester,
13419 BuildInputDecorator::new(
13420 InputDecoration::new()
13421 .hint_text(HINT_TEXT)
13422 .maintain_hint_size(maintain_hint_size),
13423 )
13424 .is_empty(true)
13425 .text_direction(text_direction),
13426 );
13427 get_hint_rect(tester)
13428 }
13429
13430 #[test]
13433 fn the_hint_keeps_its_position_without_maintain_hint_size_in_ltr() {
13434 test_widgets(|tester| {
13435 let expected_hint_left = hint_edge(tester, true, TextDirection::Ltr).left;
13436 assert_eq!(
13437 hint_edge(tester, false, TextDirection::Ltr).left,
13438 expected_hint_left
13439 );
13440 });
13441 }
13442
13443 #[test]
13446 fn the_hint_keeps_its_position_without_maintain_hint_size_in_rtl() {
13447 test_widgets(|tester| {
13448 let expected_hint_right = hint_edge(tester, true, TextDirection::Rtl).right;
13449 assert_eq!(
13450 hint_edge(tester, false, TextDirection::Rtl).right,
13451 expected_hint_right
13452 );
13453 });
13454 }
13455 }
13456
13457 mod supporting_and_affixes {
13460 #![allow(deprecated)]
13461 use waft_widgets::testing::{
13462 TargetPlatformVariant, WidgetTester, test_widgets, test_widgets_variant,
13463 };
13464
13465 use std::cell::Cell;
13466 use std::rc::Rc;
13467 use std::time::Duration;
13468
13469 use waft_embedder::{Color, Rect, Size};
13470 use waft_foundation::Listener;
13471 use waft_painting::{EdgeInsetsGeometry, TextDirection, TextStyle};
13472 use waft_rendering::BoxConstraints;
13473 use waft_widgets::{
13474 AnimatedOpacity, Center, FadeTransition, Focus, GestureDetector, Icon, IconData,
13475 IntoWidget, RichText, SizedBox, Text, WidgetRef, downcast_widget,
13476 };
13477
13478 use super::super::*;
13479 use super::common::K_TRANSITION_DURATION;
13480 use super::common::*;
13481 use crate::icons::Icons;
13482 use crate::input_border::OutlineInputBorder;
13483 use crate::theme::Theme;
13484 use crate::theme_data::ThemeData;
13485
13486 const TOP_PADDING: f64 = 8.0;
13495 const FLOATING_LABEL_HEIGHT: f64 = 12.0;
13496 const LABEL_INPUT_GAP: f64 = 4.0;
13497 const INPUT_HEIGHT: f64 = 24.0;
13498 const BOTTOM_PADDING: f64 = 8.0;
13499 const HELPER_GAP: f64 = 4.0;
13500 const HELPER_HEIGHT: f64 = 16.0;
13501 const CONTAINER_HEIGHT: f64 =
13502 TOP_PADDING + FLOATING_LABEL_HEIGHT + LABEL_INPUT_GAP + INPUT_HEIGHT + BOTTOM_PADDING; const FULL_HEIGHT: f64 = CONTAINER_HEIGHT + HELPER_GAP + HELPER_HEIGHT; const ERROR_HEIGHT: f64 = HELPER_HEIGHT;
13505 const HINT_HEIGHT: f64 = INPUT_HEIGHT;
13506 const HELPER_START_PADDING: f64 = 16.0;
13507 const COUNTER_END_PADDING: f64 = 16.0;
13508
13509 fn pump(tester: &mut WidgetTester, decorator: BuildInputDecorator) {
13511 tester.pump_widget(decorator.build());
13512 }
13513
13514 fn decorator_theme(tester: &WidgetTester) -> Rc<ThemeData> {
13516 let element = tester.element(find::by_type::<InputDecorator>());
13517 let mut app = tester.cell.borrow_mut();
13518 Theme::of(&mut app, element)
13519 }
13520
13521 fn text_style(tester: &WidgetTester, text: &str) -> TextStyle {
13524 let rich_text = tester.element(find::descendant(
13525 find::text(text),
13526 find::by_type::<RichText>(),
13527 ));
13528 tester.widget(rich_text, |rich_text: &RichText| {
13529 rich_text.text.style().cloned().expect("a styled text")
13530 })
13531 }
13532
13533 fn string_rect(tester: &WidgetTester, text: &str) -> Rect {
13535 tester.get_rect(find::text(text))
13536 }
13537
13538 fn body_small_with(theme: &ThemeData, color: Color) -> TextStyle {
13539 theme
13540 .text_theme
13541 .body_small
13542 .clone()
13543 .expect("bodySmall")
13544 .copy_with()
13545 .color(color)
13546 }
13547
13548 fn filled() -> InputDecoration {
13549 InputDecoration::new().filled(true)
13550 }
13551
13552 fn outlined() -> InputDecoration {
13553 InputDecoration::new().border(OutlineInputBorder::new())
13554 }
13555
13556 fn measured_supporting(base: InputDecoration) -> InputDecoration {
13559 base.label_text(LABEL_TEXT)
13560 .helper_text(HELPER_TEXT)
13561 .counter_text(COUNTER_TEXT)
13562 }
13563
13564 fn styled_supporting(base: InputDecoration) -> InputDecoration {
13566 base.label_text(LABEL_TEXT)
13567 .helper_text(HELPER_TEXT)
13568 .counter_text(COUNTER_TEXT)
13569 }
13570
13571 fn check_helper_and_counter_positions(tester: &WidgetTester) {
13572 assert_eq!(get_decorator_rect(tester).height(), FULL_HEIGHT);
13573 assert_eq!(get_border_bottom(tester), CONTAINER_HEIGHT);
13574 let helper = tester.get_rect(find::text(HELPER_TEXT));
13575 assert_eq!(helper.top, CONTAINER_HEIGHT + HELPER_GAP);
13576 assert_eq!(helper.height(), HELPER_HEIGHT);
13577 assert_eq!(helper.left, HELPER_START_PADDING);
13578 let counter = tester.get_rect(find::text(COUNTER_TEXT));
13579 assert_eq!(counter.top, CONTAINER_HEIGHT + HELPER_GAP);
13580 assert_eq!(counter.height(), HELPER_HEIGHT);
13581 assert_f32_equals(counter.right, 800.0 - COUNTER_END_PADDING);
13583 }
13584
13585 fn check_error_and_counter_positions(tester: &WidgetTester) {
13586 assert_eq!(get_decorator_rect(tester).height(), FULL_HEIGHT);
13587 assert_eq!(get_border_bottom(tester), CONTAINER_HEIGHT);
13588 let error = tester.get_rect(find::text(ERROR_TEXT));
13589 assert_eq!(error.top, CONTAINER_HEIGHT + HELPER_GAP);
13590 assert_eq!(error.height(), ERROR_HEIGHT);
13591 assert_eq!(error.left, HELPER_START_PADDING);
13592 let counter = tester.get_rect(find::text(COUNTER_TEXT));
13593 assert_eq!(counter.top, CONTAINER_HEIGHT + HELPER_GAP);
13594 assert_eq!(counter.height(), ERROR_HEIGHT);
13595 assert_f32_equals(counter.right, 800.0 - COUNTER_END_PADDING);
13597 }
13598
13599 fn check_helper_and_counter_style(
13600 tester: &WidgetTester,
13601 color: impl Fn(&ThemeData) -> Color,
13602 ) {
13603 let theme = decorator_theme(tester);
13604 let expected_style = body_small_with(&theme, color(&theme));
13605 assert_eq!(text_style(tester, HELPER_TEXT), expected_style);
13606 assert_eq!(text_style(tester, COUNTER_TEXT), expected_style);
13607 }
13608
13609 fn check_error_and_counter_style(tester: &WidgetTester) {
13610 let theme = decorator_theme(tester);
13611 assert_eq!(
13612 text_style(tester, ERROR_TEXT),
13613 body_small_with(&theme, theme.color_scheme.error())
13614 );
13615 assert_eq!(
13616 text_style(tester, COUNTER_TEXT),
13617 body_small_with(&theme, theme.color_scheme.on_surface_variant())
13618 );
13619 }
13620
13621 fn on_surface_variant(theme: &ThemeData) -> Color {
13622 theme.color_scheme.on_surface_variant()
13623 }
13624
13625 fn disabled_on_surface(theme: &ThemeData) -> Color {
13626 theme.color_scheme.on_surface().with_opacity(0.38)
13627 }
13628
13629 #[test]
13632 fn filled_enabled_helper_and_counter_are_correctly_positioned() {
13633 test_widgets(|tester| {
13634 pump(
13635 tester,
13636 BuildInputDecorator::new(measured_supporting(filled())),
13637 );
13638 check_helper_and_counter_positions(tester);
13639 });
13640 }
13641
13642 #[test]
13645 fn filled_enabled_helper_and_counter_are_correctly_styled() {
13646 test_widgets(|tester| {
13647 pump(
13648 tester,
13649 BuildInputDecorator::new(styled_supporting(filled())),
13650 );
13651 check_helper_and_counter_style(tester, on_surface_variant);
13652 });
13653 }
13654
13655 #[test]
13658 fn filled_disabled_helper_and_counter_are_correctly_positioned() {
13659 test_widgets(|tester| {
13660 pump(
13661 tester,
13662 BuildInputDecorator::new(measured_supporting(filled().enabled(false))),
13663 );
13664 check_helper_and_counter_positions(tester);
13665 });
13666 }
13667
13668 #[test]
13671 fn filled_disabled_helper_and_counter_are_correctly_styled() {
13672 test_widgets(|tester| {
13673 pump(
13674 tester,
13675 BuildInputDecorator::new(styled_supporting(filled().enabled(false))),
13676 );
13677 check_helper_and_counter_style(tester, disabled_on_surface);
13678 });
13679 }
13680
13681 #[test]
13684 fn filled_hovered_helper_and_counter_are_correctly_positioned() {
13685 test_widgets(|tester| {
13686 pump(
13687 tester,
13688 BuildInputDecorator::new(measured_supporting(filled())).is_hovering(true),
13689 );
13690 check_helper_and_counter_positions(tester);
13691 });
13692 }
13693
13694 #[test]
13697 fn filled_hovered_helper_and_counter_are_correctly_styled() {
13698 test_widgets(|tester| {
13699 pump(
13700 tester,
13701 BuildInputDecorator::new(styled_supporting(filled())).is_hovering(true),
13702 );
13703 check_helper_and_counter_style(tester, on_surface_variant);
13704 });
13705 }
13706
13707 #[test]
13710 fn filled_focused_helper_and_counter_are_correctly_positioned() {
13711 test_widgets(|tester| {
13712 pump(
13713 tester,
13714 BuildInputDecorator::new(measured_supporting(filled())).is_focused(true),
13715 );
13716 check_helper_and_counter_positions(tester);
13717 });
13718 }
13719
13720 #[test]
13723 fn filled_focused_helper_and_counter_are_correctly_styled() {
13724 test_widgets(|tester| {
13725 pump(
13726 tester,
13727 BuildInputDecorator::new(styled_supporting(filled())).is_focused(true),
13728 );
13729 check_helper_and_counter_style(tester, on_surface_variant);
13730 });
13731 }
13732
13733 fn check_error_shows_and_helper_hides(tester: &mut WidgetTester, base: InputDecoration) {
13734 pump(
13735 tester,
13736 BuildInputDecorator::new(styled_supporting(base).error_text(ERROR_TEXT))
13737 .is_focused(true),
13738 );
13739 expect(tester, &find::text(ERROR_TEXT), finds_one_widget());
13740 expect(tester, &find::text(COUNTER_TEXT), finds_one_widget());
13741 expect(tester, &find::text(HELPER_TEXT), finds_nothing());
13742 }
13743
13744 fn check_error_positions(tester: &mut WidgetTester, base: InputDecoration) {
13745 pump(
13746 tester,
13747 BuildInputDecorator::new(measured_supporting(base).error_text(ERROR_TEXT))
13748 .is_focused(true),
13749 );
13750 check_error_and_counter_positions(tester);
13751 }
13752
13753 fn check_error_style(tester: &mut WidgetTester, base: InputDecoration) {
13754 pump(
13755 tester,
13756 BuildInputDecorator::new(styled_supporting(base).error_text(ERROR_TEXT))
13757 .is_focused(true),
13758 );
13759 check_error_and_counter_style(tester);
13760 }
13761
13762 #[test]
13765 fn filled_error_and_counter_are_visible_and_the_helper_is_not() {
13766 test_widgets(|tester| check_error_shows_and_helper_hides(tester, filled()));
13767 }
13768
13769 #[test]
13772 fn filled_error_and_counter_are_correctly_positioned() {
13773 test_widgets(|tester| check_error_positions(tester, filled()));
13774 }
13775
13776 #[test]
13779 fn filled_error_and_counter_are_correctly_styled() {
13780 test_widgets(|tester| check_error_style(tester, filled()));
13781 }
13782
13783 #[test]
13786 fn outlined_enabled_helper_and_counter_are_correctly_positioned() {
13787 test_widgets(|tester| {
13788 pump(
13789 tester,
13790 BuildInputDecorator::new(measured_supporting(outlined())),
13791 );
13792 check_helper_and_counter_positions(tester);
13793 });
13794 }
13795
13796 #[test]
13799 fn outlined_enabled_helper_and_counter_are_correctly_styled() {
13800 test_widgets(|tester| {
13801 pump(
13802 tester,
13803 BuildInputDecorator::new(styled_supporting(outlined())),
13804 );
13805 check_helper_and_counter_style(tester, on_surface_variant);
13806 });
13807 }
13808
13809 #[test]
13812 fn outlined_disabled_helper_and_counter_are_correctly_positioned() {
13813 test_widgets(|tester| {
13814 pump(
13815 tester,
13816 BuildInputDecorator::new(measured_supporting(outlined().enabled(false))),
13817 );
13818 check_helper_and_counter_positions(tester);
13819 });
13820 }
13821
13822 #[test]
13825 fn outlined_disabled_helper_and_counter_are_correctly_styled() {
13826 test_widgets(|tester| {
13827 pump(
13828 tester,
13829 BuildInputDecorator::new(styled_supporting(outlined().enabled(false))),
13830 );
13831 check_helper_and_counter_style(tester, disabled_on_surface);
13832 });
13833 }
13834
13835 #[test]
13838 fn outlined_hovered_helper_and_counter_are_correctly_positioned() {
13839 test_widgets(|tester| {
13840 pump(
13841 tester,
13842 BuildInputDecorator::new(measured_supporting(outlined())).is_hovering(true),
13843 );
13844 check_helper_and_counter_positions(tester);
13845 });
13846 }
13847
13848 #[test]
13851 fn outlined_hovered_helper_and_counter_are_correctly_styled() {
13852 test_widgets(|tester| {
13853 pump(
13854 tester,
13855 BuildInputDecorator::new(styled_supporting(outlined())).is_hovering(true),
13856 );
13857 check_helper_and_counter_style(tester, on_surface_variant);
13858 });
13859 }
13860
13861 #[test]
13864 fn outlined_focused_helper_and_counter_are_correctly_positioned() {
13865 test_widgets(|tester| {
13866 pump(
13867 tester,
13868 BuildInputDecorator::new(measured_supporting(outlined())).is_focused(true),
13869 );
13870 check_helper_and_counter_positions(tester);
13871 });
13872 }
13873
13874 #[test]
13877 fn outlined_focused_helper_and_counter_are_correctly_styled() {
13878 test_widgets(|tester| {
13879 pump(
13880 tester,
13881 BuildInputDecorator::new(styled_supporting(outlined())).is_focused(true),
13882 );
13883 check_helper_and_counter_style(tester, on_surface_variant);
13884 });
13885 }
13886
13887 #[test]
13890 fn outlined_error_and_counter_are_visible_and_the_helper_is_not() {
13891 test_widgets(|tester| check_error_shows_and_helper_hides(tester, outlined()));
13892 }
13893
13894 #[test]
13897 fn outlined_error_and_counter_are_correctly_positioned() {
13898 test_widgets(|tester| check_error_positions(tester, outlined()));
13899 }
13900
13901 #[test]
13904 fn outlined_error_and_counter_are_correctly_styled() {
13905 test_widgets(|tester| check_error_style(tester, outlined()));
13906 }
13907
13908 fn check_supporting_lines(
13910 tester: &mut WidgetTester,
13911 decoration: InputDecoration,
13912 text: &str,
13913 lines: f64,
13914 ) {
13915 pump(tester, BuildInputDecorator::new(decoration));
13916 assert_more_or_less_equals_within(
13917 string_rect(tester, text).height(),
13918 ERROR_HEIGHT * lines,
13919 0.25,
13920 );
13921 assert_more_or_less_equals_within(
13922 get_decorator_rect(tester).height(),
13923 CONTAINER_HEIGHT + HELPER_GAP + ERROR_HEIGHT * lines,
13924 0.25,
13925 );
13926 }
13927
13928 #[test]
13931 fn the_error_grows_to_accommodate_its_text() {
13932 test_widgets(|tester| {
13933 check_supporting_lines(
13934 tester,
13935 filled()
13936 .label_text("label")
13937 .error_text(THREE_LINES)
13938 .error_max_lines(3),
13939 THREE_LINES,
13940 3.0,
13941 );
13942 });
13943 }
13944
13945 #[test]
13948 fn the_error_height_is_correct_when_error_max_lines_is_restricted() {
13949 test_widgets(|tester| {
13950 check_supporting_lines(
13951 tester,
13952 filled()
13953 .label_text("label")
13954 .error_text(THREE_LINES)
13955 .error_max_lines(2),
13956 THREE_LINES,
13957 2.0,
13958 );
13959 });
13960 }
13961
13962 #[test]
13965 fn the_error_height_is_its_line_count_when_error_max_lines_is_bigger() {
13966 test_widgets(|tester| {
13967 check_supporting_lines(
13968 tester,
13969 filled()
13970 .label_text("label")
13971 .error_text(TWO_LINES)
13972 .error_max_lines(3),
13973 TWO_LINES,
13974 2.0,
13975 );
13976 });
13977 }
13978
13979 #[test]
13982 fn the_error_height_is_not_limited_by_default() {
13983 test_widgets(|tester| {
13984 check_supporting_lines(
13985 tester,
13986 filled().label_text("label").error_text(THREE_LINES),
13987 THREE_LINES,
13988 3.0,
13989 );
13990 });
13991 }
13992
13993 #[test]
13996 fn the_theme_hint_max_lines_is_the_default() {
13997 test_widgets(|tester| {
13998 pump(
13999 tester,
14000 BuildInputDecorator::new(InputDecoration::new().hint_text(THREE_LINES))
14001 .input_decoration_theme(InputDecorationThemeData::new().hint_max_lines(2)),
14002 );
14003 assert_more_or_less_equals_within(
14004 string_rect(tester, THREE_LINES).height(),
14005 HINT_HEIGHT * 2.0,
14006 0.25,
14007 );
14008 assert_more_or_less_equals_within(
14009 get_decorator_rect(tester).height(),
14010 TOP_PADDING + HINT_HEIGHT * 2.0 + BOTTOM_PADDING,
14011 0.25,
14012 );
14013 });
14014 }
14015
14016 #[test]
14019 fn the_hint_expands_with_its_text_by_default() {
14020 test_widgets(|tester| {
14021 pump(
14022 tester,
14023 BuildInputDecorator::new(InputDecoration::new().hint_text(THREE_LINES))
14024 .input_decoration_theme(InputDecorationThemeData::new()),
14025 );
14026 assert_eq!(
14027 get_decorator_rect(tester).height(),
14028 TOP_PADDING + HINT_HEIGHT * 3.0 + BOTTOM_PADDING
14029 );
14030 });
14031 }
14032
14033 #[test]
14036 fn the_helper_grows_to_accommodate_its_text() {
14037 test_widgets(|tester| {
14038 check_supporting_lines(
14039 tester,
14040 filled()
14041 .label_text("label")
14042 .helper_text(THREE_LINES)
14043 .helper_max_lines(3),
14044 THREE_LINES,
14045 3.0,
14046 );
14047 });
14048 }
14049
14050 #[test]
14053 fn the_helper_height_is_correct_when_helper_max_lines_is_restricted() {
14054 test_widgets(|tester| {
14055 check_supporting_lines(
14056 tester,
14057 filled()
14058 .label_text("label")
14059 .helper_text(THREE_LINES)
14060 .helper_max_lines(2),
14061 THREE_LINES,
14062 2.0,
14063 );
14064 });
14065 }
14066
14067 #[test]
14070 fn the_helper_height_is_its_line_count_when_helper_max_lines_is_bigger() {
14071 test_widgets(|tester| {
14072 check_supporting_lines(
14073 tester,
14074 filled()
14075 .label_text("label")
14076 .helper_text(TWO_LINES)
14077 .helper_max_lines(3),
14078 TWO_LINES,
14079 2.0,
14080 );
14081 });
14082 }
14083
14084 #[test]
14087 fn the_helper_height_is_not_limited_by_default() {
14088 test_widgets(|tester| {
14089 check_supporting_lines(
14090 tester,
14091 filled().label_text("label").helper_text(THREE_LINES),
14092 THREE_LINES,
14093 3.0,
14094 );
14095 });
14096 }
14097
14098 #[test]
14100 fn the_decorator_shows_a_helper_widget() {
14101 test_widgets(|tester| {
14102 pump(
14103 tester,
14104 BuildInputDecorator::new(
14105 InputDecoration::new()
14106 .helper(Text::new("helper").style(TextStyle::new().font_size(20.0))),
14107 ),
14108 );
14109 expect(tester, &find::text("helper"), finds_one_widget());
14110 });
14111 }
14112
14113 #[test]
14116 #[should_panic(expected = "Declaring both helper and helperText is not supported.")]
14117 fn a_helper_text_and_a_helper_widget_together_panic() {
14118 test_widgets(|tester| {
14119 pump(
14120 tester,
14121 BuildInputDecorator::new(
14122 InputDecoration::new()
14123 .helper_text("helperText")
14124 .helper(Text::new("helper").style(TextStyle::new().font_size(20.0))),
14125 ),
14126 );
14127 });
14128 }
14129
14130 #[test]
14132 fn the_decorator_shows_an_error_widget() {
14133 test_widgets(|tester| {
14134 pump(
14135 tester,
14136 BuildInputDecorator::new(
14137 InputDecoration::new()
14138 .error(Text::new("error").style(TextStyle::new().font_size(20.0))),
14139 ),
14140 );
14141 expect(tester, &find::text("error"), finds_one_widget());
14142 });
14143 }
14144
14145 #[test]
14148 #[should_panic(expected = "Declaring both error and errorText is not supported.")]
14149 fn an_error_text_and_an_error_widget_together_panic() {
14150 test_widgets(|tester| {
14151 pump(
14152 tester,
14153 BuildInputDecorator::new(
14154 InputDecoration::new()
14155 .error_text("errorText")
14156 .error(Text::new("error").style(TextStyle::new().font_size(20.0))),
14157 ),
14158 );
14159 });
14160 }
14161
14162 #[test]
14165 fn an_error_widget_text_style_defaults_to_the_error_style() {
14166 test_widgets(|tester| {
14167 pump(
14168 tester,
14169 BuildInputDecorator::new(InputDecoration::new().error(Text::new(ERROR_TEXT))),
14170 );
14171 expect(tester, &find::text(ERROR_TEXT), finds_one_widget());
14172 let theme = decorator_theme(tester);
14173 assert_eq!(
14174 text_style(tester, ERROR_TEXT),
14175 body_small_with(&theme, theme.color_scheme.error())
14176 );
14177 });
14178 }
14179
14180 #[test]
14183 fn a_decorator_with_a_counter_does_not_crash_at_zero_size() {
14184 test_widgets(|tester| {
14185 let decorator = BuildInputDecorator::new(
14186 InputDecoration::new()
14187 .content_padding(EdgeInsetsGeometry::directional(99.0, 99.0, 99.0, 99.0))
14188 .prefix_icon(Focus::new(Icon::new(Some(Icons::search()))))
14189 .counter(Text::new("COUNTER")),
14190 )
14191 .build();
14192 tester.pump_widget(
14193 Center::new()
14194 .child(SizedBox::new().width(0.0).height(0.0).child(decorator))
14195 .into_widget(),
14196 );
14197 tester.pump_by(K_TRANSITION_DURATION);
14198
14199 expect(
14200 tester,
14201 &find::by_type::<InputDecorator>(),
14202 finds_one_widget(),
14203 );
14204 assert_eq!(tester.get_size(find::text("COUNTER")), Size::ZERO);
14205 });
14206 }
14207
14208 #[test]
14211 fn without_constraints_the_decorator_fills_the_width_at_the_default_height() {
14212 test_widgets(|tester| {
14213 pump(tester, BuildInputDecorator::new(InputDecoration::new()));
14214 assert_eq!(
14216 get_decorator_rect(tester).size(),
14217 Size::new(800.0, crate::constants::K_MIN_INTERACTIVE_DIMENSION)
14218 );
14219 });
14220 }
14221
14222 fn theme_with_constraints(app: &App) -> ThemeData {
14223 ThemeData::builder()
14224 .input_decoration_theme(InputDecorationThemeData::new().constraints(
14225 BoxConstraints {
14226 max_width: 300.0,
14227 max_height: 40.0,
14228 ..BoxConstraints::new()
14229 },
14230 ))
14231 .build(app)
14232 }
14233
14234 #[test]
14237 fn the_theme_constraints_apply() {
14238 test_widgets(|tester| {
14239 pump(
14240 tester,
14241 BuildInputDecorator::new(InputDecoration::new()).theme(theme_with_constraints),
14242 );
14243 assert_eq!(get_decorator_rect(tester).size(), Size::new(300.0, 40.0));
14245 });
14246 }
14247
14248 #[test]
14251 fn the_decoration_constraints_override_the_theme() {
14252 test_widgets(|tester| {
14253 pump(
14254 tester,
14255 BuildInputDecorator::new(InputDecoration::new().constraints(BoxConstraints {
14256 max_width: 200.0,
14257 max_height: 32.0,
14258 ..BoxConstraints::new()
14259 }))
14260 .theme(theme_with_constraints),
14261 );
14262 assert_eq!(get_decorator_rect(tester).size(), Size::new(200.0, 32.0));
14265 });
14266 }
14267
14268 fn prefix_icon() -> IconData {
14269 Icons::search()
14270 }
14271
14272 fn suffix_icon() -> IconData {
14273 Icons::cancel_outlined()
14274 }
14275
14276 fn icon_rect(tester: &WidgetTester, icon: &IconData) -> Rect {
14278 tester.get_rect(find::by_icon(icon))
14279 }
14280
14281 fn icon_inner_rect(tester: &WidgetTester, icon: &IconData) -> Rect {
14283 tester.get_rect(find::descendant(
14284 find::by_icon(icon),
14285 find::by_type::<RichText>(),
14286 ))
14287 }
14288
14289 fn icon_style(tester: &WidgetTester, icon: &IconData) -> TextStyle {
14291 tester.glyph_style(tester.element(find::by_icon(icon)))
14292 }
14293
14294 fn input_rect(tester: &WidgetTester) -> Rect {
14295 tester.get_rect(find::text(INPUT_TEXT))
14296 }
14297
14298 fn check_prefix_icon_position(tester: &WidgetTester, text_direction: TextDirection) {
14300 let icon = prefix_icon();
14301 assert_eq!(icon_rect(tester, &icon).size(), Size::new(48.0, 48.0));
14303 assert_eq!(icon_style(tester, &icon).font_size, Some(24.0));
14305 assert_eq!(
14307 icon_rect(tester, &icon).center().dy(),
14308 get_container_rect(tester).center().dy()
14309 );
14310 let inner = icon_inner_rect(tester, &icon);
14311 match text_direction {
14312 TextDirection::Ltr => {
14313 assert_eq!(inner.left, 12.0);
14315 assert_eq!(input_rect(tester).left - inner.right, 16.0);
14317 }
14318 TextDirection::Rtl => {
14319 assert_eq!(get_decorator_rect(tester).right - inner.right, 12.0);
14321 assert_eq!(inner.left - input_rect(tester).right, 16.0);
14322 }
14323 }
14324 }
14325
14326 fn check_suffix_icon_position(tester: &WidgetTester, text_direction: TextDirection) {
14328 let icon = suffix_icon();
14329 assert_eq!(icon_rect(tester, &icon).size(), Size::new(48.0, 48.0));
14331 assert_eq!(icon_style(tester, &icon).font_size, Some(24.0));
14333 assert_eq!(
14335 icon_rect(tester, &icon).center().dy(),
14336 get_container_rect(tester).center().dy()
14337 );
14338 let inner = icon_inner_rect(tester, &icon);
14339 match text_direction {
14340 TextDirection::Ltr => {
14341 assert_eq!(get_decorator_rect(tester).right - inner.right, 12.0);
14343 assert_eq!(inner.left - input_rect(tester).right, 16.0);
14345 }
14346 TextDirection::Rtl => {
14347 assert_eq!(inner.left, 12.0);
14349 assert_eq!(input_rect(tester).left - inner.right, 16.0);
14350 }
14351 }
14352 }
14353
14354 fn prefixed(base: InputDecoration) -> InputDecoration {
14355 base.label_text(LABEL_TEXT)
14356 .prefix_icon(Icon::new(Some(prefix_icon())))
14357 }
14358
14359 fn suffixed(base: InputDecoration) -> InputDecoration {
14360 base.label_text(LABEL_TEXT)
14361 .suffix_icon(Icon::new(Some(suffix_icon())))
14362 }
14363
14364 fn check_icon_color(
14365 tester: &mut WidgetTester,
14366 decorator: BuildInputDecorator,
14367 icon: IconData,
14368 color: impl Fn(&ThemeData) -> Color,
14369 ) {
14370 pump(tester, decorator);
14371 let theme = decorator_theme(tester);
14372 assert_eq!(
14373 icon_style(tester, &icon).color.map(|color| *color),
14374 Some(color(&theme))
14375 );
14376 }
14377
14378 fn error(theme: &ThemeData) -> Color {
14379 theme.color_scheme.error()
14380 }
14381
14382 fn on_error_container(theme: &ThemeData) -> Color {
14383 theme.color_scheme.on_error_container()
14384 }
14385
14386 macro_rules! affix_icon_cases {
14388 ($(
14389 $(#[$doc:meta])*
14390 $position:ident: $decorate:ident, $base:expr, |$builder:ident| $configure:expr,
14391 $check:ident, $direction:expr;
14392 )*) => {
14393 $(
14394 $(#[$doc])*
14395 #[test]
14396 fn $position() {
14397 test_widgets(|tester| {
14398 let $builder = BuildInputDecorator::new($decorate($base));
14399 pump(tester, $configure);
14400 $check(tester, $direction);
14401 });
14402 }
14403 )*
14404 };
14405 }
14406
14407 affix_icon_cases! {
14408 filled_enabled_prefix_icon_is_correctly_positioned_ltr: prefixed, filled(),
14411 |b| b, check_prefix_icon_position, TextDirection::Ltr;
14412 filled_enabled_prefix_icon_is_correctly_positioned_rtl: prefixed, filled(),
14414 |b| b.text_direction(TextDirection::Rtl), check_prefix_icon_position,
14415 TextDirection::Rtl;
14416 filled_enabled_suffix_icon_is_correctly_positioned_ltr: suffixed, filled(),
14418 |b| b, check_suffix_icon_position, TextDirection::Ltr;
14419 filled_enabled_suffix_icon_is_correctly_positioned_rtl: suffixed, filled(),
14421 |b| b.text_direction(TextDirection::Rtl), check_suffix_icon_position,
14422 TextDirection::Rtl;
14423 filled_disabled_prefix_icon_is_correctly_positioned: prefixed, filled().enabled(false),
14426 |b| b, check_prefix_icon_position, TextDirection::Ltr;
14427 filled_disabled_suffix_icon_is_correctly_positioned: suffixed, filled().enabled(false),
14430 |b| b, check_suffix_icon_position, TextDirection::Ltr;
14431 filled_hovered_prefix_icon_is_correctly_positioned: prefixed, filled(),
14434 |b| b.is_hovering(true), check_prefix_icon_position, TextDirection::Ltr;
14435 filled_hovered_suffix_icon_is_correctly_positioned: suffixed, filled(),
14438 |b| b.is_hovering(true), check_suffix_icon_position, TextDirection::Ltr;
14439 filled_focused_prefix_icon_is_correctly_positioned: prefixed, filled(),
14442 |b| b.is_focused(true), check_prefix_icon_position, TextDirection::Ltr;
14443 filled_focused_suffix_icon_is_correctly_positioned: suffixed, filled(),
14446 |b| b.is_focused(true), check_suffix_icon_position, TextDirection::Ltr;
14447 filled_error_prefix_icon_is_correctly_positioned: prefixed, filled().error_text(ERROR_TEXT),
14450 |b| b, check_prefix_icon_position, TextDirection::Ltr;
14451 filled_error_suffix_icon_is_correctly_positioned: suffixed, filled().error_text(ERROR_TEXT),
14454 |b| b, check_suffix_icon_position, TextDirection::Ltr;
14455 outlined_enabled_prefix_icon_is_correctly_positioned: prefixed, outlined(),
14458 |b| b, check_prefix_icon_position, TextDirection::Ltr;
14459 outlined_enabled_suffix_icon_is_correctly_positioned: suffixed, outlined(),
14462 |b| b, check_suffix_icon_position, TextDirection::Ltr;
14463 outlined_disabled_prefix_icon_is_correctly_positioned: prefixed, outlined().enabled(false),
14466 |b| b, check_prefix_icon_position, TextDirection::Ltr;
14467 outlined_disabled_suffix_icon_is_correctly_positioned: suffixed, outlined().enabled(false),
14470 |b| b, check_suffix_icon_position, TextDirection::Ltr;
14471 outlined_hovered_prefix_icon_is_correctly_positioned: prefixed, outlined(),
14474 |b| b.is_hovering(true), check_prefix_icon_position, TextDirection::Ltr;
14475 outlined_hovered_suffix_icon_is_correctly_positioned: suffixed, outlined(),
14478 |b| b.is_hovering(true), check_suffix_icon_position, TextDirection::Ltr;
14479 outlined_focused_prefix_icon_is_correctly_positioned: prefixed, outlined(),
14482 |b| b.is_focused(true), check_prefix_icon_position, TextDirection::Ltr;
14483 outlined_focused_suffix_icon_is_correctly_positioned: suffixed, outlined(),
14486 |b| b.is_focused(true), check_suffix_icon_position, TextDirection::Ltr;
14487 outlined_error_prefix_icon_is_correctly_positioned: prefixed,
14490 outlined().error_text(ERROR_TEXT), |b| b, check_prefix_icon_position,
14491 TextDirection::Ltr;
14492 outlined_error_suffix_icon_is_correctly_positioned: suffixed,
14495 outlined().error_text(ERROR_TEXT), |b| b, check_suffix_icon_position,
14496 TextDirection::Ltr;
14497 }
14498
14499 macro_rules! affix_icon_color_cases {
14501 ($(
14502 $name:ident: $decorate:ident, $base:expr, |$builder:ident| $configure:expr,
14503 $icon:expr, $color:expr;
14504 )*) => {
14505 $(
14506 #[test]
14507 fn $name() {
14508 test_widgets(|tester| {
14509 let $builder = BuildInputDecorator::new($decorate($base));
14510 check_icon_color(tester, $configure, $icon, $color);
14511 });
14512 }
14513 )*
14514 };
14515 }
14516
14517 affix_icon_color_cases! {
14521 filled_enabled_prefix_icon_has_correct_color: prefixed, filled(), |b| b,
14522 prefix_icon(), on_surface_variant;
14523 filled_enabled_suffix_icon_has_correct_color: suffixed, filled(), |b| b,
14524 suffix_icon(), on_surface_variant;
14525 filled_disabled_prefix_icon_has_correct_color: prefixed, filled().enabled(false), |b| b,
14526 prefix_icon(), disabled_on_surface;
14527 filled_disabled_suffix_icon_has_correct_color: suffixed, filled().enabled(false), |b| b,
14528 suffix_icon(), disabled_on_surface;
14529 filled_hovered_prefix_icon_has_correct_color: prefixed, filled(), |b| b.is_hovering(true),
14530 prefix_icon(), on_surface_variant;
14531 filled_hovered_suffix_icon_has_correct_color: suffixed, filled(), |b| b.is_hovering(true),
14532 suffix_icon(), on_surface_variant;
14533 filled_focused_prefix_icon_has_correct_color: prefixed, filled(), |b| b.is_focused(true),
14534 prefix_icon(), on_surface_variant;
14535 filled_focused_suffix_icon_has_correct_color: suffixed, filled(), |b| b.is_focused(true),
14536 suffix_icon(), on_surface_variant;
14537 filled_error_prefix_icon_has_correct_color: prefixed, filled().error_text(ERROR_TEXT),
14538 |b| b, prefix_icon(), on_surface_variant;
14539 filled_error_prefix_icon_has_correct_color_when_hovered: prefixed,
14540 filled().error_text(ERROR_TEXT), |b| b.is_hovering(true), prefix_icon(),
14541 on_surface_variant;
14542 filled_error_suffix_icon_has_correct_color: suffixed, filled().error_text(ERROR_TEXT),
14543 |b| b, suffix_icon(), error;
14544 filled_error_suffix_icon_has_correct_color_when_hovered: suffixed,
14545 filled().error_text(ERROR_TEXT), |b| b.is_hovering(true), suffix_icon(),
14546 on_error_container;
14547 outlined_enabled_prefix_icon_has_correct_color: prefixed, outlined(), |b| b,
14548 prefix_icon(), on_surface_variant;
14549 outlined_enabled_suffix_icon_has_correct_color: suffixed, outlined(), |b| b,
14550 suffix_icon(), on_surface_variant;
14551 outlined_disabled_prefix_icon_has_correct_color: prefixed, outlined().enabled(false),
14552 |b| b, prefix_icon(), disabled_on_surface;
14553 outlined_disabled_suffix_icon_has_correct_color: suffixed, outlined().enabled(false),
14554 |b| b, suffix_icon(), disabled_on_surface;
14555 outlined_hovered_prefix_icon_has_correct_color: prefixed, outlined(),
14556 |b| b.is_hovering(true), prefix_icon(), on_surface_variant;
14557 outlined_hovered_suffix_icon_has_correct_color: suffixed, outlined(),
14558 |b| b.is_hovering(true), suffix_icon(), on_surface_variant;
14559 outlined_focused_prefix_icon_has_correct_color: prefixed, outlined(),
14560 |b| b.is_focused(true), prefix_icon(), on_surface_variant;
14561 outlined_focused_suffix_icon_has_correct_color: suffixed, outlined(),
14562 |b| b.is_focused(true), suffix_icon(), on_surface_variant;
14563 outlined_error_prefix_icon_has_correct_color: prefixed, outlined().error_text(ERROR_TEXT),
14564 |b| b, prefix_icon(), on_surface_variant;
14565 outlined_error_prefix_icon_has_correct_color_when_hovered: prefixed,
14566 outlined().error_text(ERROR_TEXT), |b| b.is_hovering(true), prefix_icon(),
14567 on_surface_variant;
14568 outlined_error_suffix_icon_has_correct_color: suffixed, outlined().error_text(ERROR_TEXT),
14569 |b| b, suffix_icon(), error;
14570 outlined_error_suffix_icon_has_correct_color_when_hovered: suffixed,
14571 outlined().error_text(ERROR_TEXT), |b| b.is_hovering(true), suffix_icon(),
14572 on_error_container;
14573 }
14574
14575 fn icon_theme_color(tester: &WidgetTester, icon: &IconData) -> Option<Color> {
14578 let icon_element = tester.element(find::by_icon(icon));
14579 let icon_theme = tester
14580 .ancestors(icon_element)
14581 .into_iter()
14582 .find(|element| {
14583 let app = tester.cell.borrow();
14584 downcast_widget::<waft_widgets::IconTheme>(&**element.widget(&app)).is_some()
14585 })
14586 .expect("an IconTheme above the icon");
14587 tester.widget(icon_theme, |theme: &waft_widgets::IconTheme| {
14588 theme.data.color.as_ref().map(|color| **color)
14589 })
14590 }
14591
14592 #[test]
14596 fn the_icon_colors_reach_the_icon_themes() {
14597 test_widgets(|tester| {
14598 pump(
14599 tester,
14600 BuildInputDecorator::new(
14601 InputDecoration::new()
14602 .icon(Icon::new(Some(Icons::cabin())))
14603 .prefix_icon(Icon::new(Some(Icons::sailing())))
14604 .suffix_icon(Icon::new(Some(Icons::close())))
14605 .icon_color(crate::colors::Colors::AMBER.primary())
14606 .prefix_icon_color(crate::colors::Colors::GREEN.primary())
14607 .suffix_icon_color(crate::colors::Colors::RED.primary())
14608 .filled(true),
14609 ),
14610 );
14611 assert_eq!(
14612 icon_theme_color(tester, &Icons::cabin()),
14613 Some(crate::colors::Colors::AMBER.primary())
14614 );
14615 assert_eq!(
14616 icon_theme_color(tester, &Icons::sailing()),
14617 Some(crate::colors::Colors::GREEN.primary())
14618 );
14619 assert_eq!(
14620 icon_theme_color(tester, &Icons::close()),
14621 Some(crate::colors::Colors::RED.primary())
14622 );
14623 });
14624 }
14625
14626 fn affix_opacity(tester: &WidgetTester) -> f64 {
14628 let icon = tester.element(find::by_type::<Icon>());
14629 let opacity = tester
14630 .ancestors(icon)
14631 .into_iter()
14632 .find(|element| {
14633 let app = tester.cell.borrow();
14634 downcast_widget::<AnimatedOpacity>(&**element.widget(&app)).is_some()
14635 })
14636 .expect("an AnimatedOpacity above the icon");
14637 tester.widget(opacity, |opacity: &AnimatedOpacity| opacity.opacity)
14638 }
14639
14640 fn check_affix_ignores_pointer_when_hidden(
14643 tester: &mut WidgetTester,
14644 affix: fn(InputDecoration, WidgetRef) -> InputDecoration,
14645 ) {
14646 let tapped = Rc::new(Cell::new(false));
14647 let build = |is_focused: bool| {
14648 let tapped = Rc::clone(&tapped);
14649 let affix_widget = GestureDetector::new()
14650 .on_tap(Listener::new(move |_app| tapped.set(true)))
14651 .child(Icon::new(Some(Icons::search())))
14652 .into_widget();
14653 BuildInputDecorator::new(affix(
14654 InputDecoration::new().label_text("label"),
14655 affix_widget,
14656 ))
14657 .is_empty(true)
14658 .is_focused(is_focused)
14659 .build()
14660 };
14661 tester.pump_widget(build(false));
14662 assert!(!tapped.get());
14663
14664 assert_eq!(affix_opacity(tester), 0.0);
14666
14667 let icon_center = tester.get_rect(find::by_type::<Icon>()).center();
14668 tester.tap_at(icon_center, 1);
14669 tester.pump();
14670
14671 assert!(!tapped.get());
14673
14674 tester.pump_widget(build(true));
14676
14677 assert_eq!(affix_opacity(tester), 1.0);
14679
14680 let icon_center = tester.get_rect(find::by_type::<Icon>()).center();
14682 tester.tap_at(icon_center, 2);
14683 tester.pump();
14684
14685 assert!(tapped.get());
14687 }
14688
14689 #[test]
14692 fn the_prefix_ignores_the_pointer_when_hidden() {
14693 test_widgets(|tester| {
14694 check_affix_ignores_pointer_when_hidden(tester, |decoration, prefix| {
14695 decoration.prefix(prefix)
14696 })
14697 });
14698 }
14699
14700 #[test]
14703 fn the_suffix_ignores_the_pointer_when_hidden() {
14704 test_widgets(|tester| {
14705 check_affix_ignores_pointer_when_hidden(tester, |decoration, suffix| {
14706 decoration.suffix(suffix)
14707 })
14708 });
14709 }
14710
14711 fn hint_opacity(tester: &WidgetTester) -> f64 {
14713 let hint = tester.element(find::text(HINT_TEXT));
14714 let fade = tester
14715 .ancestors(hint)
14716 .into_iter()
14717 .find(|element| {
14718 let app = tester.cell.borrow();
14719 downcast_widget::<FadeTransition>(&**element.widget(&app)).is_some()
14720 })
14721 .expect("a FadeTransition above the hint");
14722 let opacity = tester.widget(fade, |fade: &FadeTransition| fade.opacity.clone());
14723 opacity.value(&tester.cell.borrow())
14724 }
14725
14726 fn collapsed() -> InputDecoration {
14727 InputDecoration::collapsed(None).hint_text(HINT_TEXT)
14728 }
14729
14730 const COLLAPSED_INPUT_HEIGHT: f64 = 24.0;
14733
14734 fn check_collapsed_height(tester: &mut WidgetTester) {
14735 tester.pump_widget(BuildInputDecorator::new(collapsed()).build());
14736
14737 assert_eq!(
14738 get_decorator_rect(tester).size(),
14739 Size::new(800.0, COLLAPSED_INPUT_HEIGHT)
14740 );
14741 assert_eq!(input_rect(tester).height(), COLLAPSED_INPUT_HEIGHT);
14742 assert_eq!(input_rect(tester).top, 0.0);
14743 assert_eq!(hint_opacity(tester), 0.0);
14744
14745 tester.pump_widget(
14747 BuildInputDecorator::new(collapsed())
14748 .is_empty(true)
14749 .is_focused(true)
14750 .build(),
14751 );
14752 tester.pump_and_settle();
14753
14754 assert_eq!(
14755 get_decorator_rect(tester).size(),
14756 Size::new(800.0, COLLAPSED_INPUT_HEIGHT)
14757 );
14758 assert_eq!(input_rect(tester).height(), COLLAPSED_INPUT_HEIGHT);
14759 assert_eq!(input_rect(tester).top, 0.0);
14760 assert_eq!(hint_opacity(tester), 1.0);
14761 assert_eq!(
14762 tester.get_rect(find::text(HINT_TEXT)).height(),
14763 COLLAPSED_INPUT_HEIGHT
14764 );
14765 assert_eq!(tester.get_rect(find::text(HINT_TEXT)).top, 0.0);
14766 }
14767
14768 #[test]
14771 fn a_collapsed_decoration_is_the_input_height_on_mobile() {
14772 test_widgets_variant(TargetPlatformVariant::mobile(), check_collapsed_height);
14773 }
14774
14775 #[test]
14778 fn a_collapsed_decoration_is_the_input_height_on_desktop() {
14779 test_widgets_variant(TargetPlatformVariant::desktop(), check_collapsed_height);
14780 }
14781
14782 #[test]
14785 fn a_collapsed_decoration_has_no_border() {
14786 test_widgets(|tester| {
14787 pump(
14788 tester,
14789 BuildInputDecorator::new(InputDecoration::collapsed(Some(
14790 HINT_TEXT.to_owned(),
14791 ))),
14792 );
14793 assert_eq!(get_border_weight(tester), 0.0);
14794 });
14795 }
14796
14797 #[test]
14800 fn a_collapsed_decoration_takes_constraints() {
14801 test_widgets(|tester| {
14802 pump(
14803 tester,
14804 BuildInputDecorator::new(
14805 InputDecoration::collapsed(Some(HINT_TEXT.to_owned()))
14806 .constraints(BoxConstraints::tight_for(Some(200.0), Some(32.0))),
14807 ),
14808 );
14809 assert_eq!(get_decorator_rect(tester).size(), Size::new(200.0, 32.0));
14810 });
14811 }
14812
14813 #[test]
14816 fn a_collapsed_decoration_takes_hint_max_lines() {
14817 test_widgets(|tester| {
14818 pump(
14819 tester,
14820 BuildInputDecorator::new(
14821 InputDecoration::collapsed(Some(THREE_LINES.to_owned())).hint_max_lines(2),
14822 ),
14823 );
14824 const HINT_LINE_HEIGHT: f64 = 24.0; assert_eq!(
14826 get_decorator_rect(tester).size(),
14827 Size::new(800.0, 2.0 * HINT_LINE_HEIGHT)
14828 );
14829 });
14830 }
14831
14832 #[test]
14835 fn a_collapsed_decoration_takes_hint_fade_duration() {
14836 test_widgets(|tester| {
14837 let decoration = || collapsed().hint_fade_duration(Duration::from_millis(120));
14838 tester.pump_widget(
14840 BuildInputDecorator::new(decoration())
14841 .is_empty(true)
14842 .build(),
14843 );
14844
14845 assert_eq!(hint_opacity(tester), 1.0);
14847
14848 tester.pump_widget(BuildInputDecorator::new(decoration()).build());
14850
14851 tester.pump_by(Duration::from_millis(50));
14854 let hint_opacity_50ms = hint_opacity(tester);
14855 assert!(hint_opacity_50ms > 0.0 && hint_opacity_50ms < 1.0);
14856 tester.pump_by(Duration::from_millis(50));
14857 let hint_opacity_100ms = hint_opacity(tester);
14858 assert!(hint_opacity_100ms > 0.0 && hint_opacity_100ms < hint_opacity_50ms);
14859 tester.pump_by(Duration::from_millis(50));
14860 assert_eq!(hint_opacity(tester), 0.0);
14861 });
14862 }
14863
14864 fn floating_label_decorator(
14866 text_direction: TextDirection,
14867 has_icon: bool,
14868 alignment: FloatingLabelAlignment,
14869 border_is_outline: bool,
14870 ) -> WidgetRef {
14871 let mut decoration = InputDecoration::new()
14872 .content_padding(EdgeInsetsGeometry::directional(40.0, 12.0, 0.0, 12.0))
14873 .floating_label_alignment(alignment)
14874 .label_text(LABEL_TEXT)
14875 .hint_text(HINT_TEXT)
14876 .filled(true);
14877 if has_icon {
14878 decoration = decoration.icon(Icon::new(Some(Icons::insert_link())));
14879 }
14880 if border_is_outline {
14881 decoration = decoration.border(OutlineInputBorder::new());
14882 }
14883 BuildInputDecorator::new(decoration)
14884 .text_direction(text_direction)
14885 .build()
14886 }
14887
14888 fn check_floating_label(
14891 tester: &mut WidgetTester,
14892 text_direction: TextDirection,
14893 has_icon: bool,
14894 alignment: FloatingLabelAlignment,
14895 check: impl Fn(Rect),
14896 ) {
14897 for border_is_outline in [false, true] {
14898 tester.pump_widget(floating_label_decorator(
14899 text_direction,
14900 has_icon,
14901 alignment,
14902 border_is_outline,
14903 ));
14904 check(get_label_rect(tester));
14905 }
14906 }
14907
14908 #[test]
14911 fn ltr_with_an_icon_the_floating_label_aligns_to_the_start() {
14912 test_widgets(|tester| {
14913 check_floating_label(
14914 tester,
14915 TextDirection::Ltr,
14916 true,
14917 FloatingLabelAlignment::START,
14918 |label| assert_eq!(label.left, 84.0),
14920 );
14921 });
14922 }
14923
14924 #[test]
14926 fn ltr_with_an_icon_the_floating_label_aligns_to_the_center() {
14927 test_widgets(|tester| {
14928 check_floating_label(
14929 tester,
14930 TextDirection::Ltr,
14931 true,
14932 FloatingLabelAlignment::CENTER,
14933 |label| assert_eq!(label.center().dx(), 420.0),
14935 );
14936 });
14937 }
14938
14939 #[test]
14941 fn ltr_without_an_icon_the_floating_label_aligns_to_the_start() {
14942 test_widgets(|tester| {
14943 check_floating_label(
14944 tester,
14945 TextDirection::Ltr,
14946 false,
14947 FloatingLabelAlignment::START,
14948 |label| assert_eq!(label.left, 44.0),
14950 );
14951 });
14952 }
14953
14954 #[test]
14956 fn ltr_without_an_icon_the_floating_label_aligns_to_the_center() {
14957 test_widgets(|tester| {
14958 check_floating_label(
14959 tester,
14960 TextDirection::Ltr,
14961 false,
14962 FloatingLabelAlignment::CENTER,
14963 |label| assert_eq!(label.center().dx(), 400.0),
14965 );
14966 });
14967 }
14968
14969 #[test]
14971 fn rtl_with_an_icon_the_floating_label_aligns_to_the_start() {
14972 test_widgets(|tester| {
14973 check_floating_label(
14974 tester,
14975 TextDirection::Rtl,
14976 true,
14977 FloatingLabelAlignment::START,
14978 |label| assert_eq!(label.right, 716.0),
14980 );
14981 });
14982 }
14983
14984 #[test]
14986 fn rtl_with_an_icon_the_floating_label_aligns_to_the_center() {
14987 test_widgets(|tester| {
14988 check_floating_label(
14989 tester,
14990 TextDirection::Rtl,
14991 true,
14992 FloatingLabelAlignment::CENTER,
14993 |label| assert_eq!(label.center().dx(), 380.0),
14995 );
14996 });
14997 }
14998
14999 #[test]
15001 fn rtl_without_an_icon_the_floating_label_aligns_to_the_start() {
15002 test_widgets(|tester| {
15003 check_floating_label(
15004 tester,
15005 TextDirection::Rtl,
15006 false,
15007 FloatingLabelAlignment::START,
15008 |label| assert_eq!(label.right, 756.0),
15010 );
15011 });
15012 }
15013
15014 #[test]
15016 fn rtl_without_an_icon_the_floating_label_aligns_to_the_center() {
15017 test_widgets(|tester| {
15018 check_floating_label(
15019 tester,
15020 TextDirection::Rtl,
15021 false,
15022 FloatingLabelAlignment::CENTER,
15023 |label| assert_eq!(label.center().dx(), 400.0),
15025 );
15026 });
15027 }
15028
15029 const K_INPUT_EXTRA_PADDING: f64 = 4.0;
15031
15032 #[test]
15035 fn the_dense_layout_of_an_outlined_decoration() {
15036 test_widgets(|tester| {
15037 pump(
15038 tester,
15039 BuildInputDecorator::new(outlined().label_text(LABEL_TEXT).is_dense(true)),
15040 );
15041 let container_rect = get_container_rect(tester);
15042 let input_rect = input_rect(tester);
15043
15044 assert_eq!(container_rect.height(), 48.0);
15048 assert_eq!(input_rect.center().dy(), container_rect.center().dy());
15049 assert_eq!(
15050 input_rect.left,
15051 container_rect.left + 12.0 + K_INPUT_EXTRA_PADDING
15052 );
15053 assert_eq!(
15054 input_rect.right,
15055 container_rect.right - 12.0 - K_INPUT_EXTRA_PADDING
15056 );
15057 });
15058 }
15059
15060 #[test]
15063 fn the_dense_layout_of_a_filled_decoration() {
15064 test_widgets(|tester| {
15065 pump(
15066 tester,
15067 BuildInputDecorator::new(filled().label_text(LABEL_TEXT).is_dense(true)),
15068 );
15069 let container_rect = get_container_rect(tester);
15070 let input_rect = input_rect(tester);
15071 let label_rect = get_label_rect(tester);
15072
15073 assert_eq!(container_rect.height(), 48.0);
15075 assert_eq!(label_rect.top, container_rect.top + 4.0);
15076 assert_eq!(input_rect.bottom, container_rect.bottom - 4.0);
15077 assert_eq!(
15078 input_rect.left,
15079 container_rect.left + 12.0 + K_INPUT_EXTRA_PADDING
15080 );
15081 assert_eq!(
15082 input_rect.right,
15083 container_rect.right - 12.0 - K_INPUT_EXTRA_PADDING
15084 );
15085 });
15086 }
15087
15088 #[test]
15091 fn the_dense_layout_of_an_unfilled_underlined_decoration() {
15092 test_widgets(|tester| {
15093 pump(
15094 tester,
15095 BuildInputDecorator::new(
15096 InputDecoration::new().label_text(LABEL_TEXT).is_dense(true),
15097 ),
15098 );
15099 let container_rect = get_container_rect(tester);
15100 let input_rect = input_rect(tester);
15101 let label_rect = get_label_rect(tester);
15102
15103 assert_eq!(container_rect.height(), 48.0);
15105 assert_eq!(label_rect.top, container_rect.top + 4.0);
15106 assert_eq!(input_rect.bottom, container_rect.bottom - 4.0);
15107 assert_eq!(input_rect.left, container_rect.left);
15108 assert_eq!(input_rect.right, container_rect.right);
15109 });
15110 }
15111
15112 #[test]
15114 fn the_ambient_theme_is_dense() {
15115 test_widgets(|tester| {
15116 pump(
15117 tester,
15118 BuildInputDecorator::new(InputDecoration::new().label_text(LABEL_TEXT))
15119 .input_decoration_theme(InputDecorationThemeData::new().is_dense(true)),
15120 );
15121 assert_eq!(get_container_rect(tester).height(), 48.0);
15122 });
15123 }
15124
15125 #[test]
15128 fn a_decoration_with_every_slot_has_eleven_children() {
15129 test_widgets(|tester| {
15130 pump(
15131 tester,
15132 BuildInputDecorator::new(
15133 InputDecoration::new()
15134 .icon(Text::new("icon"))
15135 .label_text("label")
15136 .hint_text("hint")
15137 .prefix_text("prefix")
15138 .suffix_text("suffix")
15139 .prefix_icon(Text::new("prefixIcon"))
15140 .suffix_icon(Text::new("suffixIcon"))
15141 .helper_text("helper")
15142 .counter_text("counter"),
15143 )
15144 .child(Text::new("text")),
15145 );
15146 let render = tester.first::<RenderDecoration>();
15147 let app = tester.cell.borrow();
15148 let children = SlottedContainerRenderObjectMixin::children(render, &app);
15149 assert_eq!(children.len(), 11);
15150 for (index, child) in children.iter().enumerate() {
15151 assert!(
15152 !children[..index].contains(child),
15153 "each slot has its own child"
15154 );
15155 }
15156 });
15157 }
15158 }
15159
15160 mod themes_and_intrinsics {
15163 use std::time::Duration;
15164 use waft_painting::OutlinedBorderRef;
15165 use waft_widgets::testing::{TestPointer, WidgetTester, test_widgets};
15166
15167 use crate::test_support::PaintInspection;
15168 use waft_embedder::{Color, Offset, Radius, Size};
15169 use waft_foundation::Listener;
15170 use waft_painting::{
15171 AlignmentGeometry, BorderRadius, BorderSide, EdgeInsets, EdgeInsetsDirectional,
15172 EdgeInsetsGeometry, RoundedRectangleBorder, ShapeBorder, TextDirection, TextStyle,
15173 };
15174 use waft_rendering::BoxConstraints;
15175 use waft_widgets::{
15176 Builder, Center, Column, ConstrainedBox, CrossAxisAlignment, Directionality, Expanded,
15177 FadeTransition, GlobalKey, Icon, IconData, IconTheme, IntoWidget, IntrinsicHeight,
15178 MainAxisSize, RichText, Row, SizedBox, Text, UnconstrainedBox, WidgetRef, WidgetState,
15179 WidgetStateProperty, WidgetStatePropertyAll, WidgetStatePropertyRef,
15180 WidgetStateTextStyle, WidgetStates,
15181 };
15182
15183 use super::super::*;
15184 use super::common::K_TRANSITION_DURATION;
15185 use super::common::*;
15186 use crate::app::MaterialApp;
15187 use crate::button_style::ButtonStyle;
15188 use crate::colors::Colors;
15189 use crate::icon_button::IconButton;
15190 use crate::icon_button_theme::{IconButtonTheme, IconButtonThemeData};
15191 use crate::icons::Icons;
15192 use crate::input_border::{InputBorderRef, OutlineInputBorder};
15193 use crate::material::Material;
15194 use crate::material_state::WidgetStateInputBorder;
15195 use crate::scaffold::Scaffold;
15196 use crate::theme::Theme;
15197 use crate::theme_data::VisualDensity;
15198
15199 fn noop() -> Listener {
15200 Listener::new(|_app| {})
15201 }
15202
15203 fn icon(icon: IconData) -> Icon {
15204 Icon::new(Some(icon))
15205 }
15206
15207 fn rich_text_style(tester: &WidgetTester, text: &str) -> TextStyle {
15211 let rich_text = tester.element(find::descendant(
15212 find::text(text),
15213 find::by_type::<RichText>(),
15214 ));
15215 tester.widget(rich_text, |rich_text: &RichText| {
15216 rich_text.text.style().cloned().expect("a styled text")
15217 })
15218 }
15219
15220 fn style_color(style: &TextStyle) -> Option<Color> {
15221 style.color.as_ref().map(|color| color.color())
15222 }
15223
15224 fn get_opacity(tester: &WidgetTester, text: &str) -> f64 {
15227 let found = tester.element(find::text(text));
15228 let fade = tester
15229 .ancestors(found)
15230 .into_iter()
15231 .find(|element| {
15232 let app = tester.cell.borrow();
15233 waft_widgets::downcast_widget::<FadeTransition>(&**element.widget(&app))
15234 .is_some()
15235 })
15236 .expect("a FadeTransition above the text");
15237 let opacity = tester.widget(fade, |fade: &FadeTransition| fade.opacity.clone());
15238 opacity.value(&tester.cell.borrow())
15239 }
15240
15241 fn icon_theme_color(tester: &WidgetTester, icon: &IconData) -> Option<Color> {
15244 let found = tester.element(find::by_icon(icon));
15245 let icon_theme = tester
15246 .ancestors(found)
15247 .into_iter()
15248 .find(|element| {
15249 let app = tester.cell.borrow();
15250 waft_widgets::downcast_widget::<IconTheme>(&**element.widget(&app)).is_some()
15251 })
15252 .expect("an IconTheme above the icon");
15253 tester.widget(icon_theme, |theme: &IconTheme| {
15254 theme.data.color.as_ref().map(|color| color.color())
15255 })
15256 }
15257
15258 fn icon_size(tester: &WidgetTester, icon: &IconData) -> Size {
15260 tester.get_size(find::by_icon(icon))
15261 }
15262
15263 fn icon_glyph_color(tester: &WidgetTester, icon: &IconData) -> Option<Color> {
15265 let found = tester.element(find::by_icon(icon));
15266 style_color(&tester.glyph_style(found))
15267 }
15268
15269 fn outline(color: Color, width: f64) -> OutlineInputBorder {
15270 OutlineInputBorder::new().border_side(BorderSide {
15271 color,
15272 width,
15273 ..BorderSide::default()
15274 })
15275 }
15276
15277 fn border_is(tester: &WidgetTester, expected: &dyn ShapeBorder) -> bool {
15278 get_border(tester).expect("a border").eq_shape(expected)
15279 }
15280
15281 fn constraints(
15282 min_width: f64,
15283 max_width: f64,
15284 min_height: f64,
15285 max_height: f64,
15286 ) -> BoxConstraints {
15287 BoxConstraints {
15288 min_width,
15289 max_width,
15290 min_height,
15291 max_height,
15292 }
15293 }
15294
15295 fn decorator_render(tester: &WidgetTester) -> waft_rendering::AnyRenderBox {
15296 tester
15297 .render_object(find::by_type::<InputDecorator>())
15298 .as_box()
15299 .expect("a render box")
15300 }
15301
15302 fn key_size(tester: &WidgetTester, key: &GlobalKey) -> Size {
15303 let render = tester.render_object(key).as_box().expect("a render box");
15304 render.size(&tester.cell.borrow())
15305 }
15306
15307 fn pump(tester: &mut WidgetTester, decorator: BuildInputDecorator) {
15308 tester.pump_widget(decorator.build());
15309 }
15310
15311 #[test]
15313 fn widget_state_values_go_from_the_theme_to_the_decoration_unless_it_has_its_own() {
15314 test_widgets(|_tester| {
15315 let theme_style: AnyTextStyle =
15316 WidgetStateTextStyle::resolve_with(|_states: &WidgetStates| {
15317 TextStyle::new().color(Colors::GREEN.primary())
15318 })
15319 .into_any();
15320 let decoration_style: AnyTextStyle =
15321 WidgetStateTextStyle::resolve_with(|_states: &WidgetStates| {
15322 TextStyle::new().color(Colors::BLUE.primary())
15323 })
15324 .into_any();
15325 let none = InputBorderRef::none();
15326
15327 let decoration = InputDecoration::new().apply_defaults(
15329 InputDecorationThemeData::new()
15330 .label_style(theme_style.clone())
15331 .helper_style(theme_style.clone())
15332 .hint_style(theme_style.clone())
15333 .error_style(theme_style.clone())
15334 .is_dense(true)
15335 .content_padding(EdgeInsets::all(1.0))
15336 .prefix_style(theme_style.clone())
15337 .suffix_style(theme_style.clone())
15338 .counter_style(theme_style.clone())
15339 .filled(true)
15340 .fill_color(Colors::RED.primary())
15341 .focus_color(Colors::BLUE.primary())
15342 .border(InputBorderRef::none())
15343 .align_label_with_hint(true)
15344 .constraints(constraints(10.0, 20.0, 30.0, 40.0)),
15345 );
15346
15347 assert_eq!(decoration.label_style, Some(theme_style.clone()));
15348 assert_eq!(decoration.helper_style, Some(theme_style.clone()));
15349 assert_eq!(decoration.hint_style, Some(theme_style.clone()));
15350 assert_eq!(decoration.error_style, Some(theme_style.clone()));
15351 assert_eq!(decoration.is_dense, Some(true));
15352 assert_eq!(
15353 decoration.content_padding,
15354 Some(EdgeInsets::all(1.0).into())
15355 );
15356 assert_eq!(decoration.prefix_style, Some(theme_style.clone()));
15357 assert_eq!(decoration.suffix_style, Some(theme_style.clone()));
15358 assert_eq!(decoration.counter_style, Some(theme_style.clone()));
15359 assert_eq!(decoration.filled, Some(true));
15360 assert_eq!(
15361 decoration.fill_color,
15362 Some(AnyColor::from(Colors::RED.primary()))
15363 );
15364 assert!(decoration.border.as_deref() == Some(&*none));
15365 assert_eq!(decoration.align_label_with_hint, Some(true));
15366 assert_eq!(
15367 decoration.constraints,
15368 Some(constraints(10.0, 20.0, 30.0, 40.0))
15369 );
15370
15371 let border = WidgetStateInputBorder::resolve_with(|_states: &WidgetStates| {
15373 InputBorderRef::new(OutlineInputBorder::new())
15374 });
15375 let decoration = InputDecoration::new()
15376 .label_style(decoration_style.clone())
15377 .helper_style(decoration_style.clone())
15378 .hint_style(decoration_style.clone())
15379 .error_style(decoration_style.clone())
15380 .is_dense(false)
15381 .content_padding(EdgeInsets::all(4.0))
15382 .prefix_style(decoration_style.clone())
15383 .suffix_style(decoration_style.clone())
15384 .counter_style(decoration_style.clone())
15385 .filled(false)
15386 .fill_color(Colors::BLUE.primary())
15387 .border(border)
15388 .align_label_with_hint(false)
15389 .constraints(constraints(10.0, 20.0, 30.0, 40.0))
15390 .apply_defaults(
15391 InputDecorationThemeData::new()
15392 .label_style(theme_style.clone())
15393 .helper_style(theme_style.clone())
15394 .helper_max_lines(5)
15395 .hint_style(theme_style.clone())
15396 .error_style(theme_style.clone())
15397 .error_max_lines(4)
15398 .is_dense(true)
15399 .content_padding(EdgeInsets::all(1.0))
15400 .prefix_style(theme_style.clone())
15401 .suffix_style(theme_style.clone())
15402 .counter_style(theme_style.clone())
15403 .filled(true)
15404 .fill_color(Colors::RED.primary())
15405 .focus_color(Colors::BLUE.primary())
15406 .border(InputBorderRef::none())
15407 .align_label_with_hint(true)
15408 .constraints(constraints(40.0, 30.0, 20.0, 10.0)),
15409 );
15410
15411 assert_eq!(decoration.label_style, Some(decoration_style.clone()));
15412 assert_eq!(decoration.helper_style, Some(decoration_style.clone()));
15413 assert_eq!(decoration.helper_max_lines, Some(5));
15414 assert_eq!(decoration.hint_style, Some(decoration_style.clone()));
15415 assert_eq!(decoration.error_style, Some(decoration_style.clone()));
15416 assert_eq!(decoration.error_max_lines, Some(4));
15417 assert_eq!(decoration.is_dense, Some(false));
15418 assert_eq!(
15419 decoration.content_padding,
15420 Some(EdgeInsets::all(4.0).into())
15421 );
15422 assert_eq!(decoration.prefix_style, Some(decoration_style.clone()));
15423 assert_eq!(decoration.suffix_style, Some(decoration_style.clone()));
15424 assert_eq!(decoration.counter_style, Some(decoration_style.clone()));
15425 assert_eq!(decoration.filled, Some(false));
15426 assert_eq!(
15427 decoration.fill_color,
15428 Some(AnyColor::from(Colors::BLUE.primary()))
15429 );
15430 assert!(
15431 decoration
15432 .border
15433 .as_ref()
15434 .expect("a border")
15435 .as_any()
15436 .is::<WidgetStateInputBorder>()
15437 );
15438 assert_eq!(decoration.align_label_with_hint, Some(false));
15439 assert_eq!(
15440 decoration.constraints,
15441 Some(constraints(10.0, 20.0, 30.0, 40.0))
15442 );
15443 });
15444 }
15445
15446 #[test]
15450 fn a_widget_state_input_border_paints_the_border_of_the_state() {
15451 test_widgets(|tester| {
15452 let outline_input_border = || {
15453 WidgetStateInputBorder::from_map(vec![
15454 (
15455 WidgetState::Focused.into(),
15456 InputBorderRef::new(outline(Colors::BLUE.primary(), 4.0)),
15457 ),
15458 (
15459 WidgetState::Hovered.into(),
15460 InputBorderRef::new(outline(Colors::CYAN.primary(), 8.0)),
15461 ),
15462 (
15463 WidgetState::any(),
15464 InputBorderRef::new(OutlineInputBorder::new()),
15465 ),
15466 ])
15467 };
15468 let build = |is_focused: bool, is_hovering: bool| {
15469 BuildInputDecorator::new(InputDecoration::new().border(outline_input_border()))
15470 .is_focused(is_focused)
15471 .is_hovering(is_hovering)
15472 .build()
15473 };
15474 tester.pump_widget(build(false, false));
15475 assert_paints(
15476 &border_painter_ops(tester),
15477 &[PaintCall::RRect {
15478 rrect: None,
15479 color: None,
15480 style: Some(PaintedStyle::Stroke(Some(1.0))),
15481 has_mask_filter: None,
15482 }],
15483 );
15484
15485 tester.pump_widget(build(true, false));
15486 tester.pump_and_settle();
15487 assert_paints(
15488 &border_painter_ops(tester),
15489 &[PaintCall::RRect {
15490 rrect: None,
15491 color: Some(Colors::BLUE.primary()),
15492 style: Some(PaintedStyle::Stroke(Some(4.0))),
15493 has_mask_filter: None,
15494 }],
15495 );
15496
15497 tester.pump_widget(build(false, true));
15498 tester.pump_and_settle();
15499 assert_paints(
15502 &border_painter_ops(tester),
15503 &[PaintCall::Path {
15504 color: Some(Colors::CYAN.primary()),
15505 style: Some(PaintedStyle::Stroke(Some(8.0))),
15506 includes: Vec::new(),
15507 excludes: Vec::new(),
15508 }],
15509 );
15510 });
15511 }
15512
15513 #[test]
15515 fn a_decorator_constrained_to_zero_by_zero_lays_out() {
15516 test_widgets(|tester| {
15517 tester.pump_widget(
15518 crate::material::Material::new()
15519 .child(Directionality::new(
15520 TextDirection::Ltr,
15521 UnconstrainedBox::new().child(
15522 ConstrainedBox::new(BoxConstraints::tight(Size::ZERO)).child(
15523 InputDecorator::new(
15524 InputDecoration::new()
15525 .label_text("XP")
15526 .border(OutlineInputBorder::new()),
15527 ),
15528 ),
15529 ),
15530 ))
15531 .into_widget(),
15532 );
15533 });
15534 }
15535
15536 fn labeled_input_decorator(show_fix: bool, is_focused: bool) -> WidgetRef {
15537 const PREFIX_TEXT: &str = "The five boxing wizards jump quickly.";
15538 const SUFFIX_TEXT: &str = "Suffix";
15539 let mut decoration = InputDecoration::new()
15540 .icon(icon(Icons::assistant()))
15541 .suffix_icon(icon(Icons::threesixty()))
15542 .label_text("My Label");
15543 if show_fix {
15544 decoration = decoration.prefix_text(PREFIX_TEXT).suffix_text(SUFFIX_TEXT);
15545 }
15546 MaterialApp::new()
15547 .home(Material::new().child(Builder::new(move |app, context| {
15548 Theme::new(
15549 Theme::of(app, context),
15550 waft_widgets::Align::new()
15551 .alignment(AlignmentGeometry::Alignment(
15552 waft_painting::Alignment::TOP_LEFT,
15553 ))
15554 .child(
15555 InputDecorator::new(decoration.clone())
15556 .is_empty(true)
15557 .is_focused(is_focused)
15558 .child(input_text()),
15559 ),
15560 )
15561 .into_widget()
15562 })))
15563 .into_widget()
15564 }
15565
15566 fn text_size(tester: &WidgetTester, text: &str) -> Size {
15567 tester.get_size(find::text(text))
15568 }
15569
15570 #[test]
15573 fn the_label_width_is_not_affected_by_the_prefix_or_suffix() {
15574 test_widgets(|tester| {
15575 const PREFIX_TEXT: &str = "The five boxing wizards jump quickly.";
15576 const SUFFIX_TEXT: &str = "Suffix";
15577 tester.pump_widget(labeled_input_decorator(false, false));
15579
15580 expect(tester, &find::text(PREFIX_TEXT), finds_nothing());
15582 expect(tester, &find::text(SUFFIX_TEXT), finds_nothing());
15583 let label_width = text_size(tester, "My Label").width();
15584
15585 tester.pump_widget(labeled_input_decorator(true, false));
15587
15588 expect(tester, &find::text(PREFIX_TEXT), finds_one_widget());
15591 assert_eq!(get_opacity(tester, PREFIX_TEXT), 0.0);
15592 expect(tester, &find::text(SUFFIX_TEXT), finds_one_widget());
15593 assert_eq!(get_opacity(tester, SUFFIX_TEXT), 0.0);
15594 assert_eq!(text_size(tester, "My Label").width(), label_width);
15595
15596 tester.pump_widget(labeled_input_decorator(true, true));
15598 tester.pump_and_settle();
15599
15600 assert_eq!(text_size(tester, "My Label").width(), label_width);
15603 assert_eq!(get_opacity(tester, PREFIX_TEXT), 1.0);
15604 });
15605 }
15606
15607 #[test]
15609 fn the_prefix_and_suffix_are_hidden_when_the_decorator_is_empty() {
15610 test_widgets(|tester| {
15611 pump(
15612 tester,
15613 BuildInputDecorator::new(
15614 InputDecoration::new()
15615 .filled(true)
15616 .label_text(LABEL_TEXT)
15617 .prefix_text("Prefix")
15618 .suffix_text("Suffix"),
15619 )
15620 .is_empty(true),
15621 );
15622
15623 assert_eq!(get_opacity(tester, "Prefix"), 0.0);
15625 assert_eq!(get_opacity(tester, "Suffix"), 0.0);
15626 });
15627 }
15628
15629 #[test]
15632 fn the_prefix_and_suffix_show_when_the_decorator_is_empty_and_the_label_always_floats() {
15633 test_widgets(|tester| {
15634 pump(
15635 tester,
15636 BuildInputDecorator::new(
15637 InputDecoration::new()
15638 .filled(true)
15639 .label_text(LABEL_TEXT)
15640 .prefix_text("Prefix")
15641 .suffix_text("Suffix")
15642 .floating_label_behavior(FloatingLabelBehavior::Always),
15643 )
15644 .is_empty(true),
15645 );
15646
15647 assert_eq!(get_opacity(tester, "Prefix"), 1.0);
15649 assert_eq!(get_opacity(tester, "Suffix"), 1.0);
15650 });
15651 }
15652
15653 fn long_labeled_input_decorator(
15654 floating_label_behavior: FloatingLabelBehavior,
15655 is_focused: bool,
15656 ) -> WidgetRef {
15657 const LONG_LABEL: &str = "Flutter is Google’s UI toolkit for building beautiful, natively compiled applications for mobile, web, and desktop from a single codebase.";
15658 MaterialApp::new()
15659 .home(
15660 Scaffold::new().body(
15661 SizedBox::new().width(300.0).child(
15662 InputDecorator::new(
15663 InputDecoration::new()
15664 .border(OutlineInputBorder::new().border_side(BorderSide {
15665 color: Colors::GREEN_ACCENT.primary(),
15666 ..BorderSide::default()
15667 }))
15668 .suffix_icon(icon(Icons::arrow_drop_down()))
15669 .floating_label_behavior(floating_label_behavior)
15670 .label_text(LONG_LABEL),
15671 )
15672 .is_empty(true)
15673 .is_focused(is_focused)
15674 .child(input_text()),
15675 ),
15676 ),
15677 )
15678 .into_widget()
15679 }
15680
15681 #[test]
15685 fn a_long_floating_outlined_label_is_wider_than_its_inline_width() {
15686 test_widgets(|tester| {
15687 tester.pump_widget(long_labeled_input_decorator(
15688 FloatingLabelBehavior::Never,
15689 false,
15690 ));
15691
15692 let label_width = get_label_rect(tester).width();
15693
15694 tester.pump_widget(long_labeled_input_decorator(
15695 FloatingLabelBehavior::Always,
15696 false,
15697 ));
15698 tester.pump_by(K_TRANSITION_DURATION);
15699
15700 let floated_label_width = get_label_rect(tester).width();
15701
15702 assert!(floated_label_width > label_width);
15703
15704 tester.pump_widget(long_labeled_input_decorator(
15705 FloatingLabelBehavior::Auto,
15706 false,
15707 ));
15708 tester.pump_by(K_TRANSITION_DURATION);
15709
15710 assert_eq!(get_label_rect(tester).width(), label_width);
15711
15712 tester.pump_widget(long_labeled_input_decorator(
15714 FloatingLabelBehavior::Auto,
15715 true,
15716 ));
15717 tester.pump_by(Duration::from_millis(80));
15719
15720 assert!(get_label_rect(tester).width() > label_width);
15721 assert!(get_label_rect(tester).width() <= floated_label_width);
15722
15723 tester.pump_by(K_TRANSITION_DURATION);
15724
15725 assert_eq!(get_label_rect(tester).width(), floated_label_width);
15726 });
15727 }
15728
15729 fn density_row(
15730 visual_density: VisualDensity,
15731 unconstrained: &GlobalKey,
15732 constrained: &GlobalKey,
15733 ) -> WidgetRef {
15734 let unconstrained = unconstrained.clone();
15735 let constrained = constrained.clone();
15736 MaterialApp::new()
15737 .home(Material::new().child(Builder::new(move |app, context| {
15738 Theme::new(
15739 Theme::of(app, context)
15740 .copy_with()
15741 .visual_density(visual_density)
15742 .build(),
15743 Center::new().child(
15744 Row::new().children([
15745 SizedBox::new()
15746 .width(35.0)
15747 .child(
15748 crate::TextField::new().key(unconstrained.clone().into()),
15749 )
15750 .into_widget(),
15751 SizedBox::new()
15752 .width(35.0)
15753 .height(48.0)
15756 .child(crate::TextField::new().key(constrained.clone().into()))
15757 .into_widget(),
15758 ]),
15759 ),
15760 )
15761 .into_widget()
15762 })))
15763 .into_widget()
15764 }
15765
15766 #[test]
15769 fn constrained_and_unconstrained_heights_give_the_same_size_given_enough_space() {
15770 test_widgets(|tester| {
15771 let key_unconstrained = GlobalKey::new();
15772 let key_constrained = GlobalKey::new();
15773 tester.pump_widget(density_row(
15774 VisualDensity::STANDARD,
15775 &key_unconstrained,
15776 &key_constrained,
15777 ));
15778 let constrained_height = key_size(tester, &key_constrained).height();
15779 let unconstrained_height = key_size(tester, &key_unconstrained).height();
15780 assert_eq!(constrained_height, unconstrained_height);
15781
15782 tester.pump_widget(density_row(
15783 VisualDensity::COMPACT,
15784 &key_unconstrained,
15785 &key_constrained,
15786 ));
15787 let constrained_height_compact = key_size(tester, &key_constrained).height();
15788 let unconstrained_height_compact = key_size(tester, &key_unconstrained).height();
15789 assert_eq!(constrained_height_compact, unconstrained_height_compact);
15790 });
15791 }
15792
15793 #[test]
15796 fn a_vertically_constrained_decorator_positions_its_text_inside_itself() {
15797 test_widgets(|tester| {
15798 tester.pump_widget(
15799 MaterialApp::new()
15800 .home(
15801 Material::new().child(
15802 Center::new().child(
15803 SizedBox::new().width(200.0).height(28.0).child(
15804 InputDecorator::new(InputDecoration::new()).child(
15805 Text::new("A").style(
15806 TextStyle::new().font_size(16.0).height(1.5),
15807 ),
15808 ),
15809 ),
15810 ),
15811 ),
15812 )
15813 .into_widget(),
15814 );
15815
15816 let field = get_decorator_rect(tester);
15817 let text = tester.get_rect(find::text("A"));
15818
15819 assert!(text.height() < field.bottom - field.top);
15821 assert!(text.top > field.top);
15822 assert!(text.top < field.bottom);
15823 });
15824 }
15825
15826 #[test]
15829 fn the_visual_density_is_included_in_the_intrinsic_height() {
15830 test_widgets(|tester| {
15831 let key = GlobalKey::new();
15832 let intrinsic_height_key = GlobalKey::new();
15833 let (k1, k2) = (key.clone(), intrinsic_height_key.clone());
15834 tester.pump_widget(
15835 MaterialApp::new()
15836 .home(Material::new().child(Builder::new(move |app, context| {
15837 Theme::new(
15838 Theme::of(app, context)
15839 .copy_with()
15840 .visual_density(VisualDensity::COMPACT)
15841 .build(),
15842 Center::new().child(
15843 Row::new().children([
15844 SizedBox::new()
15845 .width(35.0)
15846 .child(
15847 InputDecorator::new(InputDecoration::new())
15848 .key(k1.clone().into())
15849 .child(input_text()),
15850 )
15851 .into_widget(),
15852 SizedBox::new()
15853 .width(35.0)
15854 .child(
15855 IntrinsicHeight::new().child(
15856 InputDecorator::new(InputDecoration::new())
15857 .key(k2.clone().into())
15858 .child(input_text()),
15859 ),
15860 )
15861 .into_widget(),
15862 ]),
15863 ),
15864 )
15865 .into_widget()
15866 })))
15867 .into_widget(),
15868 );
15869
15870 let height = key_size(tester, &key).height();
15871 let intrinsic_height = key_size(tester, &intrinsic_height_key).height();
15872 assert_eq!(intrinsic_height, height);
15873 });
15874 }
15875
15876 fn pump_intrinsic_column(
15880 tester: &mut WidgetTester,
15881 width: Option<f64>,
15882 decoration: InputDecoration,
15883 ) {
15884 let column = IntrinsicHeight::new().child(
15885 Column::new().children([InputDecorator::new(decoration)
15886 .child(input_text())
15887 .into_widget()]),
15888 );
15889 let body = match width {
15890 Some(width) => SizedBox::new().width(width).child(column).into_widget(),
15891 None => column.into_widget(),
15892 };
15893 tester.pump_widget(
15894 MaterialApp::new()
15895 .home(Material::new().child(Center::new().child(body)))
15896 .into_widget(),
15897 );
15898 }
15899
15900 #[test]
15902 fn the_min_intrinsic_height_lays_out_with_no_content_padding() {
15903 test_widgets(|tester| {
15904 pump_intrinsic_column(
15905 tester,
15906 None,
15907 InputDecoration::new()
15908 .label_text("Label Text")
15909 .helper_text("Helper Text")
15910 .content_padding(EdgeInsets::ZERO),
15911 );
15912 });
15913 }
15914
15915 #[test]
15917 fn the_min_intrinsic_height_lays_out_with_a_prefix_icon() {
15918 test_widgets(|tester| {
15919 pump_intrinsic_column(
15920 tester,
15921 Some(100.0),
15922 InputDecoration::new().prefix_icon(icon(Icons::search())),
15923 );
15924 });
15925 }
15926
15927 #[test]
15929 fn the_min_intrinsic_height_lays_out_with_a_suffix_icon() {
15930 test_widgets(|tester| {
15931 pump_intrinsic_column(
15932 tester,
15933 Some(100.0),
15934 InputDecoration::new().suffix_icon(icon(Icons::search())),
15935 );
15936 });
15937 }
15938
15939 #[test]
15941 fn the_min_intrinsic_height_lays_out_with_a_prefix() {
15942 test_widgets(|tester| {
15943 pump_intrinsic_column(
15944 tester,
15945 Some(100.0),
15946 InputDecoration::new().prefix(Text::new("prefix")),
15947 );
15948 });
15949 }
15950
15951 #[test]
15953 fn the_min_intrinsic_height_lays_out_with_a_suffix() {
15954 test_widgets(|tester| {
15955 pump_intrinsic_column(
15956 tester,
15957 Some(100.0),
15958 InputDecoration::new().suffix(Text::new("suffix")),
15959 );
15960 });
15961 }
15962
15963 #[test]
15965 fn the_min_intrinsic_height_lays_out_with_an_icon() {
15966 test_widgets(|tester| {
15967 pump_intrinsic_column(
15968 tester,
15969 Some(100.0),
15970 InputDecoration::new().icon(icon(Icons::search())),
15971 );
15972 });
15973 }
15974
15975 fn helper_decoration(width: f64) -> WidgetRef {
15976 MaterialApp::new()
15977 .home(
15978 Scaffold::new().body(
15979 SizedBox::new().width(width).child(
15980 IntrinsicHeight::new().child(InputDecorator::new(
15981 InputDecoration::new()
15982 .filled(true)
15983 .label_text(LABEL_TEXT)
15984 .helper_text("0123456789")
15985 .helper_max_lines(2),
15986 )),
15987 ),
15988 ),
15989 )
15990 .into_widget()
15991 }
15992
15993 #[test]
15996 fn the_min_intrinsic_height_does_not_change_the_container_height() {
15997 test_widgets(|tester| {
15998 tester.pump_widget(helper_decoration(300.0));
16000 let default_container_height = get_container_rect(tester).height();
16001
16002 tester.pump_widget(helper_decoration(150.0));
16004
16005 assert_eq!(
16007 get_container_rect(tester).height(),
16008 default_container_height
16009 );
16010 });
16011 }
16012
16013 fn intrinsic_decoration(name: &str) -> InputDecoration {
16016 match name {
16017 "isDense and isCollapsed" => InputDecoration::new()
16018 .is_dense(true)
16019 .is_collapsed(true)
16020 .content_padding(EdgeInsets::ZERO)
16021 .border(InputBorderRef::none())
16022 .hint_text(HINT_TEXT),
16023 "isDense with zero content padding" => InputDecoration::new()
16024 .is_dense(true)
16025 .content_padding(EdgeInsets::ZERO)
16026 .hint_text(HINT_TEXT),
16027 "isCollapsed" => InputDecoration::new()
16028 .is_collapsed(true)
16029 .hint_text(HINT_TEXT),
16030 "InputDecoration.collapsed" => {
16031 InputDecoration::collapsed(None).hint_text(HINT_TEXT)
16032 }
16033 _ => unreachable!("one of the group's decorations"),
16034 }
16035 }
16036
16037 fn check_min_intrinsic_height_matches(
16040 tester: &mut WidgetTester,
16041 name: &str,
16042 visual_density: VisualDensity,
16043 ) {
16044 let decoration = intrinsic_decoration(name);
16045 let theme = ThemeData::builder()
16046 .visual_density(visual_density)
16047 .build(&tester.cell.borrow());
16048 tester.pump_widget(crate::test_support::material_app(
16049 theme,
16050 crate::material::Material::new()
16051 .child(
16052 Center::new().child(
16053 SizedBox::new().width(200.0).child(
16054 InputDecorator::new(decoration)
16055 .is_empty(true)
16056 .child(input_text()),
16057 ),
16058 ),
16059 )
16060 .into_widget(),
16061 ));
16062
16063 let decorator = decorator_render(tester);
16064 let mut app = tester.cell.borrow_mut();
16065 let height = decorator.size(&app).height();
16066 assert_eq!(decorator.get_min_intrinsic_height(&mut app, 200.0), height);
16067 assert_eq!(decorator.get_max_intrinsic_height(&mut app, 200.0), height);
16068 }
16069
16070 #[test]
16073 fn the_min_intrinsic_height_matches_the_layout_dense_and_collapsed_at_standard_density() {
16074 test_widgets(|tester| {
16075 check_min_intrinsic_height_matches(
16076 tester,
16077 "isDense and isCollapsed",
16078 VisualDensity::STANDARD,
16079 )
16080 });
16081 }
16082
16083 #[test]
16086 fn the_min_intrinsic_height_matches_the_layout_dense_without_padding_at_standard_density() {
16087 test_widgets(|tester| {
16088 check_min_intrinsic_height_matches(
16089 tester,
16090 "isDense with zero content padding",
16091 VisualDensity::STANDARD,
16092 )
16093 });
16094 }
16095
16096 #[test]
16099 fn the_min_intrinsic_height_matches_the_layout_collapsed_at_standard_density() {
16100 test_widgets(|tester| {
16101 check_min_intrinsic_height_matches(tester, "isCollapsed", VisualDensity::STANDARD)
16102 });
16103 }
16104
16105 #[test]
16108 fn the_min_intrinsic_height_matches_the_layout_of_a_collapsed_decoration_at_standard_density()
16109 {
16110 test_widgets(|tester| {
16111 check_min_intrinsic_height_matches(
16112 tester,
16113 "InputDecoration.collapsed",
16114 VisualDensity::STANDARD,
16115 )
16116 });
16117 }
16118
16119 #[test]
16122 fn the_min_intrinsic_height_matches_the_layout_dense_and_collapsed_at_compact_density() {
16123 test_widgets(|tester| {
16124 check_min_intrinsic_height_matches(
16125 tester,
16126 "isDense and isCollapsed",
16127 VisualDensity::COMPACT,
16128 )
16129 });
16130 }
16131
16132 #[test]
16135 fn the_min_intrinsic_height_matches_the_layout_dense_without_padding_at_compact_density() {
16136 test_widgets(|tester| {
16137 check_min_intrinsic_height_matches(
16138 tester,
16139 "isDense with zero content padding",
16140 VisualDensity::COMPACT,
16141 )
16142 });
16143 }
16144
16145 #[test]
16148 fn the_min_intrinsic_height_matches_the_layout_collapsed_at_compact_density() {
16149 test_widgets(|tester| {
16150 check_min_intrinsic_height_matches(tester, "isCollapsed", VisualDensity::COMPACT)
16151 });
16152 }
16153
16154 #[test]
16157 fn the_min_intrinsic_height_matches_the_layout_of_a_collapsed_decoration_at_compact_density()
16158 {
16159 test_widgets(|tester| {
16160 check_min_intrinsic_height_matches(
16161 tester,
16162 "InputDecoration.collapsed",
16163 VisualDensity::COMPACT,
16164 )
16165 });
16166 }
16167
16168 #[test]
16172 fn an_intrinsic_height_does_not_overflow_with_a_dense_collapsed_decorator() {
16173 test_widgets(|tester| {
16174 let theme = ThemeData::builder()
16175 .visual_density(VisualDensity::COMPACT)
16176 .build(&tester.cell.borrow());
16177 tester.pump_widget(crate::test_support::material_app(
16178 theme,
16179 Scaffold::new()
16180 .body(
16181 IntrinsicHeight::new().child(
16182 Row::new()
16183 .cross_axis_alignment(CrossAxisAlignment::Stretch)
16184 .children([
16185 Expanded::new(
16186 Column::new()
16187 .main_axis_size(MainAxisSize::Min)
16188 .children([
16189 Text::new("Text").into_widget(),
16190 InputDecorator::new(
16191 InputDecoration::new()
16192 .is_dense(true)
16193 .is_collapsed(true)
16194 .content_padding(EdgeInsets::ZERO)
16195 .border(InputBorderRef::none())
16196 .hint_text(HINT_TEXT),
16197 )
16198 .is_empty(true)
16199 .child(input_text())
16200 .into_widget(),
16201 ]),
16202 )
16203 .into_widget(),
16204 Text::new("Right text").into_widget(),
16205 ]),
16206 ),
16207 )
16208 .into_widget(),
16209 ));
16210 });
16211 }
16212
16213 fn intrinsic_padding() -> EdgeInsetsGeometry {
16214 EdgeInsetsDirectional::only(12.0, 0.0, 24.0, 0.0).into()
16215 }
16216
16217 fn measure_text(
16219 tester: &mut WidgetTester,
16220 decoration: InputDecoration,
16221 direction: TextDirection,
16222 ) -> Size {
16223 tester.pump_widget(
16224 BuildInputDecorator::new(decoration)
16225 .use_intrinsic_width(true)
16226 .text_direction(direction)
16227 .build(),
16228 );
16229 tester.get_size(find::text(INPUT_TEXT))
16230 }
16231
16232 fn without_icons() -> InputDecoration {
16233 InputDecoration::new().content_padding(intrinsic_padding())
16234 }
16235
16236 fn with_prefix() -> InputDecoration {
16237 without_icons().prefix_icon(icon(Icons::search()))
16238 }
16239
16240 fn with_suffix() -> InputDecoration {
16241 without_icons().suffix_icon(icon(Icons::search()))
16242 }
16243
16244 fn with_affixes() -> InputDecoration {
16245 without_icons()
16246 .prefix_icon(icon(Icons::search()))
16247 .suffix_icon(icon(Icons::search()))
16248 }
16249
16250 #[test]
16252 fn the_intrinsic_width_keeps_the_text_width_with_a_prefix_icon_ltr() {
16253 test_widgets(|tester| {
16254 assert_eq!(
16255 measure_text(tester, with_prefix(), TextDirection::Ltr).width(),
16256 measure_text(tester, without_icons(), TextDirection::Ltr).width()
16257 );
16258 });
16259 }
16260
16261 #[test]
16263 fn the_intrinsic_width_keeps_the_text_width_with_a_suffix_icon_ltr() {
16264 test_widgets(|tester| {
16265 assert_eq!(
16266 measure_text(tester, with_suffix(), TextDirection::Ltr).width(),
16267 measure_text(tester, without_icons(), TextDirection::Ltr).width()
16268 );
16269 });
16270 }
16271
16272 #[test]
16274 fn the_intrinsic_width_keeps_the_text_width_with_both_icons_ltr() {
16275 test_widgets(|tester| {
16276 assert_eq!(
16277 measure_text(tester, with_affixes(), TextDirection::Ltr).width(),
16278 measure_text(tester, without_icons(), TextDirection::Ltr).width()
16279 );
16280 });
16281 }
16282
16283 #[test]
16285 fn the_intrinsic_width_keeps_the_text_width_with_a_prefix_icon_rtl() {
16286 test_widgets(|tester| {
16287 assert_eq!(
16288 measure_text(tester, with_prefix(), TextDirection::Rtl).width(),
16289 measure_text(tester, without_icons(), TextDirection::Rtl).width()
16290 );
16291 });
16292 }
16293
16294 #[test]
16296 fn the_intrinsic_width_keeps_the_text_width_with_a_suffix_icon_rtl() {
16297 test_widgets(|tester| {
16298 assert_eq!(
16299 measure_text(tester, with_suffix(), TextDirection::Rtl).width(),
16300 measure_text(tester, without_icons(), TextDirection::Rtl).width()
16301 );
16302 });
16303 }
16304
16305 #[test]
16307 fn the_intrinsic_width_keeps_the_text_width_with_both_icons_rtl() {
16308 test_widgets(|tester| {
16309 assert_eq!(
16310 measure_text(tester, with_affixes(), TextDirection::Rtl).width(),
16311 measure_text(tester, without_icons(), TextDirection::Rtl).width()
16312 );
16313 });
16314 }
16315
16316 fn intrinsic_width_of(
16317 tester: &mut WidgetTester,
16318 decoration: InputDecoration,
16319 is_empty: bool,
16320 child: WidgetRef,
16321 ) -> f64 {
16322 pump(
16323 tester,
16324 BuildInputDecorator::new(decoration)
16325 .use_intrinsic_width(true)
16326 .is_empty(is_empty)
16327 .child(child),
16328 );
16329 get_decorator_rect(tester).width()
16330 }
16331
16332 fn sized_width(width: f64) -> WidgetRef {
16333 SizedBox::new().width(width).into_widget()
16334 }
16335
16336 #[test]
16339 fn the_intrinsic_width_of_an_empty_decorator_is_the_wider_of_hint_and_content() {
16340 test_widgets(|tester| {
16341 let decoration_with_hint = || {
16342 InputDecoration::new()
16343 .content_padding(EdgeInsets::ZERO)
16344 .hint_text("Hint")
16345 };
16346 const HINT_TEXT_WIDTH: f64 = 66.0;
16347 const SMALL_CONTENT_WIDTH: f64 = 20.0;
16348 const LARGE_CONTENT_WIDTH: f64 = 80.0;
16349
16350 assert_eq!(
16352 intrinsic_width_of(
16353 tester,
16354 decoration_with_hint(),
16355 true,
16356 sized_width(SMALL_CONTENT_WIDTH)
16357 ),
16358 HINT_TEXT_WIDTH
16359 );
16360
16361 assert_eq!(
16363 intrinsic_width_of(
16364 tester,
16365 decoration_with_hint(),
16366 true,
16367 sized_width(LARGE_CONTENT_WIDTH)
16368 ),
16369 LARGE_CONTENT_WIDTH
16370 );
16371 });
16372 }
16373
16374 #[test]
16377 fn the_intrinsic_width_ignores_the_hint_when_not_empty_and_not_maintaining_its_size() {
16378 test_widgets(|tester| {
16379 let decoration_with_hint = InputDecoration::new()
16380 .content_padding(EdgeInsets::ZERO)
16381 .hint_text("Hint")
16382 .maintain_hint_size(false);
16383 const CONTENT_WIDTH: f64 = 20.0;
16384
16385 assert_eq!(
16387 intrinsic_width_of(
16388 tester,
16389 decoration_with_hint,
16390 false,
16391 sized_width(CONTENT_WIDTH)
16392 ),
16393 CONTENT_WIDTH
16394 );
16395 });
16396 }
16397
16398 #[test]
16401 fn the_intrinsic_width_keeps_the_hint_width_when_maintaining_its_size() {
16402 test_widgets(|tester| {
16403 let decoration_with_hint = InputDecoration::new()
16404 .content_padding(EdgeInsets::ZERO)
16405 .hint_text("Hint");
16406 const CONTENT_WIDTH: f64 = 20.0;
16407 const HINT_TEXT_WIDTH: f64 = 66.0;
16408 assert_eq!(
16409 intrinsic_width_of(
16410 tester,
16411 decoration_with_hint,
16412 false,
16413 sized_width(CONTENT_WIDTH)
16414 ),
16415 HINT_TEXT_WIDTH
16416 );
16417 });
16418 }
16419
16420 fn decoration_with_label(maintain_label_size: bool) -> InputDecoration {
16421 InputDecoration::new()
16422 .content_padding(EdgeInsets::ZERO)
16423 .label(SizedBox::new().width(30.0))
16424 .maintain_label_size(maintain_label_size)
16425 }
16426
16427 #[test]
16430 fn the_intrinsic_width_of_an_empty_decorator_ignores_the_label() {
16431 test_widgets(|tester| {
16432 assert_eq!(
16434 intrinsic_width_of(
16435 tester,
16436 decoration_with_label(false),
16437 true,
16438 SizedBox::shrink().into_widget()
16439 ),
16440 0.0
16441 );
16442 });
16443 }
16444
16445 #[test]
16448 fn the_intrinsic_width_of_an_empty_decorator_keeps_the_label_when_maintaining_its_size() {
16449 test_widgets(|tester| {
16450 assert_eq!(
16451 intrinsic_width_of(
16452 tester,
16453 decoration_with_label(true),
16454 true,
16455 SizedBox::shrink().into_widget()
16456 ),
16457 30.0
16458 );
16459 });
16460 }
16461
16462 #[test]
16465 fn the_intrinsic_width_ignores_the_label() {
16466 test_widgets(|tester| {
16467 assert_eq!(
16469 intrinsic_width_of(
16470 tester,
16471 decoration_with_label(false),
16472 false,
16473 sized_width(20.0)
16474 ),
16475 20.0
16476 );
16477 });
16478 }
16479
16480 #[test]
16483 fn the_intrinsic_width_keeps_the_label_when_maintaining_its_size() {
16484 test_widgets(|tester| {
16485 assert_eq!(
16486 intrinsic_width_of(
16487 tester,
16488 decoration_with_label(true),
16489 false,
16490 sized_width(20.0)
16491 ),
16492 30.0
16493 );
16494 });
16495 }
16496
16497 #[test]
16500 fn the_label_style_height_is_unchanged_when_focused() {
16501 test_widgets(|tester| {
16502 let build = |is_focused: bool| {
16503 MaterialApp::new()
16504 .home(
16505 Material::new().child(
16506 InputDecorator::new(InputDecoration::new().label_text("label"))
16507 .is_empty(true)
16508 .is_focused(is_focused)
16509 .child(input_text()),
16510 ),
16511 )
16512 .into_widget()
16513 };
16514 tester.pump_widget(build(false));
16515 let before_style = get_label_style(tester);
16516 tester.pump_widget(build(true));
16518 tester.pump_by(K_TRANSITION_DURATION);
16519
16520 assert_eq!(get_label_style(tester).height, before_style.height);
16521 });
16522 }
16523
16524 fn local(
16525 theme: InputDecorationThemeData,
16526 decoration: InputDecoration,
16527 ) -> BuildInputDecorator {
16528 BuildInputDecorator::new(decoration).local_input_decoration_theme(theme)
16529 }
16530
16531 fn indigo() -> Color {
16532 Colors::INDIGO.primary()
16533 }
16534
16535 fn indigo_style() -> TextStyle {
16536 TextStyle::new().color(indigo())
16537 }
16538
16539 #[test]
16541 fn a_local_theme_label_style_overrides_the_default() {
16542 test_widgets(|tester| {
16543 pump(
16544 tester,
16545 local(
16546 InputDecorationThemeData::new().label_style(indigo_style()),
16547 InputDecoration::new().label_text(LABEL_TEXT),
16548 ),
16549 );
16550 assert_eq!(style_color(&get_label_style(tester)), Some(indigo()));
16551 });
16552 }
16553
16554 #[test]
16557 fn a_local_theme_floating_label_style_overrides_the_default() {
16558 test_widgets(|tester| {
16559 pump(
16560 tester,
16561 local(
16562 InputDecorationThemeData::new().floating_label_style(indigo_style()),
16563 InputDecoration::new().label_text(LABEL_TEXT),
16564 )
16565 .is_focused(true), );
16567 assert_eq!(style_color(&get_label_style(tester)), Some(indigo()));
16568 });
16569 }
16570
16571 #[test]
16573 fn a_local_theme_helper_style_overrides_the_default() {
16574 test_widgets(|tester| {
16575 pump(
16576 tester,
16577 local(
16578 InputDecorationThemeData::new().helper_style(indigo_style()),
16579 InputDecoration::new()
16580 .label_text(LABEL_TEXT)
16581 .helper_text(HELPER_TEXT),
16582 ),
16583 );
16584 assert_eq!(
16585 style_color(&rich_text_style(tester, HELPER_TEXT)),
16586 Some(indigo())
16587 );
16588 });
16589 }
16590
16591 fn string_rect(tester: &WidgetTester, text: &str) -> waft_embedder::Rect {
16593 tester.get_rect(find::text(text))
16594 }
16595
16596 #[test]
16599 fn a_local_theme_helper_max_lines_overrides_the_default() {
16600 test_widgets(|tester| {
16601 const HELPER_MAX_LINES: i32 = 2;
16602 pump(
16603 tester,
16604 local(
16605 InputDecorationThemeData::new().helper_max_lines(HELPER_MAX_LINES),
16606 InputDecoration::new()
16607 .label_text(LABEL_TEXT)
16608 .helper_text(THREE_LINES),
16609 ),
16610 );
16611
16612 const HELPER_GAP: f64 = 4.0;
16613 const HELPER_HEIGHT: f64 = 16.0;
16614 const CONTAINER_HEIGHT: f64 = 56.0;
16615 let helper_rect = string_rect(tester, THREE_LINES);
16616 assert!(
16617 (helper_rect.height() - HELPER_HEIGHT * f64::from(HELPER_MAX_LINES)).abs()
16618 <= 0.25,
16619 "{helper_rect:?}"
16620 );
16621 let height = get_decorator_rect(tester).height();
16622 assert!(
16623 (height
16624 - (CONTAINER_HEIGHT
16625 + HELPER_GAP
16626 + HELPER_HEIGHT * f64::from(HELPER_MAX_LINES)))
16627 .abs()
16628 <= 0.25,
16629 "{height}"
16630 );
16631 });
16632 }
16633
16634 #[test]
16636 fn a_local_theme_hint_style_overrides_the_default() {
16637 test_widgets(|tester| {
16638 pump(
16639 tester,
16640 local(
16641 InputDecorationThemeData::new().hint_style(indigo_style()),
16642 InputDecoration::new()
16643 .label_text(LABEL_TEXT)
16644 .hint_text(HINT_TEXT),
16645 ),
16646 );
16647 assert_eq!(
16648 style_color(&rich_text_style(tester, HINT_TEXT)),
16649 Some(indigo())
16650 );
16651 });
16652 }
16653
16654 #[test]
16657 fn a_local_theme_hint_fade_duration_overrides_the_default() {
16658 test_widgets(|tester| {
16659 const HINT_FADE_DURATION: Duration = Duration::from_millis(404);
16660 let build = |is_empty: bool| {
16661 local(
16662 InputDecorationThemeData::new().hint_fade_duration(HINT_FADE_DURATION),
16663 InputDecoration::new().hint_text(HINT_TEXT),
16664 )
16665 .is_empty(is_empty)
16666 .build()
16667 };
16668
16669 tester.pump_widget(build(true));
16671
16672 assert_eq!(get_opacity(tester, HINT_TEXT), 1.0);
16674
16675 tester.pump_widget(build(false));
16677
16678 tester.pump_by(Duration::from_millis(200));
16681 let hint_opacity_200ms = get_opacity(tester, HINT_TEXT);
16682 assert!(hint_opacity_200ms > 0.0 && hint_opacity_200ms < 1.0);
16683 tester.pump_by(Duration::from_millis(200));
16684 let hint_opacity_400ms = get_opacity(tester, HINT_TEXT);
16685 assert!(hint_opacity_400ms > 0.0 && hint_opacity_400ms < hint_opacity_200ms);
16686 tester.pump_by(Duration::from_millis(4));
16687 assert_eq!(get_opacity(tester, HINT_TEXT), 0.0);
16688 });
16689 }
16690
16691 #[test]
16693 fn a_local_theme_hint_max_lines_overrides_the_default() {
16694 test_widgets(|tester| {
16695 const HINT_MAX_LINES: i32 = 2;
16696 pump(
16697 tester,
16698 local(
16699 InputDecorationThemeData::new().hint_max_lines(HINT_MAX_LINES),
16700 InputDecoration::collapsed(Some(THREE_LINES.to_owned())),
16701 ),
16702 );
16703
16704 const HINT_LINE_HEIGHT: f64 = 24.0; assert_eq!(
16706 get_decorator_rect(tester).size(),
16707 Size::new(800.0, f64::from(HINT_MAX_LINES) * HINT_LINE_HEIGHT)
16708 );
16709 });
16710 }
16711
16712 #[test]
16714 fn a_local_theme_error_style_overrides_the_default() {
16715 test_widgets(|tester| {
16716 pump(
16717 tester,
16718 local(
16719 InputDecorationThemeData::new().error_style(indigo_style()),
16720 InputDecoration::new()
16721 .label_text(LABEL_TEXT)
16722 .error_text(ERROR_TEXT),
16723 )
16724 .is_focused(true),
16725 );
16726 assert_eq!(
16727 style_color(&rich_text_style(tester, ERROR_TEXT)),
16728 Some(indigo())
16729 );
16730 });
16731 }
16732
16733 #[test]
16736 fn a_local_theme_error_max_lines_overrides_the_default() {
16737 test_widgets(|tester| {
16738 const ERROR_MAX_LINES: i32 = 2;
16739 pump(
16740 tester,
16741 local(
16742 InputDecorationThemeData::new().error_max_lines(ERROR_MAX_LINES),
16743 InputDecoration::new()
16744 .error_text(THREE_LINES)
16745 .label_text("label")
16746 .filled(true),
16747 ),
16748 );
16749
16750 const HELPER_GAP: f64 = 4.0;
16751 const CONTAINER_HEIGHT: f64 = 56.0;
16752 const ERROR_HEIGHT: f64 = 16.0;
16753
16754 let error_rect = string_rect(tester, THREE_LINES);
16755 assert!(
16756 (error_rect.height() - ERROR_HEIGHT * f64::from(ERROR_MAX_LINES)).abs() <= 0.25,
16757 "{error_rect:?}"
16758 );
16759 let height = get_decorator_rect(tester).height();
16760 assert!(
16761 (height
16762 - (CONTAINER_HEIGHT
16763 + HELPER_GAP
16764 + ERROR_HEIGHT * f64::from(ERROR_MAX_LINES)))
16765 .abs()
16766 <= 0.25,
16767 "{height}"
16768 );
16769 });
16770 }
16771
16772 #[test]
16775 fn a_local_theme_floating_label_behavior_overrides_the_default() {
16776 test_widgets(|tester| {
16777 pump(
16778 tester,
16779 local(
16780 InputDecorationThemeData::new()
16781 .floating_label_behavior(FloatingLabelBehavior::Never),
16782 InputDecoration::new().label_text(LABEL_TEXT),
16783 ),
16784 );
16785
16786 assert_eq!(get_decorator_rect(tester).size(), Size::new(800.0, 56.0));
16787 assert_eq!(get_label_rect(tester).top, 20.0);
16790 assert_eq!(get_label_rect(tester).bottom, 36.0);
16791 });
16792 }
16793
16794 #[test]
16797 fn a_local_theme_floating_label_alignment_overrides_the_default() {
16798 test_widgets(|tester| {
16799 pump(
16800 tester,
16801 BuildInputDecorator::new(
16802 InputDecoration::new()
16803 .content_padding(EdgeInsetsDirectional::only(40.0, 12.0, 0.0, 12.0))
16804 .icon(icon(Icons::insert_link()))
16805 .label_text(LABEL_TEXT)
16806 .hint_text(HINT_TEXT)
16807 .filled(true),
16808 )
16809 .local_input_decoration_theme(
16810 InputDecorationThemeData::new()
16811 .floating_label_alignment(FloatingLabelAlignment::CENTER),
16812 ),
16813 );
16814 assert_eq!(get_label_rect(tester).center().dx(), 420.0);
16816 });
16817 }
16818
16819 #[test]
16821 fn a_local_theme_is_dense_overrides_the_default() {
16822 test_widgets(|tester| {
16823 pump(
16824 tester,
16825 local(
16826 InputDecorationThemeData::new().is_dense(true),
16827 InputDecoration::new().label_text(LABEL_TEXT),
16828 ),
16829 );
16830 assert_eq!(get_container_rect(tester).height(), 48.0);
16831 });
16832 }
16833
16834 #[test]
16837 fn a_local_theme_content_padding_overrides_the_default() {
16838 test_widgets(|tester| {
16839 const START: f64 = 11.0;
16840 const TOP: f64 = 13.0;
16841 const END: f64 = 15.0;
16842 const BOTTOM: f64 = 17.0;
16843
16844 pump(
16845 tester,
16846 local(
16847 InputDecorationThemeData::new()
16848 .content_padding(EdgeInsetsDirectional::only(START, TOP, END, BOTTOM)),
16849 InputDecoration::new().label_text(LABEL_TEXT),
16850 ),
16851 );
16852
16853 const LABEL_HEIGHT: f64 = 16.0;
16854 const INPUT_HEIGHT: f64 = 24.0;
16855 assert_eq!(
16856 get_container_rect(tester).height(),
16857 TOP + LABEL_HEIGHT + INPUT_HEIGHT + BOTTOM
16858 );
16859 assert_eq!(tester.get_rect(find::text(INPUT_TEXT)).left, START);
16860 assert_eq!(tester.get_rect(find::text(INPUT_TEXT)).right, 800.0 - END);
16861 });
16862 }
16863
16864 #[test]
16866 fn a_local_theme_is_collapsed_overrides_the_default() {
16867 test_widgets(|tester| {
16868 const INPUT_HEIGHT: f64 = 24.0;
16871
16872 pump(
16873 tester,
16874 local(
16875 InputDecorationThemeData::new().is_collapsed(true),
16876 InputDecoration::new().label_text(LABEL_TEXT),
16877 )
16878 .is_focused(true),
16879 );
16880
16881 assert_eq!(
16882 get_decorator_rect(tester).size(),
16883 Size::new(800.0, INPUT_HEIGHT)
16884 );
16885 });
16886 }
16887
16888 #[test]
16890 fn a_local_theme_icon_color_overrides_the_default() {
16891 test_widgets(|tester| {
16892 pump(
16893 tester,
16894 local(
16895 InputDecorationThemeData::new().icon_color(indigo()),
16896 InputDecoration::new().icon(icon(Icons::cabin())),
16897 ),
16898 );
16899 assert_eq!(icon_theme_color(tester, &Icons::cabin()), Some(indigo()));
16900 });
16901 }
16902
16903 #[test]
16905 fn a_local_theme_prefix_style_overrides_the_default() {
16906 test_widgets(|tester| {
16907 const PREFIX_LABEL: &str = "prefix";
16908 pump(
16909 tester,
16910 local(
16911 InputDecorationThemeData::new().prefix_style(indigo_style()),
16912 InputDecoration::new().prefix(Text::new(PREFIX_LABEL)),
16913 )
16914 .is_focused(true),
16915 );
16916 assert_eq!(
16917 style_color(&rich_text_style(tester, PREFIX_LABEL)),
16918 Some(indigo())
16919 );
16920 });
16921 }
16922
16923 #[test]
16926 fn a_local_theme_prefix_icon_color_overrides_the_default() {
16927 test_widgets(|tester| {
16928 pump(
16929 tester,
16930 local(
16931 InputDecorationThemeData::new().prefix_icon_color(indigo()),
16932 InputDecoration::new().prefix_icon(icon(Icons::cabin())),
16933 ),
16934 );
16935 assert_eq!(icon_theme_color(tester, &Icons::cabin()), Some(indigo()));
16936 });
16937 }
16938
16939 #[test]
16942 fn a_local_theme_prefix_icon_constraints_override_the_default() {
16943 test_widgets(|tester| {
16944 pump(
16945 tester,
16946 local(
16947 InputDecorationThemeData::new().prefix_icon_constraints(BoxConstraints {
16948 min_width: 60.0,
16949 min_height: 60.0,
16950 ..BoxConstraints::new()
16951 }),
16952 InputDecoration::new().prefix_icon(icon(Icons::cabin())),
16953 ),
16954 );
16955 assert_eq!(icon_size(tester, &Icons::cabin()), Size::new(60.0, 60.0));
16956 });
16957 }
16958
16959 #[test]
16961 fn a_local_theme_suffix_style_overrides_the_default() {
16962 test_widgets(|tester| {
16963 const SUFFIX_LABEL: &str = "suffix";
16964 pump(
16965 tester,
16966 local(
16967 InputDecorationThemeData::new().suffix_style(indigo_style()),
16968 InputDecoration::new().suffix(Text::new(SUFFIX_LABEL)),
16969 )
16970 .is_focused(true),
16971 );
16972 assert_eq!(
16973 style_color(&rich_text_style(tester, SUFFIX_LABEL)),
16974 Some(indigo())
16975 );
16976 });
16977 }
16978
16979 #[test]
16982 fn a_local_theme_suffix_icon_color_overrides_the_default() {
16983 test_widgets(|tester| {
16984 pump(
16985 tester,
16986 local(
16987 InputDecorationThemeData::new().suffix_icon_color(indigo()),
16988 InputDecoration::new().suffix_icon(icon(Icons::cabin())),
16989 ),
16990 );
16991 assert_eq!(icon_theme_color(tester, &Icons::cabin()), Some(indigo()));
16992 });
16993 }
16994
16995 #[test]
16998 fn a_local_theme_suffix_icon_constraints_override_the_default() {
16999 test_widgets(|tester| {
17000 pump(
17001 tester,
17002 local(
17003 InputDecorationThemeData::new().suffix_icon_constraints(BoxConstraints {
17004 min_width: 60.0,
17005 min_height: 60.0,
17006 ..BoxConstraints::new()
17007 }),
17008 InputDecoration::new().suffix_icon(icon(Icons::cabin())),
17009 ),
17010 );
17011 assert_eq!(icon_size(tester, &Icons::cabin()), Size::new(60.0, 60.0));
17012 });
17013 }
17014
17015 #[test]
17018 fn a_local_theme_counter_style_overrides_the_default() {
17019 test_widgets(|tester| {
17020 pump(
17021 tester,
17022 local(
17023 InputDecorationThemeData::new().counter_style(indigo_style()),
17024 InputDecoration::new()
17025 .label_text(LABEL_TEXT)
17026 .counter_text(COUNTER_TEXT),
17027 ),
17028 );
17029 assert_eq!(
17030 style_color(&rich_text_style(tester, COUNTER_TEXT)),
17031 Some(indigo())
17032 );
17033 });
17034 }
17035
17036 #[test]
17039 fn a_local_theme_filled_and_fill_color_override_the_default() {
17040 test_widgets(|tester| {
17041 pump(
17042 tester,
17043 local(
17044 InputDecorationThemeData::new()
17045 .filled(true)
17046 .fill_color(indigo()),
17047 InputDecoration::new()
17048 .label_text(LABEL_TEXT)
17049 .counter_text(COUNTER_TEXT),
17050 ),
17051 );
17052 assert_paints(
17053 &border_painter_ops(tester),
17054 &[PaintCall::RRect {
17055 rrect: None,
17056 color: Some(indigo()),
17057 style: Some(PaintedStyle::Fill),
17058 has_mask_filter: None,
17059 }],
17060 );
17061 });
17062 }
17063
17064 #[test]
17067 fn a_local_theme_active_indicator_border_overrides_the_default() {
17068 test_widgets(|tester| {
17069 let active_indicator_border = BorderSide {
17070 color: Colors::AMBER.primary(),
17071 width: 2.0,
17072 ..BorderSide::default()
17073 };
17074 pump(
17075 tester,
17076 local(
17077 InputDecorationThemeData::new()
17078 .filled(true)
17079 .active_indicator_border(active_indicator_border),
17080 InputDecoration::new().label_text(LABEL_TEXT),
17081 ),
17082 );
17083
17084 assert_eq!(get_border_color(tester), active_indicator_border.color);
17085 assert_eq!(get_border_weight(tester), active_indicator_border.width);
17086 });
17087 }
17088
17089 #[test]
17091 fn a_local_theme_hover_color_overrides_the_default() {
17092 test_widgets(|tester| {
17093 pump(
17094 tester,
17095 local(
17096 InputDecorationThemeData::new()
17097 .filled(true)
17098 .hover_color(indigo()),
17099 InputDecoration::new()
17100 .label_text(LABEL_TEXT)
17101 .helper_text(HELPER_TEXT),
17102 )
17103 .is_focused(true)
17104 .is_hovering(true),
17105 );
17106
17107 let theme = {
17108 let decorator = tester.element(find::by_type::<InputDecorator>());
17109 let mut app = tester.cell.borrow_mut();
17110 Theme::of(&mut app, decorator)
17111 };
17112 let focus_color = theme.color_scheme.surface_container_highest();
17113 assert_paints(
17114 &border_painter_ops(tester),
17115 &[PaintCall::RRect {
17116 rrect: None,
17117 color: Some(Color::alpha_blend(indigo(), focus_color)),
17118 style: Some(PaintedStyle::Fill),
17119 has_mask_filter: None,
17120 }],
17121 );
17122 });
17123 }
17124
17125 fn settled(tester: &mut WidgetTester, decorator: BuildInputDecorator) {
17126 pump(tester, decorator);
17127 tester.pump_and_settle(); }
17129
17130 #[test]
17132 fn a_local_theme_error_border_overrides_the_default() {
17133 test_widgets(|tester| {
17134 let error_border = outline(Colors::RED.primary(), 1.5);
17135 settled(
17136 tester,
17137 local(
17138 InputDecorationThemeData::new().error_border(error_border),
17139 InputDecoration::new().error_text("error"),
17140 ),
17141 );
17142 assert!(border_is(tester, &error_border));
17143 });
17144 }
17145
17146 #[test]
17149 fn a_local_theme_focused_error_border_overrides_the_default() {
17150 test_widgets(|tester| {
17151 let focused_error_border = outline(Colors::RED.primary(), 1.5);
17152 settled(
17153 tester,
17154 local(
17155 InputDecorationThemeData::new().focused_error_border(focused_error_border),
17156 InputDecoration::new().error_text("error"),
17157 )
17158 .is_focused(true),
17159 );
17160 assert!(border_is(tester, &focused_error_border));
17161 });
17162 }
17163
17164 #[test]
17167 fn a_local_theme_focused_border_overrides_the_default() {
17168 test_widgets(|tester| {
17169 let focused_border = outline(Colors::GREEN.primary(), 1.5);
17170 settled(
17171 tester,
17172 local(
17173 InputDecorationThemeData::new().focused_border(focused_border),
17174 InputDecoration::new(),
17175 )
17176 .is_focused(true),
17177 );
17178 assert!(border_is(tester, &focused_border));
17179 });
17180 }
17181
17182 #[test]
17185 fn a_local_theme_enabled_border_overrides_the_default() {
17186 test_widgets(|tester| {
17187 let enabled_border = outline(Colors::TEAL.primary(), 5.0);
17188 settled(
17189 tester,
17190 local(
17191 InputDecorationThemeData::new().enabled_border(enabled_border),
17192 InputDecoration::new(),
17193 ),
17194 );
17195 assert!(border_is(tester, &enabled_border));
17196 });
17197 }
17198
17199 #[test]
17202 fn a_local_theme_disabled_border_overrides_the_default() {
17203 test_widgets(|tester| {
17204 let disabled_border = outline(Colors::TEAL.primary(), 3.0);
17205 settled(
17206 tester,
17207 local(
17208 InputDecorationThemeData::new().disabled_border(disabled_border),
17209 InputDecoration::new().enabled(false),
17210 ),
17211 );
17212 assert!(border_is(tester, &disabled_border));
17213 });
17214 }
17215
17216 #[test]
17218 fn a_local_theme_border_overrides_the_default() {
17219 test_widgets(|tester| {
17220 let border_radius = BorderRadius::all(Radius::circular(6.0));
17221 pump(
17222 tester,
17223 local(
17224 InputDecorationThemeData::new()
17225 .border(OutlineInputBorder::new().border_radius(border_radius)),
17226 InputDecoration::new(),
17227 ),
17228 );
17229
17230 assert!(
17233 get_border(tester)
17234 .expect("a border")
17235 .as_any()
17236 .is::<OutlineInputBorder>()
17237 );
17238 assert_eq!(get_border_radius(tester), Some(border_radius));
17239 });
17240 }
17241
17242 #[test]
17245 fn a_local_theme_align_label_with_hint_overrides_the_default() {
17246 test_widgets(|tester| {
17247 pump(
17248 tester,
17249 local(
17250 InputDecorationThemeData::new().align_label_with_hint(true),
17251 InputDecoration::new()
17252 .prefix_icon(icon(Icons::ac_unit()))
17253 .label_text(LABEL_TEXT)
17254 .border(OutlineInputBorder::new()),
17255 )
17256 .is_focused(true),
17257 );
17258 assert_eq!(
17259 get_label_rect(tester).left,
17260 tester.get_rect(find::text(INPUT_TEXT)).left
17261 );
17262 });
17263 }
17264
17265 #[test]
17267 fn local_theme_constraints_override_the_default() {
17268 test_widgets(|tester| {
17269 pump(
17270 tester,
17271 local(
17272 InputDecorationThemeData::new().constraints(BoxConstraints {
17273 max_width: 300.0,
17274 max_height: 40.0,
17275 ..BoxConstraints::new()
17276 }),
17277 InputDecoration::new().label_text(LABEL_TEXT),
17278 ),
17279 );
17280
17281 assert_eq!(get_decorator_rect(tester).size(), Size::new(300.0, 40.0));
17283 });
17284 }
17285
17286 #[test]
17289 fn a_local_theme_visual_density_overrides_the_default() {
17290 test_widgets(|tester| {
17291 pump(
17292 tester,
17293 local(
17294 InputDecorationThemeData::new()
17295 .visual_density(VisualDensity::new(2.0, 2.0)),
17296 InputDecoration::new()
17297 .label_text(LABEL_TEXT)
17298 .hint_text(HINT_TEXT),
17299 )
17300 .is_empty(true),
17301 );
17302
17303 assert_eq!(get_decorator_rect(tester).size().height(), 64.0);
17310 });
17311 }
17312
17313 #[test]
17315 fn the_focused_filled_helper_and_counter_are_styled_on_surface_variant() {
17316 test_widgets(|tester| {
17317 pump(
17318 tester,
17319 BuildInputDecorator::new(
17320 InputDecoration::new()
17321 .filled(true)
17322 .label_text(LABEL_TEXT)
17323 .helper_text(HELPER_TEXT)
17324 .counter_text(COUNTER_TEXT),
17325 )
17326 .is_focused(true),
17327 );
17328
17329 let theme = {
17330 let decorator = tester.element(find::by_type::<InputDecorator>());
17331 let mut app = tester.cell.borrow_mut();
17332 Theme::of(&mut app, decorator)
17333 };
17334 let expected_color = theme.color_scheme.on_surface_variant();
17335 let expected_style = theme
17336 .text_theme
17337 .body_small
17338 .clone()
17339 .expect("bodySmall")
17340 .copy_with()
17341 .color(expected_color);
17342 assert_eq!(rich_text_style(tester, HELPER_TEXT), expected_style);
17343 assert_eq!(rich_text_style(tester, COUNTER_TEXT), expected_style);
17344 });
17345 }
17346
17347 fn property<T: Clone + std::fmt::Debug + PartialEq + 'static>(
17348 value: T,
17349 ) -> WidgetStatePropertyRef<Option<T>> {
17350 Rc::new(WidgetStatePropertyAll::new(Some(value)))
17351 }
17352
17353 fn check_icon_button_inherits_icon_button_theme(
17355 tester: &mut WidgetTester,
17356 prefix: bool,
17357 button_icon: IconData,
17358 ) {
17359 let background_color = Color::new(0xffff0000);
17360 let foreground_color = Color::new(0xff00ff00);
17361 let overlay_color = Color::new(0xff0000ff);
17362 let shadow_color = Color::new(0xff0ff0ff);
17363 const ELEVATION: f64 = 4.0;
17364 let shape = RoundedRectangleBorder::new(
17365 BorderSide::NONE,
17366 BorderRadius::all(Radius::circular(10.0)).into(),
17367 );
17368 let icon_button_style = ButtonStyle::new()
17369 .background_color(property(background_color))
17370 .foreground_color(property(foreground_color))
17371 .overlay_color(property(overlay_color))
17372 .shadow_color(property(shadow_color))
17373 .elevation(property(ELEVATION))
17374 .shape(property(OutlinedBorderRef::new(shape)));
17375
17376 let button = IconButton::new(Some(noop()), icon(button_icon.clone()));
17377 let decoration = if prefix {
17378 InputDecoration::new().prefix_icon(button)
17379 } else {
17380 InputDecoration::new().suffix_icon(button)
17381 };
17382 tester.pump_widget(
17383 IconButtonTheme::new(
17384 IconButtonThemeData::new().style(icon_button_style),
17385 BuildInputDecorator::new(decoration).build(),
17386 )
17387 .into_widget(),
17388 );
17389
17390 let material = tester.element(
17391 find::descendant(
17392 find::ancestor(find::by_icon(&button_icon), find::by_type::<IconButton>())
17393 .first(),
17394 find::by_type::<Material>(),
17395 )
17396 .first(),
17397 );
17398 tester.widget(material, |material: &Material| {
17399 assert_eq!(material.color, Some(background_color));
17400 assert_eq!(material.shadow_color, Some(shadow_color));
17401 assert_eq!(material.elevation, ELEVATION);
17402 assert!(material.shape.as_ref().expect("a shape").eq_shape(&shape));
17403 });
17404
17405 assert_eq!(
17406 icon_glyph_color(tester, &button_icon),
17407 Some(foreground_color)
17408 );
17409
17410 let center = { tester.get_center(find::by_icon(&button_icon)) };
17411 let mut gesture = TestPointer::mouse(1, 0);
17412 tester.add_pointer(&mut gesture, Offset::ZERO);
17413 tester.move_to(&mut gesture, center, false);
17414 tester.pump_and_settle();
17415 let ink = tester.first::<crate::material::RenderInkFeatures>();
17416 assert_paints(
17417 &tester.painted_ops(waft_rendering::RenderBox::as_object(ink)),
17418 &[PaintCall::rect(Some(overlay_color))],
17419 );
17420 }
17421
17422 #[test]
17424 fn a_prefix_icon_button_inherits_the_icon_button_theme() {
17425 test_widgets(|tester| {
17426 check_icon_button_inherits_icon_button_theme(tester, true, Icons::person())
17427 });
17428 }
17429
17430 #[test]
17432 fn a_suffix_icon_button_inherits_the_icon_button_theme() {
17433 test_widgets(|tester| {
17434 check_icon_button_inherits_icon_button_theme(tester, false, Icons::delete())
17435 });
17436 }
17437
17438 fn check_icon_button_color_respects_foreground_states(
17441 tester: &mut WidgetTester,
17442 prefix: bool,
17443 button_icon: IconData,
17444 ) {
17445 let icon_error_color = Color::new(0xffff0000);
17446 let icon_color = Color::new(0xff00ff00);
17447 let icon_button_style =
17448 ButtonStyle::new().foreground_color(
17449 <dyn WidgetStateProperty<Option<Color>>>::resolve_with(move |states| {
17450 if states.contains(&WidgetState::Error) {
17451 return Some(icon_error_color);
17452 }
17453 Some(icon_color)
17454 }),
17455 );
17456 let build = |error_text: Option<&str>| {
17457 let button = IconButton::new(Some(noop()), icon(button_icon.clone()));
17458 let mut decoration = if prefix {
17459 InputDecoration::new().prefix_icon(button)
17460 } else {
17461 InputDecoration::new().suffix_icon(button)
17462 };
17463 if let Some(error_text) = error_text {
17464 decoration = decoration.error_text(error_text);
17465 }
17466 BuildInputDecorator::new(decoration)
17467 .icon_button_theme(IconButtonThemeData::new().style(icon_button_style.clone()))
17468 .build()
17469 };
17470
17471 tester.pump_widget(build(Some("error")));
17473
17474 assert_eq!(
17475 icon_glyph_color(tester, &button_icon),
17476 Some(icon_error_color)
17477 );
17478
17479 tester.pump_widget(build(None));
17481 tester.pump_and_settle();
17482
17483 assert_eq!(icon_glyph_color(tester, &button_icon), Some(icon_color));
17484 }
17485
17486 #[test]
17489 fn a_prefix_icon_button_colour_follows_the_icon_button_theme_foreground_states() {
17490 test_widgets(|tester| {
17491 check_icon_button_color_respects_foreground_states(tester, true, Icons::person())
17492 });
17493 }
17494
17495 #[test]
17498 fn a_suffix_icon_button_colour_follows_the_icon_button_theme_foreground_states() {
17499 test_widgets(|tester| {
17500 check_icon_button_color_respects_foreground_states(tester, false, Icons::search())
17501 });
17502 }
17503
17504 #[test]
17506 fn a_decoration_shows_its_custom_focused_border() {
17507 test_widgets(|tester| {
17508 let focused_border = outline(Colors::TEAL.primary(), 5.0);
17509 settled(
17510 tester,
17511 BuildInputDecorator::new(InputDecoration::new().focused_border(focused_border))
17512 .is_focused(true),
17513 );
17514 assert!(border_is(tester, &focused_border));
17515 });
17516 }
17517
17518 #[test]
17520 fn a_decoration_shows_its_custom_error_borders() {
17521 test_widgets(|tester| {
17522 let error_border = outline(Colors::RED.primary(), 1.5);
17523 let focused_error_border = outline(Colors::TEAL.primary(), 5.0);
17524
17525 let mut check_borders =
17526 |is_focused: bool,
17527 enabled: bool,
17528 error_text: Option<&str>,
17529 error: Option<WidgetRef>,
17530 expected_border: &OutlineInputBorder| {
17531 let mut decoration = InputDecoration::new()
17532 .enabled(enabled)
17533 .error_border(error_border)
17534 .focused_error_border(focused_error_border);
17535 if let Some(error_text) = error_text {
17536 decoration = decoration.error_text(error_text);
17537 }
17538 if let Some(error) = error {
17539 decoration = decoration.error(error);
17540 }
17541 tester.pump_widget(
17542 BuildInputDecorator::new(decoration)
17543 .is_focused(is_focused)
17544 .build(),
17545 );
17546 tester.pump_and_settle(); assert!(border_is(tester, expected_border));
17548 };
17549
17550 check_borders(true, true, Some("error"), None, &focused_error_border);
17552 check_borders(false, true, Some("error"), None, &error_border);
17553 check_borders(false, false, Some("error"), None, &error_border);
17554
17555 let error = || Text::new("error").into_widget();
17557 check_borders(true, true, None, Some(error()), &focused_error_border);
17558 check_borders(false, true, None, Some(error()), &error_border);
17559 check_borders(false, false, None, Some(error()), &error_border);
17560 });
17561 }
17562
17563 #[test]
17565 fn a_decoration_shows_its_custom_enabled_border() {
17566 test_widgets(|tester| {
17567 let enabled_border = outline(Colors::TEAL.primary(), 5.0);
17568 settled(
17569 tester,
17570 BuildInputDecorator::new(InputDecoration::new().enabled_border(enabled_border)),
17571 );
17572 assert!(border_is(tester, &enabled_border));
17573 });
17574 }
17575
17576 #[test]
17579 fn a_decoration_shows_its_custom_disabled_border() {
17580 test_widgets(|tester| {
17581 let disabled_border = outline(Colors::TEAL.primary(), 3.0);
17582 settled(
17583 tester,
17584 BuildInputDecorator::new(
17585 InputDecoration::new()
17586 .enabled(false)
17587 .disabled_border(disabled_border),
17588 ),
17589 );
17590 assert!(border_is(tester, &disabled_border));
17591 });
17592 }
17593
17594 #[test]
17596 fn a_decoration_describes_its_children_by_slot() {
17597 test_widgets(|tester| {
17598 tester.pump_widget(
17599 BuildInputDecorator::new(
17600 InputDecoration::new()
17601 .icon(Text::new("icon"))
17602 .label_text("label")
17603 .hint_text("hint")
17604 .prefix_text("prefix")
17605 .suffix_text("suffix")
17606 .prefix_icon(Text::new("prefixIcon"))
17607 .suffix_icon(Text::new("suffixIcon"))
17608 .helper_text("helper")
17609 .counter_text("counter"),
17610 )
17611 .child(Text::new("text"))
17612 .build(),
17613 );
17614
17615 let decoration = tester
17618 .render_object(find::by_type::<InputDecorator>())
17619 .downcast::<RenderDecoration>(&tester.cell.borrow())
17620 .expect("the decorator's render object");
17621 let names: Vec<String> = {
17622 let app = tester.cell.borrow();
17623 decoration
17624 .as_object()
17625 .to_diagnostics_node(&app, None)
17626 .children()
17627 .iter()
17628 .map(|node| node.name().expect("a named child").to_owned())
17629 .collect()
17630 };
17631 assert_eq!(
17632 names,
17633 [
17634 "icon",
17635 "input",
17636 "prefixIcon",
17637 "suffixIcon",
17638 "prefix",
17639 "suffix",
17640 "label",
17641 "hint",
17642 "helperError",
17643 "counter",
17644 "container",
17645 ]
17646 );
17647 });
17648 }
17649 }
17650
17651 mod material2_and_layout {
17654 use waft_embedder::{Color, Offset, Rect, Size};
17655 use waft_painting::{
17656 BorderRadius, BorderSide, EdgeInsets, EdgeInsetsGeometry, TextAlignVertical,
17657 TextDirection, TextStyle,
17658 };
17659 use waft_rendering::BoxConstraints;
17660 use waft_widgets::testing::{WidgetTester, test_widgets};
17661 use waft_widgets::{
17662 AnyElement, Builder, Directionality, FadeTransition, GlobalKey, Icon, IntoWidget,
17663 IntrinsicWidth, Padding, SizedBox, WidgetRef, downcast_widget,
17664 };
17665
17666 use super::super::*;
17667 use super::common::*;
17668
17669 use crate::colors::Colors;
17670 use crate::icons::Icons;
17671 use crate::input_border::{InputBorderRef, OutlineInputBorder, UnderlineInputBorder};
17672 use crate::material::Material;
17673 use crate::test_support::material_app;
17674 use crate::text_theme::TextTheme;
17675 use crate::theme::Theme;
17676 use crate::theme_data::{ThemeData, VisualDensity};
17677
17678 const K_MIN_INTERACTIVE_DIMENSION: f64 = 48.0;
17679
17680 struct M2 {
17683 decoration: InputDecoration,
17684 theme: Option<fn(&App) -> ThemeData>,
17685 input_decoration_theme: Option<InputDecorationThemeData>,
17686 text_direction: TextDirection,
17687 expands: bool,
17688 is_empty: bool,
17689 is_focused: bool,
17690 is_hovering: bool,
17691 use_intrinsic_width: bool,
17692 base_style: Option<TextStyle>,
17693 text_align_vertical: Option<TextAlignVertical>,
17694 visual_density: Option<VisualDensity>,
17695 child: Option<WidgetRef>,
17696 }
17697
17698 #[allow(clippy::wrong_self_convention)]
17700 impl M2 {
17701 fn new(decoration: InputDecoration) -> M2 {
17702 M2 {
17703 decoration,
17704 theme: None,
17705 input_decoration_theme: None,
17706 text_direction: TextDirection::Ltr,
17707 expands: false,
17708 is_empty: false,
17709 is_focused: false,
17710 is_hovering: false,
17711 use_intrinsic_width: false,
17712 base_style: None,
17713 text_align_vertical: None,
17714 visual_density: None,
17715 child: None,
17716 }
17717 }
17718
17719 fn theme(mut self, theme: fn(&App) -> ThemeData) -> M2 {
17720 self.theme = Some(theme);
17721 self
17722 }
17723
17724 fn input_decoration_theme(mut self, theme: InputDecorationThemeData) -> M2 {
17725 self.input_decoration_theme = Some(theme);
17726 self
17727 }
17728
17729 fn text_direction(mut self, text_direction: TextDirection) -> M2 {
17730 self.text_direction = text_direction;
17731 self
17732 }
17733
17734 fn expands(mut self, expands: bool) -> M2 {
17735 self.expands = expands;
17736 self
17737 }
17738
17739 fn is_empty(mut self, is_empty: bool) -> M2 {
17740 self.is_empty = is_empty;
17741 self
17742 }
17743
17744 fn is_focused(mut self, is_focused: bool) -> M2 {
17745 self.is_focused = is_focused;
17746 self
17747 }
17748
17749 fn is_hovering(mut self, is_hovering: bool) -> M2 {
17750 self.is_hovering = is_hovering;
17751 self
17752 }
17753
17754 fn use_intrinsic_width(mut self, use_intrinsic_width: bool) -> M2 {
17755 self.use_intrinsic_width = use_intrinsic_width;
17756 self
17757 }
17758
17759 fn base_style(mut self, base_style: TextStyle) -> M2 {
17760 self.base_style = Some(base_style);
17761 self
17762 }
17763
17764 fn text_align_vertical(mut self, text_align_vertical: TextAlignVertical) -> M2 {
17765 self.text_align_vertical = Some(text_align_vertical);
17766 self
17767 }
17768
17769 fn visual_density(mut self, visual_density: VisualDensity) -> M2 {
17770 self.visual_density = Some(visual_density);
17771 self
17772 }
17773
17774 fn child<K>(mut self, child: impl IntoWidget<K>) -> M2 {
17775 self.child = Some(child.into_widget());
17776 self
17777 }
17778
17779 fn pump(self, tester: &mut WidgetTester) {
17781 let widget = self.build(tester);
17782 tester.pump_widget(widget);
17783 }
17784
17785 fn build(self, tester: &WidgetTester) -> WidgetRef {
17787 let mut decorator = InputDecorator::new(self.decoration)
17788 .expands(self.expands)
17789 .is_empty(self.is_empty)
17790 .is_focused(self.is_focused)
17791 .is_hovering(self.is_hovering)
17792 .child(self.child.unwrap_or_else(m2_input_text));
17793 if let Some(base_style) = self.base_style {
17794 decorator = decorator.base_style(base_style);
17795 }
17796 if let Some(text_align_vertical) = self.text_align_vertical {
17797 decorator = decorator.text_align_vertical(text_align_vertical);
17798 }
17799 let mut widget = decorator.into_widget();
17800 if self.use_intrinsic_width {
17801 widget = IntrinsicWidth::new().child(widget).into_widget();
17802 }
17803
17804 let theme = self.theme;
17805 let input_decoration_theme = self.input_decoration_theme;
17806 let visual_density = self.visual_density;
17807 let text_direction = self.text_direction;
17808 let home = Material::new().child(Builder::new(move |app, context| {
17809 let base = match theme {
17810 Some(theme) => Rc::new(theme(app)),
17811 None => Theme::of(app, context),
17812 };
17813 let mut copy = base.copy_with();
17814 if let Some(input_decoration_theme) = &input_decoration_theme {
17815 copy = copy.input_decoration_theme(input_decoration_theme.clone());
17816 }
17817 if let Some(visual_density) = visual_density {
17818 copy = copy.visual_density(visual_density);
17819 }
17820 copy = copy
17821 .text_theme(TextTheme::new().body_large(TextStyle::new().font_size(16.0)));
17822 Theme::new(
17823 copy.build(),
17824 waft_widgets::Align::new()
17825 .alignment(waft_painting::AlignmentGeometry::Alignment(
17826 waft_painting::Alignment::TOP_LEFT,
17827 ))
17828 .child(Directionality::new(text_direction, widget.clone())),
17829 )
17830 .into_widget()
17831 }));
17832 let app_theme = m2(&tester.cell.borrow());
17833 material_app(app_theme, home.into_widget())
17834 }
17835 }
17836
17837 fn m2_input_text() -> WidgetRef {
17839 Text::new(INPUT_TEXT)
17840 .style(TextStyle::new().font_size(16.0))
17841 .into_widget()
17842 }
17843
17844 fn top(tester: &WidgetTester, text: &str) -> f64 {
17845 tester.get_rect(find::text(text)).top
17846 }
17847
17848 fn bottom(tester: &WidgetTester, text: &str) -> f64 {
17849 tester.get_rect(find::text(text)).bottom
17850 }
17851
17852 fn decorator_size(tester: &WidgetTester) -> Size {
17853 get_decorator_rect(tester).size()
17854 }
17855
17856 fn key_rect(tester: &WidgetTester, key: &GlobalKey) -> Rect {
17857 let render = tester.render_object(key).as_box().expect("a render box");
17858 tester.get_rect(render)
17859 }
17860
17861 fn sized(key: &GlobalKey, width: f64, height: f64) -> WidgetRef {
17862 SizedBox::new()
17863 .key(key.clone().into())
17864 .width(width)
17865 .height(height)
17866 .into_widget()
17867 }
17868
17869 fn icon_rect(tester: &WidgetTester, icon: &waft_widgets::IconData) -> Rect {
17870 tester.get_rect(find::by_icon(icon))
17871 }
17872
17873 fn get_opacity(tester: &WidgetTester, text: &str) -> f64 {
17876 let element = tester.element(find::text(text));
17877 let fade = tester
17878 .ancestors(element)
17879 .into_iter()
17880 .find(|ancestor: &AnyElement| {
17881 let app = tester.cell.borrow();
17882 downcast_widget::<FadeTransition>(&**ancestor.widget(&app)).is_some()
17883 })
17884 .expect("a FadeTransition above the text");
17885 let opacity = tester.widget(fade, |fade: &FadeTransition| fade.opacity.clone());
17886 opacity.value(&tester.cell.borrow())
17887 }
17888
17889 fn in_exclusive_range(value: f64, low: f64, high: f64) -> bool {
17890 low < value && value < high
17891 }
17892
17893 #[test]
17895 fn material2_input_and_label_text_layout() {
17896 test_widgets(|tester| {
17897 M2::new(InputDecoration::new().label_text(LABEL_TEXT)).pump(tester);
17899 tester.pump_and_settle();
17900
17901 assert_eq!(decorator_size(tester), Size::new(800.0, 56.0));
17908 assert_eq!(top(tester, INPUT_TEXT), 28.0);
17909 assert_eq!(bottom(tester, INPUT_TEXT), 44.0);
17910 assert_eq!(top(tester, LABEL_TEXT), 12.0);
17911 assert_eq!(bottom(tester, LABEL_TEXT), 24.0);
17912 assert_eq!(get_border_bottom(tester), 56.0);
17913 assert_eq!(get_border_weight(tester), 1.0);
17914
17915 M2::new(InputDecoration::new().label_text(LABEL_TEXT))
17917 .is_empty(true)
17918 .pump(tester);
17919 tester.pump_and_settle();
17920 assert_eq!(top(tester, LABEL_TEXT), 20.0);
17921
17922 M2::new(
17925 InputDecoration::new()
17926 .label_text(LABEL_TEXT)
17927 .floating_label_behavior(FloatingLabelBehavior::Always),
17928 )
17929 .is_empty(true)
17930 .pump(tester);
17931 tester.pump_and_settle();
17932 assert_eq!(top(tester, LABEL_TEXT), 12.0);
17933
17934 M2::new(
17937 InputDecoration::new()
17938 .label_text(LABEL_TEXT)
17939 .floating_label_behavior(FloatingLabelBehavior::Never),
17940 )
17941 .pump(tester);
17942 tester.pump_and_settle();
17943 assert_eq!(top(tester, LABEL_TEXT), 20.0);
17944
17945 M2::new(InputDecoration::new().label_text(LABEL_TEXT))
17948 .is_focused(true)
17949 .pump(tester);
17950 tester.pump_and_settle();
17951 assert_eq!(decorator_size(tester), Size::new(800.0, 56.0));
17952 assert_eq!(top(tester, INPUT_TEXT), 28.0);
17953 assert_eq!(bottom(tester, INPUT_TEXT), 44.0);
17954 assert_eq!(top(tester, LABEL_TEXT), 12.0);
17955 assert_eq!(bottom(tester, LABEL_TEXT), 24.0);
17956 assert_eq!(get_border_bottom(tester), 56.0);
17957 assert_eq!(get_border_weight(tester), 2.0);
17958
17959 M2::new(InputDecoration::new().label_text(LABEL_TEXT))
17961 .is_empty(true)
17962 .pump(tester);
17963
17964 tester.pump_by(Duration::from_millis(50));
17967 let label_y_50ms = top(tester, LABEL_TEXT);
17968 assert!(
17969 in_exclusive_range(label_y_50ms, 12.0, 20.0),
17970 "{label_y_50ms}"
17971 );
17972 tester.pump_by(Duration::from_millis(50));
17973 let label_y_100ms = top(tester, LABEL_TEXT);
17974 assert!(
17975 in_exclusive_range(label_y_100ms, label_y_50ms, 20.0),
17976 "{label_y_100ms}"
17977 );
17978 tester.pump_and_settle();
17979 assert_eq!(decorator_size(tester), Size::new(800.0, 56.0));
17980 assert_eq!(top(tester, INPUT_TEXT), 28.0);
17981 assert_eq!(bottom(tester, INPUT_TEXT), 44.0);
17982 assert_eq!(top(tester, LABEL_TEXT), 20.0);
17983 assert_eq!(bottom(tester, LABEL_TEXT), 36.0);
17984 assert_eq!(get_border_bottom(tester), 56.0);
17985 assert_eq!(get_border_weight(tester), 1.0);
17986
17987 M2::new(InputDecoration::new().label_text(LABEL_TEXT))
17989 .is_empty(true)
17990 .is_focused(true)
17991 .pump(tester);
17992
17993 tester.pump_by(Duration::from_millis(50));
17996 let label_y_50ms = top(tester, LABEL_TEXT);
17997 assert!(
17998 in_exclusive_range(label_y_50ms, 12.0, 28.0),
17999 "{label_y_50ms}"
18000 );
18001 tester.pump_by(Duration::from_millis(50));
18002 let label_y_100ms = top(tester, LABEL_TEXT);
18003 assert!(
18004 in_exclusive_range(label_y_100ms, 12.0, label_y_50ms),
18005 "{label_y_100ms}"
18006 );
18007
18008 tester.pump_and_settle();
18009 assert_eq!(decorator_size(tester), Size::new(800.0, 56.0));
18010 assert_eq!(top(tester, INPUT_TEXT), 28.0);
18011 assert_eq!(bottom(tester, INPUT_TEXT), 44.0);
18012 assert_eq!(top(tester, LABEL_TEXT), 12.0);
18013 assert_eq!(bottom(tester, LABEL_TEXT), 24.0);
18014 assert_eq!(get_border_bottom(tester), 56.0);
18015 assert_eq!(get_border_weight(tester), 2.0);
18016
18017 M2::new(InputDecoration::new().label_text(LABEL_TEXT).enabled(false))
18020 .is_empty(true)
18021 .pump(tester);
18022 tester.pump_and_settle();
18023 assert_eq!(decorator_size(tester), Size::new(800.0, 56.0));
18024 assert_eq!(top(tester, INPUT_TEXT), 28.0);
18025 assert_eq!(bottom(tester, INPUT_TEXT), 44.0);
18026 assert_eq!(top(tester, LABEL_TEXT), 20.0);
18027 assert_eq!(bottom(tester, LABEL_TEXT), 36.0);
18028 assert_eq!(get_border_weight(tester), 0.0);
18029
18030 M2::new(
18033 InputDecoration::new()
18034 .label_text(LABEL_TEXT)
18035 .enabled(false)
18036 .filled(true),
18037 )
18038 .is_empty(true)
18039 .pump(tester);
18040 tester.pump_and_settle();
18041 assert_eq!(decorator_size(tester), Size::new(800.0, 56.0));
18042 assert_eq!(top(tester, INPUT_TEXT), 28.0);
18043 assert_eq!(bottom(tester, INPUT_TEXT), 44.0);
18044 assert_eq!(top(tester, LABEL_TEXT), 20.0);
18045 assert_eq!(bottom(tester, LABEL_TEXT), 36.0);
18046 assert_eq!(get_border_color(tester), Colors::TRANSPARENT);
18047
18048 M2::new(
18051 InputDecoration::new()
18052 .label_text(LABEL_TEXT)
18053 .align_label_with_hint(true)
18054 .hint_text(HINT_TEXT),
18055 )
18056 .is_empty(true)
18057 .pump(tester);
18058 tester.pump_and_settle();
18059 assert_eq!(decorator_size(tester), Size::new(800.0, 56.0));
18060 assert_eq!(top(tester, LABEL_TEXT), top(tester, HINT_TEXT));
18061 assert_eq!(bottom(tester, LABEL_TEXT), bottom(tester, HINT_TEXT));
18062 });
18063 }
18064
18065 fn pump_floating_label(tester: &mut WidgetTester, is_focused: bool) {
18066 M2::new(
18067 InputDecoration::new()
18068 .label_text(LABEL_TEXT)
18069 .floating_label_behavior(FloatingLabelBehavior::Auto),
18070 )
18071 .is_empty(true)
18072 .is_focused(is_focused)
18073 .pump(tester);
18074 }
18075
18076 #[test]
18079 fn material2_floating_label_animation_duration_and_curve() {
18080 test_widgets(|tester| {
18081 pump_floating_label(tester, false);
18082 assert_eq!(top(tester, LABEL_TEXT), 20.0);
18083
18084 pump_floating_label(tester, true);
18087 tester.pump_by(Duration::from_millis(42));
18088 assert_more_or_less_equals_within(top(tester, LABEL_TEXT), 18.06, 0.5);
18089 tester.pump_by(Duration::from_millis(42));
18090 assert_more_or_less_equals_within(top(tester, LABEL_TEXT), 13.78, 0.5);
18091 tester.pump_by(Duration::from_millis(42));
18092 assert_more_or_less_equals_within(top(tester, LABEL_TEXT), 12.31, 0.5);
18093 tester.pump_by(Duration::from_millis(41));
18094 assert_eq!(top(tester, LABEL_TEXT), 12.0);
18095
18096 tester.pump_and_settle();
18101
18102 pump_floating_label(tester, false);
18105 tester.pump_by(Duration::from_millis(42));
18106 assert_more_or_less_equals_within(top(tester, LABEL_TEXT), 13.94, 0.5);
18107 tester.pump_by(Duration::from_millis(42));
18108 assert_more_or_less_equals_within(top(tester, LABEL_TEXT), 18.22, 0.5);
18109 tester.pump_by(Duration::from_millis(42));
18110 assert_more_or_less_equals_within(top(tester, LABEL_TEXT), 19.69, 0.5);
18111 tester.pump_by(Duration::from_millis(41));
18112 assert_eq!(top(tester, LABEL_TEXT), 20.0);
18113 });
18114 }
18115
18116 #[test]
18118 fn material2_input_and_hint_layout() {
18119 test_widgets(|tester| {
18120 M2::new(InputDecoration::new().hint_text(HINT_TEXT))
18122 .is_empty(true)
18123 .pump(tester);
18124
18125 assert_eq!(
18126 decorator_size(tester),
18127 Size::new(800.0, K_MIN_INTERACTIVE_DIMENSION)
18128 );
18129 assert_eq!(top(tester, INPUT_TEXT), 16.0);
18130 assert_eq!(bottom(tester, INPUT_TEXT), 32.0);
18131 assert_eq!(top(tester, HINT_TEXT), 16.0);
18132 assert_eq!(bottom(tester, HINT_TEXT), 32.0);
18133 assert_eq!(get_border_bottom(tester), 48.0);
18134 assert_eq!(get_border_weight(tester), 1.0);
18135
18136 assert_eq!(
18137 tester.get_rect(find::text(HINT_TEXT)).width(),
18138 tester.get_rect(find::text(INPUT_TEXT)).width()
18139 );
18140 });
18141 }
18142
18143 fn label_and_hint() -> InputDecoration {
18144 InputDecoration::new()
18145 .label_text(LABEL_TEXT)
18146 .hint_text(HINT_TEXT)
18147 }
18148
18149 #[test]
18151 fn material2_input_label_and_hint_layout() {
18152 test_widgets(|tester| {
18153 M2::new(label_and_hint()).is_empty(true).pump(tester);
18155
18156 assert_eq!(decorator_size(tester), Size::new(800.0, 56.0));
18158 assert_eq!(top(tester, INPUT_TEXT), 28.0);
18159 assert_eq!(bottom(tester, INPUT_TEXT), 44.0);
18160 assert_eq!(top(tester, LABEL_TEXT), 20.0);
18161 assert_eq!(bottom(tester, LABEL_TEXT), 36.0);
18162 assert_eq!(get_opacity(tester, HINT_TEXT), 0.0);
18163 assert_eq!(get_border_bottom(tester), 56.0);
18164 assert_eq!(get_border_weight(tester), 1.0);
18165
18166 M2::new(label_and_hint())
18168 .is_empty(true)
18169 .is_focused(true)
18170 .pump(tester);
18171
18172 tester.pump_by(Duration::from_millis(9));
18175 let hint_opacity_9ms = get_opacity(tester, HINT_TEXT);
18176 assert!(in_exclusive_range(hint_opacity_9ms, 0.0, 1.0));
18177 tester.pump_by(Duration::from_millis(9));
18178 let hint_opacity_18ms = get_opacity(tester, HINT_TEXT);
18179 assert!(in_exclusive_range(hint_opacity_18ms, hint_opacity_9ms, 1.0));
18180
18181 tester.pump_and_settle();
18182 assert_eq!(decorator_size(tester), Size::new(800.0, 56.0));
18183 assert_eq!(top(tester, INPUT_TEXT), 28.0);
18184 assert_eq!(bottom(tester, INPUT_TEXT), 44.0);
18185 assert_eq!(top(tester, LABEL_TEXT), 12.0);
18186 assert_eq!(bottom(tester, LABEL_TEXT), 24.0);
18187 assert_eq!(top(tester, HINT_TEXT), 28.0);
18188 assert_eq!(bottom(tester, HINT_TEXT), 44.0);
18189 assert_eq!(get_opacity(tester, HINT_TEXT), 1.0);
18190 assert_eq!(get_border_bottom(tester), 56.0);
18191 assert_eq!(get_border_weight(tester), 2.0);
18192
18193 M2::new(label_and_hint()).is_focused(true).pump(tester);
18194
18195 tester.pump_by(Duration::from_millis(9));
18198 let hint_opacity_9ms = get_opacity(tester, HINT_TEXT);
18199 assert!(in_exclusive_range(hint_opacity_9ms, 0.0, 1.0));
18200 tester.pump_by(Duration::from_millis(9));
18201 let hint_opacity_18ms = get_opacity(tester, HINT_TEXT);
18202 assert!(in_exclusive_range(hint_opacity_18ms, 0.0, hint_opacity_9ms));
18203
18204 tester.pump_and_settle();
18205 assert_eq!(decorator_size(tester), Size::new(800.0, 56.0));
18206 assert_eq!(top(tester, INPUT_TEXT), 28.0);
18207 assert_eq!(bottom(tester, INPUT_TEXT), 44.0);
18208 assert_eq!(top(tester, LABEL_TEXT), 12.0);
18209 assert_eq!(bottom(tester, LABEL_TEXT), 24.0);
18210 assert_eq!(top(tester, HINT_TEXT), 28.0);
18211 assert_eq!(bottom(tester, HINT_TEXT), 44.0);
18212 assert_eq!(get_opacity(tester, HINT_TEXT), 0.0);
18213 assert_eq!(get_border_bottom(tester), 56.0);
18214 assert_eq!(get_border_weight(tester), 2.0);
18215 });
18216 }
18217
18218 #[test]
18220 fn material2_input_label_and_hint_dense_layout() {
18221 test_widgets(|tester| {
18222 M2::new(label_and_hint().is_dense(true))
18224 .is_empty(true)
18225 .pump(tester);
18226
18227 assert_eq!(decorator_size(tester), Size::new(800.0, 48.0));
18229 assert_eq!(top(tester, INPUT_TEXT), 24.0);
18230 assert_eq!(bottom(tester, INPUT_TEXT), 40.0);
18231 assert_eq!(top(tester, LABEL_TEXT), 16.0);
18232 assert_eq!(bottom(tester, LABEL_TEXT), 32.0);
18233 assert_eq!(get_opacity(tester, HINT_TEXT), 0.0);
18234 assert_eq!(get_border_bottom(tester), 48.0);
18235 assert_eq!(get_border_weight(tester), 1.0);
18236
18237 M2::new(label_and_hint().is_dense(true))
18239 .is_empty(true)
18240 .is_focused(true)
18241 .pump(tester);
18242 tester.pump_and_settle();
18243 assert_eq!(decorator_size(tester), Size::new(800.0, 48.0));
18244 assert_eq!(top(tester, INPUT_TEXT), 24.0);
18245 assert_eq!(bottom(tester, INPUT_TEXT), 40.0);
18246 assert_eq!(top(tester, LABEL_TEXT), 8.0);
18247 assert_eq!(bottom(tester, LABEL_TEXT), 20.0);
18248 assert_eq!(get_opacity(tester, HINT_TEXT), 1.0);
18249 assert_eq!(get_border_bottom(tester), 48.0);
18250 assert_eq!(get_border_weight(tester), 2.0);
18251 });
18252 }
18253
18254 fn check_hint_fade(tester: &mut WidgetTester, build: impl Fn(bool) -> M2, step: Duration) {
18257 build(false).pump(tester);
18258
18259 assert_eq!(get_opacity(tester, HINT_TEXT), 0.0);
18261
18262 build(true).pump(tester);
18264
18265 tester.pump_by(step);
18267 let first = get_opacity(tester, HINT_TEXT);
18268 assert!(in_exclusive_range(first, 0.0, 1.0), "{first}");
18269 tester.pump_by(step);
18270 let second = get_opacity(tester, HINT_TEXT);
18271 assert!(in_exclusive_range(second, first, 1.0), "{second}");
18272 tester.pump_by(step);
18273 assert_eq!(get_opacity(tester, HINT_TEXT), 1.0);
18274
18275 build(false).pump(tester);
18277
18278 tester.pump_by(step);
18280 let first = get_opacity(tester, HINT_TEXT);
18281 assert!(in_exclusive_range(first, 0.0, 1.0), "{first}");
18282 tester.pump_by(step);
18283 let second = get_opacity(tester, HINT_TEXT);
18284 assert!(in_exclusive_range(second, 0.0, first), "{second}");
18285 tester.pump_by(step);
18286 assert_eq!(get_opacity(tester, HINT_TEXT), 0.0);
18287 }
18288
18289 #[test]
18291 fn material2_default_hint_animation_duration() {
18292 test_widgets(|tester| {
18293 check_hint_fade(
18294 tester,
18295 |is_focused| {
18296 M2::new(label_and_hint())
18297 .is_empty(true)
18298 .is_focused(is_focused)
18299 },
18300 Duration::from_millis(9),
18301 );
18302 });
18303 }
18304
18305 #[test]
18307 fn material2_custom_hint_animation_duration() {
18308 test_widgets(|tester| {
18309 check_hint_fade(
18310 tester,
18311 |is_focused| {
18312 M2::new(label_and_hint().hint_fade_duration(Duration::from_millis(120)))
18313 .is_empty(true)
18314 .is_focused(is_focused)
18315 },
18316 Duration::from_millis(50),
18317 );
18318 });
18319 }
18320
18321 #[test]
18324 fn material2_custom_hint_animation_duration_from_theme() {
18325 test_widgets(|tester| {
18326 check_hint_fade(
18327 tester,
18328 |is_focused| {
18329 M2::new(label_and_hint())
18330 .input_decoration_theme(
18331 InputDecorationThemeData::new()
18332 .hint_fade_duration(Duration::from_millis(120)),
18333 )
18334 .is_empty(true)
18335 .is_focused(is_focused)
18336 },
18337 Duration::from_millis(50),
18338 );
18339 });
18340 }
18341
18342 #[test]
18344 fn material2_decorator_with_no_input_border_has_a_zero_weight_border() {
18345 test_widgets(|tester| {
18346 M2::new(InputDecoration::new().border(InputBorderRef::none()))
18347 .is_empty(true)
18348 .pump(tester);
18349 assert_eq!(get_border_weight(tester), 0.0);
18350 });
18351 }
18352
18353 #[test]
18355 fn material2_error_helper_and_counter_layout() {
18356 test_widgets(|tester| {
18357 M2::new(
18358 InputDecoration::new()
18359 .label_text(LABEL_TEXT)
18360 .helper_text(HELPER_TEXT)
18361 .counter_text(COUNTER_TEXT)
18362 .filled(true),
18363 )
18364 .is_empty(true)
18365 .pump(tester);
18366
18367 assert_eq!(decorator_size(tester), Size::new(800.0, 76.0));
18369 assert_eq!(top(tester, INPUT_TEXT), 28.0);
18370 assert_eq!(bottom(tester, INPUT_TEXT), 44.0);
18371 assert_eq!(top(tester, LABEL_TEXT), 20.0);
18372 assert_eq!(bottom(tester, LABEL_TEXT), 36.0);
18373 assert_eq!(get_border_bottom(tester), 56.0);
18374 assert_eq!(get_border_weight(tester), 1.0);
18375 assert_eq!(
18376 tester.get_rect(find::text(HELPER_TEXT)).top_left(),
18377 Offset::new(12.0, 64.0)
18378 );
18379 assert_eq!(
18380 tester.get_rect(find::text(COUNTER_TEXT)).top_right(),
18381 Offset::new(788.0, 64.0)
18382 );
18383
18384 M2::new(
18386 InputDecoration::new()
18387 .label_text(LABEL_TEXT)
18388 .error_text(ERROR_TEXT)
18389 .helper_text(HELPER_TEXT)
18390 .counter_text(COUNTER_TEXT)
18391 .filled(true),
18392 )
18393 .pump(tester);
18394 tester.pump_and_settle();
18395
18396 assert_eq!(decorator_size(tester), Size::new(800.0, 76.0));
18398 assert_eq!(top(tester, INPUT_TEXT), 28.0);
18399 assert_eq!(bottom(tester, INPUT_TEXT), 44.0);
18400 assert_eq!(top(tester, LABEL_TEXT), 12.0);
18401 assert_eq!(bottom(tester, LABEL_TEXT), 24.0);
18402 assert_eq!(get_border_bottom(tester), 56.0);
18403 assert_eq!(get_border_weight(tester), 1.0);
18404 assert_eq!(
18405 tester.get_rect(find::text(ERROR_TEXT)).top_left(),
18406 Offset::new(12.0, 64.0)
18407 );
18408 assert_eq!(
18409 tester.get_rect(find::text(COUNTER_TEXT)).top_right(),
18410 Offset::new(788.0, 64.0)
18411 );
18412 expect(tester, &find::text(HELPER_TEXT), finds_nothing());
18413
18414 M2::new(
18417 InputDecoration::new()
18418 .is_dense(true)
18419 .label_text(LABEL_TEXT)
18420 .error_text(ERROR_TEXT)
18421 .helper_text(HELPER_TEXT)
18422 .counter_text(COUNTER_TEXT)
18423 .filled(true),
18424 )
18425 .pump(tester);
18426 tester.pump_and_settle();
18427
18428 assert_eq!(decorator_size(tester), Size::new(800.0, 68.0));
18430 assert_eq!(top(tester, INPUT_TEXT), 24.0);
18431 assert_eq!(bottom(tester, INPUT_TEXT), 40.0);
18432 assert_eq!(top(tester, LABEL_TEXT), 8.0);
18433 assert_eq!(bottom(tester, LABEL_TEXT), 20.0);
18434 assert_eq!(get_border_bottom(tester), 48.0);
18435 assert_eq!(get_border_weight(tester), 1.0);
18436 assert_eq!(
18437 tester.get_rect(find::text(ERROR_TEXT)).top_left(),
18438 Offset::new(12.0, 56.0)
18439 );
18440 assert_eq!(
18441 tester.get_rect(find::text(COUNTER_TEXT)).top_right(),
18442 Offset::new(788.0, 56.0)
18443 );
18444
18445 M2::new(
18446 InputDecoration::new()
18447 .is_dense(true)
18448 .label_text(LABEL_TEXT)
18449 .error_text(ERROR_TEXT)
18450 .helper_text(HELPER_TEXT)
18451 .counter_text(COUNTER_TEXT)
18452 .filled(true),
18453 )
18454 .is_empty(true)
18455 .pump(tester);
18456 tester.pump_and_settle();
18457
18458 assert_eq!(decorator_size(tester), Size::new(800.0, 68.0));
18460 assert_eq!(top(tester, INPUT_TEXT), 24.0);
18461 assert_eq!(bottom(tester, INPUT_TEXT), 40.0);
18462 assert_eq!(top(tester, LABEL_TEXT), 16.0);
18463 assert_eq!(bottom(tester, LABEL_TEXT), 32.0);
18464 assert_eq!(get_border_bottom(tester), 48.0);
18465 assert_eq!(get_border_weight(tester), 1.0);
18466 assert_eq!(
18467 tester.get_rect(find::text(ERROR_TEXT)).top_left(),
18468 Offset::new(12.0, 56.0)
18469 );
18470 assert_eq!(
18471 tester.get_rect(find::text(COUNTER_TEXT)).top_right(),
18472 Offset::new(788.0, 56.0)
18473 );
18474 });
18475 }
18476
18477 fn error_border() -> InputBorderRef {
18478 InputBorderRef::new(OutlineInputBorder::new().border_side(BorderSide {
18479 color: Colors::RED.primary(),
18480 width: 1.5,
18481 ..BorderSide::default()
18482 }))
18483 }
18484
18485 fn focused_error_border() -> InputBorderRef {
18486 InputBorderRef::new(OutlineInputBorder::new().border_side(BorderSide {
18487 color: Colors::TEAL.primary(),
18488 width: 5.0,
18489 ..BorderSide::default()
18490 }))
18491 }
18492
18493 #[test]
18496 fn material2_error_border_shows_for_error_text_and_error_widget() {
18497 test_widgets(|tester| {
18498 let with_text = || {
18499 InputDecoration::new()
18500 .error_text(ERROR_TEXT)
18501 .error_border(error_border())
18502 .focused_error_border(focused_error_border())
18503 };
18504 let with_widget = || {
18505 InputDecoration::new()
18506 .error(waft_widgets::Text::new(ERROR_TEXT))
18507 .error_border(error_border())
18508 .focused_error_border(focused_error_border())
18509 };
18510 for decoration in [with_text as fn() -> InputDecoration, with_widget] {
18511 M2::new(decoration()).is_focused(true).pump(tester);
18512 tester.pump_and_settle(); assert!(*get_border(tester).unwrap() == *focused_error_border());
18514
18515 M2::new(decoration()).pump(tester);
18516 tester.pump_and_settle(); assert!(*get_border(tester).unwrap() == *error_border());
18518
18519 M2::new(decoration().enabled(false)).pump(tester);
18520 tester.pump_and_settle(); assert!(*get_border(tester).unwrap() == *error_border());
18522 }
18523 });
18524 }
18525
18526 #[test]
18528 fn material2_prefix_and_suffix_texts() {
18529 test_widgets(|tester| {
18530 M2::new(
18531 InputDecoration::new()
18532 .prefix_text("p")
18533 .suffix_text("s")
18534 .filled(true),
18535 )
18536 .pump(tester);
18537
18538 assert_eq!(
18541 decorator_size(tester),
18542 Size::new(800.0, K_MIN_INTERACTIVE_DIMENSION)
18543 );
18544 assert_eq!(tester.get_rect(find::text(INPUT_TEXT)).height(), 16.0);
18545 assert_eq!(tester.get_rect(find::text("p")).height(), 16.0);
18546 assert_eq!(tester.get_rect(find::text("s")).height(), 16.0);
18547 assert_eq!(top(tester, INPUT_TEXT), 16.0);
18548 assert_eq!(top(tester, "p"), 16.0);
18549 assert_eq!(tester.get_rect(find::text("p")).left, 12.0);
18550 assert_eq!(top(tester, "s"), 16.0);
18551 assert_eq!(tester.get_rect(find::text("s")).right, 788.0);
18552
18553 assert_eq!(tester.get_rect(find::text("p")).left, 12.0);
18555 assert!(
18556 tester.get_rect(find::text("p")).right
18557 <= tester.get_rect(find::text(INPUT_TEXT)).left
18558 );
18559 assert!(
18560 tester.get_rect(find::text(INPUT_TEXT)).right
18561 <= tester.get_rect(find::text("s")).left
18562 );
18563 });
18564 }
18565
18566 #[test]
18568 fn material2_icon_prefix_and_suffix() {
18569 test_widgets(|tester| {
18570 M2::new(
18571 InputDecoration::new()
18572 .prefix_text("p")
18573 .suffix_text("s")
18574 .icon(Icon::new(Some(Icons::android())))
18575 .filled(true),
18576 )
18577 .pump(tester);
18578
18579 assert_eq!(
18580 decorator_size(tester),
18581 Size::new(800.0, K_MIN_INTERACTIVE_DIMENSION)
18582 );
18583 assert_eq!(tester.get_rect(find::text(INPUT_TEXT)).height(), 16.0);
18584 assert_eq!(tester.get_rect(find::text("p")).height(), 16.0);
18585 assert_eq!(tester.get_rect(find::text("s")).height(), 16.0);
18586 assert_eq!(top(tester, INPUT_TEXT), 16.0);
18587 assert_eq!(top(tester, "p"), 16.0);
18588 assert_eq!(top(tester, "s"), 16.0);
18589 assert_eq!(tester.get_rect(find::text("s")).right, 788.0);
18590 let icon = icon_rect(tester, &Icons::android());
18591 assert_eq!(icon.height(), 24.0);
18592
18593 assert_eq!(icon.top, 12.0);
18595
18596 assert_eq!(icon.left, 0.0);
18598 assert!(icon.right <= tester.get_rect(find::text("p")).left);
18599 assert!(
18600 tester.get_rect(find::text("p")).right
18601 <= tester.get_rect(find::text(INPUT_TEXT)).left
18602 );
18603 assert!(
18604 tester.get_rect(find::text(INPUT_TEXT)).right
18605 <= tester.get_rect(find::text("s")).left
18606 );
18607 });
18608 }
18609
18610 #[test]
18612 fn material2_prefix_and_suffix_widgets() {
18613 test_widgets(|tester| {
18614 let p_key = GlobalKey::new();
18615 let s_key = GlobalKey::new();
18616 M2::new(
18617 InputDecoration::new()
18618 .prefix(
18619 Padding::new(EdgeInsetsGeometry::all(4.0))
18620 .key(p_key.clone().into())
18621 .child(Text::new("p")),
18622 )
18623 .suffix(
18624 Padding::new(EdgeInsetsGeometry::all(4.0))
18625 .key(s_key.clone().into())
18626 .child(Text::new("s")),
18627 )
18628 .filled(true),
18629 )
18630 .pump(tester);
18631
18632 assert_eq!(decorator_size(tester), Size::new(800.0, 48.0));
18640 assert_eq!(tester.get_rect(find::text(INPUT_TEXT)).height(), 16.0);
18641 assert_eq!(key_rect(tester, &p_key).height(), 24.0);
18642 assert_eq!(tester.get_rect(find::text("p")).height(), 16.0);
18643 assert_eq!(key_rect(tester, &s_key).height(), 24.0);
18644 assert_eq!(tester.get_rect(find::text("s")).height(), 16.0);
18645 assert_eq!(top(tester, INPUT_TEXT), 16.0);
18646 assert_eq!(key_rect(tester, &p_key).top, 12.0);
18647 assert_eq!(top(tester, "p"), 16.0);
18648 assert_eq!(key_rect(tester, &s_key).top, 12.0);
18649 assert_eq!(top(tester, "s"), 16.0);
18650 assert_eq!(key_rect(tester, &s_key).right, 788.0);
18651 assert_eq!(tester.get_rect(find::text("s")).right, 784.0);
18652
18653 assert_eq!(key_rect(tester, &p_key).left, 12.0);
18655 assert_eq!(
18656 key_rect(tester, &p_key).right,
18657 tester.get_rect(find::text(INPUT_TEXT)).left
18658 );
18659 assert!(
18660 tester.get_rect(find::text(INPUT_TEXT)).right <= key_rect(tester, &s_key).right
18661 );
18662 });
18663 }
18664
18665 #[test]
18667 fn material2_tall_prefix() {
18668 test_widgets(|tester| {
18669 let p_key = GlobalKey::new();
18670 M2::new(
18671 InputDecoration::new()
18672 .prefix(sized(&p_key, 10.0, 100.0))
18673 .filled(true),
18674 )
18675 .child(
18677 Text::new(INPUT_TEXT)
18678 .style(TextStyle::new().font_size(20.0))
18679 .into_widget(),
18680 )
18681 .pump(tester);
18682
18683 assert_eq!(decorator_size(tester).width(), 800.0);
18684 assert_eq!(decorator_size(tester).height(), 129.0);
18685 assert_eq!(tester.get_rect(find::text(INPUT_TEXT)).height(), 20.0);
18686 assert_eq!(key_rect(tester, &p_key).height(), 100.0);
18687 assert_eq!(top(tester, INPUT_TEXT), 97.0); assert_eq!(key_rect(tester, &p_key).top, 12.0);
18689
18690 assert_eq!(key_rect(tester, &p_key).left, 12.0);
18692 assert_eq!(
18693 key_rect(tester, &p_key).right,
18694 tester.get_rect(find::text(INPUT_TEXT)).left
18695 );
18696 });
18697 }
18698
18699 #[test]
18701 fn material2_tall_prefix_with_border() {
18702 test_widgets(|tester| {
18703 let p_key = GlobalKey::new();
18704 M2::new(
18705 InputDecoration::new()
18706 .border(OutlineInputBorder::new())
18707 .prefix(sized(&p_key, 10.0, 100.0))
18708 .filled(true),
18709 )
18710 .child(
18712 Text::new(INPUT_TEXT)
18713 .style(TextStyle::new().font_size(20.0))
18714 .into_widget(),
18715 )
18716 .pump(tester);
18717
18718 assert_eq!(decorator_size(tester).width(), 800.0);
18724 assert_eq!(decorator_size(tester).height(), 145.0);
18725 assert_eq!(tester.get_rect(find::text(INPUT_TEXT)).height(), 20.0);
18726 assert_eq!(key_rect(tester, &p_key).height(), 100.0);
18727 assert_eq!(top(tester, INPUT_TEXT), 105.0);
18728 assert_eq!(key_rect(tester, &p_key).top, 20.0);
18729
18730 assert_eq!(key_rect(tester, &p_key).left, 12.0);
18732 assert_eq!(
18733 key_rect(tester, &p_key).right,
18734 tester.get_rect(find::text(INPUT_TEXT)).left
18735 );
18736 });
18737 }
18738
18739 #[test]
18741 fn material2_prefix_icon_and_suffix_icon() {
18742 test_widgets(|tester| {
18743 M2::new(
18744 InputDecoration::new()
18745 .prefix_icon(Icon::new(Some(Icons::pages())))
18746 .suffix_icon(Icon::new(Some(Icons::satellite())))
18747 .filled(true),
18748 )
18749 .pump(tester);
18750
18751 assert_eq!(decorator_size(tester), Size::new(800.0, 48.0));
18754 assert_eq!(tester.get_rect(find::text(INPUT_TEXT)).height(), 16.0);
18755 let pages = icon_rect(tester, &Icons::pages());
18756 let satellite = icon_rect(tester, &Icons::satellite());
18757 assert_eq!(pages.height(), 48.0);
18758 assert_eq!(satellite.height(), 48.0);
18759 assert_eq!(top(tester, INPUT_TEXT), 12.0);
18760 assert_eq!(pages.top, 0.0);
18761 assert_eq!(satellite.top, 0.0);
18762 assert_eq!(satellite.right, 800.0);
18763
18764 assert_eq!(pages.left, 0.0);
18766 assert!(pages.right <= tester.get_rect(find::text(INPUT_TEXT)).left);
18767 assert!(tester.get_rect(find::text(INPUT_TEXT)).right <= satellite.left);
18768 });
18769 }
18770
18771 #[test]
18774 fn material2_prefix_icon_and_suffix_icon_constraints() {
18775 test_widgets(|tester| {
18776 M2::new(
18777 InputDecoration::new()
18778 .prefix_icon(Icon::new(Some(Icons::pages())))
18779 .prefix_icon_constraints(BoxConstraints {
18780 min_width: 32.0,
18781 min_height: 32.0,
18782 ..BoxConstraints::new()
18783 })
18784 .suffix_icon(Icon::new(Some(Icons::satellite())))
18785 .suffix_icon_constraints(BoxConstraints {
18786 min_width: 25.0,
18787 min_height: 25.0,
18788 ..BoxConstraints::new()
18789 })
18790 .is_dense(true), )
18792 .pump(tester);
18793
18794 assert_eq!(decorator_size(tester), Size::new(800.0, 32.0));
18797 assert_eq!(tester.get_rect(find::text(INPUT_TEXT)).height(), 16.0);
18798 let pages = icon_rect(tester, &Icons::pages());
18799 let satellite = icon_rect(tester, &Icons::satellite());
18800 assert_eq!(pages.height(), 32.0);
18801 assert_eq!(satellite.height(), 25.0);
18802
18803 assert_eq!(top(tester, INPUT_TEXT), (32.0 - 16.0) / 2.0);
18805 assert_eq!(pages.top, 0.0);
18807 assert_eq!(satellite.top, (32.0 - 25.0) / 2.0);
18809 assert_eq!(satellite.right, 800.0);
18810 });
18811 }
18812
18813 #[test]
18816 fn material2_prefix_and_suffix_icons_are_centered_when_smaller_than_48_by_48() {
18817 test_widgets(|tester| {
18818 let prefix_key = GlobalKey::new();
18819 M2::new(
18820 InputDecoration::new()
18821 .prefix_icon(Padding::new(EdgeInsetsGeometry::all(16.0)).child(sized(
18822 &prefix_key,
18823 8.0,
18824 8.0,
18825 )))
18826 .filled(true),
18827 )
18828 .pump(tester);
18829
18830 assert_eq!(decorator_size(tester), Size::new(800.0, 48.0));
18831 assert_eq!(key_rect(tester, &prefix_key).height(), 16.0);
18832 assert_eq!(key_rect(tester, &prefix_key).top, 16.0);
18833 });
18834 }
18835
18836 struct DensityFrame {
18839 height: f64,
18840 input_top: f64,
18841 label_top: f64,
18842 floating_label_top: f64,
18843 }
18844
18845 fn check_visual_density(
18846 tester: &mut WidgetTester,
18847 density: VisualDensity,
18848 frame: DensityFrame,
18849 ) {
18850 let build = |is_empty: bool, is_focused: bool| {
18851 M2::new(label_and_hint())
18852 .is_empty(is_empty)
18853 .is_focused(is_focused)
18854 .visual_density(density)
18855 };
18856 build(true, false).pump(tester);
18858
18859 assert_eq!(decorator_size(tester), Size::new(800.0, frame.height));
18861 assert_eq!(top(tester, INPUT_TEXT), frame.input_top);
18862 assert_eq!(bottom(tester, INPUT_TEXT), frame.input_top + 16.0);
18863 assert_eq!(top(tester, LABEL_TEXT), frame.label_top);
18864 assert_eq!(bottom(tester, LABEL_TEXT), frame.label_top + 16.0);
18865 assert_eq!(get_opacity(tester, HINT_TEXT), 0.0);
18866 assert_eq!(get_border_bottom(tester), frame.height);
18867 assert_eq!(get_border_weight(tester), 1.0);
18868
18869 build(true, true).pump(tester);
18871
18872 tester.pump_by(Duration::from_millis(9));
18875 let hint_opacity_9ms = get_opacity(tester, HINT_TEXT);
18876 assert!(in_exclusive_range(hint_opacity_9ms, 0.0, 1.0));
18877 tester.pump_by(Duration::from_millis(9));
18878 let hint_opacity_18ms = get_opacity(tester, HINT_TEXT);
18879 assert!(in_exclusive_range(hint_opacity_18ms, hint_opacity_9ms, 1.0));
18880
18881 tester.pump_and_settle();
18882 let check_focused = |tester: &WidgetTester, hint_opacity: f64| {
18883 assert_eq!(decorator_size(tester), Size::new(800.0, frame.height));
18884 assert_eq!(top(tester, INPUT_TEXT), frame.input_top);
18885 assert_eq!(bottom(tester, INPUT_TEXT), frame.input_top + 16.0);
18886 assert_eq!(top(tester, LABEL_TEXT), frame.floating_label_top);
18887 assert_eq!(bottom(tester, LABEL_TEXT), frame.floating_label_top + 12.0);
18888 assert_eq!(top(tester, HINT_TEXT), frame.input_top);
18889 assert_eq!(bottom(tester, HINT_TEXT), frame.input_top + 16.0);
18890 assert_eq!(get_opacity(tester, HINT_TEXT), hint_opacity);
18891 assert_eq!(get_border_bottom(tester), frame.height);
18892 assert_eq!(get_border_weight(tester), 2.0);
18893 };
18894 check_focused(tester, 1.0);
18895
18896 build(false, true).pump(tester);
18897
18898 tester.pump_by(Duration::from_millis(9));
18901 let hint_opacity_9ms = get_opacity(tester, HINT_TEXT);
18902 assert!(in_exclusive_range(hint_opacity_9ms, 0.0, 1.0));
18903 tester.pump_by(Duration::from_millis(9));
18904 let hint_opacity_18ms = get_opacity(tester, HINT_TEXT);
18905 assert!(in_exclusive_range(hint_opacity_18ms, 0.0, hint_opacity_9ms));
18906
18907 tester.pump_and_settle();
18908 check_focused(tester, 0.0);
18909 }
18910
18911 #[test]
18913 fn material2_decorator_respects_reduced_theme_visual_density() {
18914 test_widgets(|tester| {
18915 check_visual_density(
18916 tester,
18917 VisualDensity::COMPACT,
18918 DensityFrame {
18919 height: 48.0,
18920 input_top: 24.0,
18921 label_top: 16.0,
18922 floating_label_top: 8.0,
18923 },
18924 );
18925 });
18926 }
18927
18928 #[test]
18931 fn material2_decorator_respects_increased_theme_visual_density() {
18932 test_widgets(|tester| {
18933 check_visual_density(
18934 tester,
18935 VisualDensity::new(2.0, 2.0),
18936 DensityFrame {
18937 height: 64.0,
18938 input_top: 32.0,
18939 label_top: 24.0,
18940 floating_label_top: 16.0,
18941 },
18942 );
18943 });
18944 }
18945
18946 #[test]
18949 fn material2_prefix_and_suffix_icons_increase_height_when_larger_than_48_by_48() {
18950 test_widgets(|tester| {
18951 let prefix_key = GlobalKey::new();
18952 M2::new(
18953 InputDecoration::new()
18954 .prefix_icon(sized(&prefix_key, 100.0, 100.0))
18955 .filled(true),
18956 )
18957 .pump(tester);
18958
18959 assert_eq!(decorator_size(tester), Size::new(800.0, 100.0));
18960 assert_eq!(key_rect(tester, &prefix_key).height(), 100.0);
18961 assert_eq!(key_rect(tester, &prefix_key).top, 0.0);
18962 });
18963 }
18964
18965 #[test]
18967 fn material2_no_constraints_fill_the_width_at_the_default_height() {
18968 test_widgets(|tester| {
18969 M2::new(InputDecoration::new()).pump(tester);
18970
18971 assert_eq!(decorator_size(tester), Size::new(800.0, 48.0));
18973 });
18974 }
18975
18976 fn theme_with_constraints(app: &App) -> ThemeData {
18977 ThemeData::builder()
18978 .input_decoration_theme(InputDecorationThemeData::new().constraints(
18979 BoxConstraints {
18980 max_width: 300.0,
18981 max_height: 40.0,
18982 ..BoxConstraints::new()
18983 },
18984 ))
18985 .build(app)
18986 }
18987
18988 #[test]
18990 fn material2_theme_constraints_size_the_decorator() {
18991 test_widgets(|tester| {
18992 M2::new(InputDecoration::new())
18993 .theme(theme_with_constraints)
18994 .pump(tester);
18995
18996 assert_eq!(decorator_size(tester), Size::new(300.0, 40.0));
18998 });
18999 }
19000
19001 #[test]
19003 fn material2_decoration_constraints_override_the_theme() {
19004 test_widgets(|tester| {
19005 M2::new(InputDecoration::new().constraints(BoxConstraints {
19006 max_width: 200.0,
19007 max_height: 32.0,
19008 ..BoxConstraints::new()
19009 }))
19010 .theme(theme_with_constraints)
19011 .pump(tester);
19012
19013 assert_eq!(decorator_size(tester), Size::new(200.0, 32.0));
19016 });
19017 }
19018
19019 fn text_20() -> WidgetRef {
19022 Text::new(INPUT_TEXT)
19023 .style(TextStyle::new().font_size(20.0))
19024 .into_widget()
19025 }
19026
19027 fn expanded_input_top(
19030 tester: &mut WidgetTester,
19031 decoration: InputDecoration,
19032 align: TextAlignVertical,
19033 ) -> f64 {
19034 M2::new(decoration)
19035 .expands(true) .text_align_vertical(align)
19037 .child(text_20())
19038 .pump(tester);
19039 top(tester, INPUT_TEXT)
19040 }
19041
19042 #[test]
19045 fn material2_text_align_vertical_top_puts_the_input_at_the_top() {
19046 test_widgets(|tester| {
19047 assert_eq!(
19049 expanded_input_top(
19050 tester,
19051 InputDecoration::new().filled(true),
19052 TextAlignVertical::TOP
19053 ),
19054 12.0
19055 );
19056 });
19057 }
19058
19059 #[test]
19062 fn material2_text_align_vertical_center_centers_the_input() {
19063 test_widgets(|tester| {
19064 assert_eq!(
19066 expanded_input_top(
19067 tester,
19068 InputDecoration::new().filled(true),
19069 TextAlignVertical::CENTER
19070 ),
19071 290.0
19072 );
19073 });
19074 }
19075
19076 #[test]
19079 fn material2_text_align_vertical_bottom_puts_the_input_at_the_bottom() {
19080 test_widgets(|tester| {
19081 assert_eq!(
19083 expanded_input_top(
19084 tester,
19085 InputDecoration::new().filled(true),
19086 TextAlignVertical::BOTTOM
19087 ),
19088 568.0
19089 );
19090 });
19091 }
19092
19093 #[test]
19096 fn material2_text_align_vertical_as_a_double_interpolates() {
19097 test_widgets(|tester| {
19098 assert_eq!(
19100 expanded_input_top(
19101 tester,
19102 InputDecoration::new().filled(true),
19103 TextAlignVertical::new(0.75)
19104 ),
19105 498.5
19106 );
19107 });
19108 }
19109
19110 #[test]
19113 fn material2_text_align_vertical_works_with_density_and_content_padding() {
19114 test_widgets(|tester| {
19115 let key = GlobalKey::new();
19116 let container_key = GlobalKey::new();
19117 const TOTAL_HEIGHT: f64 = 100.0;
19118 const CHILD_HEIGHT: f64 = 20.0;
19119 let visual_density = VisualDensity::new(0.0, VisualDensity::MAXIMUM_DENSITY);
19120 let content_padding = EdgeInsets::only(0.0, 6.0, 0.0, 14.0);
19121
19122 let decorator = M2::new(
19123 InputDecoration::new()
19124 .border(InputBorderRef::none())
19125 .content_padding(EdgeInsetsGeometry::from(content_padding)),
19126 )
19127 .expands(true)
19128 .text_align_vertical(TextAlignVertical::CENTER)
19129 .visual_density(visual_density)
19130 .child(SizedBox::new().key(key.clone().into()).height(CHILD_HEIGHT))
19131 .build(tester);
19132 tester.pump_widget(
19133 waft_widgets::Center::new()
19134 .child(
19135 SizedBox::new()
19136 .key(container_key.clone().into())
19137 .height(TOTAL_HEIGHT)
19138 .child(decorator),
19139 )
19140 .into_widget(),
19141 );
19142
19143 let density_dy = visual_density.base_size_adjustment().dy();
19145 let child_vertical_space_affordance =
19146 TOTAL_HEIGHT - density_dy - content_padding.vertical();
19147
19148 let child_margin = (child_vertical_space_affordance - CHILD_HEIGHT) / 2.0;
19151 let child_top = density_dy / 2.0 + content_padding.top + child_margin;
19152
19153 assert_eq!(
19154 key_rect(tester, &key).top,
19155 key_rect(tester, &container_key).top + child_top
19156 );
19157 });
19158 }
19159
19160 fn filled_outline() -> InputDecoration {
19161 InputDecoration::new()
19162 .filled(true)
19163 .border(OutlineInputBorder::new())
19164 }
19165
19166 #[test]
19169 fn material2_outline_text_align_vertical_top_leaves_room_for_the_border() {
19170 test_widgets(|tester| {
19171 assert_eq!(
19174 expanded_input_top(tester, filled_outline(), TextAlignVertical::TOP),
19175 24.0
19176 );
19177 });
19178 }
19179
19180 #[test]
19183 fn material2_outline_text_align_vertical_center_centers_the_input() {
19184 test_widgets(|tester| {
19185 assert_eq!(
19187 expanded_input_top(tester, filled_outline(), TextAlignVertical::CENTER),
19188 290.0
19189 );
19190 });
19191 }
19192
19193 #[test]
19196 fn material2_outline_text_align_vertical_bottom_puts_the_input_at_the_bottom() {
19197 test_widgets(|tester| {
19198 assert_eq!(
19200 expanded_input_top(tester, filled_outline(), TextAlignVertical::BOTTOM),
19201 564.0
19202 );
19203 });
19204 }
19205
19206 fn tall_prefix_tops(
19209 tester: &mut WidgetTester,
19210 outline: bool,
19211 expands: bool,
19212 align: TextAlignVertical,
19213 ) -> (f64, f64) {
19214 let p_key = GlobalKey::new();
19215 let mut decoration = InputDecoration::new()
19216 .prefix(sized(&p_key, 10.0, 100.0))
19217 .filled(true);
19218 if outline {
19219 decoration = decoration.border(OutlineInputBorder::new());
19220 }
19221 M2::new(decoration)
19222 .expands(expands)
19223 .text_align_vertical(align)
19224 .child(text_20())
19225 .pump(tester);
19226 (top(tester, INPUT_TEXT), key_rect(tester, &p_key).top)
19227 }
19228
19229 #[test]
19232 fn material2_tall_prefix_align_top() {
19233 test_widgets(|tester| {
19234 assert_eq!(
19236 tall_prefix_tops(tester, false, false, TextAlignVertical::TOP),
19237 (97.0, 12.0)
19238 );
19239 });
19240 }
19241
19242 #[test]
19245 fn material2_tall_prefix_align_center() {
19246 test_widgets(|tester| {
19247 assert_eq!(
19249 tall_prefix_tops(tester, false, false, TextAlignVertical::CENTER),
19250 (97.0, 12.0)
19251 );
19252 });
19253 }
19254
19255 #[test]
19258 fn material2_tall_prefix_align_bottom() {
19259 test_widgets(|tester| {
19260 assert_eq!(
19262 tall_prefix_tops(tester, false, false, TextAlignVertical::BOTTOM),
19263 (97.0, 12.0)
19264 );
19265 });
19266 }
19267
19268 #[test]
19271 fn material2_outline_tall_prefix_align_center() {
19272 test_widgets(|tester| {
19273 assert_eq!(
19275 tall_prefix_tops(tester, true, true, TextAlignVertical::CENTER),
19276 (332.5, 247.5)
19277 );
19278 });
19279 }
19280
19281 #[test]
19284 fn material2_outline_tall_prefix_align_top() {
19285 test_widgets(|tester| {
19286 assert_eq!(
19289 tall_prefix_tops(tester, true, true, TextAlignVertical::TOP),
19290 (109.0, 24.0)
19291 );
19292 });
19293 }
19294
19295 #[test]
19298 fn material2_outline_tall_prefix_align_bottom() {
19299 test_widgets(|tester| {
19300 assert_eq!(
19302 tall_prefix_tops(tester, true, true, TextAlignVertical::BOTTOM),
19303 (564.0, 479.0)
19304 );
19305 });
19306 }
19307
19308 #[test]
19311 fn material2_outline_tall_prefix_align_double() {
19312 test_widgets(|tester| {
19313 let (input_top, prefix_top) =
19315 tall_prefix_tops(tester, true, true, TextAlignVertical::new(0.1));
19316 assert_f32_equals(input_top, 355.65);
19319 assert_f32_equals(prefix_top, 270.65);
19320 });
19321 }
19322
19323 fn filled_with_label() -> InputDecoration {
19324 InputDecoration::new().label_text(LABEL_TEXT).filled(true)
19325 }
19326
19327 #[test]
19330 fn material2_label_text_align_vertical_top_starts_the_input_lower() {
19331 test_widgets(|tester| {
19332 assert_eq!(
19335 expanded_input_top(tester, filled_with_label(), TextAlignVertical::TOP),
19336 28.0
19337 );
19338 });
19339 }
19340
19341 #[test]
19343 fn material2_label_text_align_vertical_center_is_slightly_lower() {
19344 test_widgets(|tester| {
19345 assert_eq!(
19348 expanded_input_top(tester, filled_with_label(), TextAlignVertical::CENTER),
19349 298.0
19350 );
19351 });
19352 }
19353
19354 #[test]
19356 fn material2_label_text_align_vertical_bottom_keeps_the_bottom_line() {
19357 test_widgets(|tester| {
19358 assert_eq!(
19361 expanded_input_top(tester, filled_with_label(), TextAlignVertical::BOTTOM),
19362 568.0
19363 );
19364 });
19365 }
19366
19367 fn check_outline_alignment(
19370 tester: &mut WidgetTester,
19371 decoration: InputDecoration,
19372 size: Size,
19373 input_top: f64,
19374 border_weight: f64,
19375 ) {
19376 M2::new(decoration).pump(tester);
19377 assert_eq!(decorator_size(tester), size);
19378 assert_eq!(top(tester, INPUT_TEXT), input_top);
19379 assert_eq!(bottom(tester, INPUT_TEXT), input_top + 16.0);
19380 assert_eq!(get_border_bottom(tester), size.height());
19381 assert_eq!(get_border_weight(tester), border_weight);
19382 }
19383
19384 #[test]
19387 fn material2_outline_centers_the_input() {
19388 test_widgets(|tester| {
19389 check_outline_alignment(
19390 tester,
19391 InputDecoration::new().border(OutlineInputBorder::new()),
19392 Size::new(800.0, 56.0),
19393 20.0,
19394 1.0,
19395 );
19396 });
19397 }
19398
19399 #[test]
19402 fn material2_outline_with_a_label_centers_the_input() {
19403 test_widgets(|tester| {
19404 check_outline_alignment(
19405 tester,
19406 InputDecoration::new()
19407 .label_text(LABEL_TEXT)
19408 .border(OutlineInputBorder::new()),
19409 Size::new(800.0, 56.0),
19410 20.0,
19411 1.0,
19412 );
19413 });
19414 }
19415
19416 #[test]
19419 fn material2_outline_with_content_padding_centers_the_input() {
19420 test_widgets(|tester| {
19421 check_outline_alignment(
19422 tester,
19423 InputDecoration::new()
19424 .border(OutlineInputBorder::new())
19425 .content_padding(EdgeInsetsGeometry::from(EdgeInsets::from_ltrb(
19426 12.0, 14.0, 8.0, 14.0,
19427 ))),
19428 Size::new(800.0, 48.0),
19429 16.0,
19430 1.0,
19431 );
19432 });
19433 }
19434
19435 #[test]
19438 fn material2_outline_with_content_padding_and_label_centers_the_input() {
19439 test_widgets(|tester| {
19440 check_outline_alignment(
19441 tester,
19442 InputDecoration::new()
19443 .label_text(LABEL_TEXT)
19444 .border(OutlineInputBorder::new())
19445 .content_padding(EdgeInsetsGeometry::from(EdgeInsets::from_ltrb(
19446 12.0, 14.0, 8.0, 14.0,
19447 ))),
19448 Size::new(800.0, K_MIN_INTERACTIVE_DIMENSION),
19449 16.0,
19450 1.0,
19451 );
19452 });
19453 }
19454
19455 #[test]
19458 fn material2_outline_with_lopsided_content_padding_and_label_centers_the_input() {
19459 test_widgets(|tester| {
19460 check_outline_alignment(
19461 tester,
19462 InputDecoration::new()
19463 .label_text(LABEL_TEXT)
19464 .border(OutlineInputBorder::new())
19465 .content_padding(EdgeInsetsGeometry::from(EdgeInsets::from_ltrb(
19466 12.0, 104.0, 8.0, 0.0,
19467 ))),
19468 Size::new(800.0, 120.0),
19469 52.0,
19470 1.0,
19471 );
19472 });
19473 }
19474
19475 #[test]
19478 fn material2_outline_label_aligns_horizontally_with_the_text() {
19479 test_widgets(|tester| {
19480 M2::new(
19481 InputDecoration::new()
19482 .prefix_icon(Icon::new(Some(Icons::ac_unit())))
19483 .label_text(LABEL_TEXT)
19484 .border(OutlineInputBorder::new()),
19485 )
19486 .is_focused(true)
19487 .pump(tester);
19488
19489 assert_eq!(decorator_size(tester), Size::new(800.0, 56.0));
19490 assert_eq!(tester.get_rect(find::text(LABEL_TEXT)).left, 48.0);
19491 assert_eq!(tester.get_rect(find::text(INPUT_TEXT)).left, 48.0);
19492 assert_eq!(get_border_weight(tester), 2.0);
19493 });
19494 }
19495
19496 #[test]
19499 fn material2_filled_floating_label_aligns_horizontally_with_the_text() {
19500 test_widgets(|tester| {
19501 M2::new(
19502 InputDecoration::new()
19503 .prefix_icon(Icon::new(Some(Icons::ac_unit())))
19504 .label_text(LABEL_TEXT)
19505 .filled(true),
19506 )
19507 .is_focused(true)
19508 .pump(tester);
19509
19510 assert_eq!(decorator_size(tester), Size::new(800.0, 56.0));
19511 assert_eq!(tester.get_rect(find::text(LABEL_TEXT)).left, 48.0);
19512 assert_eq!(tester.get_rect(find::text(INPUT_TEXT)).left, 48.0);
19513 assert_eq!(get_border_weight(tester), 2.0);
19514 });
19515 }
19516
19517 fn check_three_point(
19520 tester: &mut WidgetTester,
19521 align: TextAlignVertical,
19522 padding: EdgeInsets,
19523 input_top: f64,
19524 ) {
19525 M2::new(
19526 InputDecoration::new()
19527 .border(OutlineInputBorder::new())
19528 .content_padding(EdgeInsetsGeometry::from(padding)),
19529 )
19530 .expands(true)
19531 .text_align_vertical(align)
19532 .pump(tester);
19533
19534 assert_eq!(decorator_size(tester), Size::new(800.0, 600.0));
19535 assert_eq!(top(tester, INPUT_TEXT), input_top);
19536 assert_eq!(bottom(tester, INPUT_TEXT), input_top + 16.0);
19537 assert_eq!(get_border_bottom(tester), 600.0);
19538 assert_eq!(get_border_weight(tester), 1.0);
19539 }
19540
19541 #[test]
19544 fn material2_three_point_top_align_includes_padding() {
19545 test_widgets(|tester| {
19546 check_three_point(
19548 tester,
19549 TextAlignVertical::TOP,
19550 EdgeInsets::from_ltrb(12.0, 24.0, 8.0, 2.0),
19551 24.0,
19552 );
19553 });
19554 }
19555
19556 #[test]
19559 fn material2_three_point_center_align_ignores_padding() {
19560 test_widgets(|tester| {
19561 check_three_point(
19563 tester,
19564 TextAlignVertical::CENTER,
19565 EdgeInsets::from_ltrb(12.0, 24.0, 8.0, 2.0),
19566 292.0,
19567 );
19568 });
19569 }
19570
19571 #[test]
19574 fn material2_three_point_bottom_align_includes_padding() {
19575 test_widgets(|tester| {
19576 check_three_point(
19578 tester,
19579 TextAlignVertical::BOTTOM,
19580 EdgeInsets::from_ltrb(12.0, 24.0, 8.0, 2.0),
19581 582.0,
19582 );
19583 });
19584 }
19585
19586 #[test]
19589 fn material2_three_point_padding_beyond_the_middle_keeps_the_top_at_the_middle() {
19590 test_widgets(|tester| {
19591 check_three_point(
19593 tester,
19594 TextAlignVertical::TOP,
19595 EdgeInsets::from_ltrb(12.0, 504.0, 8.0, 0.0),
19596 292.0,
19597 );
19598 });
19599 }
19600
19601 fn counter_layout(
19603 tester: &mut WidgetTester,
19604 text_direction: TextDirection,
19605 is_dense: bool,
19606 ) -> (Size, Rect, Rect) {
19607 M2::new(
19608 InputDecoration::new()
19609 .counter_text("test")
19610 .filled(true)
19611 .is_dense(is_dense),
19612 )
19613 .text_direction(text_direction)
19614 .pump(tester);
19615 (
19616 decorator_size(tester),
19617 get_decorator_rect(tester),
19618 tester.get_rect(find::text("test")),
19619 )
19620 }
19621
19622 #[test]
19625 fn material2_counter_right_margin_ltr_not_dense() {
19626 test_widgets(|tester| {
19627 let (size, decorator, counter) = counter_layout(tester, TextDirection::Ltr, false);
19630 assert_eq!(size, Size::new(800.0, 68.0));
19631 assert_eq!(counter.right, decorator.right - 12.0);
19632 });
19633 }
19634
19635 #[test]
19638 fn material2_counter_right_margin_rtl_not_dense() {
19639 test_widgets(|tester| {
19640 let (size, _, counter) = counter_layout(tester, TextDirection::Rtl, false);
19643 assert_eq!(size, Size::new(800.0, 68.0));
19644 assert_eq!(counter.left, 12.0);
19645 });
19646 }
19647
19648 #[test]
19651 fn material2_counter_right_margin_ltr_dense() {
19652 test_widgets(|tester| {
19653 let (size, decorator, counter) = counter_layout(tester, TextDirection::Ltr, true);
19654 assert_eq!(size, Size::new(800.0, 52.0));
19655 assert_eq!(counter.right, decorator.right - 12.0);
19656 });
19657 }
19658
19659 #[test]
19661 fn material2_counter_right_margin_rtl_dense() {
19662 test_widgets(|tester| {
19663 let (size, _, counter) = counter_layout(tester, TextDirection::Rtl, true);
19664 assert_eq!(size, Size::new(800.0, 52.0));
19665 assert_eq!(counter.left, 12.0);
19666 });
19667 }
19668
19669 #[test]
19671 fn material2_error_helper_and_counter_rtl_layout() {
19672 test_widgets(|tester| {
19673 M2::new(
19674 InputDecoration::new()
19675 .label_text(LABEL_TEXT)
19676 .helper_text(HELPER_TEXT)
19677 .counter_text(COUNTER_TEXT)
19678 .filled(true),
19679 )
19680 .text_direction(TextDirection::Rtl)
19681 .pump(tester);
19682
19683 assert_eq!(decorator_size(tester), Size::new(800.0, 76.0));
19684 assert_eq!(top(tester, INPUT_TEXT), 28.0);
19685 assert_eq!(bottom(tester, INPUT_TEXT), 44.0);
19686 assert_eq!(top(tester, LABEL_TEXT), 12.0);
19687 assert_eq!(bottom(tester, LABEL_TEXT), 24.0);
19688 assert_eq!(get_border_bottom(tester), 56.0);
19689 assert_eq!(get_border_weight(tester), 1.0);
19690 assert_eq!(
19691 tester.get_rect(find::text(COUNTER_TEXT)).top_left(),
19692 Offset::new(12.0, 64.0)
19693 );
19694 assert_eq!(
19695 tester.get_rect(find::text(HELPER_TEXT)).top_right(),
19696 Offset::new(788.0, 64.0)
19697 );
19698
19699 M2::new(
19701 InputDecoration::new()
19702 .label_text(LABEL_TEXT)
19703 .helper_text(HELPER_TEXT)
19704 .error_text(ERROR_TEXT)
19705 .counter_text(COUNTER_TEXT)
19706 .filled(true),
19707 )
19708 .text_direction(TextDirection::Rtl)
19709 .pump(tester);
19710 tester.pump_and_settle();
19711 assert_eq!(
19712 tester.get_rect(find::text(COUNTER_TEXT)).top_left(),
19713 Offset::new(12.0, 64.0)
19714 );
19715 assert_eq!(
19716 tester.get_rect(find::text(ERROR_TEXT)).top_right(),
19717 Offset::new(788.0, 64.0)
19718 );
19719 expect(tester, &find::text(HELPER_TEXT), finds_nothing());
19720 });
19721 }
19722
19723 #[test]
19725 fn material2_prefix_and_suffix_rtl() {
19726 test_widgets(|tester| {
19727 M2::new(
19728 InputDecoration::new()
19729 .prefix_text("p")
19730 .suffix_text("s")
19731 .filled(true),
19732 )
19733 .text_direction(TextDirection::Rtl)
19734 .pump(tester);
19735
19736 assert_eq!(
19737 decorator_size(tester),
19738 Size::new(800.0, K_MIN_INTERACTIVE_DIMENSION)
19739 ); assert_eq!(tester.get_rect(find::text(INPUT_TEXT)).height(), 16.0);
19741 assert_eq!(tester.get_rect(find::text("p")).height(), 16.0);
19742 assert_eq!(tester.get_rect(find::text("s")).height(), 16.0);
19743 assert_eq!(top(tester, INPUT_TEXT), 16.0);
19744 assert_eq!(top(tester, "p"), 16.0);
19745 assert_eq!(top(tester, "s"), 16.0);
19746
19747 assert_eq!(tester.get_rect(find::text("s")).left, 12.0);
19749 assert!(
19750 tester.get_rect(find::text("s")).right
19751 <= tester.get_rect(find::text(INPUT_TEXT)).left
19752 );
19753 assert!(
19754 tester.get_rect(find::text(INPUT_TEXT)).right
19755 <= tester.get_rect(find::text("p")).left
19756 );
19757 });
19758 }
19759
19760 #[test]
19762 fn material2_content_padding_rtl_layout() {
19763 test_widgets(|tester| {
19764 let decoration = || {
19765 InputDecoration::new()
19766 .content_padding(EdgeInsetsGeometry::from(EdgeInsetsDirectional::only(
19767 40.0, 12.0, 0.0, 12.0,
19768 )))
19769 .label_text(LABEL_TEXT)
19770 .hint_text(HINT_TEXT)
19771 .filled(true)
19772 };
19773 M2::new(decoration()).pump(tester);
19775 assert_eq!(decorator_size(tester), Size::new(800.0, 56.0));
19776 assert_eq!(tester.get_rect(find::text(INPUT_TEXT)).left, 40.0);
19777 assert_eq!(tester.get_rect(find::text(LABEL_TEXT)).left, 40.0);
19778 assert_eq!(tester.get_rect(find::text(HINT_TEXT)).left, 40.0);
19779
19780 M2::new(decoration())
19782 .is_focused(true) .text_direction(TextDirection::Rtl)
19784 .pump(tester);
19785 assert_eq!(decorator_size(tester), Size::new(800.0, 56.0));
19786 assert_eq!(tester.get_rect(find::text(INPUT_TEXT)).right, 760.0);
19787 assert_eq!(tester.get_rect(find::text(LABEL_TEXT)).right, 760.0);
19788 assert_eq!(tester.get_rect(find::text(HINT_TEXT)).right, 760.0);
19789 });
19790 }
19791
19792 fn check_input_width(
19793 tester: &mut WidgetTester,
19794 decoration: InputDecoration,
19795 height: f64,
19796 left: f64,
19797 right: f64,
19798 ) {
19799 M2::new(decoration).pump(tester);
19800 assert_eq!(decorator_size(tester), Size::new(800.0, height));
19801 assert_eq!(tester.get_rect(find::text(INPUT_TEXT)).left, left);
19802 assert_eq!(tester.get_rect(find::text(INPUT_TEXT)).right, right);
19803 }
19804
19805 #[test]
19807 fn material2_outline_input_text_width() {
19808 test_widgets(|tester| {
19809 check_input_width(
19810 tester,
19811 InputDecoration::new().border(OutlineInputBorder::new()),
19812 56.0,
19813 12.0,
19814 788.0,
19815 );
19816 });
19817 }
19818
19819 #[test]
19822 fn material2_outline_input_text_width_with_prefix_and_suffix_icons() {
19823 test_widgets(|tester| {
19824 check_input_width(
19825 tester,
19826 InputDecoration::new()
19827 .border(OutlineInputBorder::new())
19828 .prefix_icon(Icon::new(Some(Icons::visibility())))
19829 .suffix_icon(Icon::new(Some(Icons::close()))),
19830 56.0,
19831 48.0,
19832 752.0,
19833 );
19834 });
19835 }
19836
19837 #[test]
19839 fn material2_filled_input_text_width() {
19840 test_widgets(|tester| {
19841 check_input_width(
19842 tester,
19843 InputDecoration::new().filled(true),
19844 48.0,
19845 12.0,
19846 788.0,
19847 );
19848 });
19849 }
19850
19851 #[test]
19854 fn material2_filled_input_text_width_with_prefix_and_suffix_icons() {
19855 test_widgets(|tester| {
19856 check_input_width(
19857 tester,
19858 InputDecoration::new()
19859 .filled(true)
19860 .prefix_icon(Icon::new(Some(Icons::visibility())))
19861 .suffix_icon(Icon::new(Some(Icons::close()))),
19862 48.0,
19863 48.0,
19864 752.0,
19865 );
19866 });
19867 }
19868
19869 fn floating_label_rect(
19872 tester: &mut WidgetTester,
19873 text_direction: TextDirection,
19874 has_icon: bool,
19875 alignment: FloatingLabelAlignment,
19876 border_is_outline: bool,
19877 ) -> Rect {
19878 let mut decoration = InputDecoration::new()
19879 .content_padding(EdgeInsetsGeometry::from(EdgeInsetsDirectional::only(
19880 40.0, 12.0, 0.0, 12.0,
19881 )))
19882 .floating_label_alignment(alignment)
19883 .label_text(LABEL_TEXT)
19884 .hint_text(HINT_TEXT)
19885 .filled(true);
19886 if has_icon {
19887 decoration = decoration.icon(Icon::new(Some(Icons::insert_link())));
19888 }
19889 if border_is_outline {
19890 decoration = decoration.border(OutlineInputBorder::new());
19891 }
19892 M2::new(decoration)
19893 .text_direction(text_direction)
19894 .pump(tester);
19895 get_label_rect(tester)
19896 }
19897
19898 #[test]
19901 fn material2_floating_label_starts_after_the_icon_ltr() {
19902 test_widgets(|tester| {
19903 for outline in [false, true] {
19904 let rect = floating_label_rect(
19906 tester,
19907 TextDirection::Ltr,
19908 true,
19909 FloatingLabelAlignment::START,
19910 outline,
19911 );
19912 assert_eq!(rect.left, 80.0);
19913 }
19914 });
19915 }
19916
19917 #[test]
19920 fn material2_floating_label_centers_beside_the_icon_ltr() {
19921 test_widgets(|tester| {
19922 for outline in [false, true] {
19923 let rect = floating_label_rect(
19925 tester,
19926 TextDirection::Ltr,
19927 true,
19928 FloatingLabelAlignment::CENTER,
19929 outline,
19930 );
19931 assert_eq!(rect.center().dx(), 420.0);
19932 }
19933 });
19934 }
19935
19936 #[test]
19939 fn material2_floating_label_starts_at_the_padding_ltr() {
19940 test_widgets(|tester| {
19941 for outline in [false, true] {
19942 let rect = floating_label_rect(
19944 tester,
19945 TextDirection::Ltr,
19946 false,
19947 FloatingLabelAlignment::START,
19948 outline,
19949 );
19950 assert_eq!(rect.left, 40.0);
19951 }
19952 });
19953 }
19954
19955 #[test]
19958 fn material2_floating_label_centers_in_the_decorator_ltr() {
19959 test_widgets(|tester| {
19960 for outline in [false, true] {
19961 let rect = floating_label_rect(
19963 tester,
19964 TextDirection::Ltr,
19965 false,
19966 FloatingLabelAlignment::CENTER,
19967 outline,
19968 );
19969 assert_eq!(rect.center().dx(), 400.0);
19970 }
19971 });
19972 }
19973
19974 #[test]
19977 fn material2_floating_label_starts_after_the_icon_rtl() {
19978 test_widgets(|tester| {
19979 for outline in [false, true] {
19980 let rect = floating_label_rect(
19982 tester,
19983 TextDirection::Rtl,
19984 true,
19985 FloatingLabelAlignment::START,
19986 outline,
19987 );
19988 assert_eq!(rect.right, 720.0);
19989 }
19990 });
19991 }
19992
19993 #[test]
19996 fn material2_floating_label_centers_beside_the_icon_rtl() {
19997 test_widgets(|tester| {
19998 for outline in [false, true] {
19999 let rect = floating_label_rect(
20001 tester,
20002 TextDirection::Rtl,
20003 true,
20004 FloatingLabelAlignment::CENTER,
20005 outline,
20006 );
20007 assert_eq!(rect.center().dx(), 380.0);
20008 }
20009 });
20010 }
20011
20012 #[test]
20015 fn material2_floating_label_starts_at_the_padding_rtl() {
20016 test_widgets(|tester| {
20017 for outline in [false, true] {
20018 let rect = floating_label_rect(
20020 tester,
20021 TextDirection::Rtl,
20022 false,
20023 FloatingLabelAlignment::START,
20024 outline,
20025 );
20026 assert_eq!(rect.right, 760.0);
20027 }
20028 });
20029 }
20030
20031 #[test]
20034 fn material2_floating_label_centers_in_the_decorator_rtl() {
20035 test_widgets(|tester| {
20036 for outline in [false, true] {
20037 let rect = floating_label_rect(
20039 tester,
20040 TextDirection::Rtl,
20041 false,
20042 FloatingLabelAlignment::CENTER,
20043 outline,
20044 );
20045 assert_eq!(rect.center().dx(), 400.0);
20046 }
20047 });
20048 }
20049
20050 #[test]
20052 fn material2_prefix_and_suffix_dense_layout() {
20053 test_widgets(|tester| {
20054 M2::new(
20055 InputDecoration::new()
20056 .is_dense(true)
20057 .prefix_text("p")
20058 .suffix_text("s")
20059 .filled(true),
20060 )
20061 .is_focused(true)
20062 .pump(tester);
20063
20064 assert_eq!(decorator_size(tester), Size::new(800.0, 32.0));
20067 assert_eq!(tester.get_rect(find::text(INPUT_TEXT)).height(), 16.0);
20068 assert_eq!(tester.get_rect(find::text("p")).height(), 16.0);
20069 assert_eq!(tester.get_rect(find::text("s")).height(), 16.0);
20070 assert_eq!(top(tester, INPUT_TEXT), 8.0);
20071 assert_eq!(top(tester, "p"), 8.0);
20072 assert_eq!(tester.get_rect(find::text("p")).left, 12.0);
20073 assert_eq!(top(tester, "s"), 8.0);
20074 assert_eq!(tester.get_rect(find::text("s")).right, 788.0);
20075
20076 assert_eq!(tester.get_rect(find::text("p")).left, 12.0);
20078 assert!(
20079 tester.get_rect(find::text("p")).right
20080 <= tester.get_rect(find::text(INPUT_TEXT)).left
20081 );
20082 assert!(
20083 tester.get_rect(find::text(INPUT_TEXT)).right
20084 <= tester.get_rect(find::text("s")).left
20085 );
20086
20087 assert_eq!(get_border_bottom(tester), 32.0);
20088 assert_eq!(get_border_weight(tester), 2.0);
20089 });
20090 }
20091
20092 #[test]
20094 fn material2_empty_decoration_is_bumped_up_to_the_minimum_height() {
20095 test_widgets(|tester| {
20096 M2::new(InputDecoration::new()).pump(tester);
20097
20098 assert_eq!(
20099 decorator_size(tester),
20100 Size::new(800.0, K_MIN_INTERACTIVE_DIMENSION)
20101 ); assert_eq!(tester.get_rect(find::text(INPUT_TEXT)).height(), 16.0);
20103 assert_eq!(top(tester, INPUT_TEXT), 16.0);
20104 assert_eq!(get_border_bottom(tester), K_MIN_INTERACTIVE_DIMENSION); assert_eq!(get_border_weight(tester), 1.0);
20106 });
20107 }
20108
20109 #[test]
20112 fn material2_content_padding_smaller_than_the_minimum_interactive_dimension() {
20113 test_widgets(|tester| {
20114 const VERTICAL_PADDING: f64 = 1.0;
20115 M2::new(
20116 InputDecoration::new()
20117 .hint_text(HINT_TEXT)
20118 .content_padding(EdgeInsetsGeometry::from(EdgeInsets::symmetric(
20119 VERTICAL_PADDING,
20120 0.0,
20121 )))
20122 .is_dense(true),
20123 )
20124 .pump(tester);
20125
20126 assert_eq!(decorator_size(tester), Size::new(800.0, 18.0));
20130 assert_eq!(tester.get_rect(find::text(INPUT_TEXT)).height(), 16.0);
20131 assert_eq!(top(tester, INPUT_TEXT), 1.0);
20132 assert_eq!(get_opacity(tester, HINT_TEXT), 0.0);
20133 assert_eq!(get_border_weight(tester), 1.0);
20134
20135 M2::new(InputDecoration::collapsed(None).hint_text(HINT_TEXT)).pump(tester);
20137
20138 assert_eq!(decorator_size(tester), Size::new(800.0, 16.0));
20143 assert_eq!(tester.get_rect(find::text(INPUT_TEXT)).height(), 16.0);
20144 assert_eq!(top(tester, INPUT_TEXT), 0.0);
20145 assert_eq!(get_opacity(tester, HINT_TEXT), 0.0);
20146 assert_eq!(get_border_weight(tester), 1.0);
20147
20148 M2::new(InputDecoration::new().hint_text(HINT_TEXT).content_padding(
20149 EdgeInsetsGeometry::from(EdgeInsets::symmetric(VERTICAL_PADDING, 0.0)),
20150 ))
20151 .pump(tester);
20152
20153 assert_eq!(
20156 decorator_size(tester),
20157 Size::new(800.0, K_MIN_INTERACTIVE_DIMENSION)
20158 );
20159 assert_eq!(tester.get_rect(find::text(INPUT_TEXT)).height(), 16.0);
20160 assert_eq!(top(tester, INPUT_TEXT), 16.0);
20161 assert_eq!(get_opacity(tester, HINT_TEXT), 0.0);
20162 assert_eq!(get_border_weight(tester), 0.0);
20163 });
20164 }
20165
20166 #[test]
20168 fn material2_collapsed_decorator_has_no_minimum_height() {
20169 test_widgets(|tester| {
20170 M2::new(InputDecoration::collapsed(None).hint_text(HINT_TEXT)).pump(tester);
20171
20172 assert_eq!(decorator_size(tester), Size::new(800.0, 16.0));
20175 assert_eq!(tester.get_rect(find::text(INPUT_TEXT)).height(), 16.0);
20176 assert_eq!(top(tester, INPUT_TEXT), 0.0);
20177 assert_eq!(get_opacity(tester, HINT_TEXT), 0.0);
20178 assert_eq!(get_border_weight(tester), 0.0);
20179
20180 M2::new(InputDecoration::collapsed(None).hint_text(HINT_TEXT))
20182 .is_empty(true)
20183 .is_focused(true)
20184 .pump(tester);
20185 tester.pump_and_settle();
20186
20187 assert_eq!(decorator_size(tester), Size::new(800.0, 16.0));
20188 assert_eq!(tester.get_rect(find::text(INPUT_TEXT)).height(), 16.0);
20189 assert_eq!(top(tester, INPUT_TEXT), 0.0);
20190 assert_eq!(tester.get_rect(find::text(HINT_TEXT)).height(), 16.0);
20191 assert_eq!(top(tester, HINT_TEXT), 0.0);
20192 assert_eq!(get_border_weight(tester), 0.0);
20193 });
20194 }
20195
20196 #[test]
20198 fn material2_decorator_with_base_style() {
20199 test_widgets(|tester| {
20200 let style = TextStyle::new().font_size(10.0);
20203 M2::new(
20204 InputDecoration::new()
20205 .hint_text(HINT_TEXT)
20206 .label_text(LABEL_TEXT),
20207 )
20208 .is_empty(true)
20209 .base_style(style.clone())
20210 .child(Text::new(INPUT_TEXT).style(style))
20211 .pump(tester);
20212
20213 assert_eq!(
20214 decorator_size(tester),
20215 Size::new(800.0, K_MIN_INTERACTIVE_DIMENSION)
20216 ); assert_eq!(tester.get_rect(find::text(HINT_TEXT)).height(), 10.0);
20218 assert_eq!(tester.get_rect(find::text(LABEL_TEXT)).height(), 10.0);
20219 assert_eq!(tester.get_rect(find::text(INPUT_TEXT)).height(), 10.0);
20220 assert_eq!(top(tester, HINT_TEXT), 24.75);
20221 assert_eq!(top(tester, LABEL_TEXT), 19.0);
20222 assert_eq!(top(tester, INPUT_TEXT), 24.75);
20223 });
20224 }
20225
20226 #[test]
20228 fn material2_empty_style_overrides_lay_out_as_no_overrides() {
20229 test_widgets(|tester| {
20230 M2::new(
20232 InputDecoration::new()
20233 .label_text(LABEL_TEXT)
20234 .hint_text(HINT_TEXT)
20235 .helper_text(HELPER_TEXT)
20236 .counter_text(COUNTER_TEXT)
20237 .label_style(TextStyle::new())
20238 .hint_style(TextStyle::new())
20239 .error_style(TextStyle::new())
20240 .helper_style(TextStyle::new())
20241 .filled(true),
20242 )
20243 .pump(tester);
20244
20245 assert_eq!(decorator_size(tester), Size::new(800.0, 76.0));
20247 assert_eq!(top(tester, INPUT_TEXT), 28.0);
20248 assert_eq!(bottom(tester, INPUT_TEXT), 44.0);
20249 assert_eq!(top(tester, LABEL_TEXT), 12.0);
20250 assert_eq!(bottom(tester, LABEL_TEXT), 24.0);
20251 assert_eq!(get_border_bottom(tester), 56.0);
20252 assert_eq!(get_border_weight(tester), 1.0);
20253 assert_eq!(
20254 tester.get_rect(find::text(HELPER_TEXT)).top_left(),
20255 Offset::new(12.0, 64.0)
20256 );
20257 assert_eq!(
20258 tester.get_rect(find::text(COUNTER_TEXT)).top_right(),
20259 Offset::new(788.0, 64.0)
20260 );
20261 });
20262 }
20263
20264 fn borderless_outline_never_floating() -> InputDecoration {
20265 InputDecoration::new()
20266 .border(OutlineInputBorder::new().border_side(BorderSide::NONE))
20267 .floating_label_behavior(FloatingLabelBehavior::Never)
20268 .label_text(LABEL_TEXT)
20269 }
20270
20271 #[test]
20274 fn material2_outline_shape_with_no_border_and_no_floating_placeholder() {
20275 test_widgets(|tester| {
20276 M2::new(borderless_outline_never_floating())
20277 .is_empty(true)
20278 .pump(tester);
20279
20280 assert_eq!(decorator_size(tester), Size::new(800.0, 56.0));
20281 assert_eq!(top(tester, LABEL_TEXT), 20.0);
20282 assert_eq!(bottom(tester, LABEL_TEXT), 36.0);
20283 assert_eq!(get_border_bottom(tester), 56.0);
20284 assert_eq!(get_border_weight(tester), 0.0);
20285 });
20286 }
20287
20288 #[test]
20291 fn material2_outline_shape_with_no_border_and_no_floating_placeholder_not_empty() {
20292 test_widgets(|tester| {
20293 M2::new(borderless_outline_never_floating()).pump(tester);
20294
20295 assert_eq!(decorator_size(tester), Size::new(800.0, 56.0));
20296 assert_eq!(top(tester, LABEL_TEXT), 20.0);
20297 assert_eq!(bottom(tester, LABEL_TEXT), 36.0);
20298 assert_eq!(get_border_bottom(tester), 56.0);
20299 assert_eq!(get_border_weight(tester), 0.0);
20300
20301 assert_eq!(get_opacity(tester, LABEL_TEXT), 0.0);
20303 });
20304 }
20305
20306 #[test]
20308 fn material2_theme_outline_border_centers_the_label() {
20309 test_widgets(|tester| {
20310 M2::new(InputDecoration::new().label_text(LABEL_TEXT))
20311 .is_empty(true) .input_decoration_theme(
20313 InputDecorationThemeData::new().border(OutlineInputBorder::new()),
20314 )
20315 .pump(tester);
20316
20317 assert_eq!(decorator_size(tester), Size::new(800.0, 56.0));
20318 assert_eq!(top(tester, LABEL_TEXT), 20.0);
20319 assert_eq!(bottom(tester, LABEL_TEXT), 36.0);
20320 assert_eq!(get_border_bottom(tester), 56.0);
20321 assert_eq!(get_border_weight(tester), 1.0);
20322 });
20323 }
20324
20325 #[test]
20328 fn material2_theme_outline_border_dense_layout() {
20329 test_widgets(|tester| {
20330 M2::new(label_and_hint())
20331 .is_empty(true) .input_decoration_theme(
20333 InputDecorationThemeData::new()
20334 .border(OutlineInputBorder::new())
20335 .is_dense(true),
20336 )
20337 .pump(tester);
20338
20339 assert_eq!(decorator_size(tester), Size::new(800.0, 48.0));
20340 assert_eq!(top(tester, LABEL_TEXT), 16.0);
20341 assert_eq!(bottom(tester, LABEL_TEXT), 32.0);
20342 assert_eq!(get_border_bottom(tester), 48.0);
20343 assert_eq!(get_border_weight(tester), 1.0);
20344 });
20345 }
20346
20347 struct OverrideStyles {
20349 label_style: TextStyle,
20350 floating_label_style: TextStyle,
20351 hint_style: TextStyle,
20352 prefix_style: TextStyle,
20353 suffix_style: TextStyle,
20354 helper_style: TextStyle,
20355 counter_style: TextStyle,
20356 }
20357
20358 impl OverrideStyles {
20359 fn new() -> OverrideStyles {
20360 let default_style = TextStyle::new().font_size(16.0);
20361 let style12 = TextStyle::new().font_size(12.0);
20362 let with_color = |style: &TextStyle, color: Color| {
20363 style.merge(Some(&TextStyle::new().color(color)))
20364 };
20365 OverrideStyles {
20366 label_style: with_color(&default_style, Colors::RED.primary()),
20367 floating_label_style: with_color(&default_style, Colors::INDIGO.primary()),
20368 hint_style: with_color(&default_style, Colors::GREEN.primary()),
20369 prefix_style: with_color(&default_style, Colors::BLUE.primary()),
20370 suffix_style: with_color(&default_style, Colors::PURPLE.primary()),
20371 helper_style: with_color(&style12, Colors::ORANGE.primary()),
20372 counter_style: with_color(&style12, Colors::ORANGE.primary()),
20373 }
20374 }
20375
20376 fn theme(&self, floating: bool) -> InputDecorationThemeData {
20377 let mut theme = InputDecorationThemeData::new()
20378 .label_style(self.label_style.clone())
20379 .hint_style(self.hint_style.clone())
20380 .prefix_style(self.prefix_style.clone())
20381 .suffix_style(self.suffix_style.clone())
20382 .helper_style(self.helper_style.clone())
20383 .counter_style(self.counter_style.clone());
20384 if floating {
20385 theme = theme.floating_label_style(self.floating_label_style.clone());
20386 }
20387 theme
20389 }
20390 }
20391
20392 fn text_style_color(tester: &WidgetTester, text: &str) -> Option<Color> {
20393 let element = tester.element(find::text(text));
20394 tester.widget(element, |text: &waft_widgets::Text| {
20395 text.style
20396 .as_ref()
20397 .and_then(|style| style.color.as_ref().map(|color| **color))
20398 })
20399 }
20400
20401 fn color_of(style: &TextStyle) -> Option<Color> {
20402 style.color.as_ref().map(|color| **color)
20403 }
20404
20405 fn check_style_overrides(
20409 tester: &mut WidgetTester,
20410 focused: bool,
20411 label_top: f64,
20412 label_height: f64,
20413 weight: f64,
20414 ) {
20415 let styles = OverrideStyles::new();
20416 M2::new(
20417 InputDecoration::new()
20418 .label_text(LABEL_TEXT)
20419 .hint_text(HINT_TEXT)
20420 .prefix_text("prefix")
20421 .suffix_text("suffix")
20422 .helper_text(HELPER_TEXT)
20423 .counter_text(COUNTER_TEXT),
20424 )
20425 .is_empty(true)
20426 .is_focused(focused)
20427 .input_decoration_theme(styles.theme(focused))
20428 .pump(tester);
20429
20430 assert_eq!(decorator_size(tester), Size::new(800.0, 76.0));
20432 assert_eq!(top(tester, LABEL_TEXT), label_top);
20433 assert_eq!(bottom(tester, LABEL_TEXT), label_top + label_height);
20434 assert_eq!(get_border_bottom(tester), 56.0);
20435 assert_eq!(get_border_weight(tester), weight);
20436 assert_eq!(
20437 tester.get_rect(find::text(HELPER_TEXT)).top_left(),
20438 Offset::new(0.0, 64.0)
20439 );
20440 assert_eq!(
20441 tester.get_rect(find::text(COUNTER_TEXT)).top_right(),
20442 Offset::new(800.0, 64.0)
20443 );
20444
20445 M2::new(
20447 InputDecoration::new()
20448 .label_text(LABEL_TEXT)
20449 .hint_text(HINT_TEXT)
20450 .prefix_text("prefix")
20451 .suffix_text("suffix")
20452 .helper_text(HELPER_TEXT)
20453 .counter_text(COUNTER_TEXT),
20454 )
20455 .is_empty(true)
20456 .is_focused(focused)
20457 .input_decoration_theme(styles.theme(focused))
20458 .pump(tester);
20459 assert_eq!(
20460 text_style_color(tester, HINT_TEXT),
20461 color_of(&styles.hint_style)
20462 );
20463 assert_eq!(
20464 text_style_color(tester, "prefix"),
20465 color_of(&styles.prefix_style)
20466 );
20467 assert_eq!(
20468 text_style_color(tester, "suffix"),
20469 color_of(&styles.suffix_style)
20470 );
20471 assert_eq!(
20472 text_style_color(tester, HELPER_TEXT),
20473 color_of(&styles.helper_style)
20474 );
20475 assert_eq!(
20476 text_style_color(tester, COUNTER_TEXT),
20477 color_of(&styles.counter_style)
20478 );
20479 let label_style = if focused {
20480 &styles.floating_label_style
20481 } else {
20482 &styles.label_style
20483 };
20484 assert_eq!(color_of(&get_label_style(tester)), color_of(label_style));
20485 }
20486
20487 #[test]
20489 fn material2_theme_style_overrides() {
20490 test_widgets(|tester| check_style_overrides(tester, false, 20.0, 16.0, 1.0));
20491 }
20492
20493 #[test]
20496 fn material2_theme_style_overrides_focused() {
20497 test_widgets(|tester| check_style_overrides(tester, true, 12.0, 12.0, 2.0));
20498 }
20499
20500 #[test]
20502 fn material2_empty_border_with_a_label() {
20503 test_widgets(|tester| {
20504 M2::new(
20506 InputDecoration::new()
20507 .label_text(LABEL_TEXT)
20508 .border(InputBorderRef::none()),
20509 )
20510 .pump(tester);
20511
20512 assert_eq!(decorator_size(tester), Size::new(800.0, 56.0));
20513 assert_eq!(get_border_weight(tester), 0.0);
20514 assert_eq!(top(tester, LABEL_TEXT), 12.0);
20515 assert_eq!(bottom(tester, LABEL_TEXT), 24.0);
20516 });
20517 }
20518
20519 fn outline_with(color: Color) -> InputBorderRef {
20520 InputBorderRef::new(OutlineInputBorder::new().border_side(BorderSide {
20521 color,
20522 ..BorderSide::default()
20523 }))
20524 }
20525
20526 fn same_color(actual: Color, expected: Color) {
20527 assert!(
20528 crate::test_support::is_same_color_as(actual, expected),
20529 "{actual:?} is not the same colour as {expected:?}"
20530 );
20531 }
20532
20533 #[test]
20535 fn material2_decorator_draws_and_animates_hover_color() {
20536 test_widgets(|tester| {
20537 let fill_color = Color::new(0x0A000000);
20538 let hover_color = Color::new(0xFF00FF00);
20539 let disabled_color = Color::new(0x05000000);
20540 let enabled_border_color = Color::new(0x61000000);
20541
20542 let pump_decorator =
20543 |tester: &mut WidgetTester, hovering: bool, enabled: bool, filled: bool| {
20544 M2::new(
20545 InputDecoration::new()
20546 .enabled(enabled)
20547 .filled(filled)
20548 .hover_color(hover_color)
20549 .disabled_border(outline_with(disabled_color))
20550 .border(outline_with(enabled_border_color)),
20551 )
20552 .is_hovering(hovering)
20553 .pump(tester);
20554 };
20555
20556 pump_decorator(tester, false, true, true);
20558 same_color(get_container_color(tester), fill_color);
20559 tester.pump_by(Duration::from_secs(10));
20560 same_color(get_container_color(tester), fill_color);
20561
20562 pump_decorator(tester, true, true, true);
20563 same_color(get_container_color(tester), fill_color);
20564 tester.pump_by(Duration::from_millis(15));
20565 same_color(get_container_color(tester), hover_color);
20566
20567 pump_decorator(tester, false, true, true);
20568 same_color(get_container_color(tester), hover_color);
20569 tester.pump_by(Duration::from_millis(15));
20570 same_color(get_container_color(tester), fill_color);
20571
20572 pump_decorator(tester, true, true, true);
20575 same_color(get_container_color(tester), fill_color);
20576 tester.pump_by(Duration::from_millis(6));
20577 let hover_value =
20578 with_border_painter(tester, |app, painter| painter.hover_animation.value(app));
20579 let mid_hover_color = Color::lerp(
20580 Some(hover_color.with_alpha(0)),
20581 Some(hover_color),
20582 hover_value,
20583 )
20584 .expect("both colours are given");
20585 same_color(
20586 get_container_color(tester),
20587 Color::alpha_blend(mid_hover_color, fill_color),
20588 );
20589
20590 pump_decorator(tester, false, false, true);
20591 same_color(get_container_color(tester), disabled_color);
20592 tester.pump_by(Duration::from_secs(10));
20593 same_color(get_container_color(tester), disabled_color);
20594
20595 pump_decorator(tester, true, false, true);
20596 same_color(get_container_color(tester), disabled_color);
20597 tester.pump_by(Duration::from_secs(10));
20598 same_color(get_container_color(tester), disabled_color);
20599
20600 let blended_hover_color = Color::new(0x74004400);
20602 pump_decorator(tester, false, true, false);
20603 tester.pump_and_settle();
20604 same_color(get_border_color(tester), enabled_border_color);
20605 tester.pump_by(Duration::from_secs(10));
20606 same_color(get_border_color(tester), enabled_border_color);
20607
20608 pump_decorator(tester, true, true, false);
20609 same_color(get_border_color(tester), enabled_border_color);
20610 tester.pump_by(Duration::from_millis(167));
20611 same_color(get_border_color(tester), blended_hover_color);
20612
20613 pump_decorator(tester, false, true, false);
20614 same_color(get_border_color(tester), blended_hover_color);
20615 tester.pump_by(Duration::from_millis(167));
20616 same_color(get_border_color(tester), enabled_border_color);
20617
20618 pump_decorator(tester, false, false, false);
20619 same_color(get_border_color(tester), enabled_border_color);
20620 tester.pump_by(Duration::from_millis(167));
20621 same_color(get_border_color(tester), disabled_color);
20622
20623 pump_decorator(tester, true, false, false);
20624 same_color(get_border_color(tester), disabled_color);
20625 tester.pump_by(Duration::from_secs(10));
20626 same_color(get_border_color(tester), disabled_color);
20627 });
20628 }
20629
20630 #[test]
20632 fn material2_decorator_draws_and_animates_focus_color() {
20633 test_widgets(|tester| {
20634 let focus_color = Color::new(0xFF0000FF);
20635 let disabled_color = Color::new(0x05000000);
20636 let enabled_border_color = Color::new(0x61000000);
20637
20638 let pump_decorator = |tester: &mut WidgetTester, focused: bool, enabled: bool| {
20639 M2::new(
20640 InputDecoration::new()
20641 .enabled(enabled)
20642 .filled(false)
20643 .focus_color(focus_color)
20644 .focused_border(outline_with(focus_color))
20645 .disabled_border(outline_with(disabled_color))
20646 .border(outline_with(enabled_border_color)),
20647 )
20648 .is_focused(focused)
20649 .pump(tester);
20650 };
20651
20652 pump_decorator(tester, false, true);
20654 tester.pump_and_settle();
20655 assert_eq!(get_border_color(tester), enabled_border_color);
20656 tester.pump_by(Duration::from_secs(10));
20657 assert_eq!(get_border_color(tester), enabled_border_color);
20658
20659 pump_decorator(tester, true, true);
20660 assert_eq!(get_border_color(tester), enabled_border_color);
20661 tester.pump_by(Duration::from_millis(167));
20662 assert_eq!(get_border_color(tester), focus_color);
20663
20664 pump_decorator(tester, false, true);
20665 assert_eq!(get_border_color(tester), focus_color);
20666 tester.pump_by(Duration::from_millis(167));
20667 assert_eq!(get_border_color(tester), enabled_border_color);
20668
20669 pump_decorator(tester, false, false);
20670 assert_eq!(get_border_color(tester), enabled_border_color);
20671 tester.pump_by(Duration::from_millis(167));
20672 assert_eq!(get_border_color(tester), disabled_color);
20673
20674 pump_decorator(tester, true, false);
20675 assert_eq!(get_border_color(tester), disabled_color);
20676 tester.pump_by(Duration::from_secs(10));
20677 assert_eq!(get_border_color(tester), disabled_color);
20678 });
20679 }
20680
20681 #[test]
20684 fn material2_decorator_withdraws_the_label_when_not_empty_or_focused() {
20685 test_widgets(|tester| {
20686 let pump_decorator =
20687 |tester: &mut WidgetTester, focused: bool, enabled: bool, empty: bool| {
20688 M2::new(
20689 InputDecoration::new()
20690 .label_text("Label")
20691 .enabled(enabled)
20692 .filled(false)
20693 .focused_border(OutlineInputBorder::new())
20694 .disabled_border(OutlineInputBorder::new())
20695 .border(OutlineInputBorder::new()),
20696 )
20697 .is_empty(empty)
20698 .is_focused(focused)
20699 .pump(tester);
20700 tester.pump_and_settle();
20701 };
20702 let label_size = Size::new(80.0, 16.0);
20703 let floating_size = Size::new(80.0 * 0.75, 16.0 * 0.75);
20704 let check = |tester: &WidgetTester, top_left: Offset, size: Size| {
20705 let rect = get_label_rect(tester);
20706 assert_eq!(rect.top_left(), top_left);
20707 assert_eq!(rect.size(), size);
20708 };
20709
20710 pump_decorator(tester, false, true, true);
20711 check(tester, Offset::new(12.0, 20.0), label_size);
20712
20713 pump_decorator(tester, false, true, false);
20714 check(tester, Offset::new(12.0, -5.5), floating_size);
20715
20716 pump_decorator(tester, true, true, true);
20717 check(tester, Offset::new(12.0, -5.5), floating_size);
20718
20719 pump_decorator(tester, true, true, false);
20720 check(tester, Offset::new(12.0, -5.5), floating_size);
20721
20722 pump_decorator(tester, false, false, true);
20723 check(tester, Offset::new(12.0, 20.0), label_size);
20724
20725 pump_decorator(tester, false, false, false);
20726 check(tester, Offset::new(12.0, -5.5), floating_size);
20727
20728 pump_decorator(tester, true, false, true);
20730 check(tester, Offset::new(12.0, 20.0), label_size);
20731
20732 pump_decorator(tester, true, false, false);
20733 check(tester, Offset::new(12.0, -5.5), floating_size);
20734 });
20735 }
20736
20737 #[test]
20739 #[allow(deprecated)]
20740 fn material2_default_border_uses_the_color_scheme() {
20741 test_widgets(|tester| {
20742 let theme = m2(&tester.cell.borrow());
20743 let enabled_color = theme.color_scheme.on_surface().with_opacity(0.38);
20745 let disabled_color = theme.disabled_color;
20746 let hover_color =
20747 Color::alpha_blend(theme.hover_color.with_opacity(0.12), enabled_color);
20748
20749 M2::new(InputDecoration::new()).theme(m2).pump(tester);
20751 tester.pump_and_settle();
20752 assert_eq!(get_border_color(tester), enabled_color);
20753
20754 M2::new(InputDecoration::new().filled(true))
20756 .theme(m2)
20757 .pump(tester);
20758 tester.pump_and_settle();
20759 assert_eq!(get_border_color(tester), theme.hint_color);
20760
20761 M2::new(InputDecoration::new())
20763 .theme(m2)
20764 .is_hovering(true)
20765 .pump(tester);
20766 tester.pump_and_settle();
20767 assert_eq!(get_border_color(tester), hover_color);
20768
20769 M2::new(InputDecoration::new())
20771 .theme(m2)
20772 .is_focused(true)
20773 .pump(tester);
20774 tester.pump_and_settle();
20775 assert_eq!(get_border_color(tester), theme.color_scheme.primary());
20776
20777 M2::new(InputDecoration::new().error_text("Nope"))
20779 .theme(m2)
20780 .pump(tester);
20781 tester.pump_and_settle();
20782 assert_eq!(get_border_color(tester), theme.color_scheme.error());
20783
20784 M2::new(InputDecoration::new().enabled(false))
20786 .theme(m2)
20787 .pump(tester);
20788 tester.pump_and_settle();
20789 assert_eq!(get_border_color(tester), disabled_color);
20790
20791 M2::new(InputDecoration::new().enabled(false).filled(true))
20793 .theme(m2)
20794 .pump(tester);
20795 tester.pump_and_settle();
20796 assert_eq!(get_border_color(tester), Colors::TRANSPARENT);
20797 });
20798 }
20799
20800 fn outline(color: Color, width: f64) -> InputBorderRef {
20801 InputBorderRef::new(OutlineInputBorder::new().border_side(BorderSide {
20802 color,
20803 width,
20804 ..BorderSide::default()
20805 }))
20806 }
20807
20808 #[test]
20810 fn material2_decoration_borders_per_state() {
20811 test_widgets(|tester| {
20812 let error_border = outline(Colors::RED.primary(), 1.5);
20813 let focused_border = outline(Colors::GREEN.primary(), 4.0);
20814 let focused_error_border = outline(Colors::TEAL.primary(), 5.0);
20815 let disabled_border = outline(Colors::GREY.primary(), 0.0);
20816 let enabled_border = outline(Colors::BLUE.primary(), 2.5);
20817 let decoration = || {
20818 InputDecoration::new()
20819 .error_border(error_border.clone())
20820 .focused_border(focused_border.clone())
20821 .focused_error_border(focused_error_border.clone())
20822 .disabled_border(disabled_border.clone())
20823 .enabled_border(enabled_border.clone())
20824 };
20825 let check = |tester: &mut WidgetTester,
20826 decoration: InputDecoration,
20827 focused: bool,
20828 expected: &dyn InputBorder| {
20829 M2::new(decoration).is_focused(focused).pump(tester);
20830 tester.pump_and_settle(); assert!(
20832 *get_border(tester).unwrap() == *expected,
20833 "{:?}",
20834 get_border(tester).unwrap().border_side()
20835 );
20836 };
20837
20838 check(tester, decoration(), false, &*enabled_border);
20839 check(tester, decoration(), true, &*focused_border);
20840 check(
20841 tester,
20842 decoration().error_text(ERROR_TEXT),
20843 true,
20844 &*focused_error_border,
20845 );
20846 check(
20847 tester,
20848 decoration().error_text(ERROR_TEXT),
20849 false,
20850 &*error_border,
20851 );
20852 check(
20853 tester,
20854 decoration().error_text(ERROR_TEXT).enabled(false),
20855 false,
20856 &*error_border,
20857 );
20858 check(
20859 tester,
20860 decoration().enabled(false),
20861 false,
20862 &*disabled_border,
20863 );
20864 check(tester, decoration().enabled(false), true, &*disabled_border);
20865 });
20866 }
20867
20868 #[test]
20870 fn material2_floating_label_y_coordinate() {
20871 test_widgets(|tester| {
20872 M2::new(
20874 InputDecoration::new()
20875 .label_text(LABEL_TEXT)
20876 .enabled_border(OutlineInputBorder::new().border_side(BorderSide {
20877 width: 4.0,
20878 ..BorderSide::default()
20879 }))
20880 .floating_label_behavior(FloatingLabelBehavior::Always),
20881 )
20882 .is_empty(true)
20883 .pump(tester);
20884 tester.pump_and_settle();
20885
20886 assert_eq!(top(tester, LABEL_TEXT), -4.0);
20889 });
20890 }
20891
20892 #[test]
20895 fn material2_floating_label_obeys_floating_label_behavior() {
20896 test_widgets(|tester| {
20897 M2::new(
20898 InputDecoration::new()
20899 .label_text(LABEL_TEXT)
20900 .floating_label_behavior(FloatingLabelBehavior::Never),
20901 )
20902 .pump(tester);
20903
20904 assert_eq!(bottom(tester, LABEL_TEXT), 36.0);
20907 assert_eq!(top(tester, LABEL_TEXT), 20.0);
20908 });
20909 }
20910
20911 #[test]
20914 fn material2_hint_is_displayed_when_the_label_always_floats() {
20915 test_widgets(|tester| {
20916 M2::new(
20917 InputDecoration::new()
20918 .floating_label_behavior(FloatingLabelBehavior::Always)
20919 .hint_text(HINT_TEXT)
20920 .label_text(LABEL_TEXT),
20921 )
20922 .is_empty(true)
20923 .pump(tester);
20924 tester.pump_and_settle();
20925
20926 assert_eq!(get_opacity(tester, HINT_TEXT), 1.0);
20927 });
20928 }
20929
20930 #[test]
20934 fn material2_floating_label_width_scales_when_focused() {
20935 test_widgets(|tester| {
20936 let long_string_a: String = "A".repeat(200);
20937 let long_string_b: String = "B".repeat(200);
20938 let pump = |tester: &mut WidgetTester, text: &str, is_focused: bool| {
20939 let decorator = M2::new(InputDecoration::new().label_text(text))
20940 .is_empty(true)
20941 .is_focused(is_focused)
20942 .build(tester);
20943 tester.pump_widget(
20944 waft_widgets::Center::new()
20945 .child(SizedBox::new().width(100.0).height(100.0).child(decorator))
20946 .into_widget(),
20947 );
20948 tester.pump_and_settle();
20949 };
20950
20951 pump(tester, &long_string_a, false);
20952 let size = tester.get_size(find::text(&long_string_a));
20953 assert_eq!(size, Size::new(100.0, 16.0));
20954
20955 pump(tester, &long_string_b, true);
20956 let size = tester.get_size(find::text(&long_string_b));
20957 assert_f32_equals(size.width(), 100.0 / 0.75);
20959 assert_eq!(size.height(), 16.0);
20960 });
20961 }
20962
20963 #[test]
20966 fn material2_text_align_vertical_can_be_updated() {
20967 test_widgets(|tester| {
20968 let alignment: Rc<std::cell::Cell<Option<TextAlignVertical>>> =
20970 Rc::new(std::cell::Cell::new(Some(TextAlignVertical::TOP)));
20971 let setter: Rc<std::cell::RefCell<Option<waft_widgets::StateSetter>>> =
20972 Rc::new(std::cell::RefCell::new(None));
20973 let home = {
20974 let alignment = Rc::clone(&alignment);
20975 let setter = Rc::clone(&setter);
20976 waft_widgets::StatefulBuilder::new(move |_app, _context, set_state| {
20977 *setter.borrow_mut() = Some(set_state);
20978 let mut decorator =
20979 InputDecorator::new(InputDecoration::new().hint_text(HINT_TEXT));
20980 if let Some(alignment) = alignment.get() {
20981 decorator = decorator.text_align_vertical(alignment);
20982 }
20983 decorator.into_widget()
20984 })
20985 .into_widget()
20986 };
20987 let theme = m2(&tester.cell.borrow());
20988 tester.pump_widget(material_app(theme, home));
20989 let hint_top = |tester: &WidgetTester| tester.get_rect(find::text(HINT_TEXT)).top;
20990 let set = |tester: &mut WidgetTester, value: Option<TextAlignVertical>| {
20991 let set_state = setter.borrow().clone().expect("built");
20992 let alignment = Rc::clone(&alignment);
20993 set_state(&mut tester.cell.borrow_mut(), &mut move |_app| {
20994 alignment.set(value)
20995 });
20996 tester.pump();
20997 };
20998
20999 let top_position = hint_top(tester);
21000
21001 set(tester, Some(TextAlignVertical::BOTTOM));
21002 assert!(hint_top(tester) > top_position);
21003
21004 set(tester, None);
21006 assert_eq!(hint_top(tester), top_position);
21007 });
21008 }
21009
21010 #[test]
21013 fn material2_theme_floating_label_style_styles_a_label_widget() {
21014 test_widgets(|tester| {
21015 let floating_label_style = TextStyle::new()
21016 .font_size(16.0)
21017 .merge(Some(&TextStyle::new().color(Colors::INDIGO.primary())));
21018 M2::new(InputDecoration::new().label_text(LABEL_TEXT))
21019 .is_empty(true)
21020 .is_focused(true) .input_decoration_theme(
21022 InputDecorationThemeData::new()
21023 .floating_label_style(floating_label_style.clone()),
21024 )
21025 .pump(tester);
21026
21027 assert_eq!(decorator_size(tester), Size::new(800.0, 56.0));
21028 assert_eq!(top(tester, LABEL_TEXT), 12.0);
21029 assert_eq!(bottom(tester, LABEL_TEXT), 24.0);
21030 assert_eq!(get_border_bottom(tester), 56.0);
21031 assert_eq!(get_border_weight(tester), 2.0);
21032
21033 assert_eq!(
21035 color_of(&get_label_style(tester)),
21036 color_of(&floating_label_style)
21037 );
21038 });
21039 }
21040
21041 #[test]
21044 fn material2_theme_label_style_styles_a_label_widget() {
21045 test_widgets(|tester| {
21046 let label_style = TextStyle::new()
21047 .font_size(16.0)
21048 .merge(Some(&TextStyle::new().color(Colors::PURPLE.primary())));
21049 M2::new(InputDecoration::new().label_text(LABEL_TEXT))
21050 .is_empty(true) .input_decoration_theme(
21052 InputDecorationThemeData::new().label_style(label_style.clone()),
21053 )
21054 .pump(tester);
21055
21056 assert_eq!(decorator_size(tester), Size::new(800.0, 56.0));
21057 assert_eq!(top(tester, LABEL_TEXT), 20.0);
21058 assert_eq!(bottom(tester, LABEL_TEXT), 36.0);
21059 assert_eq!(get_border_bottom(tester), 56.0);
21060 assert_eq!(get_border_weight(tester), 1.0);
21061
21062 assert_eq!(color_of(&get_label_style(tester)), color_of(&label_style));
21064 });
21065 }
21066
21067 #[test]
21069 fn material2_prefix_icon_in_rtl_with_asymmetric_padding_stays_inside() {
21070 test_widgets(|tester| {
21071 M2::new(
21073 InputDecoration::new()
21074 .content_padding(EdgeInsetsGeometry::from(EdgeInsetsDirectional::only(
21075 0.0, 0.0, 24.0, 0.0,
21076 )))
21077 .prefix_icon(waft_widgets::Focus::new(Icon::new(Some(Icons::search())))),
21078 )
21079 .text_direction(TextDirection::Rtl)
21080 .pump(tester);
21081 tester.pump_and_settle();
21082
21083 expect(
21084 tester,
21085 &find::by_type::<InputDecorator>(),
21086 finds_one_widget(),
21087 );
21088 expect(tester, &find::by_type::<Icon>(), finds_one_widget());
21089
21090 let decorator_right = get_decorator_rect(tester).right;
21091 let prefix_right = tester.get_rect(find::by_type::<Icon>()).right;
21092
21093 assert!(decorator_right <= prefix_right);
21095 });
21096 }
21097
21098 #[test]
21100 fn material2_intrinsic_width_with_prefix_and_suffix_icons_keeps_the_text_width() {
21101 test_widgets(|tester| {
21102 for direction in [TextDirection::Rtl, TextDirection::Ltr] {
21104 let mut measure_text = |decoration: InputDecoration| {
21105 M2::new(decoration)
21106 .use_intrinsic_width(true)
21107 .text_direction(direction)
21108 .pump(tester);
21109 tester.pump_and_settle();
21110 expect(tester, &find::text(INPUT_TEXT), finds_one_widget());
21111 tester.get_rect(find::text(INPUT_TEXT)).size()
21112 };
21113 let padding = || EdgeInsetsDirectional::only(12.0, 0.0, 24.0, 0.0);
21114 let icon =
21115 || waft_widgets::Focus::new(Icon::new(Some(Icons::search()))).into_widget();
21116
21117 let without_icons = measure_text(
21118 InputDecoration::new().content_padding(EdgeInsetsGeometry::from(padding())),
21119 );
21120 let with_prefix_icon = measure_text(
21121 InputDecoration::new()
21122 .content_padding(EdgeInsetsGeometry::from(padding()))
21123 .prefix_icon(icon()),
21124 );
21125 let with_suffix_icon = measure_text(
21126 InputDecoration::new()
21127 .content_padding(EdgeInsetsGeometry::from(padding()))
21128 .suffix_icon(icon()),
21129 );
21130
21131 assert_eq!(
21132 with_prefix_icon.width(),
21133 without_icons.width(),
21134 "text width is different with prefixIcon and {direction:?}"
21135 );
21136 assert_eq!(
21137 with_suffix_icon.width(),
21138 without_icons.width(),
21139 "text width is different with prefixIcon and {direction:?}"
21140 );
21141 }
21142 });
21143 }
21144
21145 #[test]
21148 fn an_underline_border_with_no_style_paints_no_ring() {
21149 test_widgets(|tester| {
21150 tester.pump_widget(
21152 BuildInputDecorator::new(
21153 InputDecoration::new().enabled_border(
21154 UnderlineInputBorder::new()
21155 .border_side(BorderSide {
21156 style: waft_painting::BorderStyle::None,
21157 ..BorderSide::default()
21158 })
21159 .border_radius(BorderRadius::all(waft_embedder::Radius::circular(
21160 4.0,
21161 ))),
21162 ),
21163 )
21164 .build(),
21165 );
21166 assert!(paints(&decorator_ops(tester), &[PaintCall::drrect(None)]).is_err());
21167 });
21168 }
21169
21170 #[test]
21174 fn the_error_text_updates_when_the_decoration_changes() {
21175 test_widgets(|tester| {
21176 const HELPER_TEXT_VALUE: &str = "helper text";
21177 let pump = |tester: &mut WidgetTester, value: Option<&str>| {
21178 let mut decoration = InputDecoration::new().helper_text(HELPER_TEXT_VALUE);
21179 if let Some(value) = value {
21180 decoration = decoration.error_text(value);
21181 }
21182 tester.pump_widget(BuildInputDecorator::new(decoration).build());
21183 tester.pump_and_settle();
21184 };
21185 pump(tester, Some("initial error"));
21186
21187 expect(tester, &find::text("initial error"), finds_one_widget());
21188 expect(tester, &find::text(HELPER_TEXT_VALUE), finds_nothing());
21189
21190 pump(tester, None);
21191
21192 expect(tester, &find::text("initial error"), finds_nothing());
21193 expect(tester, &find::text(HELPER_TEXT_VALUE), finds_one_widget());
21194 });
21195 }
21196
21197 fn pump_in_scaffold(
21201 tester: &mut WidgetTester,
21202 theme: fn(&App) -> ThemeData,
21203 direction: TextDirection,
21204 width: f64,
21205 decoration: InputDecoration,
21206 ) {
21207 let theme = theme(&tester.cell.borrow());
21208 let home = crate::scaffold::Scaffold::new().body(Directionality::new(
21209 direction,
21210 SizedBox::new()
21211 .width(width)
21212 .child(InputDecorator::new(decoration)),
21213 ));
21214 tester.pump_widget(material_app(theme, home.into_widget()));
21215 tester.pump_and_settle();
21216 }
21217
21218 fn string_rect(tester: &WidgetTester, text: &str) -> Rect {
21219 tester.get_rect(find::text(text))
21220 }
21221
21222 #[test]
21225 fn helper_and_error_text_default_padding_is_correct() {
21226 test_widgets(|tester| {
21227 const DEFAULT_PADDING: f64 = 16.0; const LONG_TEXT: &str =
21229 "This is a very long text that should wrap and fill the available width";
21230 const INPUT_WIDTH: f64 = 300.0;
21231
21232 for direction in [TextDirection::Ltr, TextDirection::Rtl] {
21233 for error in [false, true] {
21234 let mut decoration = InputDecoration::new().filled(true);
21235 decoration = if error {
21236 decoration.error_text(LONG_TEXT)
21237 } else {
21238 decoration.helper_text(LONG_TEXT)
21239 };
21240 pump_in_scaffold(tester, m3, direction, INPUT_WIDTH, decoration);
21241 let rect = string_rect(tester, LONG_TEXT);
21242 assert_eq!(rect.left, DEFAULT_PADDING, "{direction:?} error: {error}");
21243 assert_eq!(
21244 rect.right,
21245 INPUT_WIDTH - DEFAULT_PADDING,
21246 "{direction:?} error: {error}"
21247 );
21248 }
21249 }
21250 });
21251 }
21252
21253 #[test]
21255 fn the_decorator_does_not_crash_at_zero_area() {
21256 test_widgets(|tester| {
21257 for decoration in [
21258 InputDecoration::new(),
21259 InputDecoration::new().border(OutlineInputBorder::new()),
21260 ] {
21261 let theme = m3(&tester.cell.borrow());
21262 let home = crate::scaffold::Scaffold::new().body(
21263 waft_widgets::Center::new()
21264 .child(SizedBox::shrink().child(InputDecorator::new(decoration))),
21265 );
21266 tester.pump_widget(material_app(theme, home.into_widget()));
21267 assert_eq!(decorator_size(tester), Size::ZERO);
21268 }
21269 });
21270 }
21271
21272 fn supporting_decoration(
21273 error_text: Option<&str>,
21274 helper_text: Option<&str>,
21275 supporting_text_padding: Option<EdgeInsetsGeometry>,
21276 ) -> InputDecoration {
21277 let mut decoration = InputDecoration::new()
21278 .filled(true)
21279 .counter_text(COUNTER_TEXT);
21280 if let Some(error_text) = error_text {
21281 decoration = decoration.error_text(error_text);
21282 }
21283 if let Some(helper_text) = helper_text {
21284 decoration = decoration.helper_text(HELPER_TEXT);
21285 assert_eq!(helper_text, HELPER_TEXT);
21286 }
21287 if let Some(padding) = supporting_text_padding {
21288 decoration = decoration.supporting_text_padding(padding);
21289 }
21290 decoration
21291 }
21292
21293 fn edge_eq(actual: f64, expected: f64) {
21296 assert_f32_equals(actual, expected);
21297 }
21298
21299 const INPUT_GAP: f64 = 4.0; #[test]
21305 fn supporting_text_padding_of_the_decoration_places_the_supporting_text() {
21306 test_widgets(|tester| {
21307 const START: f64 = 32.0;
21308 const END: f64 = 24.0;
21309 const TOP: f64 = 16.0;
21310 const BOTTOM: f64 = 12.0;
21311 const INPUT_WIDTH: f64 = 300.0;
21312 let padding = Some(EdgeInsetsGeometry::directional(START, 0.0, END, 0.0));
21313
21314 pump_in_scaffold(
21317 tester,
21318 m3,
21319 TextDirection::Ltr,
21320 INPUT_WIDTH,
21321 supporting_decoration(Some(ERROR_TEXT), None, None),
21322 );
21323 let default_error_dy = string_rect(tester, ERROR_TEXT).top;
21324 let default_total_height = decorator_size(tester).height();
21325
21326 pump_in_scaffold(
21328 tester,
21329 m3,
21330 TextDirection::Ltr,
21331 INPUT_WIDTH,
21332 supporting_decoration(
21333 Some(ERROR_TEXT),
21334 None,
21335 Some(EdgeInsetsGeometry::directional(START, TOP, END, BOTTOM)),
21336 ),
21337 );
21338 edge_eq(string_rect(tester, ERROR_TEXT).left, START + INPUT_GAP);
21339 edge_eq(
21340 string_rect(tester, COUNTER_TEXT).right,
21341 INPUT_WIDTH - END - INPUT_GAP,
21342 );
21343
21344 assert!(string_rect(tester, ERROR_TEXT).top > default_error_dy);
21346 assert!(decorator_size(tester).height() > default_total_height);
21347
21348 pump_in_scaffold(
21350 tester,
21351 m3,
21352 TextDirection::Rtl,
21353 INPUT_WIDTH,
21354 supporting_decoration(Some(ERROR_TEXT), None, padding),
21355 );
21356 edge_eq(
21358 string_rect(tester, ERROR_TEXT).right,
21359 INPUT_WIDTH - START - INPUT_GAP,
21360 );
21361 edge_eq(string_rect(tester, COUNTER_TEXT).left, END + INPUT_GAP);
21363
21364 pump_in_scaffold(
21366 tester,
21367 m3,
21368 TextDirection::Ltr,
21369 INPUT_WIDTH,
21370 supporting_decoration(None, Some(HELPER_TEXT), padding),
21371 );
21372 edge_eq(string_rect(tester, HELPER_TEXT).left, START + INPUT_GAP);
21373 edge_eq(
21374 string_rect(tester, COUNTER_TEXT).right,
21375 INPUT_WIDTH - END - INPUT_GAP,
21376 );
21377
21378 pump_in_scaffold(
21380 tester,
21381 m3,
21382 TextDirection::Rtl,
21383 INPUT_WIDTH,
21384 supporting_decoration(None, Some(HELPER_TEXT), padding),
21385 );
21386 edge_eq(
21387 string_rect(tester, HELPER_TEXT).right,
21388 INPUT_WIDTH - START - INPUT_GAP,
21389 );
21390 edge_eq(string_rect(tester, COUNTER_TEXT).left, END + INPUT_GAP);
21391 });
21392 }
21393
21394 fn theme_with_supporting_text_padding(app: &App) -> ThemeData {
21395 ThemeData::builder()
21396 .use_material3(true)
21397 .input_decoration_theme(
21398 InputDecorationThemeData::new().supporting_text_padding(
21399 EdgeInsetsGeometry::directional(40.0, 0.0, 20.0, 0.0),
21400 ),
21401 )
21402 .build(app)
21403 }
21404
21405 #[test]
21408 fn supporting_text_padding_of_the_theme_places_the_supporting_text() {
21409 test_widgets(|tester| {
21410 const THEME_START: f64 = 40.0;
21411 const THEME_END: f64 = 20.0;
21412 const INPUT_WIDTH: f64 = 300.0;
21413 for (error, helper) in [(Some(ERROR_TEXT), None), (None, Some(HELPER_TEXT))] {
21414 let text = error.or(helper).expect("one of them");
21415 pump_in_scaffold(
21416 tester,
21417 theme_with_supporting_text_padding,
21418 TextDirection::Ltr,
21419 INPUT_WIDTH,
21420 supporting_decoration(error, helper, None),
21421 );
21422 edge_eq(string_rect(tester, text).left, THEME_START + INPUT_GAP);
21423 edge_eq(
21424 string_rect(tester, COUNTER_TEXT).right,
21425 INPUT_WIDTH - THEME_END - INPUT_GAP,
21426 );
21427
21428 pump_in_scaffold(
21430 tester,
21431 theme_with_supporting_text_padding,
21432 TextDirection::Rtl,
21433 INPUT_WIDTH,
21434 supporting_decoration(error, helper, None),
21435 );
21436 edge_eq(
21437 string_rect(tester, text).right,
21438 INPUT_WIDTH - THEME_START - INPUT_GAP,
21439 );
21440 edge_eq(
21441 string_rect(tester, COUNTER_TEXT).left,
21442 THEME_END + INPUT_GAP,
21443 );
21444 }
21445 });
21446 }
21447
21448 #[test]
21451 fn supporting_text_padding_with_an_icon_shifts_only_the_start() {
21452 test_widgets(|tester| {
21453 const INPUT_WIDTH: f64 = 300.0;
21454 let pump =
21455 |tester: &mut WidgetTester, direction: TextDirection, with_icon: bool| {
21456 let mut decoration = supporting_decoration(
21457 Some(ERROR_TEXT),
21458 None,
21459 Some(EdgeInsetsGeometry::directional(32.0, 0.0, 24.0, 0.0)),
21460 );
21461 if with_icon {
21462 decoration = decoration.icon(SizedBox::new().width(24.0).height(24.0));
21463 }
21464 pump_in_scaffold(tester, m3, direction, INPUT_WIDTH, decoration);
21465 };
21466
21467 pump(tester, TextDirection::Ltr, false);
21469 let ltr_start_without_icon = string_rect(tester, ERROR_TEXT).left;
21470 let ltr_end_without_icon = string_rect(tester, COUNTER_TEXT).right;
21471
21472 pump(tester, TextDirection::Ltr, true);
21475 let ltr_start_with_icon = string_rect(tester, ERROR_TEXT).left;
21476 let ltr_end_with_icon = string_rect(tester, COUNTER_TEXT).right;
21477 let icon_box_width = ltr_start_with_icon - ltr_start_without_icon;
21479 assert!(icon_box_width > 0.0); assert_eq!(ltr_end_with_icon, ltr_end_without_icon); pump(tester, TextDirection::Rtl, false);
21484 let rtl_start_without_icon = string_rect(tester, ERROR_TEXT).right;
21485 let rtl_end_without_icon = string_rect(tester, COUNTER_TEXT).left;
21486
21487 pump(tester, TextDirection::Rtl, true);
21490 let rtl_start_with_icon = string_rect(tester, ERROR_TEXT).right;
21491 let rtl_end_with_icon = string_rect(tester, COUNTER_TEXT).left;
21492 assert_eq!(rtl_start_without_icon - rtl_start_with_icon, icon_box_width);
21494 assert_eq!(rtl_end_with_icon, rtl_end_without_icon); });
21496 }
21497
21498 #[test]
21500 fn supporting_text_padding_in_material2_replaces_the_8_gap() {
21501 test_widgets(|tester| {
21502 let pump = |tester: &mut WidgetTester,
21503 padding: Option<EdgeInsetsGeometry>,
21504 error_text: Option<&str>,
21505 helper_text: Option<&str>| {
21506 let mut decoration = InputDecoration::new().filled(true);
21507 if let Some(error_text) = error_text {
21508 decoration = decoration.error_text(error_text);
21509 }
21510 if let Some(helper_text) = helper_text {
21511 decoration = decoration.helper_text(helper_text);
21512 }
21513 if let Some(padding) = padding {
21514 decoration = decoration.supporting_text_padding(padding);
21515 }
21516 let theme = m2(&tester.cell.borrow());
21517 let home = crate::scaffold::Scaffold::new().body(
21518 waft_widgets::Align::new()
21519 .alignment(waft_painting::AlignmentGeometry::Alignment(
21520 waft_painting::Alignment::TOP_LEFT,
21521 ))
21522 .child(
21523 SizedBox::new()
21524 .width(300.0)
21525 .child(InputDecorator::new(decoration)),
21526 ),
21527 );
21528 tester.pump_widget(material_app(theme, home.into_widget()));
21529 tester.pump_and_settle();
21530 };
21531 let gap = |tester: &WidgetTester, text: &str| {
21532 string_rect(tester, text).top - get_border_bottom(tester)
21533 };
21534
21535 pump(tester, None, None, Some(HELPER_TEXT));
21537 assert_eq!(gap(tester, HELPER_TEXT), 8.0);
21538
21539 pump(tester, None, Some(ERROR_TEXT), None);
21541 assert_eq!(gap(tester, ERROR_TEXT), 8.0);
21542
21543 const CUSTOM_TOP_PADDING: f64 = 16.0;
21545 pump(
21546 tester,
21547 Some(EdgeInsetsGeometry::directional(
21548 0.0,
21549 CUSTOM_TOP_PADDING,
21550 0.0,
21551 0.0,
21552 )),
21553 None,
21554 Some(HELPER_TEXT),
21555 );
21556 assert_eq!(gap(tester, HELPER_TEXT), CUSTOM_TOP_PADDING);
21557
21558 pump(
21561 tester,
21562 Some(EdgeInsetsGeometry::directional(24.0, 0.0, 0.0, 0.0)),
21563 None,
21564 Some(HELPER_TEXT),
21565 );
21566 assert_eq!(gap(tester, HELPER_TEXT), 0.0);
21567 });
21568 }
21569
21570 #[test]
21573 fn the_label_and_hint_take_their_own_text_directions() {
21574 test_widgets(|tester| {
21575 const HINT: &str = "Hint Text";
21576 const LABEL: &str = "Label Text";
21577 let theme = m3(&tester.cell.borrow());
21578 let home = crate::scaffold::Scaffold::new().body(Directionality::new(
21579 TextDirection::Ltr,
21580 waft_widgets::Center::new().child(
21581 InputDecorator::new(
21582 InputDecoration::new()
21583 .hint_text(HINT)
21584 .label_text(LABEL)
21585 .label_text_direction(TextDirection::Rtl)
21586 .hint_text_direction(TextDirection::Rtl),
21587 )
21588 .is_empty(true)
21589 .child(SizedBox::shrink()),
21590 ),
21591 ));
21592 tester.pump_widget(material_app(theme, home.into_widget()));
21593
21594 let direction_of = |text: &str| {
21595 let element = tester.element(find::text(text));
21596 tester.widget(element, |text: &waft_widgets::Text| text.text_direction)
21597 };
21598 assert_eq!(direction_of(HINT), Some(TextDirection::Rtl));
21599 assert_eq!(direction_of(LABEL), Some(TextDirection::Rtl));
21600 });
21601 }
21602
21603 fn check_floating_label_hit_test(tester: &mut WidgetTester, text_direction: TextDirection) {
21605 let tap_count = Rc::new(std::cell::Cell::new(0));
21606 let label_key = GlobalKey::new();
21607 let on_tap = {
21608 let tap_count = Rc::clone(&tap_count);
21609 waft_foundation::Listener::new(move |_app| tap_count.set(tap_count.get() + 1))
21610 };
21611 tester.pump_widget(
21612 BuildInputDecorator::new(
21613 InputDecoration::new()
21614 .border(OutlineInputBorder::new())
21615 .label(
21616 waft_widgets::GestureDetector::new()
21617 .key(label_key.clone().into())
21618 .behavior(waft_rendering::HitTestBehavior::Opaque)
21619 .on_tap(on_tap)
21620 .child(SizedBox::new().width(100.0).height(20.0)),
21621 ),
21622 )
21623 .text_direction(text_direction)
21624 .build(),
21625 );
21626
21627 let label_rect = key_rect(tester, &label_key);
21630 assert_eq!(label_rect.width(), 75.0);
21631
21632 tester.tap_at(label_rect.center(), 1);
21634 tester.tap_at(label_rect.bottom_left() + Offset::new(1.0, -1.0), 2);
21635 assert_eq!(tap_count.get(), 2);
21636
21637 tester.tap_at(label_rect.bottom_center() + Offset::new(0.0, 10.0), 3);
21639 assert_eq!(tap_count.get(), 2);
21640 }
21641
21642 #[test]
21645 fn the_floating_label_is_hit_where_it_is_painted_rtl() {
21646 test_widgets(|tester| check_floating_label_hit_test(tester, TextDirection::Rtl));
21647 }
21648
21649 #[test]
21652 fn the_floating_label_is_hit_where_it_is_painted_ltr() {
21653 test_widgets(|tester| check_floating_label_hit_test(tester, TextDirection::Ltr));
21654 }
21655
21656 #[test]
21658 fn a_material_2_decoration_describes_its_children_by_slot() {
21659 test_widgets(|tester| {
21660 M2::new(
21661 InputDecoration::new()
21662 .icon(Text::new("icon"))
21663 .label_text("label")
21664 .hint_text("hint")
21665 .prefix_text("prefix")
21666 .suffix_text("suffix")
21667 .prefix_icon(Text::new("prefixIcon"))
21668 .suffix_icon(Text::new("suffixIcon"))
21669 .helper_text("helper")
21670 .counter_text("counter"),
21671 )
21672 .child(Text::new("text"))
21673 .pump(tester);
21674
21675 let decoration = tester
21676 .render_object(find::by_type::<InputDecorator>())
21677 .downcast::<RenderDecoration>(&tester.cell.borrow())
21678 .expect("the decorator's render object");
21679 let names: Vec<String> = {
21680 let app = tester.cell.borrow();
21681 decoration
21682 .as_object()
21683 .to_diagnostics_node(&app, None)
21684 .children()
21685 .iter()
21686 .map(|node| node.name().expect("a named child").to_owned())
21687 .collect()
21688 };
21689 assert_eq!(
21690 names,
21691 [
21692 "icon",
21693 "input",
21694 "prefixIcon",
21695 "suffixIcon",
21696 "prefix",
21697 "suffix",
21698 "label",
21699 "hint",
21700 "helperError",
21701 "counter",
21702 "container",
21703 ]
21704 );
21705 });
21706 }
21707 }
21708 #[test]
21712 fn an_input_decoration_theme_data_describes_its_properties() {
21713 use waft_embedder::Color;
21714 use waft_foundation::{DiagnosticPropertiesBuilder, Diagnosticable};
21715 use waft_painting::{BorderSide, EdgeInsetsDirectional, TextStyle};
21716 use waft_rendering::BoxConstraints;
21717
21718 use crate::input_border::UnderlineInputBorder;
21719 use crate::{FloatingLabelAlignment, FloatingLabelBehavior, InputDecorationThemeData};
21720
21721 let red = Color::new(0xfff44336);
21722 let blue = Color::new(0xff2196f3);
21723 let green = Color::new(0xff4caf50);
21724 let constraints = BoxConstraints::new()
21725 .min_width(10.0)
21726 .max_width(10.0)
21727 .min_height(30.0)
21728 .max_height(30.0);
21729 let theme = InputDecorationThemeData::new()
21730 .label_style(TextStyle::new())
21731 .floating_label_style(TextStyle::new())
21732 .helper_style(TextStyle::new())
21733 .helper_max_lines(6)
21734 .hint_style(TextStyle::new())
21735 .error_style(TextStyle::new())
21736 .error_max_lines(5)
21737 .floating_label_behavior(FloatingLabelBehavior::Never)
21738 .floating_label_alignment(FloatingLabelAlignment::CENTER)
21739 .is_dense(true)
21740 .content_padding(EdgeInsetsDirectional::only(40.0, 12.0, 0.0, 12.0))
21741 .is_collapsed(true)
21742 .icon_color(red)
21743 .prefix_icon_color(blue)
21744 .prefix_icon_constraints(constraints)
21745 .prefix_style(TextStyle::new())
21746 .suffix_icon_color(blue)
21747 .suffix_icon_constraints(constraints)
21748 .suffix_style(TextStyle::new())
21749 .counter_style(TextStyle::new())
21750 .filled(true)
21751 .fill_color(red)
21752 .active_indicator_border(BorderSide::default())
21753 .outline_border(BorderSide::default())
21754 .focus_color(blue)
21755 .hover_color(green)
21756 .error_border(UnderlineInputBorder::new())
21757 .focused_border(UnderlineInputBorder::new())
21758 .focused_error_border(UnderlineInputBorder::new())
21759 .disabled_border(UnderlineInputBorder::new())
21760 .enabled_border(UnderlineInputBorder::new())
21761 .border(UnderlineInputBorder::new())
21762 .align_label_with_hint(true)
21763 .constraints(constraints);
21764 let description: Vec<String> = waft_widgets::testing::describe_properties(
21765 |properties: &mut DiagnosticPropertiesBuilder| theme.debug_fill_properties(properties),
21766 );
21767 assert_eq!(
21768 description,
21769 [
21770 "labelStyle: TextStyle(<all styles inherited>)".to_string(),
21771 "floatingLabelStyle: TextStyle(<all styles inherited>)".to_string(),
21772 "helperStyle: TextStyle(<all styles inherited>)".to_string(),
21773 "helperMaxLines: 6".to_string(),
21774 "hintStyle: TextStyle(<all styles inherited>)".to_string(),
21775 "errorStyle: TextStyle(<all styles inherited>)".to_string(),
21776 "errorMaxLines: 5".to_string(),
21777 "floatingLabelBehavior: FloatingLabelBehavior.never".to_string(),
21778 "floatingLabelAlignment: FloatingLabelAlignment.center".to_string(),
21779 "isDense: true".to_string(),
21780 "contentPadding: EdgeInsetsDirectional(40.0, 12.0, 0.0, 12.0)".to_string(),
21781 "isCollapsed: true".to_string(),
21782 format!("iconColor: {red}"),
21783 format!("prefixIconColor: {blue}"),
21784 "prefixIconConstraints: BoxConstraints(w=10.0, h=30.0)".to_string(),
21785 "prefixStyle: TextStyle(<all styles inherited>)".to_string(),
21786 format!("suffixIconColor: {blue}"),
21787 "suffixIconConstraints: BoxConstraints(w=10.0, h=30.0)".to_string(),
21788 "suffixStyle: TextStyle(<all styles inherited>)".to_string(),
21789 "counterStyle: TextStyle(<all styles inherited>)".to_string(),
21790 "filled: true".to_string(),
21791 format!("fillColor: {red}"),
21792 "activeIndicatorBorder: BorderSide".to_string(),
21793 "outlineBorder: BorderSide".to_string(),
21794 format!("focusColor: {blue}"),
21795 format!("hoverColor: {green}"),
21796 "errorBorder: UnderlineInputBorder()".to_string(),
21797 "focusedBorder: UnderlineInputBorder()".to_string(),
21798 "focusedErrorBorder: UnderlineInputBorder()".to_string(),
21799 "disabledBorder: UnderlineInputBorder()".to_string(),
21800 "enabledBorder: UnderlineInputBorder()".to_string(),
21801 "border: UnderlineInputBorder()".to_string(),
21802 "alignLabelWithHint: true".to_string(),
21803 "constraints: BoxConstraints(w=10.0, h=30.0)".to_string(),
21804 ]
21805 );
21806 }
21807}