waft-material 0.6.0

Material Design widgets for Waft
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
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
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
//! Flutter counterpart: material_ui's `floating_action_button_location.dart`.
//!
//! The standard locations and animators are per-thread `Rc`s handed out by functions on the
//! trait objects, so the scaffold compares them as Dart compares its `const` instances: by
//! identity (`Rc::ptr_eq`).

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

use waft_animation::{
    AlwaysStoppedAnimation, Animation, AnimationLazyListenerData, AnimationLazyListenerMixin,
    AnimationLocalListenersData, AnimationLocalListenersMixin, AnimationLocalStatusListenersData,
    AnimationLocalStatusListenersMixin, AnimationRef, AnimationStatus, AnimationStatusListener,
    AnyAnimation, CompoundAnimation, Curve, CurveTween, Curves, Drive, Interval, ReverseAnimation,
    Threshold, Tween, compound_did_start_listening, compound_did_stop_listening,
};
use waft_embedder::{Offset, TextDirection};
use waft_foundation::{App, Handle, Listener, Ref};

use crate::scaffold::ScaffoldPrelayoutGeometry;

/// The margin that a `FloatingActionButton` should leave between it and the
/// edge of the screen.
///
/// [`FloatingActionButtonLocation::end_float`](dyn FloatingActionButtonLocation::end_float)
/// uses this to set the appropriate margin between the `FloatingActionButton` and the end of
/// the screen.
pub const K_FLOATING_ACTION_BUTTON_MARGIN: f64 = 16.0;

/// The amount of time the `FloatingActionButton` takes to transition in or out.
///
/// The `Scaffold` uses this to set the duration of `FloatingActionButton`
/// motion, entrance, and exit animations.
pub const K_FLOATING_ACTION_BUTTON_SEGUE: Duration = Duration::from_millis(200);

/// The fraction of a circle the `FloatingActionButton` should turn when it enters.
///
/// Its value corresponds to 0.125 of a full circle, equivalent to 45 degrees or pi/4 radians.
pub const K_FLOATING_ACTION_BUTTON_TURN_INTERVAL: f64 = 0.125;

/// If a `FloatingActionButton` is used on a `Scaffold` in certain positions,
/// it is moved [`K_MINI_BUTTON_OFFSET_ADJUSTMENT`] pixels closer to the edge of the screen.
///
/// This is intended to be used with `FloatingActionButton.mini` set to true,
/// so that the floating action button appears to align with `CircleAvatar`s
/// in the `ListTile.leading` slot of a `ListTile` in a `ListView` in the
/// `Scaffold.body`.
///
/// More specifically:
/// * In the following positions, the `FloatingActionButton` is moved *horizontally*
///   closer to the edge of the screen:
///   * `FloatingActionButtonLocation.miniStartTop`
///   * `FloatingActionButtonLocation.miniStartFloat`
///   * `FloatingActionButtonLocation.miniStartDocked`
///   * `FloatingActionButtonLocation.miniEndTop`
///   * `FloatingActionButtonLocation.miniEndFloat`
///   * `FloatingActionButtonLocation.miniEndDocked`
/// * In the following positions, the `FloatingActionButton` is moved *vertically*
///   closer to the bottom of the screen:
///   * `FloatingActionButtonLocation.miniStartFloat`
///   * `FloatingActionButtonLocation.miniCenterFloat`
///   * `FloatingActionButtonLocation.miniEndFloat`
pub const K_MINI_BUTTON_OFFSET_ADJUSTMENT: f64 = 4.0;

/// An object that defines a position for the `FloatingActionButton`
/// based on the `Scaffold`'s [`ScaffoldPrelayoutGeometry`].
///
/// Flutter provides [`FloatingActionButtonLocation`]s for the common
/// `FloatingActionButton` placements in Material Design applications. These
/// locations are available as functions on `dyn FloatingActionButtonLocation`
/// (`<dyn FloatingActionButtonLocation>::end_float()`).
///
/// See also:
///
///  * `FloatingActionButton`, which is a circular button typically shown in the
///    bottom right corner of the app.
///  * [`FloatingActionButtonAnimator`], which is used to animate the
///    `Scaffold.floatingActionButton` from one [`FloatingActionButtonLocation`] to
///    another.
///  * [`ScaffoldPrelayoutGeometry`], the geometry that
///    [`FloatingActionButtonLocation`]s use to position the `FloatingActionButton`.
pub trait FloatingActionButtonLocation: Debug + 'static {
    /// Places the `FloatingActionButton` based on the `Scaffold`'s layout.
    ///
    /// This uses a [`ScaffoldPrelayoutGeometry`], which the `Scaffold` constructs
    /// during its layout phase after it has laid out every widget it can lay out
    /// except the `FloatingActionButton`. The `Scaffold` uses the [`Offset`]
    /// returned from this method to position the `FloatingActionButton` and
    /// complete its layout.
    fn get_offset(&self, scaffold_geometry: &ScaffoldPrelayoutGeometry) -> Offset;
}

/// A shared [`FloatingActionButtonLocation`], compared by identity as Dart compares its
/// locations.
pub type FloatingActionButtonLocationRef = Rc<dyn FloatingActionButtonLocation>;

/// Defines a function on `dyn FloatingActionButtonLocation` handing out clones of one
/// per-thread `Rc` of a standard location: Dart's `static const`.
macro_rules! standard_locations {
    ($( $(#[$doc:meta])* $name:ident = $value:expr; )*) => {
        impl dyn FloatingActionButtonLocation {
            $(
                $(#[$doc])*
                pub fn $name() -> FloatingActionButtonLocationRef {
                    thread_local! {
                        static LOCATION: FloatingActionButtonLocationRef = Rc::new($value);
                    }
                    LOCATION.with(Rc::clone)
                }
            )*
        }
    };
}

standard_locations! {
    /// Start-aligned `FloatingActionButton`, floating over the transition between
    /// the `Scaffold.appBar` and the `Scaffold.body`.
    ///
    /// To align a floating action button with `CircleAvatar`s in the
    /// `ListTile.leading` slots of `ListTile`s in a `ListView` in the `Scaffold.body`,
    /// use [`mini_start_top`](Self::mini_start_top) and set `FloatingActionButton.mini` to
    /// true.
    ///
    /// This is unlikely to be a useful location for apps that lack a top `AppBar`
    /// or that use a `SliverAppBar` in the scaffold body itself.
    start_top = StartTopFabLocation;

    /// Start-aligned `FloatingActionButton`, floating over the transition between
    /// the `Scaffold.appBar` and the `Scaffold.body`, optimized for mini floating
    /// action buttons.
    ///
    /// This is intended to be used with `FloatingActionButton.mini` set to true,
    /// so that the floating action button appears to align with `CircleAvatar`s
    /// in the `ListTile.leading` slot of a `ListTile` in a `ListView` in the
    /// `Scaffold.body`.
    ///
    /// This is unlikely to be a useful location for apps that lack a top `AppBar`
    /// or that use a `SliverAppBar` in the scaffold body itself.
    mini_start_top = MiniStartTopFabLocation;

    /// Centered `FloatingActionButton`, floating over the transition between
    /// the `Scaffold.appBar` and the `Scaffold.body`.
    ///
    /// This is unlikely to be a useful location for apps that lack a top `AppBar`
    /// or that use a `SliverAppBar` in the scaffold body itself.
    center_top = CenterTopFabLocation;

    /// Centered `FloatingActionButton`, floating over the transition between
    /// the `Scaffold.appBar` and the `Scaffold.body`, intended to be used with
    /// `FloatingActionButton.mini` set to true.
    ///
    /// This is unlikely to be a useful location for apps that lack a top `AppBar`
    /// or that use a `SliverAppBar` in the scaffold body itself.
    mini_center_top = MiniCenterTopFabLocation;

    /// End-aligned `FloatingActionButton`, floating over the transition between
    /// the `Scaffold.appBar` and the `Scaffold.body`.
    ///
    /// To align a floating action button with `CircleAvatar`s in the
    /// `ListTile.trailing` slots of `ListTile`s in a `ListView` in the `Scaffold.body`,
    /// use [`mini_end_top`](Self::mini_end_top) and set `FloatingActionButton.mini` to true.
    ///
    /// This is unlikely to be a useful location for apps that lack a top `AppBar`
    /// or that use a `SliverAppBar` in the scaffold body itself.
    end_top = EndTopFabLocation;

    /// End-aligned `FloatingActionButton`, floating over the transition between
    /// the `Scaffold.appBar` and the `Scaffold.body`, optimized for mini floating
    /// action buttons.
    ///
    /// This is intended to be used with `FloatingActionButton.mini` set to true,
    /// so that the floating action button appears to align with `CircleAvatar`s
    /// in the `ListTile.trailing` slot of a `ListTile` in a `ListView` in the
    /// `Scaffold.body`.
    ///
    /// This is unlikely to be a useful location for apps that lack a top `AppBar`
    /// or that use a `SliverAppBar` in the scaffold body itself.
    mini_end_top = MiniEndTopFabLocation;

    /// Start-aligned `FloatingActionButton`, floating at the bottom of the screen.
    ///
    /// To align a floating action button with `CircleAvatar`s in the
    /// `ListTile.leading` slots of `ListTile`s in a `ListView` in the `Scaffold.body`,
    /// use [`mini_start_float`](Self::mini_start_float) and set `FloatingActionButton.mini`
    /// to true.
    start_float = StartFloatFabLocation;

    /// Start-aligned `FloatingActionButton`, floating at the bottom of the screen,
    /// optimized for mini floating action buttons.
    ///
    /// This is intended to be used with `FloatingActionButton.mini` set to true,
    /// so that the floating action button appears to align with `CircleAvatar`s
    /// in the `ListTile.leading` slot of a `ListTile` in a `ListView` in the
    /// `Scaffold.body`.
    ///
    /// Compared to [`start_float`](Self::start_float), floating action
    /// buttons using this location will move horizontally _and_ vertically
    /// closer to the edges, by [`K_MINI_BUTTON_OFFSET_ADJUSTMENT`] each.
    mini_start_float = MiniStartFloatFabLocation;

    /// Centered `FloatingActionButton`, floating at the bottom of the screen.
    ///
    /// To position a mini floating action button, use
    /// [`mini_center_float`](Self::mini_center_float) and set `FloatingActionButton.mini` to
    /// true.
    center_float = CenterFloatFabLocation;

    /// Centered `FloatingActionButton`, floating at the bottom of the screen,
    /// optimized for mini floating action buttons.
    ///
    /// This is intended to be used with `FloatingActionButton.mini` set to true,
    /// so that the floating action button appears to align horizontally with
    /// the locations [`mini_start_float`](Self::mini_start_float)
    /// and [`mini_end_float`](Self::mini_end_float).
    ///
    /// Compared to [`center_float`](Self::center_float), floating action
    /// buttons using this location will move vertically down
    /// by [`K_MINI_BUTTON_OFFSET_ADJUSTMENT`].
    mini_center_float = MiniCenterFloatFabLocation;

    /// End-aligned `FloatingActionButton`, floating at the bottom of the screen.
    ///
    /// This is the default alignment of `FloatingActionButton`s in Material applications.
    ///
    /// To align a floating action button with `CircleAvatar`s in the
    /// `ListTile.trailing` slots of `ListTile`s in a `ListView` in the `Scaffold.body`,
    /// use [`mini_end_float`](Self::mini_end_float) and set `FloatingActionButton.mini` to
    /// true.
    end_float = EndFloatFabLocation;

    /// End-aligned `FloatingActionButton`, floating at the bottom of the screen,
    /// optimized for mini floating action buttons.
    ///
    /// This is intended to be used with `FloatingActionButton.mini` set to true,
    /// so that the floating action button appears to align with `CircleAvatar`s
    /// in the `ListTile.trailing` slot of a `ListTile` in a `ListView` in the
    /// `Scaffold.body`.
    ///
    /// Compared to [`end_float`](Self::end_float), floating action
    /// buttons using this location will move horizontally _and_ vertically
    /// closer to the edges, by [`K_MINI_BUTTON_OFFSET_ADJUSTMENT`] each.
    mini_end_float = MiniEndFloatFabLocation;

    /// Start-aligned `FloatingActionButton`, floating over the
    /// `Scaffold.bottomNavigationBar` so that the center of the floating
    /// action button lines up with the top of the bottom navigation bar.
    ///
    /// To align a floating action button with `CircleAvatar`s in the
    /// `ListTile.leading` slots of `ListTile`s in a `ListView` in the `Scaffold.body`,
    /// use [`mini_start_docked`](Self::mini_start_docked) and set
    /// `FloatingActionButton.mini` to true.
    ///
    /// If the value of `Scaffold.bottomNavigationBar` is a `BottomAppBar`,
    /// the bottom app bar can include a "notch" in its shape that accommodates
    /// the overlapping floating action button.
    ///
    /// This is unlikely to be a useful location for apps that lack a bottom
    /// navigation bar.
    start_docked = StartDockedFabLocation;

    /// Start-aligned `FloatingActionButton`, floating over the
    /// `Scaffold.bottomNavigationBar` so that the center of the floating
    /// action button lines up with the top of the bottom navigation bar,
    /// optimized for mini floating action buttons.
    ///
    /// If the value of `Scaffold.bottomNavigationBar` is a `BottomAppBar`,
    /// the bottom app bar can include a "notch" in its shape that accommodates
    /// the overlapping floating action button.
    ///
    /// This is intended to be used with `FloatingActionButton.mini` set to true,
    /// so that the floating action button appears to align with `CircleAvatar`s
    /// in the `ListTile.leading` slot of a `ListTile` in a `ListView` in the
    /// `Scaffold.body`.
    ///
    /// This is unlikely to be a useful location for apps that lack a bottom
    /// navigation bar.
    mini_start_docked = MiniStartDockedFabLocation;

    /// Centered `FloatingActionButton`, floating over the
    /// `Scaffold.bottomNavigationBar` so that the center of the floating
    /// action button lines up with the top of the bottom navigation bar.
    ///
    /// If the value of `Scaffold.bottomNavigationBar` is a `BottomAppBar`,
    /// the bottom app bar can include a "notch" in its shape that accommodates
    /// the overlapping floating action button.
    ///
    /// This is unlikely to be a useful location for apps that lack a bottom
    /// navigation bar.
    center_docked = CenterDockedFabLocation;

    /// Centered `FloatingActionButton`, floating over the
    /// `Scaffold.bottomNavigationBar` so that the center of the floating
    /// action button lines up with the top of the bottom navigation bar;
    /// intended to be used with `FloatingActionButton.mini` set to true.
    ///
    /// If the value of `Scaffold.bottomNavigationBar` is a `BottomAppBar`,
    /// the bottom app bar can include a "notch" in its shape that accommodates
    /// the overlapping floating action button.
    ///
    /// This is unlikely to be a useful location for apps that lack a bottom
    /// navigation bar.
    mini_center_docked = MiniCenterDockedFabLocation;

    /// End-aligned `FloatingActionButton`, floating over the
    /// `Scaffold.bottomNavigationBar` so that the center of the floating
    /// action button lines up with the top of the bottom navigation bar.
    ///
    /// If the value of `Scaffold.bottomNavigationBar` is a `BottomAppBar`,
    /// the bottom app bar can include a "notch" in its shape that accommodates
    /// the overlapping floating action button.
    ///
    /// This is unlikely to be a useful location for apps that lack a bottom
    /// navigation bar.
    end_docked = EndDockedFabLocation;

    /// End-aligned `FloatingActionButton`, floating over the
    /// `Scaffold.bottomNavigationBar` so that the center of the floating
    /// action button lines up with the top of the bottom navigation bar,
    /// optimized for mini floating action buttons.
    ///
    /// To align a floating action button with `CircleAvatar`s in the
    /// `ListTile.trailing` slots of `ListTile`s in a `ListView` in the `Scaffold.body`,
    /// use [`mini_end_docked`](Self::mini_end_docked) and set `FloatingActionButton.mini` to
    /// true.
    ///
    /// If the value of `Scaffold.bottomNavigationBar` is a `BottomAppBar`,
    /// the bottom app bar can include a "notch" in its shape that accommodates
    /// the overlapping floating action button.
    ///
    /// This is intended to be used with `FloatingActionButton.mini` set to true,
    /// so that the floating action button appears to align with `CircleAvatar`s
    /// in the `ListTile.trailing` slot of a `ListTile` in a `ListView` in the
    /// `Scaffold.body`.
    ///
    /// This is unlikely to be a useful location for apps that lack a bottom
    /// navigation bar.
    mini_end_docked = MiniEndDockedFabLocation;

    /// End-aligned `FloatingActionButton`, floating over the
    /// `Scaffold.bottomNavigationBar` so that the floating
    /// action button lines up with the center of the bottom navigation bar.
    ///
    /// This is unlikely to be a useful location for apps which has a `BottomNavigationBar`
    /// or a non material 3 `BottomAppBar`.
    end_contained = EndContainedFabLocation;
}

/// A base trait that simplifies building [`FloatingActionButtonLocation`]s when
/// used with the mixins [`FabTopOffsetY`], [`FabFloatOffsetY`], [`FabDockedOffsetY`],
/// [`FabStartOffsetX`], [`FabCenterOffsetX`], [`FabEndOffsetX`], and
/// [`FabMiniOffsetAdjustment`].
///
/// A subclass of [`FloatingActionButtonLocation`] which implements its
/// [`get_offset`](FloatingActionButtonLocation::get_offset) method using three other methods:
/// [`get_offset_x`](Self::get_offset_x), [`get_offset_y`](Self::get_offset_y), and
/// [`is_mini`](Self::is_mini).
///
/// Different mixins on this class override different methods, so that combining
/// a set of mixins creates a floating action button location. A location implements the
/// mixins it uses and forwards [`get_offset_x`](Self::get_offset_x),
/// [`get_offset_y`](Self::get_offset_y) and [`is_mini`](Self::is_mini) to them, and its
/// [`FloatingActionButtonLocation::get_offset`] to [`StandardFabLocation::get_offset`].
///
/// For example: the location `<dyn FloatingActionButtonLocation>::mini_end_top()`
/// is based on a type that implements [`StandardFabLocation`]
/// with the mixins [`FabMiniOffsetAdjustment`], [`FabEndOffsetX`], and [`FabTopOffsetY`].
///
/// You can create your own implementation of [`StandardFabLocation`]
/// to implement a custom [`FloatingActionButtonLocation`].
pub trait StandardFabLocation: FloatingActionButtonLocation {
    /// Obtains the x-offset to place the `FloatingActionButton` based on the
    /// `Scaffold`'s layout.
    ///
    /// Used by [`get_offset`](Self::get_offset) to compute its x-coordinate.
    fn get_offset_x(&self, scaffold_geometry: &ScaffoldPrelayoutGeometry, adjustment: f64) -> f64;

    /// Obtains the y-offset to place the `FloatingActionButton` based on the
    /// `Scaffold`'s layout.
    ///
    /// Used by [`get_offset`](Self::get_offset) to compute its y-coordinate.
    fn get_offset_y(&self, scaffold_geometry: &ScaffoldPrelayoutGeometry, adjustment: f64) -> f64;

    /// A function returning whether this [`StandardFabLocation`] is optimized for
    /// mini `FloatingActionButton`s.
    fn is_mini(&self) -> bool {
        false
    }

    /// Dart's `StandardFabLocation.getOffset` override, which the implementing location's
    /// [`FloatingActionButtonLocation::get_offset`] calls.
    fn get_offset(&self, scaffold_geometry: &ScaffoldPrelayoutGeometry) -> Offset {
        let adjustment = if StandardFabLocation::is_mini(self) {
            K_MINI_BUTTON_OFFSET_ADJUSTMENT
        } else {
            0.0
        };
        Offset::new(
            StandardFabLocation::get_offset_x(self, scaffold_geometry, adjustment),
            StandardFabLocation::get_offset_y(self, scaffold_geometry, adjustment),
        )
    }
}

/// Calculates x-offset for left-aligned [`FloatingActionButtonLocation`]s; Dart's
/// `StandardFabLocation._leftOffsetX`.
fn left_offset_x(scaffold_geometry: &ScaffoldPrelayoutGeometry, adjustment: f64) -> f64 {
    K_FLOATING_ACTION_BUTTON_MARGIN + scaffold_geometry.min_insets.left - adjustment
}

/// Calculates x-offset for right-aligned [`FloatingActionButtonLocation`]s; Dart's
/// `StandardFabLocation._rightOffsetX`.
fn right_offset_x(scaffold_geometry: &ScaffoldPrelayoutGeometry, adjustment: f64) -> f64 {
    scaffold_geometry.scaffold_size.width()
        - K_FLOATING_ACTION_BUTTON_MARGIN
        - scaffold_geometry.min_insets.right
        - scaffold_geometry.floating_action_button_size.width()
        + adjustment
}

/// Mixin for a "top" floating action button location, such as
/// `<dyn FloatingActionButtonLocation>::start_top()`.
///
/// The `adjustment`, typically [`K_MINI_BUTTON_OFFSET_ADJUSTMENT`], is ignored in the
/// Y axis of "top" positions. For "top" positions, the X offset is adjusted to
/// move closer to the edge of the screen. This is so that a minified floating
/// action button appears to align with `CircleAvatar`s in the
/// `ListTile.leading` slot of a `ListTile` in a `ListView` in the
/// `Scaffold.body`.
pub trait FabTopOffsetY: StandardFabLocation {
    /// Calculates y-offset for [`FloatingActionButtonLocation`]s floating over
    /// the transition between the `Scaffold.appBar` and the `Scaffold.body`.
    fn get_offset_y(&self, scaffold_geometry: &ScaffoldPrelayoutGeometry, adjustment: f64) -> f64 {
        let _ = adjustment;
        if scaffold_geometry.content_top > scaffold_geometry.min_view_padding.top {
            let fab_half_height = scaffold_geometry.floating_action_button_size.height() / 2.0;
            return scaffold_geometry.content_top - fab_half_height;
        }
        // Otherwise, ensure we are placed within the bounds of a safe area.
        scaffold_geometry.min_view_padding.top
    }
}

/// Mixin for a "float" floating action button location, such as
/// `<dyn FloatingActionButtonLocation>::center_float()`.
pub trait FabFloatOffsetY: StandardFabLocation {
    /// Calculates y-offset for [`FloatingActionButtonLocation`]s floating at
    /// the bottom of the screen.
    fn get_offset_y(&self, scaffold_geometry: &ScaffoldPrelayoutGeometry, adjustment: f64) -> f64 {
        let content_bottom = scaffold_geometry.content_bottom;
        let bottom_content_height = scaffold_geometry.scaffold_size.height() - content_bottom;
        let bottom_sheet_height = scaffold_geometry.bottom_sheet_size.height();
        let fab_height = scaffold_geometry.floating_action_button_size.height();
        let snack_bar_height = scaffold_geometry.snack_bar_size.height();
        let safe_margin = f64::max(
            K_FLOATING_ACTION_BUTTON_MARGIN,
            scaffold_geometry.min_view_padding.bottom - bottom_content_height
                + K_FLOATING_ACTION_BUTTON_MARGIN,
        );

        let mut fab_y = content_bottom - fab_height - safe_margin;
        if snack_bar_height > 0.0 {
            fab_y = f64::min(
                fab_y,
                content_bottom - snack_bar_height - fab_height - K_FLOATING_ACTION_BUTTON_MARGIN,
            );
        }
        if bottom_sheet_height > 0.0 {
            fab_y = f64::min(
                fab_y,
                content_bottom - bottom_sheet_height - fab_height / 2.0,
            );
        }
        fab_y + adjustment
    }
}

/// Mixin for a "docked" floating action button location, such as
/// `<dyn FloatingActionButtonLocation>::end_docked()`.
pub trait FabDockedOffsetY: StandardFabLocation {
    /// Calculates y-offset for [`FloatingActionButtonLocation`]s floating over the
    /// `Scaffold.bottomNavigationBar` so that the center of the floating
    /// action button lines up with the top of the bottom navigation bar.
    fn get_offset_y(&self, scaffold_geometry: &ScaffoldPrelayoutGeometry, adjustment: f64) -> f64 {
        let _ = adjustment;
        let content_bottom = scaffold_geometry.content_bottom;
        let content_margin = scaffold_geometry.scaffold_size.height() - content_bottom;
        let bottom_view_padding = scaffold_geometry.min_view_padding.bottom;
        let bottom_sheet_height = scaffold_geometry.bottom_sheet_size.height();
        let fab_height = scaffold_geometry.floating_action_button_size.height();
        let snack_bar_height = scaffold_geometry.snack_bar_size.height();
        let bottom_min_inset = scaffold_geometry.min_insets.bottom;

        let safe_margin = if content_margin > bottom_min_inset + fab_height / 2.0 {
            // If contentMargin is higher than bottomMinInset enough to display the
            // FAB without clipping, don't provide a margin
            0.0
        } else if bottom_min_inset == 0.0 {
            // If bottomMinInset is zero(the software keyboard is not on the screen)
            // provide bottomViewPadding as margin
            bottom_view_padding
        } else {
            // Provide a margin that would shift the FAB enough so that it stays away
            // from the keyboard
            fab_height / 2.0 + K_FLOATING_ACTION_BUTTON_MARGIN
        };

        let mut fab_y = content_bottom - fab_height / 2.0 - safe_margin;
        // The FAB should sit with a margin between it and the snack bar.
        if snack_bar_height > 0.0 {
            fab_y = f64::min(
                fab_y,
                content_bottom - snack_bar_height - fab_height - K_FLOATING_ACTION_BUTTON_MARGIN,
            );
        }
        // The FAB should sit with its center in front of the top of the bottom sheet.
        if bottom_sheet_height > 0.0 {
            fab_y = f64::min(
                fab_y,
                content_bottom - bottom_sheet_height - fab_height / 2.0,
            );
        }
        let max_fab_y = scaffold_geometry.scaffold_size.height() - fab_height - safe_margin;
        f64::min(max_fab_y, fab_y)
    }
}

/// Mixin for a "contained" floating action button location, such as
/// `<dyn FloatingActionButtonLocation>::end_contained()`.
pub trait FabContainedOffsetY: StandardFabLocation {
    /// Calculates y-offset for [`FloatingActionButtonLocation`]s floating over the
    /// `Scaffold.bottomNavigationBar` so that the center of the floating
    /// action button lines up with the center of the bottom navigation bar.
    fn get_offset_y(&self, scaffold_geometry: &ScaffoldPrelayoutGeometry, adjustment: f64) -> f64 {
        let _ = adjustment;
        let content_bottom = scaffold_geometry.content_bottom;
        let content_margin = scaffold_geometry.scaffold_size.height() - content_bottom;
        let bottom_view_padding = scaffold_geometry.min_view_padding.bottom;
        let fab_height = scaffold_geometry.floating_action_button_size.height();

        let safe_margin = if content_margin > bottom_view_padding + fab_height {
            // If contentMargin is higher than bottomViewPadding enough to display the
            // FAB without clipping, don't provide a margin
            0.0
        } else {
            bottom_view_padding
        };

        // This is to compute the distance between the content bottom to the top edge
        // of the floating action button. This can be negative if content margin is
        // too small.
        let content_bottom_to_fab_top = (content_margin - bottom_view_padding - fab_height) / 2.0;
        let fab_y = content_bottom + content_bottom_to_fab_top;
        let max_fab_y = scaffold_geometry.scaffold_size.height() - fab_height - safe_margin;

        f64::min(max_fab_y, fab_y)
    }
}

/// Mixin for a "start" floating action button location, such as
/// `<dyn FloatingActionButtonLocation>::start_top()`.
pub trait FabStartOffsetX: StandardFabLocation {
    /// Calculates x-offset for start-aligned [`FloatingActionButtonLocation`]s.
    fn get_offset_x(&self, scaffold_geometry: &ScaffoldPrelayoutGeometry, adjustment: f64) -> f64 {
        match scaffold_geometry.text_direction {
            TextDirection::Rtl => right_offset_x(scaffold_geometry, adjustment),
            TextDirection::Ltr => left_offset_x(scaffold_geometry, adjustment),
        }
    }
}

/// Mixin for a "center" floating action button location, such as
/// `<dyn FloatingActionButtonLocation>::center_float()`.
pub trait FabCenterOffsetX: StandardFabLocation {
    /// Calculates x-offset for center-aligned [`FloatingActionButtonLocation`]s.
    fn get_offset_x(&self, scaffold_geometry: &ScaffoldPrelayoutGeometry, adjustment: f64) -> f64 {
        let _ = adjustment;
        (scaffold_geometry.scaffold_size.width()
            - scaffold_geometry.floating_action_button_size.width())
            / 2.0
    }
}

/// Mixin for an "end" floating action button location, such as
/// `<dyn FloatingActionButtonLocation>::end_docked()`.
pub trait FabEndOffsetX: StandardFabLocation {
    /// Calculates x-offset for end-aligned [`FloatingActionButtonLocation`]s.
    fn get_offset_x(&self, scaffold_geometry: &ScaffoldPrelayoutGeometry, adjustment: f64) -> f64 {
        match scaffold_geometry.text_direction {
            TextDirection::Rtl => left_offset_x(scaffold_geometry, adjustment),
            TextDirection::Ltr => right_offset_x(scaffold_geometry, adjustment),
        }
    }
}

/// Mixin for a "mini" floating action button location, such as
/// `<dyn FloatingActionButtonLocation>::mini_start_top()`.
pub trait FabMiniOffsetAdjustment: StandardFabLocation {
    /// Dart's `isMini` override.
    fn is_mini(&self) -> bool {
        true
    }
}

/// Declares one of Dart's private standard locations: a unit struct that extends
/// [`StandardFabLocation`] with an x mixin and a y mixin (and, when `mini`, with
/// [`FabMiniOffsetAdjustment`]), and whose `Debug` is Dart's `toString`.
macro_rules! standard_fab_location {
    ($name:ident, $label:literal, $x:ident, $y:ident $(, $mini:ident)?) => {
        #[doc = concat!("Dart's `_", stringify!($name), "`.")]
        struct $name;

        impl FloatingActionButtonLocation for $name {
            fn get_offset(&self, scaffold_geometry: &ScaffoldPrelayoutGeometry) -> Offset {
                StandardFabLocation::get_offset(self, scaffold_geometry)
            }
        }

        impl StandardFabLocation for $name {
            fn get_offset_x(
                &self,
                scaffold_geometry: &ScaffoldPrelayoutGeometry,
                adjustment: f64,
            ) -> f64 {
                $x::get_offset_x(self, scaffold_geometry, adjustment)
            }

            fn get_offset_y(
                &self,
                scaffold_geometry: &ScaffoldPrelayoutGeometry,
                adjustment: f64,
            ) -> f64 {
                $y::get_offset_y(self, scaffold_geometry, adjustment)
            }

            $(
                fn is_mini(&self) -> bool {
                    $mini::is_mini(self)
                }
            )?
        }

        $(impl $mini for $name {})?
        impl $x for $name {}
        impl $y for $name {}

        impl Debug for $name {
            fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
                f.write_str(concat!("FloatingActionButtonLocation.", $label))
            }
        }
    };
}

standard_fab_location!(
    StartTopFabLocation,
    "startTop",
    FabStartOffsetX,
    FabTopOffsetY
);
standard_fab_location!(
    MiniStartTopFabLocation,
    "miniStartTop",
    FabStartOffsetX,
    FabTopOffsetY,
    FabMiniOffsetAdjustment
);
standard_fab_location!(
    CenterTopFabLocation,
    "centerTop",
    FabCenterOffsetX,
    FabTopOffsetY
);
standard_fab_location!(
    MiniCenterTopFabLocation,
    "miniCenterTop",
    FabCenterOffsetX,
    FabTopOffsetY,
    FabMiniOffsetAdjustment
);
standard_fab_location!(EndTopFabLocation, "endTop", FabEndOffsetX, FabTopOffsetY);
standard_fab_location!(
    MiniEndTopFabLocation,
    "miniEndTop",
    FabEndOffsetX,
    FabTopOffsetY,
    FabMiniOffsetAdjustment
);
standard_fab_location!(
    StartFloatFabLocation,
    "startFloat",
    FabStartOffsetX,
    FabFloatOffsetY
);
standard_fab_location!(
    MiniStartFloatFabLocation,
    "miniStartFloat",
    FabStartOffsetX,
    FabFloatOffsetY,
    FabMiniOffsetAdjustment
);
standard_fab_location!(
    CenterFloatFabLocation,
    "centerFloat",
    FabCenterOffsetX,
    FabFloatOffsetY
);
standard_fab_location!(
    MiniCenterFloatFabLocation,
    "miniCenterFloat",
    FabCenterOffsetX,
    FabFloatOffsetY,
    FabMiniOffsetAdjustment
);
standard_fab_location!(
    EndFloatFabLocation,
    "endFloat",
    FabEndOffsetX,
    FabFloatOffsetY
);
standard_fab_location!(
    MiniEndFloatFabLocation,
    "miniEndFloat",
    FabEndOffsetX,
    FabFloatOffsetY,
    FabMiniOffsetAdjustment
);
standard_fab_location!(
    StartDockedFabLocation,
    "startDocked",
    FabStartOffsetX,
    FabDockedOffsetY
);
standard_fab_location!(
    MiniStartDockedFabLocation,
    "miniStartDocked",
    FabStartOffsetX,
    FabDockedOffsetY,
    FabMiniOffsetAdjustment
);
standard_fab_location!(
    CenterDockedFabLocation,
    "centerDocked",
    FabCenterOffsetX,
    FabDockedOffsetY
);
standard_fab_location!(
    MiniCenterDockedFabLocation,
    "miniCenterDocked",
    FabCenterOffsetX,
    FabDockedOffsetY,
    FabMiniOffsetAdjustment
);
standard_fab_location!(
    EndDockedFabLocation,
    "endDocked",
    FabEndOffsetX,
    FabDockedOffsetY
);
standard_fab_location!(
    MiniEndDockedFabLocation,
    "miniEndDocked",
    FabEndOffsetX,
    FabDockedOffsetY,
    FabMiniOffsetAdjustment
);
standard_fab_location!(
    EndContainedFabLocation,
    "endContained",
    FabEndOffsetX,
    FabContainedOffsetY
);

/// Provider of animations to move the `FloatingActionButton` between
/// [`FloatingActionButtonLocation`]s.
///
/// The `Scaffold` uses `Scaffold.floatingActionButtonAnimator` to define:
///
///  * The [`Offset`] of the `FloatingActionButton` between the old and new
///    [`FloatingActionButtonLocation`]s as part of the transition animation.
///  * An `Animation` to scale the `FloatingActionButton` during the transition.
///  * An `Animation` to rotate the `FloatingActionButton` during the transition.
///  * Where to start a new animation from if an animation is interrupted.
///
/// See also:
///
///  * `FloatingActionButton`, which is a circular button typically shown in the
///    bottom right corner of the app.
///  * [`FloatingActionButtonLocation`], which the `Scaffold` uses to place the
///    `Scaffold.floatingActionButton` within the `Scaffold`'s layout.
pub trait FloatingActionButtonAnimator: Debug + 'static {
    /// Gets the `FloatingActionButton`'s position relative to the origin of the
    /// `Scaffold` based on `progress`.
    ///
    /// `begin` is the [`Offset`] provided by the previous
    /// [`FloatingActionButtonLocation`].
    ///
    /// `end` is the [`Offset`] provided by the new
    /// [`FloatingActionButtonLocation`].
    ///
    /// `progress` is the current progress of the transition animation.
    /// When `progress` is 0.0, the returned [`Offset`] should be equal to `begin`.
    /// when `progress` is 1.0, the returned [`Offset`] should be equal to `end`.
    fn get_offset(&self, begin: Offset, end: Offset, progress: f64) -> Offset;

    /// Animates the scale of the `FloatingActionButton`.
    ///
    /// The animation should both start and end with a value of 1.0.
    ///
    /// For example, to create an animation that linearly scales out and then back in,
    /// you could join animations that pass each other with a `TrainHoppingAnimation`.
    fn get_scale_animation(&self, app: &mut App, parent: AnimationRef<f64>) -> AnimationRef<f64>;

    /// Animates the rotation of `Scaffold.floatingActionButton`.
    ///
    /// The animation should both start and end with a value of 0.0 or 1.0.
    ///
    /// The animation values are a fraction of a full circle, with 0.0 and 1.0
    /// corresponding to 0 and 360 degrees, while 0.5 corresponds to 180 degrees.
    ///
    /// For example, to create a rotation animation that rotates the
    /// `FloatingActionButton` through a full circle, drive a `Tween` from 0.0 to 1.0 with
    /// `parent`.
    fn get_rotation_animation(&self, app: &mut App, parent: AnimationRef<f64>)
    -> AnimationRef<f64>;

    /// Gets the progress value to restart a motion animation from when the animation is
    /// interrupted.
    ///
    /// `previous_value` is the value of the animation before it was interrupted.
    ///
    /// The restart of the animation will affect all three parts of the motion animation:
    /// offset animation, scale animation, and rotation animation.
    ///
    /// An interruption triggers if the `Scaffold` is given a new
    /// [`FloatingActionButtonLocation`] while it is still animating a transition between two
    /// previous [`FloatingActionButtonLocation`]s.
    ///
    /// A sensible default is usually 0.0, which is the same as restarting
    /// the animation from the beginning, regardless of the original state of the animation.
    fn get_animation_restart(&self, previous_value: f64) -> f64 {
        let _ = previous_value;
        0.0
    }
}

/// A shared [`FloatingActionButtonAnimator`], compared by identity as Dart compares its
/// animators.
pub type FloatingActionButtonAnimatorRef = Rc<dyn FloatingActionButtonAnimator>;

impl dyn FloatingActionButtonAnimator {
    /// Moves the `FloatingActionButton` by scaling out and then in at a new
    /// [`FloatingActionButtonLocation`].
    ///
    /// This animator shrinks the `FloatingActionButton` down until it disappears, then
    /// grows it back to full size at its new [`FloatingActionButtonLocation`].
    ///
    /// This is the default `FloatingActionButton` motion animation.
    pub fn scaling() -> FloatingActionButtonAnimatorRef {
        thread_local! {
            static ANIMATOR: FloatingActionButtonAnimatorRef = Rc::new(ScalingFabMotionAnimator);
        }
        ANIMATOR.with(Rc::clone)
    }

    /// Creates an instance of [`FloatingActionButtonAnimator`] where the
    /// `FloatingActionButton` does not animate on entrance and exit when
    /// [`FloatingActionButtonLocation`] is shown or hidden and when transitioning between
    /// [`FloatingActionButtonLocation`]s.
    pub fn no_animation() -> FloatingActionButtonAnimatorRef {
        thread_local! {
            static ANIMATOR: FloatingActionButtonAnimatorRef =
                Rc::new(NoAnimationFabMotionAnimator);
        }
        ANIMATOR.with(Rc::clone)
    }
}

/// Dart's `_ScalingFabMotionAnimator`.
#[derive(Debug)]
struct ScalingFabMotionAnimator;

/// Dart's `static final` tweens of `_ScalingFabMotionAnimator`, kept per [`App`] and made on
/// first use. They are shared by every animation the animator returns, so the `App` keeps them for
/// its life.
#[derive(Default)]
struct ScalingFabMotionAnimatorStatics {
    rotation_tween: Option<Ref<Tween<f64>>>,
    threshold_center_tween: Option<Ref<CurveTween>>,
}

impl ScalingFabMotionAnimator {
    // Because we only see the last half of the rotation tween,
    // it needs to go twice as far.
    fn rotation_tween(app: &mut App) -> Ref<Tween<f64>> {
        let statics = app.singleton::<ScalingFabMotionAnimatorStatics>();
        if let Some(rotation_tween) = app.get(statics).rotation_tween.clone() {
            return rotation_tween;
        }
        let rotation_tween = Tween::new(
            app,
            Some(1.0 - K_FLOATING_ACTION_BUTTON_TURN_INTERVAL * 2.0),
            Some(1.0),
        );
        app.get_mut(statics).rotation_tween = Some(rotation_tween.clone());
        rotation_tween
    }

    fn threshold_center_tween(app: &mut App) -> Ref<CurveTween> {
        let statics = app.singleton::<ScalingFabMotionAnimatorStatics>();
        if let Some(threshold_center_tween) = app.get(statics).threshold_center_tween.clone() {
            return threshold_center_tween;
        }
        let threshold_center_tween = CurveTween::new(app, Rc::new(Threshold::new(0.5)));
        app.get_mut(statics).threshold_center_tween = Some(threshold_center_tween.clone());
        threshold_center_tween
    }
}

impl FloatingActionButtonAnimator for ScalingFabMotionAnimator {
    fn get_offset(&self, begin: Offset, end: Offset, progress: f64) -> Offset {
        if progress < 0.5 { begin } else { end }
    }

    fn get_scale_animation(&self, app: &mut App, parent: AnimationRef<f64>) -> AnimationRef<f64> {
        // Animate the scale down from 1 to 0 in the first half of the animation
        // then from 0 back to 1 in the second half.
        let curve: Rc<dyn Curve> = Rc::new(Interval::new(0.5, 1.0, Curves::ease()));
        let flipped_tween = CurveTween::new(app, Rc::new(Rc::clone(&curve).flipped()));
        let flipped = parent.drive(app, flipped_tween);
        let first = ReverseAnimation::new(app, flipped).into_erased();
        let curve_tween = CurveTween::new(app, curve);
        let next = parent.drive(app, curve_tween);
        app.create(AnimationSwap::new(first, next, parent, 0.5))
            .into_erased()
    }

    fn get_rotation_animation(
        &self,
        app: &mut App,
        parent: AnimationRef<f64>,
    ) -> AnimationRef<f64> {
        // This rotation will turn on the way in, but not on the way out.
        let rotation_tween = ScalingFabMotionAnimator::rotation_tween(app);
        let first = parent.drive(app, rotation_tween);
        let threshold_center_tween = ScalingFabMotionAnimator::threshold_center_tween(app);
        let threshold = parent.drive(app, threshold_center_tween);
        let next = ReverseAnimation::new(app, threshold).into_erased();
        app.create(AnimationSwap::new(first, next, parent, 0.5))
            .into_erased()
    }

    // If the animation was just starting, we'll continue from where we left off.
    // If the animation was finishing, we'll treat it as if we were starting at that point in
    // reverse. This avoids a size jump during the animation.
    fn get_animation_restart(&self, previous_value: f64) -> f64 {
        f64::min(1.0 - previous_value, previous_value)
    }
}

/// Dart's `_NoAnimationFabMotionAnimator`.
#[derive(Debug)]
struct NoAnimationFabMotionAnimator;

/// Dart's `const AlwaysStoppedAnimation<double>(1.0)`, which Dart canonicalizes into one
/// object, kept per [`App`] and made on first use; the `App` keeps it for its life.
#[derive(Default)]
struct AlwaysStoppedAtOne {
    animation: Option<Ref<AlwaysStoppedAnimation<f64>>>,
}

impl AlwaysStoppedAtOne {
    fn instance(app: &mut App) -> AnimationRef<f64> {
        let statics = app.singleton::<AlwaysStoppedAtOne>();
        if let Some(animation) = app.get(statics).animation.clone() {
            return animation.into_erased();
        }
        let animation = app.create(AlwaysStoppedAnimation::new(1.0));
        app.get_mut(statics).animation = Some(animation.clone());
        animation.into_erased()
    }
}

impl FloatingActionButtonAnimator for NoAnimationFabMotionAnimator {
    fn get_offset(&self, begin: Offset, end: Offset, progress: f64) -> Offset {
        let _ = (begin, progress);
        end
    }

    fn get_rotation_animation(
        &self,
        app: &mut App,
        parent: AnimationRef<f64>,
    ) -> AnimationRef<f64> {
        let _ = parent;
        AlwaysStoppedAtOne::instance(app)
    }

    fn get_scale_animation(&self, app: &mut App, parent: AnimationRef<f64>) -> AnimationRef<f64> {
        let _ = parent;
        AlwaysStoppedAtOne::instance(app)
    }
}

/// An animation that swaps from one animation to the next when the
/// [`parent`](Self::parent) passes [`swap_threshold`](Self::swap_threshold).
///
/// The value of this animation is the value of `first` when `parent.value` <
/// [`swap_threshold`](Self::swap_threshold) and the value of `next` otherwise.
struct AnimationSwap<T: Clone + PartialEq + 'static> {
    first: AnimationRef<T>,
    next: AnimationRef<T>,
    parent: AnimationRef<f64>,
    swap_threshold: f64,
    last_status: Option<AnimationStatus>,
    last_value: Option<T>,
    lazy_listener: AnimationLazyListenerData,
    local_listeners: AnimationLocalListenersData,
    local_status_listeners: AnimationLocalStatusListenersData,
}

impl<T: Clone + PartialEq + 'static> AnimationSwap<T> {
    /// Creates an [`AnimationSwap`].
    ///
    /// Either argument can be an [`AnimationSwap`] itself to combine multiple
    /// animations.
    fn new(
        first: AnimationRef<T>,
        next: AnimationRef<T>,
        parent: AnimationRef<f64>,
        swap_threshold: f64,
    ) -> AnimationSwap<T> {
        AnimationSwap {
            first,
            next,
            parent,
            swap_threshold,
            last_status: None,
            last_value: None,
            lazy_listener: AnimationLazyListenerData::new(),
            local_listeners: AnimationLocalListenersData::new(),
            local_status_listeners: AnimationLocalStatusListenersData::new(),
        }
    }
}

impl<T: Clone + PartialEq + 'static> AnimationLazyListenerMixin for AnimationSwap<T> {
    fn lazy_listener_data(self: Handle<Self>, app: &App) -> &AnimationLazyListenerData {
        &app.get(self).lazy_listener
    }

    fn lazy_listener_data_mut(self: Handle<Self>, app: &mut App) -> &mut AnimationLazyListenerData {
        &mut app.get_mut(self).lazy_listener
    }

    fn did_start_listening(self: Handle<Self>, app: &mut App) {
        compound_did_start_listening::<AnimationSwap<T>, T>(self, app)
    }

    fn did_stop_listening(self: Handle<Self>, app: &mut App) {
        compound_did_stop_listening::<AnimationSwap<T>, T>(self, app)
    }
}

impl<T: Clone + PartialEq + 'static> AnimationLocalListenersMixin for AnimationSwap<T> {
    fn local_listeners_data(self: Handle<Self>, app: &App) -> &AnimationLocalListenersData {
        &app.get(self).local_listeners
    }

    fn local_listeners_data_mut(
        self: Handle<Self>,
        app: &mut App,
    ) -> &mut AnimationLocalListenersData {
        &mut app.get_mut(self).local_listeners
    }

    fn did_register_listener(self: Handle<Self>, app: &mut App) {
        AnimationLazyListenerMixin::did_register_listener(self, app)
    }

    fn did_unregister_listener(self: Handle<Self>, app: &mut App) {
        AnimationLazyListenerMixin::did_unregister_listener(self, app)
    }
}

impl<T: Clone + PartialEq + 'static> AnimationLocalStatusListenersMixin for AnimationSwap<T> {
    fn local_status_listeners_data(
        self: Handle<Self>,
        app: &App,
    ) -> &AnimationLocalStatusListenersData {
        &app.get(self).local_status_listeners
    }

    fn local_status_listeners_data_mut(
        self: Handle<Self>,
        app: &mut App,
    ) -> &mut AnimationLocalStatusListenersData {
        &mut app.get_mut(self).local_status_listeners
    }

    fn did_register_listener(self: Handle<Self>, app: &mut App) {
        AnimationLazyListenerMixin::did_register_listener(self, app)
    }

    fn did_unregister_listener(self: Handle<Self>, app: &mut App) {
        AnimationLazyListenerMixin::did_unregister_listener(self, app)
    }
}

impl<T: Clone + PartialEq + 'static> CompoundAnimation<T> for AnimationSwap<T> {
    fn first(self: Handle<Self>, app: &App) -> AnyAnimation<T> {
        app.get(self).first.handle()
    }

    fn next(self: Handle<Self>, app: &App) -> AnyAnimation<T> {
        app.get(self).next.handle()
    }

    fn last_status(self: Handle<Self>, app: &App) -> Option<AnimationStatus> {
        app.get(self).last_status
    }

    fn last_status_mut(self: Handle<Self>, app: &mut App) -> &mut Option<AnimationStatus> {
        &mut app.get_mut(self).last_status
    }

    fn last_value(self: Handle<Self>, app: &App) -> &Option<T> {
        &app.get(self).last_value
    }

    fn last_value_mut(self: Handle<Self>, app: &mut App) -> &mut Option<T> {
        &mut app.get_mut(self).last_value
    }
}

// Dart: `class _AnimationSwap<T> extends CompoundAnimation<T>`.
impl<T: Clone + PartialEq + 'static> Animation<T> for AnimationSwap<T> {
    fn add_listener(self: Handle<Self>, app: &mut App, listener: Listener) {
        AnimationLocalListenersMixin::add_listener(self, app, listener)
    }

    fn remove_listener(self: Handle<Self>, app: &mut App, listener: &Listener) {
        AnimationLocalListenersMixin::remove_listener(self, app, listener)
    }

    fn add_status_listener(self: Handle<Self>, app: &mut App, listener: AnimationStatusListener) {
        AnimationLocalStatusListenersMixin::add_status_listener(self, app, listener)
    }

    fn remove_status_listener(
        self: Handle<Self>,
        app: &mut App,
        listener: &AnimationStatusListener,
    ) {
        AnimationLocalStatusListenersMixin::remove_status_listener(self, app, listener)
    }

    fn status(self: Handle<Self>, app: &App) -> AnimationStatus {
        CompoundAnimation::status(self, app)
    }

    fn value(self: Handle<Self>, app: &App) -> T {
        let animation = app.get(self);
        if animation.parent.value(app) < animation.swap_threshold {
            animation.first.value(app)
        } else {
            animation.next.value(app)
        }
    }
}

#[cfg(test)]
mod tests {
    //! The cases of `test/floating_action_button_location_test.dart`.
    //!
    //! Dart's `_singleFabScaffold` and `buildTest` put a `BottomNavigationBar` in the scaffold,
    //! which is not ported. A case whose asserted values do not depend on it is ported without
    //! it, and says so; a case whose values depend on its height waits for it (listed at the
    //! end of this module).
    use waft_widgets::Text;

    use std::cell::RefCell;

    use waft_animation::AlwaysStoppedAnimation;
    use waft_embedder::{Rect, Size};
    use waft_foundation::Listenable;
    use waft_painting::EdgeInsets;
    use waft_rendering::CustomPainter;
    use waft_scheduler::SchedulerBinding;
    use waft_widgets::{
        AnyElement, BuildContext, Builder, Center, CustomPaint, Directionality, GlobalKey,
        IntoWidget, KeyRef, MediaQuery, MediaQueryData, PreferredSizeWidgetRef, RotationTransition,
        SizedBox, State, StateData, StatefulWidget, WidgetRef, WidgetsBinding, downcast_widget,
    };

    use waft_widgets::testing::{
        WidgetTester, expect, find, finds_nothing, finds_one_widget, test_widgets,
    };

    use super::*;
    use crate::app_bar::AppBar;
    use crate::floating_action_button::FloatingActionButton;
    use crate::material_localizations::tests::us_english;
    use crate::scaffold::{
        Scaffold, ScaffoldGeometry, ScaffoldGeometryNotifier, ScaffoldMessenger,
    };
    use crate::snack_bar::SnackBar;
    use crate::test_support::material_app;
    use crate::theme_data::ThemeData;

    /// Dart's `const FloatingActionButton(onPressed: null, child: Text('1'))`.
    fn default_fab() -> FloatingActionButton {
        FloatingActionButton::new(None).child(Text::new("1"))
    }

    /// Dart's `_buildFrame`.
    fn build_frame(
        fab: Option<FloatingActionButton>,
        location: Option<FloatingActionButtonLocationRef>,
        listener: Option<WidgetRef>,
        text_direction: TextDirection,
        view_insets: EdgeInsets,
        bab: Option<WidgetRef>,
    ) -> WidgetRef {
        let mut scaffold = Scaffold::new().app_bar(PreferredSizeWidgetRef::new(
            AppBar::new().title(Text::new("FabLocation Test")),
        ));
        if let Some(location) = location {
            scaffold = scaffold.floating_action_button_location(location);
        }
        if let Some(fab) = fab {
            scaffold = scaffold.floating_action_button(fab);
        }
        if let Some(bab) = bab {
            scaffold = scaffold.bottom_navigation_bar(bab);
        }
        if let Some(listener) = listener {
            scaffold = scaffold.body(listener);
        }
        us_english(Directionality::new(
            text_direction,
            MediaQuery::new(MediaQueryData::new().view_insets(view_insets), scaffold).into_widget(),
        ))
    }

    /// `_buildFrame(location:)` with Dart's other defaults.
    fn frame(location: Option<FloatingActionButtonLocationRef>) -> WidgetRef {
        build_frame(
            Some(default_fab()),
            location,
            None,
            TextDirection::Ltr,
            EdgeInsets::only(0.0, 0.0, 0.0, 200.0),
            None,
        )
    }

    fn fab_box(tester: &WidgetTester) -> waft_rendering::AnyRenderBox {
        let found = find::by_type::<FloatingActionButton>().evaluate(tester);
        assert_eq!(found.len(), 1, "one FloatingActionButton");
        tester.render_box_of(found[0])
    }

    /// `tester.getCenter(find.byType(FloatingActionButton))`.
    fn fab_center(tester: &WidgetTester) -> Offset {
        let render = fab_box(tester);
        let app = tester.cell.borrow();
        let size = render.size(&app);
        render.local_to_global(&app, size.center(Offset::ZERO), None)
    }

    /// `tester.getTopLeft(find.byType(FloatingActionButton))`.
    fn fab_top_left(tester: &WidgetTester) -> Offset {
        let render = fab_box(tester);
        let app = tester.cell.borrow();
        render.local_to_global(&app, Offset::ZERO, None)
    }

    /// `tester.getTopRight(find.byType(FloatingActionButton))`.
    fn fab_top_right(tester: &WidgetTester) -> Offset {
        let render = fab_box(tester);
        let app = tester.cell.borrow();
        let size = render.size(&app);
        render.local_to_global(&app, Offset::new(size.width(), 0.0), None)
    }

    fn transient_callback_count(tester: &WidgetTester) -> usize {
        SchedulerBinding::transient_callback_count(&mut tester.cell.borrow_mut())
    }

    /// The tolerance of a position read through `local_to_global` across the FAB's
    /// `RotationTransition`: `Matrix4` is valo's f32 matrix (waft-gestures' PORTING.md), so a
    /// rotated position is exact only to f32 where Dart's f64 one is exact.
    const POSITION_TOLERANCE: f64 = 1e-4;

    /// Dart's `expect(x, y)` on a coordinate read through the FAB's transitions.
    fn assert_coordinate_eq(actual: f64, expected: f64) {
        assert!(
            (actual - expected).abs() < POSITION_TOLERANCE,
            "{actual} is not {expected}"
        );
    }

    /// Dart's `expect(offset, Offset(..))` on a point read through the FAB's transitions.
    fn assert_offset_eq(actual: Offset, expected: Offset) {
        assert!(
            (actual.dx() - expected.dx()).abs() < POSITION_TOLERANCE
                && (actual.dy() - expected.dy()).abs() < POSITION_TOLERANCE,
            "{actual:?} is not {expected:?}"
        );
    }

    /// Dart's `rectMoreOrLessEquals`, within [`POSITION_TOLERANCE`] for the reason there.
    fn assert_rect_more_or_less_equals(actual: Rect, expected: Rect) {
        let close = |a: f64, b: f64| (a - b).abs() < POSITION_TOLERANCE;
        assert!(
            close(actual.left, expected.left)
                && close(actual.top, expected.top)
                && close(actual.right, expected.right)
                && close(actual.bottom, expected.bottom),
            "{actual:?} is not {expected:?}"
        );
    }

    /// Dart's `_StartTopFloatingActionButtonLocation`.
    #[derive(Debug)]
    struct StartTopFloatingActionButtonLocation;

    impl FloatingActionButtonLocation for StartTopFloatingActionButtonLocation {
        fn get_offset(&self, scaffold_geometry: &ScaffoldPrelayoutGeometry) -> Offset {
            let fab_x = match scaffold_geometry.text_direction {
                TextDirection::Rtl => {
                    let start_padding =
                        K_FLOATING_ACTION_BUTTON_MARGIN + scaffold_geometry.min_insets.right;
                    scaffold_geometry.scaffold_size.width()
                        - scaffold_geometry.floating_action_button_size.width()
                        - start_padding
                }
                TextDirection::Ltr => {
                    K_FLOATING_ACTION_BUTTON_MARGIN + scaffold_geometry.min_insets.left
                }
            };
            let fab_y = scaffold_geometry.content_top
                - (scaffold_geometry.floating_action_button_size.height() / 2.0);
            Offset::new(fab_x, fab_y)
        }
    }

    /// Dart's `_NoScalingFabAnimator`.
    #[derive(Debug)]
    struct NoScalingFabAnimator;

    impl FloatingActionButtonAnimator for NoScalingFabAnimator {
        fn get_offset(&self, begin: Offset, end: Offset, progress: f64) -> Offset {
            if progress < 0.5 { begin } else { end }
        }

        fn get_scale_animation(
            &self,
            app: &mut App,
            _parent: AnimationRef<f64>,
        ) -> AnimationRef<f64> {
            app.create(AlwaysStoppedAnimation::new(1.0))
                .into_erased::<AnyAnimation<f64>>()
        }

        fn get_rotation_animation(
            &self,
            app: &mut App,
            _parent: AnimationRef<f64>,
        ) -> AnimationRef<f64> {
            app.create(AlwaysStoppedAnimation::new(1.0))
                .into_erased::<AnyAnimation<f64>>()
        }
    }

    // Basic floating action button locations

    // 'still animates motion when the floating action button is null'.
    #[test]
    fn a_null_floating_action_button_still_animates_its_motion() {
        test_widgets(|tester| {
            let null_frame = |location| {
                build_frame(
                    None,
                    location,
                    None,
                    TextDirection::Ltr,
                    EdgeInsets::only(0.0, 0.0, 0.0, 200.0),
                    None,
                )
            };
            tester.pump_widget(null_frame(None));

            expect(
                tester,
                &find::by_type::<FloatingActionButton>(),
                finds_nothing(),
            );
            assert_eq!(transient_callback_count(tester), 0);

            tester.pump_widget(null_frame(Some(
                <dyn FloatingActionButtonLocation>::end_float(),
            )));

            expect(
                tester,
                &find::by_type::<FloatingActionButton>(),
                finds_nothing(),
            );
            assert!(transient_callback_count(tester) > 0);

            tester.pump_widget(null_frame(Some(
                <dyn FloatingActionButtonLocation>::center_float(),
            )));

            expect(
                tester,
                &find::by_type::<FloatingActionButton>(),
                finds_nothing(),
            );
            assert!(transient_callback_count(tester) > 0);
        });
    }

    // 'moves fab from center to end and back'.
    #[test]
    fn the_fab_moves_from_center_to_end_and_back() {
        test_widgets(|tester| {
            tester.pump_widget(frame(Some(<dyn FloatingActionButtonLocation>::end_float())));

            assert_offset_eq(fab_center(tester), Offset::new(756.0, 356.0));
            assert_eq!(transient_callback_count(tester), 0);

            tester.pump_widget(frame(Some(
                <dyn FloatingActionButtonLocation>::center_float(),
            )));

            assert!(transient_callback_count(tester) > 0);

            tester.pump_and_settle();

            assert_offset_eq(fab_center(tester), Offset::new(400.0, 356.0));
            assert_eq!(transient_callback_count(tester), 0);

            tester.pump_widget(frame(Some(<dyn FloatingActionButtonLocation>::end_float())));

            assert!(transient_callback_count(tester) > 0);

            tester.pump_and_settle();

            assert_offset_eq(fab_center(tester), Offset::new(756.0, 356.0));
            assert_eq!(transient_callback_count(tester), 0);
        });
    }

    // 'moves to and from custom-defined positions'.
    #[test]
    fn the_fab_moves_to_and_from_a_custom_location() {
        test_widgets(|tester| {
            tester.pump_widget(frame(Some(Rc::new(StartTopFloatingActionButtonLocation))));

            assert_offset_eq(fab_center(tester), Offset::new(44.0, 56.0));

            tester.pump_widget(frame(Some(
                <dyn FloatingActionButtonLocation>::center_float(),
            )));
            assert!(transient_callback_count(tester) > 0);

            tester.pump_and_settle();

            assert_offset_eq(fab_center(tester), Offset::new(400.0, 356.0));
            assert_eq!(transient_callback_count(tester), 0);

            tester.pump_widget(frame(Some(Rc::new(StartTopFloatingActionButtonLocation))));

            assert!(transient_callback_count(tester) > 0);

            tester.pump_and_settle();

            assert_offset_eq(fab_center(tester), Offset::new(44.0, 56.0));
            assert_eq!(transient_callback_count(tester), 0);
        });
    }

    // interrupts in-progress animations without jumps

    /// What the scaffold's geometry and the FAB's rotations were at one geometry notification:
    /// Dart's `check` reads them in the listener; here the listener records them and
    /// [`check_steps`] runs Dart's checks over the record.
    struct Step {
        rect: Option<Size>,
        rotations: Vec<f64>,
    }

    // The maximum amounts we expect the fab width and height to change
    // during one step of a transition.
    const MAX_DELTA_WIDTH: f64 = 12.5;
    const MAX_DELTA_HEIGHT: f64 = 12.5;

    // The maximum amounts we expect the fab icon to rotate during one step
    // of a transition.
    const MAX_DELTA_ROTATION: f64 = 0.09;

    /// Dart's `_GeometryCachePainter`: caches the scaffold's geometry when it paints, the
    /// only time it can be read.
    struct GeometryCachePainter {
        geometry_listenable: Ref<ScaffoldGeometryNotifier>,
        repaint: Rc<dyn Listenable>,
        value: Rc<RefCell<Option<ScaffoldGeometry>>>,
    }

    impl CustomPainter for GeometryCachePainter {
        fn repaint(&self) -> Option<&Rc<dyn Listenable>> {
            Some(&self.repaint)
        }

        fn paint(&self, app: &mut App, _canvas: &mut waft_embedder::Canvas, _size: Size) {
            *self.value.borrow_mut() = Some(self.geometry_listenable.value(app));
        }

        fn should_repaint(&self, _app: &App, _old_delegate: &dyn CustomPainter) -> bool {
            true
        }

        fn as_any(&self) -> &dyn std::any::Any {
            self
        }
    }

    /// Dart's `_GeometryListener`, keyed so the case can reach its state.
    #[derive(Debug)]
    struct GeometryListener {
        key: KeyRef,
    }

    impl StatefulWidget for GeometryListener {
        type State = GeometryListenerState;

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

        fn create_state(&self) -> GeometryListenerState {
            GeometryListenerState {
                state: StateData::new(),
                geometry_listenable: None,
                cache: Rc::new(RefCell::new(None)),
            }
        }
    }

    /// Dart's `_GeometryListenerState`.
    struct GeometryListenerState {
        state: StateData<GeometryListener>,
        geometry_listenable: Option<Ref<ScaffoldGeometryNotifier>>,
        cache: Rc<RefCell<Option<ScaffoldGeometry>>>,
    }

    impl State for GeometryListenerState {
        type Widget = GeometryListener;
        waft_widgets::state_accessors!();

        fn did_change_dependencies(self: Handle<Self>, app: &mut App) {
            let context = self.context(app);
            let new_listenable = Scaffold::geometry_of(app, context);
            if app.get(self).geometry_listenable.as_ref() == Some(&new_listenable) {
                return;
            }
            app.get_mut(self).geometry_listenable = Some(new_listenable);
            app.get_mut(self).cache = Rc::new(RefCell::new(None));
        }

        fn build(self: Handle<Self>, app: &mut App, _context: BuildContext) -> WidgetRef {
            let geometry_listenable = app
                .get(self)
                .geometry_listenable
                .clone()
                .expect("dependencies");
            CustomPaint::new()
                .painter(GeometryCachePainter {
                    geometry_listenable: geometry_listenable.clone(),
                    repaint: Rc::new(geometry_listenable),
                    value: Rc::clone(&app.get(self).cache),
                })
                .into_widget()
        }
    }

    /// Every mounted element, parents first.
    fn elements(app: &mut App) -> Vec<AnyElement> {
        let root = WidgetsBinding::instance(app).root_element(app);
        let mut all = Vec::new();
        let mut pending: Vec<AnyElement> = root.into_iter().collect();
        while let Some(element) = pending.pop() {
            all.push(element);
            let mut children = Vec::new();
            element.visit_children(app, &mut |child| children.push(child));
            pending.extend(children.into_iter().rev());
        }
        all
    }

    /// Dart's `setupListener`: from now on, every geometry notification records a [`Step`].
    fn setup_listener(tester: &WidgetTester, key: &GlobalKey) -> Rc<RefCell<Vec<Step>>> {
        let steps = Rc::new(RefCell::new(Vec::new()));
        let mut app = tester.cell.borrow_mut();
        let state = key
            .current_state::<GeometryListenerState>(&app)
            .expect("the listener's state");
        let listenable = app
            .get(state)
            .geometry_listenable
            .clone()
            .expect("dependencies");
        listenable.add_listener(
            &mut app,
            waft_foundation::Listener::new({
                let steps = Rc::clone(&steps);
                move |app| {
                    let geometry = *app.get(state).cache.borrow();
                    let rect = geometry
                        .and_then(|geometry| geometry.floating_action_button_area)
                        .map(|area| area.size());
                    let rotations = elements(app)
                        .into_iter()
                        .filter_map(|element| {
                            downcast_widget::<RotationTransition>(&**element.widget(app))
                                .map(|transition| transition.turns().value(app))
                        })
                        .collect();
                    steps.borrow_mut().push(Step { rect, rotations });
                }
            }),
        );
        steps
    }

    /// Dart's `check`, run over every recorded notification in order.
    fn check_steps(steps: &[Step]) {
        let mut previous_rect: Option<Size> = None;
        let mut previous_rotations: Option<&[f64]> = None;
        for step in steps {
            let current_rect = step.rect;
            // Measure the delta in width and height of the rect, and check that
            // it never grows by more than a safe amount.
            if let (Some(previous), Some(current)) = (previous_rect, current_rect) {
                let delta_width = current.width() - previous.width();
                let delta_height = current.height() - previous.height();
                assert!(
                    delta_width.abs() <= MAX_DELTA_WIDTH,
                    "The Floating Action Button's width should not change faster than \
                     {MAX_DELTA_WIDTH} per animation step.\nPrevious rect: {previous:?}, \
                     current rect: {current:?}"
                );
                assert!(
                    delta_height.abs() <= MAX_DELTA_HEIGHT,
                    "The Floating Action Button's width should not change faster than \
                     {MAX_DELTA_HEIGHT} per animation step.\nPrevious rect: {previous:?}, \
                     current rect: {current:?}"
                );
            }
            previous_rect = current_rect;

            // Measure the delta in rotation.
            // Check that it never grows by more than a safe amount.
            //
            // There may be multiple transitions all active at
            // the same time. We are concerned only with the closest one.
            let current_rotations = &step.rotations;
            if previous_rotations.is_some_and(|previous| !previous.is_empty())
                && !current_rotations.is_empty()
                && previous_rect.is_some()
                && current_rect.is_some()
            {
                let previous = previous_rotations.expect("checked");
                let deltas: Vec<f64> = current_rotations
                    .iter()
                    .map(|current_rotation| {
                        // Dart's loop keeps the last delta: `minDelta = delta` precedes the
                        // `min`.
                        let mut min_delta = 0.0;
                        for previous_rotation in previous {
                            let delta = (previous_rotation - current_rotation).abs();
                            min_delta = delta;
                            min_delta = f64::min(delta, min_delta);
                        }
                        min_delta
                    })
                    .collect();

                assert!(
                    deltas.iter().any(|delta| *delta < MAX_DELTA_ROTATION),
                    "The Floating Action Button's rotation should not change faster than \
                     {MAX_DELTA_ROTATION} per animation step.\nDetected deltas were: \
                     {deltas:?}\nPrevious values: {previous:?}, current values: \
                     {current_rotations:?}"
                );
            }
            previous_rotations = Some(current_rotations);
        }
    }

    fn listener_frame(
        key: &GlobalKey,
        fab: Option<FloatingActionButton>,
        location: FloatingActionButtonLocationRef,
    ) -> WidgetRef {
        build_frame(
            fab,
            Some(location),
            Some(
                GeometryListener {
                    key: key.clone().into(),
                }
                .into_widget(),
            ),
            TextDirection::Ltr,
            EdgeInsets::only(0.0, 0.0, 0.0, 200.0),
            None,
        )
    }

    // 'moving the fab to centerFloat'.
    #[test]
    fn moving_the_fab_to_center_float_does_not_jump() {
        test_widgets(|tester| {
            let listener = GlobalKey::new();
            // Create a scaffold with the fab at endFloat
            tester.pump_widget(listener_frame(
                &listener,
                Some(default_fab()),
                <dyn FloatingActionButtonLocation>::end_float(),
            ));
            let steps = setup_listener(tester, &listener);

            // Move the fab to centerFloat'
            tester.pump_widget(listener_frame(
                &listener,
                Some(default_fab()),
                <dyn FloatingActionButtonLocation>::center_float(),
            ));
            tester.pump_and_settle();
            check_steps(&steps.borrow());
            assert!(!steps.borrow().is_empty());
        });
    }

    // 'interrupting motion towards the StartTop location.'.
    #[test]
    fn interrupting_motion_towards_the_start_top_location_does_not_jump() {
        test_widgets(|tester| {
            let listener = GlobalKey::new();
            tester.pump_widget(listener_frame(
                &listener,
                Some(default_fab()),
                <dyn FloatingActionButtonLocation>::center_float(),
            ));
            let steps = setup_listener(tester, &listener);

            // Move the fab to the top start after creating the fab.
            tester.pump_widget(listener_frame(
                &listener,
                Some(default_fab()),
                Rc::new(StartTopFloatingActionButtonLocation),
            ));
            tester.pump_by(K_FLOATING_ACTION_BUTTON_SEGUE / 2);

            // Interrupt motion to move to the end float
            tester.pump_widget(listener_frame(
                &listener,
                Some(default_fab()),
                <dyn FloatingActionButtonLocation>::end_float(),
            ));
            tester.pump_and_settle();
            check_steps(&steps.borrow());
            assert!(!steps.borrow().is_empty());
        });
    }

    // 'interrupting entrance to remove the fab.'.
    #[test]
    fn interrupting_the_entrance_to_remove_the_fab_does_not_jump() {
        test_widgets(|tester| {
            let listener = GlobalKey::new();
            tester.pump_widget(listener_frame(
                &listener,
                None,
                <dyn FloatingActionButtonLocation>::center_float(),
            ));
            let steps = setup_listener(tester, &listener);

            // Animate the fab in.
            tester.pump_widget(listener_frame(
                &listener,
                Some(default_fab()),
                <dyn FloatingActionButtonLocation>::end_float(),
            ));
            tester.pump_by(K_FLOATING_ACTION_BUTTON_SEGUE / 2);

            // Remove the fab.
            tester.pump_widget(listener_frame(
                &listener,
                None,
                <dyn FloatingActionButtonLocation>::end_float(),
            ));
            tester.pump_and_settle();
            check_steps(&steps.borrow());
            assert!(!steps.borrow().is_empty());
        });
    }

    // 'interrupting entrance of a new fab.'.
    #[test]
    fn interrupting_the_entrance_of_a_new_fab_does_not_jump() {
        test_widgets(|tester| {
            let listener = GlobalKey::new();
            tester.pump_widget(listener_frame(
                &listener,
                None,
                <dyn FloatingActionButtonLocation>::end_float(),
            ));
            let steps = setup_listener(tester, &listener);

            // Bring in a new fab.
            tester.pump_widget(listener_frame(
                &listener,
                Some(default_fab()),
                <dyn FloatingActionButtonLocation>::center_float(),
            ));
            tester.pump_by(K_FLOATING_ACTION_BUTTON_SEGUE / 2);

            // Interrupt motion to move the fab.
            tester.pump_widget(listener_frame(
                &listener,
                Some(default_fab()),
                <dyn FloatingActionButtonLocation>::end_float(),
            ));
            tester.pump_and_settle();
            check_steps(&steps.borrow());
            assert!(!steps.borrow().is_empty());
        });
    }

    fn bab_frame(location: FloatingActionButtonLocationRef, bab: Option<f64>) -> WidgetRef {
        build_frame(
            Some(default_fab()),
            Some(location),
            None,
            TextDirection::Ltr,
            EdgeInsets::ZERO,
            bab.map(|height| SizedBox::new().height(height).into_widget()),
        )
    }

    // 'Docked floating action button locations'.
    #[test]
    fn docked_locations_center_the_fab_on_the_top_of_the_bottom_bar() {
        test_widgets(|tester| {
            tester.pump_widget(bab_frame(
                <dyn FloatingActionButtonLocation>::end_docked(),
                Some(100.0),
            ));

            // Scaffold 800x600, FAB is 56x56, BAB is 800x100, FAB's center is
            // at the top of the BAB.
            assert_offset_eq(fab_center(tester), Offset::new(756.0, 500.0));

            tester.pump_widget(bab_frame(
                <dyn FloatingActionButtonLocation>::center_docked(),
                Some(100.0),
            ));
            tester.pump_and_settle();
            assert_offset_eq(fab_center(tester), Offset::new(400.0, 500.0));

            tester.pump_widget(bab_frame(
                <dyn FloatingActionButtonLocation>::end_docked(),
                Some(100.0),
            ));
            tester.pump_and_settle();
            assert_offset_eq(fab_center(tester), Offset::new(756.0, 500.0));
        });
    }

    // 'Docked floating action button locations: no BAB, small BAB'.
    #[test]
    fn docked_locations_keep_the_fab_on_screen_without_or_with_a_small_bottom_bar() {
        test_widgets(|tester| {
            tester.pump_widget(bab_frame(
                <dyn FloatingActionButtonLocation>::end_docked(),
                None,
            ));
            assert_offset_eq(fab_center(tester), Offset::new(756.0, 572.0));

            tester.pump_widget(bab_frame(
                <dyn FloatingActionButtonLocation>::end_docked(),
                Some(16.0),
            ));
            assert_offset_eq(fab_center(tester), Offset::new(756.0, 572.0));
        });
    }

    // 'Contained floating action button locations'.
    #[test]
    fn contained_locations_center_the_fab_in_the_bottom_bar() {
        test_widgets(|tester| {
            tester.pump_widget(bab_frame(
                <dyn FloatingActionButtonLocation>::end_contained(),
                Some(100.0),
            ));

            // Scaffold 800x600, FAB is 56x56, BAB is 800x100, FAB's center is
            // at the top of the BAB.
            // Formula: scaffold height - BAB height + FAB height / 2 + BAB top & bottom margins.
            assert_offset_eq(fab_center(tester), Offset::new(756.0, 550.0));
        });
    }

    /// Dart's `_singleFabScaffold`, without its `BottomNavigationBar` (see the module docs).
    fn single_fab_scaffold(
        app: &App,
        location: FloatingActionButtonLocationRef,
        animator: Option<FloatingActionButtonAnimatorRef>,
    ) -> WidgetRef {
        let mut scaffold = Scaffold::new()
            .app_bar(PreferredSizeWidgetRef::new(
                AppBar::new().title(Text::new("FloatingActionButtonLocation Test.")),
            ))
            .floating_action_button(
                FloatingActionButton::new(Some(waft_foundation::Listener::new(|_app| {})))
                    .child(Text::new("beach_access")),
            )
            .floating_action_button_location(location);
        if let Some(animator) = animator {
            scaffold = scaffold.floating_action_button_animator(animator);
        }
        material_app(
            ThemeData::builder().use_material3(false).build(app),
            Directionality::new(TextDirection::Ltr, scaffold).into_widget(),
        )
    }

    // Moves involving new locations: 'Animator can be updated', without the
    // BottomNavigationBar, which only moves the FAB vertically: every value asserted is
    // horizontal.
    #[test]
    fn the_animator_can_be_updated() {
        test_widgets(|tester| {
            let mut fab_animator = <dyn FloatingActionButtonAnimator>::scaling();
            let mut fab_location = <dyn FloatingActionButtonLocation>::start_float();

            let animation_duration = K_FLOATING_ACTION_BUTTON_SEGUE * 2;
            let quarter = animation_duration / 4;

            let frame = |tester: &WidgetTester,
                         location: &FloatingActionButtonLocationRef,
                         animator: &FloatingActionButtonAnimatorRef| {
                single_fab_scaffold(
                    &tester.cell.borrow(),
                    Rc::clone(location),
                    Some(Rc::clone(animator)),
                )
            };
            let root = frame(tester, &fab_location, &fab_animator);
            tester.pump_widget(root);

            expect(
                tester,
                &find::by_type::<FloatingActionButton>(),
                finds_one_widget(),
            );
            assert_eq!(transient_callback_count(tester), 0);
            assert_coordinate_eq(fab_center(tester).dx(), 44.0);

            fab_location = <dyn FloatingActionButtonLocation>::end_float();
            let root = frame(tester, &fab_location, &fab_animator);
            tester.pump_widget(root);

            // Dart's `lessThan(16.0)` holds through the f64 rounding of the FAB's full-turn
            // rotation, which leaves the corner 7e-15 inside; valo's f32 rotation leaves it
            // 6e-6 outside, so the bound is Dart's within POSITION_TOLERANCE.
            assert!(fab_top_left(tester).dx() < 16.0 + POSITION_TOLERANCE);

            tester.pump_by(quarter);
            assert!(fab_top_left(tester).dx() > 16.0);

            tester.pump_by(quarter);
            assert_coordinate_eq(fab_center(tester).dx(), 756.0);
            assert!(fab_top_right(tester).dx() < 800.0 - 16.0);

            tester.pump_by(quarter);
            assert!(fab_top_right(tester).dx() < 800.0 - 16.0);

            tester.pump_by(quarter);
            assert_coordinate_eq(fab_top_right(tester).dx(), 800.0 - 16.0);

            fab_location = <dyn FloatingActionButtonLocation>::start_float();
            fab_animator = Rc::new(NoScalingFabAnimator);
            let root = frame(tester, &fab_location, &fab_animator);
            tester.pump_widget(root);

            tester.pump_by(quarter);
            assert_coordinate_eq(fab_center(tester).dx(), 756.0);
            assert_coordinate_eq(fab_top_right(tester).dx(), 800.0 - 16.0);

            tester.pump_by(quarter);
            assert_coordinate_eq(fab_center(tester).dx(), 44.0);
            // As above: Dart's bound within POSITION_TOLERANCE.
            assert!(fab_top_left(tester).dx() < 16.0 + POSITION_TOLERANCE);
        });
    }

    // Locations account for safe interactive areas

    /// Dart's `buildTest`, without `bottomNavigationBar:`, which waits.
    #[allow(clippy::too_many_arguments, reason = "Dart's buildTest arguments")]
    fn build_test(
        app: &App,
        location: FloatingActionButtonLocationRef,
        data: MediaQueryData,
        key: &GlobalKey,
        mini: bool,
        app_bar: bool,
        bottom_sheet: bool,
        resize_to_avoid_bottom_inset: bool,
    ) -> WidgetRef {
        let key = key.clone();
        let mut scaffold = Scaffold::new()
            .resize_to_avoid_bottom_inset(resize_to_avoid_bottom_inset)
            .floating_action_button_location(location)
            .floating_action_button(Builder::new(move |_app, context| {
                FloatingActionButton::new(Some(waft_foundation::Listener::new(move |app| {
                    ScaffoldMessenger::of(app, context).show_snack_bar(
                        app,
                        SnackBar::new(Text::new("Snacky!")),
                        None,
                    );
                })))
                .mini(mini)
                .key(key.clone().into())
                .child(Text::new("FAB"))
                .into_widget()
            }));
        if bottom_sheet {
            scaffold = scaffold.bottom_sheet(
                SizedBox::new()
                    .height(100.0)
                    .child(Center::new().child(Text::new("BottomSheet"))),
            );
        }
        if app_bar {
            scaffold = scaffold.app_bar(PreferredSizeWidgetRef::new(
                AppBar::new().title(Text::new("Demo")),
            ));
        }
        material_app(
            ThemeData::builder().use_material3(false).build(app),
            MediaQuery::new(data, scaffold).into_widget(),
        )
    }

    struct SafeAreaCase {
        location: FloatingActionButtonLocationRef,
        mini: bool,
        key: GlobalKey,
        next_pointer: i64,
    }

    impl SafeAreaCase {
        fn new(location: FloatingActionButtonLocationRef, mini: bool) -> SafeAreaCase {
            SafeAreaCase {
                location,
                mini,
                key: GlobalKey::new(),
                next_pointer: 1,
            }
        }

        /// `tester.pumpWidget(buildTest(location, data, key, mini:, bottomSheet:,
        /// resizeToAvoidBottomInset:))`.
        fn pump(
            &self,
            tester: &mut WidgetTester,
            data: MediaQueryData,
            bottom_sheet: bool,
            resize_to_avoid_bottom_inset: bool,
        ) {
            self.pump_with(
                tester,
                data,
                false,
                bottom_sheet,
                resize_to_avoid_bottom_inset,
            );
        }

        /// [`pump`](Self::pump) with `buildTest(appBar:)`.
        fn pump_with(
            &self,
            tester: &mut WidgetTester,
            data: MediaQueryData,
            app_bar: bool,
            bottom_sheet: bool,
            resize_to_avoid_bottom_inset: bool,
        ) {
            let root = build_test(
                &tester.cell.borrow(),
                Rc::clone(&self.location),
                data,
                &self.key,
                self.mini,
                app_bar,
                bottom_sheet,
                resize_to_avoid_bottom_inset,
            );
            tester.pump_widget(root);
        }

        fn rect(&self, tester: &WidgetTester) -> Rect {
            tester.get_rect(&self.key)
        }

        /// `tester.tap(find.byKey(floatingActionButton))` and `pumpAndSettle`: shows a snack
        /// bar.
        fn tap_and_settle(&mut self, tester: &mut WidgetTester) {
            let center = tester.get_center(&self.key);
            tester.tap_at(center, self.next_pointer);
            self.next_pointer += 1;
            tester.pump_and_settle();
        }
    }

    const KEYBOARD_HEIGHT: f64 = 200.0;
    const VIEW_PADDING: f64 = 50.0;

    fn view_padding_bottom() -> MediaQueryData {
        MediaQueryData::new().view_padding(EdgeInsets::only(0.0, 0.0, 0.0, VIEW_PADDING))
    }

    fn view_padding_bottom_with_keyboard() -> MediaQueryData {
        view_padding_bottom().view_insets(EdgeInsets::only(0.0, 0.0, 0.0, KEYBOARD_HEIGHT))
    }

    /// Dart's `runFloatTests`, without the `BottomNavigationBar` steps, which wait.
    fn run_float_tests(
        tester: &mut WidgetTester,
        location: FloatingActionButtonLocationRef,
        default_rect: Rect,
        bottom_sheet_rect: Rect,
        snack_bar_rect: Rect,
        mini: bool,
    ) {
        let mut case = SafeAreaCase::new(location, mini);
        // Default
        case.pump(tester, view_padding_bottom(), false, true);
        assert_rect_more_or_less_equals(case.rect(tester), default_rect);
        // Present keyboard and check position, should change
        case.pump(tester, view_padding_bottom_with_keyboard(), false, true);
        assert_rect_more_or_less_equals(
            case.rect(tester),
            default_rect.translate(0.0, VIEW_PADDING - KEYBOARD_HEIGHT),
        );

        // With resizeToAvoidBottomInset: false
        // With keyboard presented, should maintain default position
        case.pump(tester, view_padding_bottom_with_keyboard(), false, false);
        assert_rect_more_or_less_equals(case.rect(tester), default_rect);

        // BottomSheet default
        case.pump(tester, view_padding_bottom(), true, true);
        assert_rect_more_or_less_equals(case.rect(tester), bottom_sheet_rect);
        // Present keyboard and check position, bottomSheet and FAB both resize
        case.pump(tester, view_padding_bottom_with_keyboard(), true, true);
        assert_rect_more_or_less_equals(
            case.rect(tester),
            bottom_sheet_rect.translate(0.0, -KEYBOARD_HEIGHT),
        );

        // bottomSheet with resizeToAvoidBottomInset: false
        // With keyboard presented, should maintain default bottomSheet position
        case.pump(tester, view_padding_bottom_with_keyboard(), true, false);
        assert_rect_more_or_less_equals(case.rect(tester), bottom_sheet_rect);

        // SnackBar default
        case.pump(tester, view_padding_bottom(), false, true);
        case.tap_and_settle(tester); // Show SnackBar
        assert_rect_more_or_less_equals(case.rect(tester), snack_bar_rect);

        // SnackBar when resized for presented keyboard
        case.pump(tester, view_padding_bottom_with_keyboard(), false, true);
        case.tap_and_settle(tester); // Show SnackBar
        assert_rect_more_or_less_equals(
            case.rect(tester),
            snack_bar_rect.translate(
                0.0,
                -KEYBOARD_HEIGHT + K_FLOATING_ACTION_BUTTON_MARGIN / 2.0,
            ),
        );
    }

    // 'startFloat', without the BottomNavigationBar steps.
    #[test]
    fn start_float_accounts_for_safe_interactive_areas() {
        test_widgets(|tester| {
            run_float_tests(
                tester,
                <dyn FloatingActionButtonLocation>::start_float(),
                Rect::from_ltrb(16.0, 478.0, 72.0, 534.0),
                Rect::from_ltrb(16.0, 472.0, 72.0, 528.0),
                Rect::from_ltrb(16.0, 478.0, 72.0, 534.0),
                false,
            );
        });
    }

    // 'miniStartFloat', without the BottomNavigationBar steps.
    #[test]
    fn mini_start_float_accounts_for_safe_interactive_areas() {
        test_widgets(|tester| {
            run_float_tests(
                tester,
                <dyn FloatingActionButtonLocation>::mini_start_float(),
                Rect::from_ltrb(12.0, 490.0, 60.0, 538.0),
                Rect::from_ltrb(12.0, 480.0, 60.0, 528.0),
                Rect::from_ltrb(12.0, 490.0, 60.0, 538.0),
                true,
            );
        });
    }

    // 'centerFloat', without the BottomNavigationBar steps.
    #[test]
    fn center_float_accounts_for_safe_interactive_areas() {
        test_widgets(|tester| {
            run_float_tests(
                tester,
                <dyn FloatingActionButtonLocation>::center_float(),
                Rect::from_ltrb(372.0, 478.0, 428.0, 534.0),
                Rect::from_ltrb(372.0, 472.0, 428.0, 528.0),
                Rect::from_ltrb(372.0, 478.0, 428.0, 534.0),
                false,
            );
        });
    }

    // 'miniCenterFloat', without the BottomNavigationBar steps.
    #[test]
    fn mini_center_float_accounts_for_safe_interactive_areas() {
        test_widgets(|tester| {
            run_float_tests(
                tester,
                <dyn FloatingActionButtonLocation>::mini_center_float(),
                Rect::from_ltrb(376.0, 490.0, 424.0, 538.0),
                Rect::from_ltrb(376.0, 480.0, 424.0, 528.0),
                Rect::from_ltrb(376.0, 490.0, 424.0, 538.0),
                true,
            );
        });
    }

    // 'endFloat', without the BottomNavigationBar steps.
    #[test]
    fn end_float_accounts_for_safe_interactive_areas() {
        test_widgets(|tester| {
            run_float_tests(
                tester,
                <dyn FloatingActionButtonLocation>::end_float(),
                Rect::from_ltrb(728.0, 478.0, 784.0, 534.0),
                Rect::from_ltrb(728.0, 472.0, 784.0, 528.0),
                Rect::from_ltrb(728.0, 478.0, 784.0, 534.0),
                false,
            );
        });
    }

    // 'miniEndFloat', without the BottomNavigationBar steps.
    #[test]
    fn mini_end_float_accounts_for_safe_interactive_areas() {
        test_widgets(|tester| {
            run_float_tests(
                tester,
                <dyn FloatingActionButtonLocation>::mini_end_float(),
                Rect::from_ltrb(740.0, 490.0, 788.0, 538.0),
                Rect::from_ltrb(740.0, 480.0, 788.0, 528.0),
                Rect::from_ltrb(740.0, 490.0, 788.0, 538.0),
                true,
            );
        });
    }

    /// Dart's `runDockedTests`, without the `BottomNavigationBar` steps (every step that
    /// reads `bottomSheetRect` has one), which wait.
    fn run_docked_tests(
        tester: &mut WidgetTester,
        location: FloatingActionButtonLocationRef,
        default_rect: Rect,
        snack_bar_rect: Rect,
        mini: bool,
    ) {
        const SCAFFOLD_HEIGHT: f64 = 600.0;
        let mut case = SafeAreaCase::new(location, mini);
        // Default
        case.pump(tester, view_padding_bottom(), false, true);
        assert_rect_more_or_less_equals(case.rect(tester), default_rect);
        // Present keyboard and check position, should change
        case.pump(tester, view_padding_bottom_with_keyboard(), false, true);
        assert_rect_more_or_less_equals(
            case.rect(tester),
            default_rect.translate(
                0.0,
                VIEW_PADDING - KEYBOARD_HEIGHT - K_FLOATING_ACTION_BUTTON_MARGIN,
            ),
        );
        // The FAB should be away from the keyboard
        assert!(case.rect(tester).bottom < SCAFFOLD_HEIGHT - KEYBOARD_HEIGHT);

        // With resizeToAvoidBottomInset: false
        // With keyboard presented, should maintain default position
        case.pump(tester, view_padding_bottom_with_keyboard(), false, false);
        assert_rect_more_or_less_equals(case.rect(tester), default_rect);

        // SnackBar default
        case.pump(tester, view_padding_bottom(), false, true);
        case.tap_and_settle(tester); // Show SnackBar
        assert_rect_more_or_less_equals(case.rect(tester), snack_bar_rect);

        // SnackBar when resized for presented keyboard
        case.pump(tester, view_padding_bottom_with_keyboard(), false, true);
        case.tap_and_settle(tester); // Show SnackBar
        assert_rect_more_or_less_equals(
            case.rect(tester),
            snack_bar_rect.translate(0.0, -KEYBOARD_HEIGHT),
        );
        // The FAB should be away from the keyboard
        assert!(case.rect(tester).bottom < SCAFFOLD_HEIGHT - KEYBOARD_HEIGHT);
    }

    // 'startDocked', without the BottomNavigationBar steps.
    #[test]
    fn start_docked_accounts_for_safe_interactive_areas() {
        test_widgets(|tester| {
            run_docked_tests(
                tester,
                <dyn FloatingActionButtonLocation>::start_docked(),
                Rect::from_ltrb(16.0, 494.0, 72.0, 550.0),
                Rect::from_ltrb(16.0, 486.0, 72.0, 542.0),
                false,
            );
        });
    }

    // 'miniStartDocked', without the BottomNavigationBar steps.
    #[test]
    fn mini_start_docked_accounts_for_safe_interactive_areas() {
        test_widgets(|tester| {
            run_docked_tests(
                tester,
                <dyn FloatingActionButtonLocation>::mini_start_docked(),
                Rect::from_ltrb(12.0, 502.0, 60.0, 550.0),
                Rect::from_ltrb(12.0, 494.0, 60.0, 542.0),
                true,
            );
        });
    }

    // 'centerDocked', without the BottomNavigationBar steps.
    #[test]
    fn center_docked_accounts_for_safe_interactive_areas() {
        test_widgets(|tester| {
            run_docked_tests(
                tester,
                <dyn FloatingActionButtonLocation>::center_docked(),
                Rect::from_ltrb(372.0, 494.0, 428.0, 550.0),
                Rect::from_ltrb(372.0, 486.0, 428.0, 542.0),
                false,
            );
        });
    }

    // 'miniCenterDocked', without the BottomNavigationBar steps.
    #[test]
    fn mini_center_docked_accounts_for_safe_interactive_areas() {
        test_widgets(|tester| {
            run_docked_tests(
                tester,
                <dyn FloatingActionButtonLocation>::mini_center_docked(),
                Rect::from_ltrb(376.0, 502.0, 424.0, 550.0),
                Rect::from_ltrb(376.0, 494.0, 424.0, 542.0),
                true,
            );
        });
    }

    // 'endDocked', without the BottomNavigationBar steps.
    #[test]
    fn end_docked_accounts_for_safe_interactive_areas() {
        test_widgets(|tester| {
            run_docked_tests(
                tester,
                <dyn FloatingActionButtonLocation>::end_docked(),
                Rect::from_ltrb(728.0, 494.0, 784.0, 550.0),
                Rect::from_ltrb(728.0, 486.0, 784.0, 542.0),
                false,
            );
        });
    }

    // 'miniEndDocked', without the BottomNavigationBar steps.
    #[test]
    fn mini_end_docked_accounts_for_safe_interactive_areas() {
        test_widgets(|tester| {
            run_docked_tests(
                tester,
                <dyn FloatingActionButtonLocation>::mini_end_docked(),
                Rect::from_ltrb(740.0, 502.0, 788.0, 550.0),
                Rect::from_ltrb(740.0, 494.0, 788.0, 542.0),
                true,
            );
        });
    }

    /// Dart's `runTopTests`.
    fn run_top_tests(
        tester: &mut WidgetTester,
        location: FloatingActionButtonLocationRef,
        default_rect: Rect,
        app_bar_rect: Rect,
        mini: bool,
    ) {
        let case = SafeAreaCase::new(location, mini);
        // Default
        case.pump(
            tester,
            MediaQueryData::new().view_padding(EdgeInsets::only(0.0, VIEW_PADDING, 0.0, 0.0)),
            false,
            true,
        );
        assert_rect_more_or_less_equals(case.rect(tester), default_rect);

        // AppBar default
        case.pump_with(
            tester,
            MediaQueryData::new().view_padding(EdgeInsets::only(0.0, VIEW_PADDING, 0.0, 0.0)),
            true,
            false,
            true,
        );
        assert_rect_more_or_less_equals(case.rect(tester), app_bar_rect);
    }

    // 'startTop'.
    #[test]
    fn start_top_accounts_for_safe_interactive_areas() {
        test_widgets(|tester| {
            run_top_tests(
                tester,
                <dyn FloatingActionButtonLocation>::start_top(),
                Rect::from_ltrb(16.0, 50.0, 72.0, 106.0),
                // Positioned relative to AppBar
                Rect::from_ltrb(16.0, 28.0, 72.0, 84.0),
                false,
            );
        });
    }

    // 'miniStartTop'.
    #[test]
    fn mini_start_top_accounts_for_safe_interactive_areas() {
        test_widgets(|tester| {
            run_top_tests(
                tester,
                <dyn FloatingActionButtonLocation>::mini_start_top(),
                Rect::from_ltrb(12.0, 50.0, 60.0, 98.0),
                // Positioned relative to AppBar
                Rect::from_ltrb(12.0, 32.0, 60.0, 80.0),
                true,
            );
        });
    }

    // 'centerTop'.
    #[test]
    fn center_top_accounts_for_safe_interactive_areas() {
        test_widgets(|tester| {
            run_top_tests(
                tester,
                <dyn FloatingActionButtonLocation>::center_top(),
                Rect::from_ltrb(372.0, 50.0, 428.0, 106.0),
                // Positioned relative to AppBar
                Rect::from_ltrb(372.0, 28.0, 428.0, 84.0),
                false,
            );
        });
    }

    // 'miniCenterTop'.
    #[test]
    fn mini_center_top_accounts_for_safe_interactive_areas() {
        test_widgets(|tester| {
            run_top_tests(
                tester,
                <dyn FloatingActionButtonLocation>::mini_center_top(),
                Rect::from_ltrb(376.0, 50.0, 424.0, 98.0),
                // Positioned relative to AppBar
                Rect::from_ltrb(376.0, 32.0, 424.0, 80.0),
                true,
            );
        });
    }

    // 'endTop'.
    #[test]
    fn end_top_accounts_for_safe_interactive_areas() {
        test_widgets(|tester| {
            run_top_tests(
                tester,
                <dyn FloatingActionButtonLocation>::end_top(),
                Rect::from_ltrb(728.0, 50.0, 784.0, 106.0),
                // Positioned relative to AppBar
                Rect::from_ltrb(728.0, 28.0, 784.0, 84.0),
                false,
            );
        });
    }

    // 'miniEndTop'.
    #[test]
    fn mini_end_top_accounts_for_safe_interactive_areas() {
        test_widgets(|tester| {
            run_top_tests(
                tester,
                <dyn FloatingActionButtonLocation>::mini_end_top(),
                Rect::from_ltrb(740.0, 50.0, 788.0, 98.0),
                // Positioned relative to AppBar
                Rect::from_ltrb(740.0, 32.0, 788.0, 80.0),
                true,
            );
        });
    }

    /// `MaterialApp(home: Directionality(textDirection, Scaffold(appBar: AppBar(),
    /// floatingActionButton: const FloatingActionButton(onPressed: null),
    /// floatingActionButtonLocation: location)))`; the LTR cases have no `Directionality` of
    /// their own, and the app's is left to right.
    fn top_location_rect(
        tester: &mut WidgetTester,
        location: FloatingActionButtonLocationRef,
        text_direction: TextDirection,
    ) -> Rect {
        let theme = ThemeData::new(&tester.cell.borrow());
        let key = GlobalKey::new();
        tester.pump_widget(material_app(
            theme,
            Directionality::new(
                text_direction,
                Scaffold::new()
                    .app_bar(PreferredSizeWidgetRef::new(AppBar::new()))
                    .floating_action_button(FloatingActionButton::new(None).key(key.clone().into()))
                    .floating_action_button_location(location),
            )
            .into_widget(),
        ));
        tester.get_rect(&key)
    }

    // 'Start-top floating action button location LTR'.
    #[test]
    fn the_start_top_location_left_to_right() {
        test_widgets(|tester| {
            assert_rect_more_or_less_equals(
                top_location_rect(
                    tester,
                    <dyn FloatingActionButtonLocation>::start_top(),
                    TextDirection::Ltr,
                ),
                Rect::from_ltwh(16.0, 28.0, 56.0, 56.0),
            );
        });
    }

    // 'End-top floating action button location RTL'.
    #[test]
    fn the_end_top_location_right_to_left() {
        test_widgets(|tester| {
            assert_rect_more_or_less_equals(
                top_location_rect(
                    tester,
                    <dyn FloatingActionButtonLocation>::end_top(),
                    TextDirection::Rtl,
                ),
                Rect::from_ltwh(16.0, 28.0, 56.0, 56.0),
            );
        });
    }

    // 'Start-top floating action button location RTL'.
    #[test]
    fn the_start_top_location_right_to_left() {
        test_widgets(|tester| {
            assert_rect_more_or_less_equals(
                top_location_rect(
                    tester,
                    <dyn FloatingActionButtonLocation>::start_top(),
                    TextDirection::Rtl,
                ),
                Rect::from_ltwh(800.0 - 56.0 - 16.0, 28.0, 56.0, 56.0),
            );
        });
    }

    // 'End-top floating action button location LTR'.
    #[test]
    fn the_end_top_location_left_to_right() {
        test_widgets(|tester| {
            assert_rect_more_or_less_equals(
                top_location_rect(
                    tester,
                    <dyn FloatingActionButtonLocation>::end_top(),
                    TextDirection::Ltr,
                ),
                Rect::from_ltwh(800.0 - 56.0 - 16.0, 28.0, 56.0, 56.0),
            );
        });
    }

    // Waiting:
    //  * CircleAvatar: 'Mini-start-top floating action button location'.
    //  * BottomNavigationBar: every case of 'New Floating Action Button Locations' and 'Custom
    //    Floating Action Button Locations', 'Moves between new locations and new locations',
    //    'Moves between new locations and old locations', 'Moves between new locations and old
    //    locations with custom animator', and the BottomNavigationBar steps of the float and
    //    docked cases of 'Locations account for safe interactive areas'.
}