waft-material 0.7.0

Material Design widgets for Waft
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
//! Flutter counterpart: material_ui's `text_form_field.dart`.
//!
//! [`TextFormField`] is a `FormField<String>` leaf: its widget carries the form field's
//! [`FormFieldData`] and the arguments Dart's constructor captures in the builder closure, kept
//! as the [`TextField`] the builder builds; [`TextFormField::build_field`] is that closure.
#![allow(deprecated)]

use std::any::TypeId;
use std::fmt::{self, Debug};
use std::rc::Rc;

use waft_embedder::{
    BoxHeightStyle, BoxWidthStyle, Brightness, Clip, Color, Locale, Radius, SmartDashesType,
    SmartQuotesType, TextCapitalization, TextEditingValue, TextInputAction, TextInputType,
};
use waft_foundation::{App, Handle, Listenable, Listener, OptionRetained, Ref, ValueChanged};
use waft_gestures::{DragStartBehavior, GestureTapCallback, PointerDownEvent, PointerUpEvent};
use waft_painting::{
    AnyTextStyle, EdgeInsets, StrutStyle, TextAlign, TextAlignVertical, TextDirection,
};
use waft_services::{
    MaxLengthEnforcement, MouseCursorRef, RestorationBucket, TextInputFormatterRef,
};
use waft_widgets::{
    AnyFormFieldState, AutovalidateMode, BuildContext, ContentInsertionConfiguration, EditableText,
    EditableTextContextMenuBuilder, EditableTextState, FocusNodeRef, FormFieldData, FormFieldLeaf,
    FormFieldState, FormFieldStateData, FormFieldStateLeaf, IntoWidget, KeyRef,
    RestorableListenable, RestorableProperty, RestorableTextEditingController, RestorationMixin,
    RestorationMixinData, ScrollControllerRef, ScrollPhysicsRef, SpellCheckConfiguration, State,
    StateData, StatefulWidget, SystemContextMenu, TextEditingControllerRef,
    TextMagnifierConfiguration, TextSelectionControls, ToolbarOptions, UndoHistoryController,
    UnmanagedRestorationScope, WidgetRef,
};

use crate::adaptive_text_selection_toolbar::AdaptiveTextSelectionToolbar;
use crate::input_decorator::{InputDecoration, InputDecorationTheme};
use crate::material_state::MaterialStatesController;
use crate::text_field::TextField;

/// A [`FormField`](waft_widgets::FormField) that contains a [`TextField`].
///
/// This is a convenience widget that wraps a [`TextField`] widget in a `FormField`.
///
/// A `Form` ancestor is not required. The `Form` allows one to save, reset, or validate
/// multiple fields at once. To use without a `Form`, pass a `GlobalKey` to the constructor and
/// use `GlobalKey::current_state` to save or reset the form field.
///
/// When a [`controller`](Self::controller) is specified, its `TextEditingController::text`
/// defines the [`initial_value`](FormFieldData::initial_value). If this `FormField` is part of
/// a scrolling container that lazily constructs its children, like a `ListView` or a
/// `CustomScrollView`, then a [`controller`](Self::controller) should be specified. The
/// controller's lifetime should be managed by a stateful widget ancestor of the scrolling
/// container.
///
/// If a [`controller`](Self::controller) is not specified, [`initial_value`](Self::initial_value)
/// can be used to give the automatically generated controller an initial value.
///
/// For a documentation about the various parameters, see the [`TextField`] class and
/// `TextField::new`, the constructor.
pub struct TextFormField {
    form_field: FormFieldData<String>,
    /// {@macro flutter.widgets.editableText.groupId}
    pub group_id: TypeId,
    /// Controls the text being edited.
    ///
    /// If null, this widget will create its own `TextEditingController` and initialize its
    /// `TextEditingController::text` with [`initial_value`](FormFieldData::initial_value).
    pub controller: Option<TextEditingControllerRef>,
    /// Called when the user initiates a change to the TextField's value: when they have
    /// inserted or deleted text or reset the form.
    pub on_changed: Option<ValueChanged<String>>,
    /// Dart's constructor argument `initialValue`, which fills the form field's
    /// [`initial_value`](FormFieldData::initial_value) when there is no
    /// [`controller`](Self::controller).
    initial_value: Option<String>,
    /// The constructor's other arguments, which Dart's builder closure captures: the
    /// [`TextField`] it builds before the state fills the controller, the decoration, the
    /// `on_changed` handler and `enabled`.
    text_field: TextField,
}

impl TextFormField {
    /// Creates a [`FormField`](waft_widgets::FormField) that contains a [`TextField`].
    pub fn new() -> TextFormField {
        let mut form_field = FormFieldData::new(Rc::new(TextFormField::build_field));
        form_field.initial_value = Some(String::new());
        form_field.enabled = true;
        form_field.autovalidate_mode = AutovalidateMode::Disabled;
        TextFormField {
            form_field,
            group_id: TypeId::of::<EditableText>(),
            controller: None,
            on_changed: None,
            initial_value: None,
            text_field: TextField::new()
                .autocorrect(true)
                .context_menu_builder(Some(Rc::new(Self::default_context_menu_builder))),
        }
    }

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

    /// Dart `TextFormField(groupId:)`.
    pub fn group_id(mut self, group_id: TypeId) -> TextFormField {
        self.group_id = group_id;
        self
    }

    /// Dart `TextFormField(controller:)`.
    pub fn controller(mut self, controller: TextEditingControllerRef) -> TextFormField {
        self.controller = Some(controller);
        self.form_field.initial_value = self.form_field_initial_value();
        self
    }

    /// Dart `TextFormField(initialValue:)`.
    pub fn initial_value(mut self, initial_value: impl Into<String>) -> TextFormField {
        self.initial_value = Some(initial_value.into());
        self.form_field.initial_value = self.form_field_initial_value();
        self
    }

    /// Dart `TextFormField(focusNode:)`.
    pub fn focus_node(mut self, focus_node: FocusNodeRef) -> TextFormField {
        self.text_field = self.text_field.focus_node(focus_node);
        self
    }

    /// Dart `TextFormField(forceErrorText:)`.
    pub fn force_error_text(mut self, force_error_text: impl Into<String>) -> TextFormField {
        self.form_field.force_error_text = Some(force_error_text.into());
        self
    }

    /// Dart `TextFormField(decoration:)`.
    pub fn decoration(mut self, decoration: Option<InputDecoration>) -> TextFormField {
        self.text_field = self.text_field.decoration(decoration);
        self.form_field.enabled = self.effective_enabled();
        self
    }

    /// Dart `TextFormField(keyboardType:)`.
    pub fn keyboard_type(mut self, keyboard_type: TextInputType) -> TextFormField {
        self.text_field = self.text_field.keyboard_type(keyboard_type);
        self
    }

    /// Dart `TextFormField(textCapitalization:)`.
    pub fn text_capitalization(mut self, text_capitalization: TextCapitalization) -> TextFormField {
        self.text_field = self.text_field.text_capitalization(text_capitalization);
        self
    }

    /// Dart `TextFormField(textInputAction:)`.
    pub fn text_input_action(mut self, text_input_action: TextInputAction) -> TextFormField {
        self.text_field = self.text_field.text_input_action(text_input_action);
        self
    }

    /// Dart `TextFormField(style:)`.
    pub fn style(mut self, style: impl Into<AnyTextStyle>) -> TextFormField {
        self.text_field = self.text_field.style(style);
        self
    }

    /// Dart `TextFormField(strutStyle:)`.
    pub fn strut_style(mut self, strut_style: StrutStyle) -> TextFormField {
        self.text_field = self.text_field.strut_style(strut_style);
        self
    }

    /// Dart `TextFormField(textDirection:)`.
    pub fn text_direction(mut self, text_direction: TextDirection) -> TextFormField {
        self.text_field = self.text_field.text_direction(text_direction);
        self
    }

    /// Dart `TextFormField(textAlign:)`.
    pub fn text_align(mut self, text_align: TextAlign) -> TextFormField {
        self.text_field = self.text_field.text_align(text_align);
        self
    }

    /// Dart `TextFormField(textAlignVertical:)`.
    pub fn text_align_vertical(mut self, text_align_vertical: TextAlignVertical) -> TextFormField {
        self.text_field = self.text_field.text_align_vertical(text_align_vertical);
        self
    }

    /// Dart `TextFormField(autofocus:)`.
    pub fn autofocus(mut self, autofocus: bool) -> TextFormField {
        self.text_field = self.text_field.autofocus(autofocus);
        self
    }

    /// Dart `TextFormField(readOnly:)`.
    pub fn read_only(mut self, read_only: bool) -> TextFormField {
        self.text_field = self.text_field.read_only(read_only);
        self
    }

    /// Dart `TextFormField(toolbarOptions:)`.
    #[deprecated(note = "Use `context_menu_builder` instead.")]
    pub fn toolbar_options(mut self, toolbar_options: ToolbarOptions) -> TextFormField {
        self.text_field = self.text_field.toolbar_options(toolbar_options);
        self
    }

    /// Dart `TextFormField(showCursor:)`.
    pub fn show_cursor(mut self, show_cursor: bool) -> TextFormField {
        self.text_field = self.text_field.show_cursor(show_cursor);
        self
    }

    /// Dart `TextFormField(obscuringCharacter:)`.
    pub fn obscuring_character(mut self, obscuring_character: impl Into<String>) -> TextFormField {
        self.text_field = self.text_field.obscuring_character(obscuring_character);
        self
    }

    /// Dart `TextFormField(obscureText:)`.
    pub fn obscure_text(mut self, obscure_text: bool) -> TextFormField {
        self.text_field = self.text_field.obscure_text(obscure_text);
        self
    }

    /// Dart `TextFormField(autocorrect:)`.
    pub fn autocorrect(mut self, autocorrect: bool) -> TextFormField {
        self.text_field = self.text_field.autocorrect(autocorrect);
        self
    }

    /// Dart `TextFormField(smartDashesType:)`.
    pub fn smart_dashes_type(mut self, smart_dashes_type: SmartDashesType) -> TextFormField {
        self.text_field = self.text_field.smart_dashes_type(smart_dashes_type);
        self
    }

    /// Dart `TextFormField(smartQuotesType:)`.
    pub fn smart_quotes_type(mut self, smart_quotes_type: SmartQuotesType) -> TextFormField {
        self.text_field = self.text_field.smart_quotes_type(smart_quotes_type);
        self
    }

    /// Dart `TextFormField(enableSuggestions:)`.
    pub fn enable_suggestions(mut self, enable_suggestions: bool) -> TextFormField {
        self.text_field = self.text_field.enable_suggestions(enable_suggestions);
        self
    }

    /// Dart `TextFormField(maxLengthEnforcement:)`.
    pub fn max_length_enforcement(
        mut self,
        max_length_enforcement: MaxLengthEnforcement,
    ) -> TextFormField {
        self.text_field = self
            .text_field
            .max_length_enforcement(max_length_enforcement);
        self
    }

    /// Dart `TextFormField(maxLines:)`.
    pub fn max_lines(mut self, max_lines: Option<i32>) -> TextFormField {
        self.text_field = self.text_field.max_lines(max_lines);
        self
    }

    /// Dart `TextFormField(minLines:)`.
    pub fn min_lines(mut self, min_lines: i32) -> TextFormField {
        self.text_field = self.text_field.min_lines(min_lines);
        self
    }

    /// Dart `TextFormField(expands:)`.
    pub fn expands(mut self, expands: bool) -> TextFormField {
        self.text_field = self.text_field.expands(expands);
        self
    }

    /// Dart `TextFormField(maxLength:)`.
    pub fn max_length(mut self, max_length: i32) -> TextFormField {
        self.text_field = self.text_field.max_length(max_length);
        self
    }

    /// Dart `TextFormField(onChanged:)`.
    pub fn on_changed(mut self, on_changed: impl Fn(&mut App, String) + 'static) -> TextFormField {
        self.on_changed = Some(Rc::new(on_changed));
        self
    }

    /// Dart `TextFormField(onTap:)`.
    pub fn on_tap(mut self, on_tap: GestureTapCallback) -> TextFormField {
        self.text_field = self.text_field.on_tap(on_tap);
        self
    }

    /// Dart `TextFormField(onTapAlwaysCalled:)`.
    pub fn on_tap_always_called(mut self, on_tap_always_called: bool) -> TextFormField {
        self.text_field = self.text_field.on_tap_always_called(on_tap_always_called);
        self
    }

    /// Dart `TextFormField(onTapOutside:)`.
    pub fn on_tap_outside(
        mut self,
        on_tap_outside: impl Fn(&mut App, PointerDownEvent) + 'static,
    ) -> TextFormField {
        self.text_field = self.text_field.on_tap_outside(on_tap_outside);
        self
    }

    /// Dart `TextFormField(onTapUpOutside:)`.
    pub fn on_tap_up_outside(
        mut self,
        on_tap_up_outside: impl Fn(&mut App, PointerUpEvent) + 'static,
    ) -> TextFormField {
        self.text_field = self.text_field.on_tap_up_outside(on_tap_up_outside);
        self
    }

    /// Dart `TextFormField(onEditingComplete:)`.
    pub fn on_editing_complete(
        mut self,
        on_editing_complete: impl Fn(&mut App) + 'static,
    ) -> TextFormField {
        self.text_field = self.text_field.on_editing_complete(on_editing_complete);
        self
    }

    /// Dart `TextFormField(onFieldSubmitted:)`.
    pub fn on_field_submitted(
        mut self,
        on_field_submitted: impl Fn(&mut App, String) + 'static,
    ) -> TextFormField {
        self.text_field = self.text_field.on_submitted(on_field_submitted);
        self
    }

    /// Dart `TextFormField(onSaved:)`.
    pub fn on_saved(
        mut self,
        on_saved: impl Fn(&mut App, Option<String>) + 'static,
    ) -> TextFormField {
        self.form_field.on_saved = Some(Rc::new(on_saved));
        self
    }

    /// Dart `TextFormField(validator:)`.
    pub fn validator(
        mut self,
        validator: impl Fn(&mut App, Option<&String>) -> Option<String> + 'static,
    ) -> TextFormField {
        self.form_field.validator = Some(Rc::new(validator));
        self
    }

    /// Dart `TextFormField(errorBuilder:)`.
    pub fn error_builder(
        mut self,
        error_builder: impl Fn(&mut App, BuildContext, String) -> WidgetRef + 'static,
    ) -> TextFormField {
        self.form_field.error_builder = Some(Rc::new(error_builder));
        self
    }

    /// Dart `TextFormField(inputFormatters:)`.
    pub fn input_formatters(
        mut self,
        input_formatters: Vec<TextInputFormatterRef>,
    ) -> TextFormField {
        self.text_field = self.text_field.input_formatters(input_formatters);
        self
    }

    /// Dart `TextFormField(enabled:)`.
    pub fn enabled(mut self, enabled: bool) -> TextFormField {
        self.text_field = self.text_field.enabled(enabled);
        self.form_field.enabled = self.effective_enabled();
        self
    }

    /// Dart `TextFormField(ignorePointers:)`.
    pub fn ignore_pointers(mut self, ignore_pointers: bool) -> TextFormField {
        self.text_field = self.text_field.ignore_pointers(ignore_pointers);
        self
    }

    /// Dart `TextFormField(cursorWidth:)`.
    pub fn cursor_width(mut self, cursor_width: f64) -> TextFormField {
        self.text_field = self.text_field.cursor_width(cursor_width);
        self
    }

    /// Dart `TextFormField(cursorHeight:)`.
    pub fn cursor_height(mut self, cursor_height: f64) -> TextFormField {
        self.text_field = self.text_field.cursor_height(cursor_height);
        self
    }

    /// Dart `TextFormField(cursorRadius:)`.
    pub fn cursor_radius(mut self, cursor_radius: Radius) -> TextFormField {
        self.text_field = self.text_field.cursor_radius(cursor_radius);
        self
    }

    /// Dart `TextFormField(cursorColor:)`.
    pub fn cursor_color(mut self, cursor_color: Color) -> TextFormField {
        self.text_field = self.text_field.cursor_color(cursor_color);
        self
    }

    /// Dart `TextFormField(cursorErrorColor:)`.
    pub fn cursor_error_color(mut self, cursor_error_color: Color) -> TextFormField {
        self.text_field = self.text_field.cursor_error_color(cursor_error_color);
        self
    }

    /// Dart `TextFormField(keyboardAppearance:)`.
    pub fn keyboard_appearance(mut self, keyboard_appearance: Brightness) -> TextFormField {
        self.text_field = self.text_field.keyboard_appearance(keyboard_appearance);
        self
    }

    /// Dart `TextFormField(scrollPadding:)`.
    pub fn scroll_padding(mut self, scroll_padding: EdgeInsets) -> TextFormField {
        self.text_field = self.text_field.scroll_padding(scroll_padding);
        self
    }

    /// Dart `TextFormField(enableInteractiveSelection:)`.
    pub fn enable_interactive_selection(
        mut self,
        enable_interactive_selection: bool,
    ) -> TextFormField {
        self.text_field = self
            .text_field
            .enable_interactive_selection(enable_interactive_selection);
        self
    }

    /// Dart `TextFormField(selectAllOnFocus:)`.
    pub fn select_all_on_focus(mut self, select_all_on_focus: bool) -> TextFormField {
        self.text_field = self.text_field.select_all_on_focus(select_all_on_focus);
        self
    }

    /// Dart `TextFormField(selectionControls:)`.
    pub fn selection_controls(
        mut self,
        selection_controls: Rc<dyn TextSelectionControls>,
    ) -> TextFormField {
        self.text_field = self.text_field.selection_controls(selection_controls);
        self
    }

    /// Dart `TextFormField(buildCounter:)`.
    pub fn build_counter(
        mut self,
        build_counter: impl Fn(&mut App, BuildContext, i32, Option<i32>, bool) -> Option<WidgetRef>
        + 'static,
    ) -> TextFormField {
        self.text_field = self.text_field.build_counter(build_counter);
        self
    }

    /// Dart `TextFormField(scrollPhysics:)`.
    pub fn scroll_physics(mut self, scroll_physics: ScrollPhysicsRef) -> TextFormField {
        self.text_field = self.text_field.scroll_physics(scroll_physics);
        self
    }

    /// Dart `TextFormField(autofillHints:)`.
    pub fn autofill_hints(mut self, autofill_hints: Vec<String>) -> TextFormField {
        self.text_field = self.text_field.autofill_hints(autofill_hints);
        self
    }

    /// Dart `TextFormField(autovalidateMode:)`.
    pub fn autovalidate_mode(mut self, autovalidate_mode: AutovalidateMode) -> TextFormField {
        self.form_field.autovalidate_mode = autovalidate_mode;
        self
    }

    /// Dart `TextFormField(scrollController:)`.
    pub fn scroll_controller(mut self, scroll_controller: ScrollControllerRef) -> TextFormField {
        self.text_field = self.text_field.scroll_controller(scroll_controller);
        self
    }

    /// Dart `TextFormField(restorationId:)`.
    pub fn restoration_id(mut self, restoration_id: impl Into<String>) -> TextFormField {
        let restoration_id = restoration_id.into();
        self.form_field.restoration_id = Some(restoration_id.clone());
        self.text_field = self.text_field.restoration_id(restoration_id);
        self
    }

    /// Dart `TextFormField(enableIMEPersonalizedLearning:)`.
    pub fn enable_ime_personalized_learning(
        mut self,
        enable_ime_personalized_learning: bool,
    ) -> TextFormField {
        self.text_field = self
            .text_field
            .enable_ime_personalized_learning(enable_ime_personalized_learning);
        self
    }

    /// Dart `TextFormField(enableInlinePrediction:)`.
    pub fn enable_inline_prediction(mut self, enable_inline_prediction: bool) -> TextFormField {
        self.text_field = self
            .text_field
            .enable_inline_prediction(enable_inline_prediction);
        self
    }

    /// Dart `TextFormField(mouseCursor:)`.
    pub fn mouse_cursor(mut self, mouse_cursor: MouseCursorRef) -> TextFormField {
        self.text_field = self.text_field.mouse_cursor(mouse_cursor);
        self
    }

    /// Dart `TextFormField(contextMenuBuilder:)`.
    pub fn context_menu_builder(
        mut self,
        context_menu_builder: Option<EditableTextContextMenuBuilder>,
    ) -> TextFormField {
        self.text_field = self.text_field.context_menu_builder(context_menu_builder);
        self
    }

    /// Dart `TextFormField(spellCheckConfiguration:)`.
    pub fn spell_check_configuration(
        mut self,
        spell_check_configuration: SpellCheckConfiguration,
    ) -> TextFormField {
        self.text_field = self
            .text_field
            .spell_check_configuration(spell_check_configuration);
        self
    }

    /// Dart `TextFormField(magnifierConfiguration:)`.
    pub fn magnifier_configuration(
        mut self,
        magnifier_configuration: TextMagnifierConfiguration,
    ) -> TextFormField {
        self.text_field = self
            .text_field
            .magnifier_configuration(magnifier_configuration);
        self
    }

    /// Dart `TextFormField(undoController:)`.
    pub fn undo_controller(mut self, undo_controller: Ref<UndoHistoryController>) -> TextFormField {
        self.text_field = self.text_field.undo_controller(undo_controller);
        self
    }

    /// Dart `TextFormField(onAppPrivateCommand:)`.
    pub fn on_app_private_command(
        mut self,
        on_app_private_command: impl Fn(&mut App, &str) + 'static,
    ) -> TextFormField {
        self.text_field = self
            .text_field
            .on_app_private_command(on_app_private_command);
        self
    }

    /// Dart `TextFormField(cursorOpacityAnimates:)`.
    pub fn cursor_opacity_animates(mut self, cursor_opacity_animates: bool) -> TextFormField {
        self.text_field = self
            .text_field
            .cursor_opacity_animates(cursor_opacity_animates);
        self
    }

    /// Dart `TextFormField(selectionHeightStyle:)`.
    pub fn selection_height_style(
        mut self,
        selection_height_style: BoxHeightStyle,
    ) -> TextFormField {
        self.text_field = self
            .text_field
            .selection_height_style(selection_height_style);
        self
    }

    /// Dart `TextFormField(selectionWidthStyle:)`.
    pub fn selection_width_style(mut self, selection_width_style: BoxWidthStyle) -> TextFormField {
        self.text_field = self.text_field.selection_width_style(selection_width_style);
        self
    }

    /// Dart `TextFormField(dragStartBehavior:)`.
    pub fn drag_start_behavior(mut self, drag_start_behavior: DragStartBehavior) -> TextFormField {
        self.text_field = self.text_field.drag_start_behavior(drag_start_behavior);
        self
    }

    /// Dart `TextFormField(contentInsertionConfiguration:)`.
    pub fn content_insertion_configuration(
        mut self,
        content_insertion_configuration: ContentInsertionConfiguration,
    ) -> TextFormField {
        self.text_field = self
            .text_field
            .content_insertion_configuration(content_insertion_configuration);
        self
    }

    /// Dart `TextFormField(statesController:)`.
    pub fn states_controller(
        mut self,
        states_controller: Ref<MaterialStatesController>,
    ) -> TextFormField {
        self.text_field = self.text_field.states_controller(states_controller);
        self
    }

    /// Dart `TextFormField(clipBehavior:)`.
    pub fn clip_behavior(mut self, clip_behavior: Clip) -> TextFormField {
        self.text_field = self.text_field.clip_behavior(clip_behavior);
        self
    }

    /// Dart `TextFormField(scribbleEnabled:)`.
    #[deprecated(note = "Use `stylus_handwriting_enabled` instead.")]
    pub fn scribble_enabled(mut self, scribble_enabled: bool) -> TextFormField {
        self.text_field = self.text_field.scribble_enabled(scribble_enabled);
        self
    }

    /// Dart `TextFormField(stylusHandwritingEnabled:)`.
    pub fn stylus_handwriting_enabled(mut self, stylus_handwriting_enabled: bool) -> TextFormField {
        self.text_field = self
            .text_field
            .stylus_handwriting_enabled(stylus_handwriting_enabled);
        self
    }

    /// Dart `TextFormField(canRequestFocus:)`.
    pub fn can_request_focus(mut self, can_request_focus: bool) -> TextFormField {
        self.text_field = self.text_field.can_request_focus(can_request_focus);
        self
    }

    /// Dart `TextFormField(hintLocales:)`.
    pub fn hint_locales(mut self, hint_locales: Vec<Locale>) -> TextFormField {
        self.text_field = self.text_field.hint_locales(hint_locales);
        self
    }

    fn default_context_menu_builder(
        app: &mut App,
        _context: BuildContext,
        editable_text_state: Handle<EditableTextState>,
    ) -> WidgetRef {
        if SystemContextMenu::is_supported_by_field(app, editable_text_state) {
            return SystemContextMenu::editable_text(app, editable_text_state).into_widget();
        }
        AdaptiveTextSelectionToolbar::editable_text(app, editable_text_state).into_widget()
    }

    /// Dart's `controller != null ? controller.text : (initialValue ?? '')`, which the
    /// constructor hands the form field; with a controller it is `None` here, as its text needs
    /// the App, and the state reads it when the widget arrives.
    fn form_field_initial_value(&self) -> Option<String> {
        match self.controller {
            Some(_) => None,
            None => Some(self.initial_value.clone().unwrap_or_default()),
        }
    }

    /// Dart's `enabled ?? decoration?.enabled ?? true`, which the constructor hands both the
    /// form field and the text field.
    fn effective_enabled(&self) -> bool {
        self.text_field
            .enabled
            .or(self
                .text_field
                .decoration
                .as_ref()
                .map(|decoration| decoration.enabled))
            .unwrap_or(true)
    }

    /// Dart's builder closure: the [`TextField`] for the form field's current state.
    fn build_field(app: &mut App, field: AnyFormFieldState<String>) -> WidgetRef {
        let state = field
            .downcast::<TextFormFieldState>(app)
            .expect("a TextFormField's state is a TextFormFieldState");
        let context = field.context(app);
        let decoration_theme = InputDecorationTheme::of(app, context);
        let widget = state.widget(app);
        let mut effective_decoration = widget
            .text_field
            .decoration
            .clone()
            .unwrap_or_default()
            .apply_defaults(decoration_theme);
        let error_builder = widget.form_field.error_builder.clone();

        if let Some(error_text) = field.error_text(app) {
            effective_decoration = match error_builder {
                Some(error_builder) => {
                    let error = error_builder(app, state.context(app), error_text);
                    effective_decoration.copy_with().error(error)
                }
                None => effective_decoration.copy_with().error_text(error_text),
            };
        }

        let widget = state.widget(app);
        let on_changed = widget.on_changed.clone();
        let on_changed_handler = move |app: &mut App, value: String| {
            field.did_change(app, Some(value.clone()));
            if let Some(on_changed) = &on_changed {
                on_changed(app, value);
            }
        };

        let text_field = &widget.text_field;
        let child = text_field
            .clone()
            .group_id(widget.group_id)
            .controller(state.effective_controller(app))
            .decoration(Some(effective_decoration))
            .smart_dashes_type(text_field.effective_smart_dashes_type())
            .smart_quotes_type(text_field.effective_smart_quotes_type())
            .on_changed(on_changed_handler)
            .enabled(widget.effective_enabled())
            .enable_interactive_selection(
                text_field
                    .enable_interactive_selection
                    .unwrap_or(!text_field.obscure_text || !text_field.read_only),
            )
            .selection_height_style(
                text_field
                    .selection_height_style
                    .unwrap_or_else(EditableText::default_selection_height_style),
            )
            .selection_width_style(
                text_field
                    .selection_width_style
                    .unwrap_or_else(EditableText::default_selection_width_style),
            );

        UnmanagedRestorationScope::new(child)
            .bucket(state.bucket(app).and_then(|bucket| bucket.upgrade(app)))
            .into_widget()
    }

    /// Dart's constructor asserts across fields.
    fn debug_check_constructor(&self) {
        let text_field = &self.text_field;
        debug_assert!(self.initial_value.is_none() || self.controller.is_none());
        debug_assert!(
            text_field
                .max_lines
                .is_none_or(|max| text_field.min_lines.is_none_or(|min| max >= min)),
            "minLines can't be greater than maxLines"
        );
        debug_assert!(
            !text_field.expands
                || (text_field.max_lines.is_none() && text_field.min_lines.is_none()),
            "minLines and maxLines must be null when expands is true."
        );
        debug_assert!(
            !text_field.obscure_text || text_field.max_lines == Some(1),
            "Obscured fields cannot be multiline."
        );
        debug_assert!(
            self.form_field.error_builder.is_none()
                || text_field
                    .decoration
                    .as_ref()
                    .is_none_or(|decoration| decoration.error_text.is_none()),
            "Declaring both errorBuilder and decoration.errorText is not supported."
        );
    }
}

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

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

impl FormFieldLeaf for TextFormField {
    type Value = String;
    waft_widgets::form_field_accessors!();
}

impl StatefulWidget for TextFormField {
    type State = TextFormFieldState;

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

    fn debug_check_constructor(&self) {
        TextFormField::debug_check_constructor(self);
    }

    fn create_state(&self) -> TextFormFieldState {
        TextFormFieldState {
            state: StateData::new(),
            restoration: RestorationMixinData::new(),
            form_field_state: FormFieldStateData::new(&self.form_field),
            controller: None,
            initial_value: None,
            widget_initial_value: None,
        }
    }
}

/// Dart's `_TextFormFieldState`.
pub struct TextFormFieldState {
    state: StateData<TextFormField>,
    restoration: RestorationMixinData,
    form_field_state: FormFieldStateData<String>,
    controller: Option<Ref<RestorableTextEditingController>>,
    initial_value: Option<String>,
    /// Dart's `widget.initialValue` for a widget with a controller: the controller's text when
    /// the widget arrived, which Dart's constructor reads.
    widget_initial_value: Option<String>,
}

impl TextFormFieldState {
    fn effective_controller(self: Handle<Self>, app: &App) -> TextEditingControllerRef {
        if let Some(controller) = self.widget(app).controller.clone() {
            return controller;
        }
        let restorable = app
            .get(self)
            .controller
            .handle()
            .expect("a local controller exists when the widget does not supply one");
        RestorableListenable::value(restorable, app)
            .keep(app)
            .into_erased()
    }

    fn register_controller(self: Handle<Self>, app: &mut App) {
        let controller = app
            .get(self)
            .controller
            .handle()
            .expect("register_controller needs a local controller");
        self.register_for_restoration(app, controller.as_property(), "controller");
    }

    fn create_local_controller(self: Handle<Self>, app: &mut App, value: Option<TextEditingValue>) {
        debug_assert!(app.get(self).controller.is_none());
        let controller = match value {
            None => RestorableTextEditingController::new(app),
            Some(value) => RestorableTextEditingController::from_value(app, value),
        };
        app.get_mut(self).controller = Some(controller.clone());
        if !self.restore_pending(app) {
            self.register_controller(app);
        }
    }

    /// Records what Dart's constructor gives the form field as `initialValue` when the widget
    /// has a controller: the controller's text.
    fn read_widget_initial_value(self: Handle<Self>, app: &mut App) {
        let text = self
            .widget(app)
            .controller
            .as_ref()
            .map(|controller| controller.text_value(app).to_owned());
        app.get_mut(self).widget_initial_value = text;
    }

    fn handle_controller_changed(self: Handle<Self>, app: &mut App) {
        // Suppress changes that originated from within this class.
        //
        // In the case where a controller has been passed in to this widget, we
        // register this change listener. In these cases, we'll also receive change
        // notifications for changes originating from within this class -- for
        // example, the reset() method. In such cases, the FormField value will
        // already have been set.
        let text = self.effective_controller(app).text_value(app).to_owned();
        if self.value(app) != Some(&text) {
            self.did_change(app, Some(text));
        }
    }
}

impl RestorationMixin for TextFormFieldState {
    waft_widgets::restoration_mixin_accessors!();

    fn restoration_id(self: Handle<Self>, app: &App) -> Option<&str> {
        FormFieldState::restoration_id(self, app)
    }

    fn restore_state(
        self: Handle<Self>,
        app: &mut App,
        old_bucket: Option<Handle<RestorationBucket>>,
        initial_restore: bool,
    ) {
        FormFieldState::restore_state(self, app, old_bucket, initial_restore);
        if app.get(self).controller.is_some() {
            self.register_controller(app);
        }
        // Make sure to update the internal [FormFieldState] value to sync up with
        // text editing controller value.
        let text = self.effective_controller(app).text_value(app).to_owned();
        self.set_value(app, Some(text));
    }
}

impl FormFieldStateLeaf for TextFormFieldState {
    waft_widgets::form_field_state_accessors!();

    fn did_change(self: Handle<Self>, app: &mut App, value: Option<String>) {
        FormFieldState::did_change(self, app, value.clone());

        let controller = self.effective_controller(app);
        if Some(controller.text_value(app)) != value.as_deref() {
            controller.set_value(app, TextEditingValue::new().text(value.unwrap_or_default()));
        }
    }

    fn reset(self: Handle<Self>, app: &mut App) {
        // Set the controller value before calling super.reset() to let
        // _handleControllerChanged suppress the change.
        let initial_value = app.get(self).initial_value.clone().unwrap_or_default();
        self.effective_controller(app)
            .set_value(app, TextEditingValue::new().text(initial_value));
        FormFieldState::reset(self, app);
        if self.widget(app).controller.is_some() {
            let widget_initial_value = app.get(self).widget_initial_value.clone();
            self.set_value(app, widget_initial_value);
        }
        if let Some(on_changed) = self.widget(app).on_changed.clone() {
            let text = self.effective_controller(app).text_value(app).to_owned();
            on_changed(app, text);
        }
    }
}

impl State for TextFormFieldState {
    type Widget = TextFormField;
    waft_widgets::state_accessors!();

    fn init_state(self: Handle<Self>, app: &mut App) {
        FormFieldState::init_state(self, app);
        match self.widget(app).controller.clone() {
            None => {
                let value = self
                    .widget(app)
                    .form_field
                    .initial_value
                    .clone()
                    .map(|initial_value| TextEditingValue::new().text(initial_value));
                self.create_local_controller(app, value);
            }
            Some(controller) => {
                controller.add_listener(
                    app,
                    Listener::handle_method(self, Self::handle_controller_changed),
                );
            }
        }
        self.read_widget_initial_value(app);
        let initial_value = self
            .widget(app)
            .form_field
            .initial_value
            .clone()
            .or_else(|| app.get(self).widget_initial_value.clone());
        app.get_mut(self).initial_value = initial_value;
        if self.widget(app).controller.is_some() {
            let value = app.get(self).widget_initial_value.clone();
            self.set_value(app, value);
        }
    }

    fn did_update_widget(self: Handle<Self>, app: &mut App, old_widget: &TextFormField) {
        FormFieldState::did_update_widget(self, app, old_widget);
        self.read_widget_initial_value(app);
        if self.widget(app).controller != old_widget.controller {
            if let Some(old_controller) = old_widget.controller.clone() {
                old_controller.remove_listener(
                    app,
                    &Listener::handle_method(self, Self::handle_controller_changed),
                );
            }
            if let Some(controller) = self.widget(app).controller.clone() {
                controller.add_listener(
                    app,
                    Listener::handle_method(self, Self::handle_controller_changed),
                );
            }

            if let Some(old_controller) = old_widget.controller.clone()
                && self.widget(app).controller.is_none()
            {
                let value = old_controller.value(app).clone();
                self.create_local_controller(app, Some(value));
            }

            if let Some(controller) = self.widget(app).controller.clone() {
                let text = controller.text_value(app).to_owned();
                self.set_value(app, Some(text));
                if old_widget.controller.is_none() {
                    let local = app
                        .get(self)
                        .controller
                        .handle()
                        .expect("a local controller while the old widget had none");
                    self.unregister_from_restoration(app, local.as_property());
                    RestorableProperty::dispose(local, app);
                    app.get_mut(self).controller = None;
                }
            }
        }
    }

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

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

    fn dispose(self: Handle<Self>, app: &mut App) {
        if let Some(controller) = self.widget(app).controller.clone() {
            controller.remove_listener(
                app,
                &Listener::handle_method(self, Self::handle_controller_changed),
            );
        }
        if let Some(controller) = app.get(self).controller.handle() {
            RestorableProperty::dispose(controller, app);
        }
        FormFieldState::dispose(self, app);
    }

    fn build(self: Handle<Self>, app: &mut App, context: BuildContext) -> WidgetRef {
        FormFieldState::build(self, app, context)
    }
}

#[cfg(test)]
mod tests {
    use std::cell::{Cell, RefCell};

    use waft_services::TextInputClient;
    use waft_widgets::{
        AnyElement, Center, Form, FormState, GlobalKey, ListView, StateSetter, StatefulBuilder,
        Text, TextEditingController,
    };

    use waft_widgets::testing::{WidgetTester, find, test_widgets};

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

    /// Dart's `MaterialApp(home: Material(child: Center(child: field)))`.
    fn centered<K>(tester: &WidgetTester, field: impl IntoWidget<K>) -> WidgetRef {
        material_app(
            ThemeData::new(&tester.cell.borrow()),
            Material::new()
                .child(Center::new().child(field))
                .into_widget(),
        )
    }

    fn editable_state(tester: &WidgetTester) -> Handle<EditableTextState> {
        find::by_type::<EditableText>().evaluate(tester)[0]
            .state_handle::<EditableTextState>(&tester.cell.borrow())
            .expect("an EditableText state")
    }

    fn field_state(tester: &WidgetTester) -> Handle<TextFormFieldState> {
        find::by_type::<TextFormField>().evaluate(tester)[0]
            .state_handle::<TextFormFieldState>(&tester.cell.borrow())
            .expect("a TextFormField state")
    }

    fn text_field(tester: &WidgetTester) -> AnyElement {
        find::by_type::<TextField>().evaluate(tester)[0]
    }

    #[test]
    fn text_form_field_accepts_text_field_no_max_length_as_value_to_max_length_parameter() {
        let _ = TextFormField::new()
            .max_length(TextField::NO_MAX_LENGTH)
            .into_widget();
    }

    #[test]
    fn passes_text_align_to_underlying_text_field() {
        test_widgets(|tester| {
            let root = centered(tester, TextFormField::new().text_align(TextAlign::Center));
            tester.pump_widget(root);

            assert_eq!(find::by_type::<TextField>().evaluate(tester).len(), 1);
            let text_align =
                tester.widget::<TextField, _>(text_field(tester), |field| field.text_align);
            assert_eq!(text_align, TextAlign::Center);
        });
    }

    #[test]
    fn passes_text_input_action_to_underlying_text_field() {
        test_widgets(|tester| {
            let root = centered(
                tester,
                TextFormField::new().text_input_action(TextInputAction::Next),
            );
            tester.pump_widget(root);

            let action =
                tester.widget::<TextField, _>(text_field(tester), |field| field.text_input_action);
            assert_eq!(action, Some(TextInputAction::Next));
        });
    }

    #[test]
    // Dart's 3.14 and 6.28 are plain values, not approximations of π and τ.
    #[allow(clippy::approx_constant)]
    fn passes_cursor_attributes_to_underlying_text_field() {
        test_widgets(|tester| {
            let cursor_color = crate::colors::Colors::PURPLE.primary();
            let root = centered(
                tester,
                TextFormField::new()
                    .cursor_width(3.14)
                    .cursor_height(6.28)
                    .cursor_radius(Radius::circular(4.0))
                    .cursor_color(cursor_color),
            );
            tester.pump_widget(root);

            let (width, height, radius, color) =
                tester.widget::<TextField, _>(text_field(tester), |field| {
                    (
                        field.cursor_width,
                        field.cursor_height,
                        field.cursor_radius,
                        field.cursor_color,
                    )
                });
            assert_eq!(width, 3.14);
            assert_eq!(height, Some(6.28));
            assert_eq!(radius, Some(Radius::circular(4.0)));
            assert_eq!(color, Some(cursor_color));
        });
    }

    #[test]
    fn on_field_submit_callbacks_are_called() {
        test_widgets(|tester| {
            let called = Rc::new(Cell::new(false));
            let flag = Rc::clone(&called);
            let root = centered(
                tester,
                TextFormField::new().on_field_submitted(move |_app, _value| flag.set(true)),
            );
            tester.pump_widget(root);

            tester.show_keyboard(find::by_type::<TextFormField>());
            tester
                .test_text_input()
                .receive_action(TextInputAction::Done);
            tester.pump();
            assert!(called.get());
        });
    }

    #[test]
    fn on_changed_callbacks_are_called() {
        test_widgets(|tester| {
            let value = Rc::new(RefCell::new(String::new()));
            let seen = Rc::clone(&value);
            let root = centered(
                tester,
                TextFormField::new().on_changed(move |_app, v| *seen.borrow_mut() = v),
            );
            tester.pump_widget(root);

            tester.enter_text(find::by_type::<TextFormField>(), "Soup");
            tester.pump();
            assert_eq!(*value.borrow(), "Soup");
        });
    }

    fn validate_counting(tester: &mut WidgetTester, enabled: Option<bool>) {
        let validate_called = Rc::new(Cell::new(0));
        let counter = Rc::clone(&validate_called);
        let mut field = TextFormField::new()
            .autovalidate_mode(AutovalidateMode::Always)
            .validator(move |_app, _value| {
                counter.set(counter.get() + 1);
                None
            });
        if let Some(enabled) = enabled {
            field = field.enabled(enabled);
        }
        let root = centered(tester, field);
        tester.pump_widget(root);

        assert_eq!(validate_called.get(), 1);
        tester.enter_text(find::by_type::<TextFormField>(), "a");
        tester.pump();
        assert_eq!(validate_called.get(), 2);
    }

    #[test]
    fn autovalidate_mode_is_passed_to_super() {
        test_widgets(|tester| validate_counting(tester, None));
    }

    #[test]
    fn validate_is_called_if_widget_is_enabled() {
        test_widgets(|tester| validate_counting(tester, Some(true)));
    }

    #[test]
    fn passing_a_build_counter_shows_returned_widget() {
        test_widgets(|tester| {
            let root = centered(
                tester,
                TextFormField::new()
                    .build_counter(|_app, _context, current_length, max_length, _is_focused| {
                        let max_length =
                            max_length.map_or_else(|| "null".to_owned(), |max| max.to_string());
                        Some(Text::new(format!("{current_length} of {max_length}")).into_widget())
                    })
                    .max_length(10),
            );
            tester.pump_widget(root);

            assert_eq!(find::text("0 of 10").evaluate(tester).len(), 1);

            tester.enter_text(find::by_type::<TextFormField>(), "01234");
            tester.pump();

            assert_eq!(find::text("5 of 10").evaluate(tester).len(), 1);
        });
    }

    #[test]
    fn readonly_text_form_field_will_hide_cursor_by_default() {
        test_widgets(|tester| {
            let root = centered(
                tester,
                TextFormField::new()
                    .initial_value("readonly")
                    .read_only(true),
            );
            tester.pump_widget(root);

            let element = find::by_type::<EditableText>().evaluate(tester)[0];
            let show_cursor =
                tester.widget::<EditableText, _>(element, EditableText::effective_show_cursor);
            assert!(!show_cursor);
        });
    }

    fn editable_text_value(tester: &WidgetTester) -> String {
        let state = editable_state(tester);
        TextInputClient::current_text_editing_value(state, &tester.cell.borrow())
            .expect("the field has a value")
            .text
    }

    #[test]
    fn reset_resets_the_text_fields_value_to_the_initial_value() {
        test_widgets(|tester| {
            let root = centered(tester, TextFormField::new().initial_value("initialValue"));
            tester.pump_widget(root);

            tester.enter_text(find::by_type::<TextFormField>(), "changedValue");

            let state = field_state(tester);
            state.reset(&mut tester.cell.borrow_mut());
            tester.pump();

            assert_eq!(editable_text_value(tester), "initialValue");
        });
    }

    #[test]
    fn reset_resets_the_text_fields_value_to_the_controller_initial_value() {
        test_widgets(|tester| {
            let controller = {
                let mut app = tester.cell.borrow_mut();
                let controller = TextEditingController::new(&mut app);
                controller.set_text(&mut app, "initialValue");
                controller
            };
            let root = centered(tester, TextFormField::new().controller(controller.erase()));
            tester.pump_widget(root);

            tester.enter_text(find::by_type::<TextFormField>(), "changedValue");

            let state = field_state(tester);
            state.reset(&mut tester.cell.borrow_mut());
            tester.pump();

            assert_eq!(editable_text_value(tester), "initialValue");
        });
    }

    #[test]
    fn did_change_changes_text_fields_value() {
        test_widgets(|tester| {
            let root = centered(tester, TextFormField::new().initial_value("initialValue"));
            tester.pump_widget(root);

            assert_eq!(editable_text_value(tester), "initialValue");

            let state = field_state(tester);
            state.did_change(&mut tester.cell.borrow_mut(), Some("changedValue".into()));
            tester.pump();

            assert_eq!(editable_text_value(tester), "changedValue");
        });
    }

    #[test]
    fn on_changed_callbacks_value_and_form_field_state_value_are_sync() {
        test_widgets(|tester| {
            let called = Rc::new(Cell::new(false));
            let state_slot: Rc<Cell<Option<Handle<TextFormFieldState>>>> = Rc::new(Cell::new(None));
            let (flag, slot) = (Rc::clone(&called), Rc::clone(&state_slot));
            let root = centered(
                tester,
                TextFormField::new().on_changed(move |app, value| {
                    flag.set(true);
                    let state = slot.get().expect("the case records the state");
                    assert_eq!(state.value(app), Some(&value));
                }),
            );
            tester.pump_widget(root);

            state_slot.set(Some(field_state(tester)));

            tester.enter_text(find::by_type::<TextFormField>(), "Soup");

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

    #[test]
    fn text_form_field_on_changed_is_called_when_the_form_is_reset() {
        test_widgets(|tester| {
            let state_key = GlobalKey::new();
            let form_key = GlobalKey::new();
            let value = Rc::new(RefCell::new("initialValue".to_owned()));
            let seen = Rc::clone(&value);
            let root = material_app(
                ThemeData::new(&tester.cell.borrow()),
                Scaffold::new()
                    .body(
                        Form::new(
                            TextFormField::new()
                                .key(state_key.clone().into())
                                .initial_value(value.borrow().clone())
                                .on_changed(move |_app, new_value| *seen.borrow_mut() = new_value),
                        )
                        .key(form_key.clone().into()),
                    )
                    .into_widget(),
            );
            tester.pump_widget(root);

            let field_value = |tester: &WidgetTester| {
                let app = tester.cell.borrow();
                let state = state_key
                    .current_state::<TextFormFieldState>(&app)
                    .expect("the field mounted");
                state.value(&app).cloned()
            };

            // Initial value is 'initialValue'.
            assert_eq!(field_value(tester).as_deref(), Some("initialValue"));
            assert_eq!(*value.borrow(), "initialValue");

            // Change value to 'changedValue'.
            tester.enter_text(find::by_type::<TextFormField>(), "changedValue");
            assert_eq!(field_value(tester).as_deref(), Some("changedValue"));
            assert_eq!(*value.borrow(), "changedValue");

            // Should be back to 'initialValue' when the form is reset.
            {
                let mut app = tester.cell.borrow_mut();
                let form = form_key
                    .current_state::<FormState>(&app)
                    .expect("the form mounted");
                form.reset(&mut app);
            }
            tester.pump();
            assert_eq!(field_value(tester).as_deref(), Some("initialValue"));
            assert_eq!(*value.borrow(), "initialValue");
        });
    }

    #[test]
    fn is_valid_returns_false_when_force_error_text_is_set_and_will_change_error_display() {
        test_widgets(|tester| {
            let field_key1 = GlobalKey::new();
            let field_key2 = GlobalKey::new();
            let valid_string = "Valid string";
            let validator = move |_app: &mut App, s: Option<&String>| {
                if s.map(String::as_str) == Some(valid_string) {
                    None
                } else {
                    Some("Error text".to_owned())
                }
            };
            let root = material_app(
                ThemeData::new(&tester.cell.borrow()),
                Center::new()
                    .child(
                        Material::new().child(Form::new(ListView::new().children(vec![
                        TextFormField::new()
                            .key(field_key1.clone().into())
                            .initial_value(valid_string)
                            .validator(validator)
                            .autovalidate_mode(AutovalidateMode::Disabled)
                            .into_widget(),
                        TextFormField::new()
                            .key(field_key2.clone().into())
                            .initial_value("")
                            .force_error_text("Forcing error.")
                            .validator(validator)
                            .autovalidate_mode(AutovalidateMode::Disabled)
                            .into_widget(),
                    ]))),
                    )
                    .into_widget(),
            );
            tester.pump_widget(root);

            let mut app = tester.cell.borrow_mut();
            let state1 = field_key1
                .current_state::<TextFormFieldState>(&app)
                .expect("the first field mounted");
            let state2 = field_key2
                .current_state::<TextFormFieldState>(&app)
                .expect("the second field mounted");
            assert!(state1.is_valid(&mut app));
            assert!(!state1.has_error(&app));
            assert!(!state2.is_valid(&mut app));
            assert!(state2.has_error(&app));
        });
    }

    #[test]
    fn widget_returned_by_error_builder_is_shown() {
        test_widgets(|tester| {
            let root = centered(
                tester,
                TextFormField::new()
                    .autovalidate_mode(AutovalidateMode::Always)
                    .validator(|_app, _value| Some("validation error".to_owned()))
                    .error_builder(|_app, _context, error_text| {
                        Text::new(format!("**{error_text}**")).into_widget()
                    }),
            );
            tester.pump_widget(root);
            tester.pump();

            assert_eq!(find::text("**validation error**").evaluate(tester).len(), 1);
        });
    }

    #[test]
    #[should_panic(
        expected = "Declaring both errorBuilder and decoration.errorText is not supported."
    )]
    fn text_form_field_asserts_when_both_error_builder_and_decoration_error_text_are_provided() {
        let _ = TextFormField::new()
            .decoration(Some(
                InputDecoration::new().error_text("decoration error text"),
            ))
            .error_builder(|_app, _context, error_text| Text::new(error_text).into_widget())
            .into_widget();
    }

    #[test]
    fn text_form_field_resets_to_initial_value_after_set_state() {
        test_widgets(|tester| {
            let form_key = GlobalKey::new();
            let controller = {
                let mut app = tester.cell.borrow_mut();
                let controller = TextEditingController::new(&mut app);
                controller.set_text(&mut app, "Initial Value");
                controller.clone()
            };
            let set_state: Rc<RefCell<Option<StateSetter>>> = Rc::new(RefCell::new(None));
            let setter_slot = Rc::clone(&set_state);
            let key_for_form = form_key.clone();
            let root = material_app(
                ThemeData::new(&tester.cell.borrow()),
                Scaffold::new()
                    .body(StatefulBuilder::new({
                        let controller = controller.clone();
                        move |_app, _context, setter| {
                            *setter_slot.borrow_mut() = Some(setter);
                            Form::new(TextFormField::new().controller(controller.erase()))
                                .key(key_for_form.clone().into())
                                .into_widget()
                        }
                    }))
                    .into_widget(),
            );
            tester.pump_widget(root);

            tester.enter_text(find::by_type::<TextFormField>(), "Changed");
            tester.pump();
            assert_eq!(controller.text_value(&tester.cell.borrow()), "Changed");

            let setter = set_state.borrow().clone().expect("the builder ran");
            setter(&mut tester.cell.borrow_mut(), &mut |_app| {});
            tester.pump();

            {
                let mut app = tester.cell.borrow_mut();
                let form = form_key
                    .current_state::<FormState>(&app)
                    .expect("the form mounted");
                form.reset(&mut app);
            }
            tester.pump();

            assert_eq!(
                controller.text_value(&tester.cell.borrow()),
                "Initial Value"
            );
        });
    }
}