File size: 59,253 Bytes
f32f436 |
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 |
<topic_start> Introduction to Flutter: Building Beautiful Cross-Platform Apps Flutter is an open-source UI software development toolkit created by Google. It is designed to build natively compiled applications for mobile, web, and desktop from a single codebase. Flutter provides a fast, expressive, and flexible way to develop visually stunning applications with high performance. It is a powerful framework for building cross-platform applications with beautiful, highly-customizable UIs. By leveraging Flutter's rich set of widgets, hot reload feature, and platform-specific integrations, developers can create high-quality apps that run smoothly on multiple platforms with minimal effort. <topic_end> <topic_start> Why Choose Flutter? One of the primary advantages of Flutter is its ability to create beautiful, highly-customized user interfaces (UI) quickly and efficiently. Here's why Flutter stands out: 1- Single Codebase: With Flutter, you write one codebase that runs on multiple platforms, including iOS, Android, web, and desktop. This means you can reach a broader audience with less effort. 2- Expressive UI: Flutter uses a declarative approach to building UI, meaning you describe what you want your UI to look like, and Flutter handles the rendering. This allows for highly expressive and customizable UI designs. 3- Fast Development: Flutter's hot reload feature allows developers to instantly see changes they make to the code reflected on the app UI, speeding up the development process significantly. 4- Native Performance: Flutter apps are compiled directly to native machine code, providing near-native performance on each platform. 5- Rich Set of Widgets: Flutter comes with an extensive collection of pre-designed widgets for building UI elements such as buttons, text inputs, sliders, and more. These widgets are highly customizable and can be combined to create complex UI layouts. <topic_end> <topic_start> Widgets: The Building Blocks of Flutter In Flutter, everything is a widget. A widget is a lightweight, reusable component that represents a part of the UI. Flutter provides two types of widgets: 1- Stateless Widgets: These widgets are immutable and do not have any internal state. Stateless widgets are used for UI elements that do not change over time, such as buttons, icons, or static text. 2- Stateful Widgets: Stateful widgets maintain internal state that can change over time. They are used for UI elements that need to update dynamically, such as input fields, animations, or lists. <topic_end> <topic_start> How Flutter Works At the heart of Flutter's architecture is the Flutter Engine, which is written primarily in C++. The Flutter Engine provides low-level rendering support through Google's Skia graphics library. Here's a simplified overview of how Flutter works: 1- UI Rendering: Flutter uses a custom rendering engine to draw UI elements directly to the screen, bypassing the platform's native UI components. This allows for consistent UI across different platforms. 2- Dart Programming Language: Flutter apps are written in the Dart programming language, which was also developed by Google. Dart provides features such as a just-in-time (JIT) compiler for rapid development and a ahead-of-time (AOT) compiler for producing highly optimized native code. 3- Widgets Layer: Developers use Flutter's extensive widget library to compose the UI of their applications. Widgets are arranged in a tree hierarchy, with each widget responsible for rendering a specific part of the UI. 4- Hot Reload: Flutter's hot reload feature allows developers to make changes to the code and see the results instantly without restarting the app. This greatly speeds up the development process and enables iterative UI design 5- Platform Channels: Flutter provides platform channels that allow communication between Flutter code and platform-specific code written in Java (for Android) or Objective-C/Swift (for iOS). This enables developers to access platform-specific features and APIs when needed. <topic_end> <topic_start> Understanding constraints in Flutter info Note If you are experiencing specific layout errors, you might check out Common Flutter errors. When someone learning Flutter asks you why some widget with width: 100 isn’t 100 pixels wide, the default answer is to tell them to put that widget inside of a Center, right? Don’t do that. If you do, they’ll come back again and again, asking why some FittedBox isn’t working, why that Column is overflowing, or what IntrinsicWidth is supposed to be doing. Instead, first tell them that Flutter layout is very different from HTML layout (which is probably where they’re coming from), and then make them memorize the following rule: Flutter layout can’t really be understood without knowing this rule, so Flutter developers should learn it early on. In more detail: For example, if a composed widget contains a column with some padding, and wants to lay out its two children as follows: The negotiation goes something like this: Widget: “Hey parent, what are my constraints?” Parent: “You must be from 0 to 300 pixels wide, and 0 to 85 tall.” Widget: “Hmmm, since I want to have 5 pixels of padding, then my children can have at most 290 pixels of width and 75 pixels of height.” Widget: “Hey first child, You must be from 0 to 290 pixels wide, and 0 to 75 tall.” First child: “OK, then I wish to be 290 pixels wide, and 20 pixels tall.” Widget: “Hmmm, since I want to put my second child below the first one, this leaves only 55 pixels of height for my second child.” Widget: “Hey second child, You must be from 0 to 290 wide, and 0 to 55 tall.” Second child: “OK, I wish to be 140 pixels wide, and 30 pixels tall.” Widget: “Very well. My first child has position x: 5 and y: 5, and my second child has x: 80 and y: 25.” Widget: “Hey parent, I’ve decided that my size is going to be 300 pixels wide, and 60 pixels tall.” <topic_end> <topic_start> Limitations Flutter’s layout engine is designed to be a one-pass process. This means that Flutter lays out its widgets very efficiently, but does result in a few limitations: A widget can decide its own size only within the constraints given to it by its parent. This means a widget usually can’t have any size it wants. A widget can’t know and doesn’t decide its own position in the screen, since it’s the widget’s parent who decides the position of the widget. Since the parent’s size and position, in its turn, also depends on its own parent, it’s impossible to precisely define the size and position of any widget without taking into consideration the tree as a whole. If a child wants a different size from its parent and the parent doesn’t have enough information to align it, then the child’s size might be ignored. Be specific when defining alignment. In Flutter, widgets are rendered by their underlying RenderBox objects. Many boxes in Flutter, especially those that just take a single child, pass their constraint on to their children. Generally, there are three kinds of boxes, in terms of how they handle their constraints: Some widgets, for example Container, vary from type to type based on their constructor arguments. The Container constructor defaults to trying to be as big as possible, but if you give it a width, for instance, it tries to honor that and be that particular size. Others, for example Row and Column (flex boxes) vary based on the constraints they are given, as described in the Flex section. <topic_end> <topic_start> Examples For an interactive experience, use the following DartPad. Use the numbered horizontal scrolling bar to switch between 29 different examples. <code_start> import 'package:flutter/material.dart'; void main() => runApp(const HomePage()); const red = Colors.red; const green = Colors.green; const blue = Colors.blue; const big = TextStyle(fontSize: 30); class HomePage extends StatelessWidget { const HomePage({super.key}); @override Widget build(BuildContext context) { return const FlutterLayoutArticle([ Example1(), Example2(), Example3(), Example4(), Example5(), Example6(), Example7(), Example8(), Example9(), Example10(), Example11(), Example12(), Example13(), Example14(), Example15(), Example16(), Example17(), Example18(), Example19(), Example20(), Example21(), Example22(), Example23(), Example24(), Example25(), Example26(), Example27(), Example28(), Example29(), ]); } } abstract class Example extends StatelessWidget { const Example({super.key}); String get code; String get explanation; } class FlutterLayoutArticle extends StatefulWidget { const FlutterLayoutArticle( this.examples, { super.key, }); final List<Example> examples; @override State<FlutterLayoutArticle> createState() => _FlutterLayoutArticleState(); } class _FlutterLayoutArticleState extends State<FlutterLayoutArticle> { late int count; late Widget example; late String code; late String explanation; @override void initState() { count = 1; code = const Example1().code; explanation = const Example1().explanation; super.initState(); } @override void didUpdateWidget(FlutterLayoutArticle oldWidget) { super.didUpdateWidget(oldWidget); var example = widget.examples[count - 1]; code = example.code; explanation = example.explanation; } @override Widget build(BuildContext context) { return MaterialApp( debugShowCheckedModeBanner: false, title: 'Flutter Layout Article', home: SafeArea( child: Material( color: Colors.black, child: FittedBox( child: Container( width: 400, height: 670, color: const Color(0xFFCCCCCC), child: Column( crossAxisAlignment: CrossAxisAlignment.center, children: [ Expanded( child: ConstrainedBox( constraints: const BoxConstraints.tightFor( width: double.infinity, height: double.infinity), child: widget.examples[count - 1])), Container( height: 50, width: double.infinity, color: Colors.black, child: SingleChildScrollView( scrollDirection: Axis.horizontal, child: Row( mainAxisSize: MainAxisSize.min, children: [ for (int i = 0; i < widget.examples.length; i++) Container( width: 58, padding: const EdgeInsets.only(left: 4, right: 4), child: button(i + 1), ), ], ), ), ), Container( height: 273, color: Colors.grey[50], child: Scrollbar( child: SingleChildScrollView( key: ValueKey(count), child: Padding( padding: const EdgeInsets.all(10), child: Column( children: [ Center(child: Text(code)), const SizedBox(height: 15), Text( explanation, style: TextStyle( color: Colors.blue[900], fontStyle: FontStyle.italic), ), ], ), ), ), ), ), ], ), ), ), ), ), ); } Widget button(int exampleNumber) { return Button( key: ValueKey('button$exampleNumber'), isSelected: count == exampleNumber, exampleNumber: exampleNumber, onPressed: () { showExample( exampleNumber, widget.examples[exampleNumber - 1].code, widget.examples[exampleNumber - 1].explanation, ); }, ); } void showExample(int exampleNumber, String code, String explanation) { setState(() { count = exampleNumber; this.code = code; this.explanation = explanation; }); } } class Button extends StatelessWidget { final bool isSelected; final int exampleNumber; final VoidCallback onPressed; const Button({ super.key, required this.isSelected, required this.exampleNumber, required this.onPressed, }); @override Widget build(BuildContext context) { return TextButton( style: TextButton.styleFrom( foregroundColor: Colors.white, backgroundColor: isSelected ? Colors.grey : Colors.grey[800], ), child: Text(exampleNumber.toString()), onPressed: () { Scrollable.ensureVisible( context, duration: const Duration(milliseconds: 350), curve: Curves.easeOut, alignment: 0.5, ); onPressed(); }, ); } } class Example1 extends Example { const Example1({super.key}); @override final code = 'Container(color: red)'; @override final explanation = 'The screen is the parent of the Container, ' 'and it forces the Container to be exactly the same size as the screen.' '\n\n' 'So the Container fills the screen and paints it red.'; @override Widget build(BuildContext context) { return Container(color: red); } } class Example2 extends Example { const Example2({super.key}); @override final code = 'Container(width: 100, height: 100, color: red)'; @override final String explanation = 'The red Container wants to be 100x100, but it can\'t, ' 'because the screen forces it to be exactly the same size as the screen.' '\n\n' 'So the Container fills the screen.'; @override Widget build(BuildContext context) { return Container(width: 100, height: 100, color: red); } } class Example3 extends Example { const Example3({super.key}); @override final code = 'Center(\n' ' child: Container(width: 100, height: 100, color: red))'; @override final String explanation = 'The screen forces the Center to be exactly the same size as the screen, ' 'so the Center fills the screen.' '\n\n' 'The Center tells the Container that it can be any size it wants, but not bigger than the screen.' 'Now the Container can indeed be 100x100.'; @override Widget build(BuildContext context) { return Center( child: Container(width: 100, height: 100, color: red), ); } } class Example4 extends Example { const Example4({super.key}); @override final code = 'Align(\n' ' alignment: Alignment.bottomRight,\n' ' child: Container(width: 100, height: 100, color: red))'; @override final String explanation = 'This is different from the previous example in that it uses Align instead of Center.' '\n\n' 'Align also tells the Container that it can be any size it wants, but if there is empty space it won\'t center the Container. ' 'Instead, it aligns the Container to the bottom-right of the available space.'; @override Widget build(BuildContext context) { return Align( alignment: Alignment.bottomRight, child: Container(width: 100, height: 100, color: red), ); } } class Example5 extends Example { const Example5({super.key}); @override final code = 'Center(\n' ' child: Container(\n' ' color: red,\n' ' width: double.infinity,\n' ' height: double.infinity))'; @override final String explanation = 'The screen forces the Center to be exactly the same size as the screen, ' 'so the Center fills the screen.' '\n\n' 'The Center tells the Container that it can be any size it wants, but not bigger than the screen.' 'The Container wants to be of infinite size, but since it can\'t be bigger than the screen, it just fills the screen.'; @override Widget build(BuildContext context) { return Center( child: Container( width: double.infinity, height: double.infinity, color: red), ); } } class Example6 extends Example { const Example6({super.key}); @override final code = 'Center(child: Container(color: red))'; @override final String explanation = 'The screen forces the Center to be exactly the same size as the screen, ' 'so the Center fills the screen.' '\n\n' 'The Center tells the Container that it can be any size it wants, but not bigger than the screen.' '\n\n' 'Since the Container has no child and no fixed size, it decides it wants to be as big as possible, so it fills the whole screen.' '\n\n' 'But why does the Container decide that? ' 'Simply because that\'s a design decision by those who created the Container widget. ' 'It could have been created differently, and you have to read the Container documentation to understand how it behaves, depending on the circumstances. '; @override Widget build(BuildContext context) { return Center( child: Container(color: red), ); } } class Example7 extends Example { const Example7({super.key}); @override final code = 'Center(\n' ' child: Container(color: red\n' ' child: Container(color: green, width: 30, height: 30)))'; @override final String explanation = 'The screen forces the Center to be exactly the same size as the screen, ' 'so the Center fills the screen.' '\n\n' 'The Center tells the red Container that it can be any size it wants, but not bigger than the screen.' 'Since the red Container has no size but has a child, it decides it wants to be the same size as its child.' '\n\n' 'The red Container tells its child that it can be any size it wants, but not bigger than the screen.' '\n\n' 'The child is a green Container that wants to be 30x30.' '\n\n' 'Since the red `Container` has no size but has a child, it decides it wants to be the same size as its child. ' 'The red color isn\'t visible, since the green Container entirely covers all of the red Container.'; @override Widget build(BuildContext context) { return Center( child: Container( color: red, child: Container(color: green, width: 30, height: 30), ), ); } } class Example8 extends Example { const Example8({super.key}); @override final code = 'Center(\n' ' child: Container(color: red\n' ' padding: const EdgeInsets.all(20),\n' ' child: Container(color: green, width: 30, height: 30)))'; @override final String explanation = 'The red Container sizes itself to its children size, but it takes its own padding into consideration. ' 'So it is also 30x30 plus padding. ' 'The red color is visible because of the padding, and the green Container has the same size as in the previous example.'; @override Widget build(BuildContext context) { return Center( child: Container( padding: const EdgeInsets.all(20), color: red, child: Container(color: green, width: 30, height: 30), ), ); } } class Example9 extends Example { const Example9({super.key}); @override final code = 'ConstrainedBox(\n' ' constraints: BoxConstraints(\n' ' minWidth: 70, minHeight: 70,\n' ' maxWidth: 150, maxHeight: 150),\n' ' child: Container(color: red, width: 10, height: 10)))'; @override final String explanation = 'You might guess that the Container has to be between 70 and 150 pixels, but you would be wrong. ' 'The ConstrainedBox only imposes ADDITIONAL constraints from those it receives from its parent.' '\n\n' 'Here, the screen forces the ConstrainedBox to be exactly the same size as the screen, ' 'so it tells its child Container to also assume the size of the screen, ' 'thus ignoring its \'constraints\' parameter.'; @override Widget build(BuildContext context) { return ConstrainedBox( constraints: const BoxConstraints( minWidth: 70, minHeight: 70, maxWidth: 150, maxHeight: 150, ), child: Container(color: red, width: 10, height: 10), ); } } class Example10 extends Example { const Example10({super.key}); @override final code = 'Center(\n' ' child: ConstrainedBox(\n' ' constraints: BoxConstraints(\n' ' minWidth: 70, minHeight: 70,\n' ' maxWidth: 150, maxHeight: 150),\n' ' child: Container(color: red, width: 10, height: 10))))'; @override final String explanation = 'Now, Center allows ConstrainedBox to be any size up to the screen size.' '\n\n' 'The ConstrainedBox imposes ADDITIONAL constraints from its \'constraints\' parameter onto its child.' '\n\n' 'The Container must be between 70 and 150 pixels. It wants to have 10 pixels, so it will end up having 70 (the MINIMUM).'; @override Widget build(BuildContext context) { return Center( child: ConstrainedBox( constraints: const BoxConstraints( minWidth: 70, minHeight: 70, maxWidth: 150, maxHeight: 150, ), child: Container(color: red, width: 10, height: 10), ), ); } } class Example11 extends Example { const Example11({super.key}); @override final code = 'Center(\n' ' child: ConstrainedBox(\n' ' constraints: BoxConstraints(\n' ' minWidth: 70, minHeight: 70,\n' ' maxWidth: 150, maxHeight: 150),\n' ' child: Container(color: red, width: 1000, height: 1000))))'; @override final String explanation = 'Center allows ConstrainedBox to be any size up to the screen size.' 'The ConstrainedBox imposes ADDITIONAL constraints from its \'constraints\' parameter onto its child' '\n\n' 'The Container must be between 70 and 150 pixels. It wants to have 1000 pixels, so it ends up having 150 (the MAXIMUM).'; @override Widget build(BuildContext context) { return Center( child: ConstrainedBox( constraints: const BoxConstraints( minWidth: 70, minHeight: 70, maxWidth: 150, maxHeight: 150, ), child: Container(color: red, width: 1000, height: 1000), ), ); } } class Example12 extends Example { const Example12({super.key}); @override final code = 'Center(\n' ' child: ConstrainedBox(\n' ' constraints: BoxConstraints(\n' ' minWidth: 70, minHeight: 70,\n' ' maxWidth: 150, maxHeight: 150),\n' ' child: Container(color: red, width: 100, height: 100))))'; @override final String explanation = 'Center allows ConstrainedBox to be any size up to the screen size.' 'ConstrainedBox imposes ADDITIONAL constraints from its \'constraints\' parameter onto its child.' '\n\n' 'The Container must be between 70 and 150 pixels. It wants to have 100 pixels, and that\'s the size it has, since that\'s between 70 and 150.'; @override Widget build(BuildContext context) { return Center( child: ConstrainedBox( constraints: const BoxConstraints( minWidth: 70, minHeight: 70, maxWidth: 150, maxHeight: 150, ), child: Container(color: red, width: 100, height: 100), ), ); } } class Example13 extends Example { const Example13({super.key}); @override final code = 'UnconstrainedBox(\n' ' child: Container(color: red, width: 20, height: 50));'; @override final String explanation = 'The screen forces the UnconstrainedBox to be exactly the same size as the screen.' 'However, the UnconstrainedBox lets its child Container be any size it wants.'; @override Widget build(BuildContext context) { return UnconstrainedBox( child: Container(color: red, width: 20, height: 50), ); } } class Example14 extends Example { const Example14({super.key}); @override final code = 'UnconstrainedBox(\n' ' child: Container(color: red, width: 4000, height: 50));'; @override final String explanation = 'The screen forces the UnconstrainedBox to be exactly the same size as the screen, ' 'and UnconstrainedBox lets its child Container be any size it wants.' '\n\n' 'Unfortunately, in this case the Container has 4000 pixels of width and is too big to fit in the UnconstrainedBox, ' 'so the UnconstrainedBox displays the much dreaded "overflow warning".'; @override Widget build(BuildContext context) { return UnconstrainedBox( child: Container(color: red, width: 4000, height: 50), ); } } class Example15 extends Example { const Example15({super.key}); @override final code = 'OverflowBox(\n' ' minWidth: 0,' ' minHeight: 0,' ' maxWidth: double.infinity,' ' maxHeight: double.infinity,' ' child: Container(color: red, width: 4000, height: 50));'; @override final String explanation = 'The screen forces the OverflowBox to be exactly the same size as the screen, ' 'and OverflowBox lets its child Container be any size it wants.' '\n\n' 'OverflowBox is similar to UnconstrainedBox, and the difference is that it won\'t display any warnings if the child doesn\'t fit the space.' '\n\n' 'In this case the Container is 4000 pixels wide, and is too big to fit in the OverflowBox, ' 'but the OverflowBox simply shows as much as it can, with no warnings given.'; @override Widget build(BuildContext context) { return OverflowBox( minWidth: 0, minHeight: 0, maxWidth: double.infinity, maxHeight: double.infinity, child: Container(color: red, width: 4000, height: 50), ); } } class Example16 extends Example { const Example16({super.key}); @override final code = 'UnconstrainedBox(\n' ' child: Container(color: Colors.red, width: double.infinity, height: 100));'; @override final String explanation = 'This won\'t render anything, and you\'ll see an error in the console.' '\n\n' 'The UnconstrainedBox lets its child be any size it wants, ' 'however its child is a Container with infinite size.' '\n\n' 'Flutter can\'t render infinite sizes, so it throws an error with the following message: ' '"BoxConstraints forces an infinite width."'; @override Widget build(BuildContext context) { return UnconstrainedBox( child: Container(color: Colors.red, width: double.infinity, height: 100), ); } } class Example17 extends Example { const Example17({super.key}); @override final code = 'UnconstrainedBox(\n' ' child: LimitedBox(maxWidth: 100,\n' ' child: Container(color: Colors.red,\n' ' width: double.infinity, height: 100));'; @override final String explanation = 'Here you won\'t get an error anymore, ' 'because when the LimitedBox is given an infinite size by the UnconstrainedBox, ' 'it passes a maximum width of 100 down to its child.' '\n\n' 'If you swap the UnconstrainedBox for a Center widget, ' 'the LimitedBox won\'t apply its limit anymore (since its limit is only applied when it gets infinite constraints), ' 'and the width of the Container is allowed to grow past 100.' '\n\n' 'This explains the difference between a LimitedBox and a ConstrainedBox.'; @override Widget build(BuildContext context) { return UnconstrainedBox( child: LimitedBox( maxWidth: 100, child: Container( color: Colors.red, width: double.infinity, height: 100, ), ), ); } } class Example18 extends Example { const Example18({super.key}); @override final code = 'FittedBox(\n' ' child: Text(\'Some Example Text.\'));'; @override final String explanation = 'The screen forces the FittedBox to be exactly the same size as the screen.' 'The Text has some natural width (also called its intrinsic width) that depends on the amount of text, its font size, and so on.' '\n\n' 'The FittedBox lets the Text be any size it wants, ' 'but after the Text tells its size to the FittedBox, ' 'the FittedBox scales the Text until it fills all of the available width.'; @override Widget build(BuildContext context) { return const FittedBox( child: Text('Some Example Text.'), ); } } class Example19 extends Example { const Example19({super.key}); @override final code = 'Center(\n' ' child: FittedBox(\n' ' child: Text(\'Some Example Text.\')));'; @override final String explanation = 'But what happens if you put the FittedBox inside of a Center widget? ' 'The Center lets the FittedBox be any size it wants, up to the screen size.' '\n\n' 'The FittedBox then sizes itself to the Text, and lets the Text be any size it wants.' '\n\n' 'Since both FittedBox and the Text have the same size, no scaling happens.'; @override Widget build(BuildContext context) { return const Center( child: FittedBox( child: Text('Some Example Text.'), ), ); } } class Example20 extends Example { const Example20({super.key}); @override final code = 'Center(\n' ' child: FittedBox(\n' ' child: Text(\'…\')));'; @override final String explanation = 'However, what happens if FittedBox is inside of a Center widget, but the Text is too large to fit the screen?' '\n\n' 'FittedBox tries to size itself to the Text, but it can\'t be bigger than the screen. ' 'It then assumes the screen size, and resizes Text so that it fits the screen, too.'; @override Widget build(BuildContext context) { return const Center( child: FittedBox( child: Text( 'This is some very very very large text that is too big to fit a regular screen in a single line.'), ), ); } } class Example21 extends Example { const Example21({super.key}); @override final code = 'Center(\n' ' child: Text(\'…\'));'; @override final String explanation = 'If, however, you remove the FittedBox, ' 'the Text gets its maximum width from the screen, ' 'and breaks the line so that it fits the screen.'; @override Widget build(BuildContext context) { return const Center( child: Text( 'This is some very very very large text that is too big to fit a regular screen in a single line.'), ); } } class Example22 extends Example { const Example22({super.key}); @override final code = 'FittedBox(\n' ' child: Container(\n' ' height: 20, width: double.infinity));'; @override final String explanation = 'FittedBox can only scale a widget that is BOUNDED (has non-infinite width and height).' 'Otherwise, it won\'t render anything, and you\'ll see an error in the console.'; @override Widget build(BuildContext context) { return FittedBox( child: Container( height: 20, width: double.infinity, color: Colors.red, ), ); } } class Example23 extends Example { const Example23({super.key}); @override final code = 'Row(children:[\n' ' Container(color: red, child: Text(\'Hello!\'))\n' ' Container(color: green, child: Text(\'Goodbye!\'))]'; @override final String explanation = 'The screen forces the Row to be exactly the same size as the screen.' '\n\n' 'Just like an UnconstrainedBox, the Row won\'t impose any constraints onto its children, ' 'and instead lets them be any size they want.' '\n\n' 'The Row then puts them side-by-side, and any extra space remains empty.'; @override Widget build(BuildContext context) { return Row( children: [ Container(color: red, child: const Text('Hello!', style: big)), Container(color: green, child: const Text('Goodbye!', style: big)), ], ); } } class Example24 extends Example { const Example24({super.key}); @override final code = 'Row(children:[\n' ' Container(color: red, child: Text(\'…\'))\n' ' Container(color: green, child: Text(\'Goodbye!\'))]'; @override final String explanation = 'Since the Row won\'t impose any constraints onto its children, ' 'it\'s quite possible that the children might be too big to fit the available width of the Row.' 'In this case, just like an UnconstrainedBox, the Row displays the "overflow warning".'; @override Widget build(BuildContext context) { return Row( children: [ Container( color: red, child: const Text( 'This is a very long text that ' 'won\'t fit the line.', style: big, ), ), Container(color: green, child: const Text('Goodbye!', style: big)), ], ); } } class Example25 extends Example { const Example25({super.key}); @override final code = 'Row(children:[\n' ' Expanded(\n' ' child: Container(color: red, child: Text(\'…\')))\n' ' Container(color: green, child: Text(\'Goodbye!\'))]'; @override final String explanation = 'When a Row\'s child is wrapped in an Expanded widget, the Row won\'t let this child define its own width anymore.' '\n\n' 'Instead, it defines the Expanded width according to the other children, and only then the Expanded widget forces the original child to have the Expanded\'s width.' '\n\n' 'In other words, once you use Expanded, the original child\'s width becomes irrelevant, and is ignored.'; @override Widget build(BuildContext context) { return Row( children: [ Expanded( child: Center( child: Container( color: red, child: const Text( 'This is a very long text that won\'t fit the line.', style: big, ), ), ), ), Container(color: green, child: const Text('Goodbye!', style: big)), ], ); } } class Example26 extends Example { const Example26({super.key}); @override final code = 'Row(children:[\n' ' Expanded(\n' ' child: Container(color: red, child: Text(\'…\')))\n' ' Expanded(\n' ' child: Container(color: green, child: Text(\'Goodbye!\'))]'; @override final String explanation = 'If all of Row\'s children are wrapped in Expanded widgets, each Expanded has a size proportional to its flex parameter, ' 'and only then each Expanded widget forces its child to have the Expanded\'s width.' '\n\n' 'In other words, Expanded ignores the preferred width of its children.'; @override Widget build(BuildContext context) { return Row( children: [ Expanded( child: Container( color: red, child: const Text( 'This is a very long text that won\'t fit the line.', style: big, ), ), ), Expanded( child: Container( color: green, child: const Text( 'Goodbye!', style: big, ), ), ), ], ); } } class Example27 extends Example { const Example27({super.key}); @override final code = 'Row(children:[\n' ' Flexible(\n' ' child: Container(color: red, child: Text(\'…\')))\n' ' Flexible(\n' ' child: Container(color: green, child: Text(\'Goodbye!\'))]'; @override final String explanation = 'The only difference if you use Flexible instead of Expanded, ' 'is that Flexible lets its child be SMALLER than the Flexible width, ' 'while Expanded forces its child to have the same width of the Expanded.' '\n\n' 'But both Expanded and Flexible ignore their children\'s width when sizing themselves.' '\n\n' 'This means that it\'s IMPOSSIBLE to expand Row children proportionally to their sizes. ' 'The Row either uses the exact child\'s width, or ignores it completely when you use Expanded or Flexible.'; @override Widget build(BuildContext context) { return Row( children: [ Flexible( child: Container( color: red, child: const Text( 'This is a very long text that won\'t fit the line.', style: big, ), ), ), Flexible( child: Container( color: green, child: const Text( 'Goodbye!', style: big, ), ), ), ], ); } } class Example28 extends Example { const Example28({super.key}); @override final code = 'Scaffold(\n' ' body: Container(color: blue,\n' ' child: Column(\n' ' children: [\n' ' Text(\'Hello!\'),\n' ' Text(\'Goodbye!\')])))'; @override final String explanation = 'The screen forces the Scaffold to be exactly the same size as the screen, ' 'so the Scaffold fills the screen.' '\n\n' 'The Scaffold tells the Container that it can be any size it wants, but not bigger than the screen.' '\n\n' 'When a widget tells its child that it can be smaller than a certain size, ' 'we say the widget supplies "loose" constraints to its child. More on that later.'; @override Widget build(BuildContext context) { return Scaffold( body: Container( color: blue, child: const Column( children: [ Text('Hello!'), Text('Goodbye!'), ], ), ), ); } } class Example29 extends Example { const Example29({super.key}); @override final code = 'Scaffold(\n' ' body: Container(color: blue,\n' ' child: SizedBox.expand(\n' ' child: Column(\n' ' children: [\n' ' Text(\'Hello!\'),\n' ' Text(\'Goodbye!\')]))))'; @override final String explanation = 'If you want the Scaffold\'s child to be exactly the same size as the Scaffold itself, ' 'you can wrap its child with SizedBox.expand.' '\n\n' 'When a widget tells its child that it must be of a certain size, ' 'we say the widget supplies "tight" constraints to its child. More on that later.'; @override Widget build(BuildContext context) { return Scaffold( body: SizedBox.expand( child: Container( color: blue, child: const Column( children: [ Text('Hello!'), Text('Goodbye!'), ], ), ), ), ); } } <code_end> If you prefer, you can grab the code from this GitHub repo. The examples are explained in the following sections. <topic_end> <topic_start> Example 1 <code_start> Container(color: red) <code_end> The screen is the parent of the Container, and it forces the Container to be exactly the same size as the screen. So the Container fills the screen and paints it red. <topic_end> <topic_start> Example 2 <code_start> Container(width: 100, height: 100, color: red) <code_end> The red Container wants to be 100 × 100, but it can’t, because the screen forces it to be exactly the same size as the screen. So the Container fills the screen. <topic_end> <topic_start> Example 3 <code_start> Center( child: Container(width: 100, height: 100, color: red), ) <code_end> The screen forces the Center to be exactly the same size as the screen, so the Center fills the screen. The Center tells the Container that it can be any size it wants, but not bigger than the screen. Now the Container can indeed be 100 × 100. <topic_end> <topic_start> Example 4 <code_start> Align( alignment: Alignment.bottomRight, child: Container(width: 100, height: 100, color: red), ) <code_end> This is different from the previous example in that it uses Align instead of Center. Align also tells the Container that it can be any size it wants, but if there is empty space it won’t center the Container. Instead, it aligns the container to the bottom-right of the available space. <topic_end> <topic_start> Example 5 <code_start> Center( child: Container( width: double.infinity, height: double.infinity, color: red), ) <code_end> The screen forces the Center to be exactly the same size as the screen, so the Center fills the screen. The Center tells the Container that it can be any size it wants, but not bigger than the screen. The Container wants to be of infinite size, but since it can’t be bigger than the screen, it just fills the screen. <topic_end> <topic_start> Example 6 <code_start> Center( child: Container(color: red), ) <code_end> The screen forces the Center to be exactly the same size as the screen, so the Center fills the screen. The Center tells the Container that it can be any size it wants, but not bigger than the screen. Since the Container has no child and no fixed size, it decides it wants to be as big as possible, so it fills the whole screen. But why does the Container decide that? Simply because that’s a design decision by those who created the Container widget. It could have been created differently, and you have to read the Container API documentation to understand how it behaves, depending on the circumstances. <topic_end> <topic_start> Example 7 <code_start> Center( child: Container( color: red, child: Container(color: green, width: 30, height: 30), ), ) <code_end> The screen forces the Center to be exactly the same size as the screen, so the Center fills the screen. The Center tells the red Container that it can be any size it wants, but not bigger than the screen. Since the red Container has no size but has a child, it decides it wants to be the same size as its child. The red Container tells its child that it can be any size it wants, but not bigger than the screen. The child is a green Container that wants to be 30 × 30. Given that the red Container sizes itself to the size of its child, it is also 30 × 30. The red color isn’t visible because the green Container entirely covers the red Container. <topic_end> <topic_start> Example 8 <code_start> Center( child: Container( padding: const EdgeInsets.all(20), color: red, child: Container(color: green, width: 30, height: 30), ), ) <code_end> The red Container sizes itself to its children’s size, but it takes its own padding into consideration. So it is also 30 × 30 plus padding. The red color is visible because of the padding, and the green Container has the same size as in the previous example. <topic_end> <topic_start> Example 9 <code_start> ConstrainedBox( constraints: const BoxConstraints( minWidth: 70, minHeight: 70, maxWidth: 150, maxHeight: 150, ), child: Container(color: red, width: 10, height: 10), ) <code_end> You might guess that the Container has to be between 70 and 150 pixels, but you would be wrong. The ConstrainedBox only imposes additional constraints from those it receives from its parent. Here, the screen forces the ConstrainedBox to be exactly the same size as the screen, so it tells its child Container to also assume the size of the screen, thus ignoring its constraints parameter. <topic_end> <topic_start> Example 10 <code_start> Center( child: ConstrainedBox( constraints: const BoxConstraints( minWidth: 70, minHeight: 70, maxWidth: 150, maxHeight: 150, ), child: Container(color: red, width: 10, height: 10), ), ) <code_end> Now, Center allows ConstrainedBox to be any size up to the screen size. The ConstrainedBox imposes additional constraints from its constraints parameter onto its child. The Container must be between 70 and 150 pixels. It wants to have 10 pixels, so it ends up having 70 (the minimum). <topic_end> <topic_start> Example 11 <code_start> Center( child: ConstrainedBox( constraints: const BoxConstraints( minWidth: 70, minHeight: 70, maxWidth: 150, maxHeight: 150, ), child: Container(color: red, width: 1000, height: 1000), ), ) <code_end> Center allows ConstrainedBox to be any size up to the screen size. The ConstrainedBox imposes additional constraints from its constraints parameter onto its child. The Container must be between 70 and 150 pixels. It wants to have 1000 pixels, so it ends up having 150 (the maximum). <topic_end> <topic_start> Example 12 <code_start> Center( child: ConstrainedBox( constraints: const BoxConstraints( minWidth: 70, minHeight: 70, maxWidth: 150, maxHeight: 150, ), child: Container(color: red, width: 100, height: 100), ), ) <code_end> Center allows ConstrainedBox to be any size up to the screen size. The ConstrainedBox imposes additional constraints from its constraints parameter onto its child. The Container must be between 70 and 150 pixels. It wants to have 100 pixels, and that’s the size it has, since that’s between 70 and 150. <topic_end> <topic_start> Example 13 <code_start> UnconstrainedBox( child: Container(color: red, width: 20, height: 50), ) <code_end> The screen forces the UnconstrainedBox to be exactly the same size as the screen. However, the UnconstrainedBox lets its child Container be any size it wants. <topic_end> <topic_start> Example 14 <code_start> UnconstrainedBox( child: Container(color: red, width: 4000, height: 50), ) <code_end> The screen forces the UnconstrainedBox to be exactly the same size as the screen, and UnconstrainedBox lets its child Container be any size it wants. Unfortunately, in this case the Container is 4000 pixels wide and is too big to fit in the UnconstrainedBox, so the UnconstrainedBox displays the much dreaded “overflow warning”. <topic_end> <topic_start> Example 15 <code_start> OverflowBox( minWidth: 0, minHeight: 0, maxWidth: double.infinity, maxHeight: double.infinity, child: Container(color: red, width: 4000, height: 50), ) <code_end> The screen forces the OverflowBox to be exactly the same size as the screen, and OverflowBox lets its child Container be any size it wants. OverflowBox is similar to UnconstrainedBox; the difference is that it won’t display any warnings if the child doesn’t fit the space. In this case, the Container has 4000 pixels of width, and is too big to fit in the OverflowBox, but the OverflowBox simply shows as much as it can, with no warnings given. <topic_end> <topic_start> Example 16 <code_start> UnconstrainedBox( child: Container(color: Colors.red, width: double.infinity, height: 100), ) <code_end> This won’t render anything, and you’ll see an error in the console. The UnconstrainedBox lets its child be any size it wants, however its child is a Container with infinite size. Flutter can’t render infinite sizes, so it throws an error with the following message: BoxConstraints forces an infinite width. <topic_end> <topic_start> Example 17 <code_start> UnconstrainedBox( child: LimitedBox( maxWidth: 100, child: Container( color: Colors.red, width: double.infinity, height: 100, ), ), ) <code_end> Here you won’t get an error anymore, because when the LimitedBox is given an infinite size by the UnconstrainedBox; it passes a maximum width of 100 down to its child. If you swap the UnconstrainedBox for a Center widget, the LimitedBox won’t apply its limit anymore (since its limit is only applied when it gets infinite constraints), and the width of the Container is allowed to grow past 100. This explains the difference between a LimitedBox and a ConstrainedBox. <topic_end> <topic_start> Example 18 <code_start> const FittedBox( child: Text('Some Example Text.'), ) <code_end> The screen forces the FittedBox to be exactly the same size as the screen. The Text has some natural width (also called its intrinsic width) that depends on the amount of text, its font size, and so on. The FittedBox lets the Text be any size it wants, but after the Text tells its size to the FittedBox, the FittedBox scales the Text until it fills all of the available width. <topic_end> <topic_start> Example 19 <code_start> const Center( child: FittedBox( child: Text('Some Example Text.'), ), ) <code_end> But what happens if you put the FittedBox inside of a Center widget? The Center lets the FittedBox be any size it wants, up to the screen size. The FittedBox then sizes itself to the Text, and lets the Text be any size it wants. Since both FittedBox and the Text have the same size, no scaling happens. <topic_end> <topic_start> Example 20 <code_start> const Center( child: FittedBox( child: Text( 'This is some very very very large text that is too big to fit a regular screen in a single line.'), ), ) <code_end> However, what happens if FittedBox is inside of a Center widget, but the Text is too large to fit the screen? FittedBox tries to size itself to the Text, but it can’t be bigger than the screen. It then assumes the screen size, and resizes Text so that it fits the screen, too. <topic_end> <topic_start> Example 21 <code_start> const Center( child: Text( 'This is some very very very large text that is too big to fit a regular screen in a single line.'), ) <code_end> If, however, you remove the FittedBox, the Text gets its maximum width from the screen, and breaks the line so that it fits the screen. <topic_end> <topic_start> Example 22 <code_start> FittedBox( child: Container( height: 20, width: double.infinity, color: Colors.red, ), ) <code_end> FittedBox can only scale a widget that is bounded (has non-infinite width and height). Otherwise, it won’t render anything, and you’ll see an error in the console. <topic_end> <topic_start> Example 23 <code_start> Row( children: [ Container(color: red, child: const Text('Hello!', style: big)), Container(color: green, child: const Text('Goodbye!', style: big)), ], ) <code_end> The screen forces the Row to be exactly the same size as the screen. Just like an UnconstrainedBox, the Row won’t impose any constraints onto its children, and instead lets them be any size they want. The Row then puts them side-by-side, and any extra space remains empty. <topic_end> <topic_start> Example 24 <code_start> Row( children: [ Container( color: red, child: const Text( 'This is a very long text that ' 'won\'t fit the line.', style: big, ), ), Container(color: green, child: const Text('Goodbye!', style: big)), ], ) <code_end> Since Row won’t impose any constraints onto its children, it’s quite possible that the children might be too big to fit the available width of the Row. In this case, just like an UnconstrainedBox, the Row displays the “overflow warning”. <topic_end> <topic_start> Example 25 <code_start> Row( children: [ Expanded( child: Center( child: Container( color: red, child: const Text( 'This is a very long text that won\'t fit the line.', style: big, ), ), ), ), Container(color: green, child: const Text('Goodbye!', style: big)), ], ) <code_end> When a Row’s child is wrapped in an Expanded widget, the Row won’t let this child define its own width anymore. Instead, it defines the Expanded width according to the other children, and only then the Expanded widget forces the original child to have the Expanded’s width. In other words, once you use Expanded, the original child’s width becomes irrelevant, and is ignored. <topic_end> <topic_start> Example 26 <code_start> Row( children: [ Expanded( child: Container( color: red, child: const Text( 'This is a very long text that won\'t fit the line.', style: big, ), ), ), Expanded( child: Container( color: green, child: const Text( 'Goodbye!', style: big, ), ), ), ], ) <code_end> If all of Row’s children are wrapped in Expanded widgets, each Expanded has a size proportional to its flex parameter, and only then each Expanded widget forces its child to have the Expanded’s width. In other words, Expanded ignores the preferred width of its children. <topic_end> <topic_start> Example 27 <code_start> Row( children: [ Flexible( child: Container( color: red, child: const Text( 'This is a very long text that won\'t fit the line.', style: big, ), ), ), Flexible( child: Container( color: green, child: const Text( 'Goodbye!', style: big, ), ), ), ], ) <code_end> The only difference if you use Flexible instead of Expanded, is that Flexible lets its child have the same or smaller width than the Flexible itself, while Expanded forces its child to have the exact same width of the Expanded. But both Expanded and Flexible ignore their children’s width when sizing themselves. info Note This means that it’s impossible to expand Row children proportionally to their sizes. The Row either uses the exact child’s width, or ignores it completely when you use Expanded or Flexible. <topic_end> <topic_start> Example 28 <code_start> Scaffold( body: Container( color: blue, child: const Column( children: [ Text('Hello!'), Text('Goodbye!'), ], ), ), ) <code_end> The screen forces the Scaffold to be exactly the same size as the screen, so the Scaffold fills the screen. The Scaffold tells the Container that it can be any size it wants, but not bigger than the screen. info Note When a widget tells its child that it can be smaller than a certain size, we say the widget supplies loose constraints to its child. More on that later. <topic_end> <topic_start> Example 29 <code_start> Scaffold( body: SizedBox.expand( child: Container( color: blue, child: const Column( children: [ Text('Hello!'), Text('Goodbye!'), ], ), ), ), ) <code_end> If you want the Scaffold’s child to be exactly the same size as the Scaffold itself, you can wrap its child with SizedBox.expand. <topic_end> <topic_start> Tight vs loose constraints It’s very common to hear that some constraint is “tight” or “loose”, so what does that mean? <topic_end> <topic_start> Tight constraints A tight constraint offers a single possibility, an exact size. In other words, a tight constraint has its maximum width equal to its minimum width; and has its maximum height equal to its minimum height. An example of this is the App widget, which is contained by the RenderView class: the box used by the child returned by the application’s build function is given a constraint that forces it to exactly fill the application’s content area (typically, the entire screen). Another example: if you nest a bunch of boxes inside each other at the root of your application’s render tree, they’ll all exactly fit in each other, forced by the box’s tight constraints. If you go to Flutter’s box.dart file and search for the BoxConstraints constructors, you’ll find the following: If you revisit Example 2, the screen forces the red Container to be exactly the same size as the screen. The screen achieves that, of course, by passing tight constraints to the Container. <topic_end> <topic_start> Loose constraints A loose constraint is one that has a minimum of zero and a maximum non-zero. Some boxes loosen the incoming constraints, meaning the maximum is maintained but the minimum is removed, so the widget can have a minimum width and height both equal to zero. Ultimately, Center’s purpose is to transform the tight constraints it received from its parent (the screen) to loose constraints for its child (the Container). If you revisit Example 3, the Center allows the red Container to be smaller, but not bigger than the screen. <topic_end> <topic_start> Unbounded constraints info Note You might be directed here if the framework detects a problem involving box constraints. The Flex section below might also apply. In certain situations, a box’s constraint is unbounded, or infinite. This means that either the maximum width or the maximum height is set to double.infinity. A box that tries to be as big as possible won’t function usefully when given an unbounded constraint and, in debug mode, throws an exception. The most common case where a render box ends up with an unbounded constraint is within a flex box (Row or Column), and within a scrollable region (such as ListView and other ScrollView subclasses). ListView, for example, tries to expand to fit the space available in its cross-direction (perhaps it’s a vertically-scrolling block and tries to be as wide as its parent). If you nest a vertically scrolling ListView inside a horizontally scrolling ListView, the inner list tries to be as wide as possible, which is infinitely wide, since the outer one is scrollable in that direction. The next section describes the error you might encounter with unbounded constraints in a Flex widget. <topic_end> <topic_start> Flex A flex box (Row and Column) behaves differently depending on whether its constraint is bounded or unbounded in its primary direction. A flex box with a bounded constraint in its primary direction tries to be as big as possible. A flex box with an unbounded constraint in its primary direction tries to fit its children in that space. Each child’s flex value must be set to zero, meaning that you can’t use Expanded when the flex box is inside another flex box or a scrollable; otherwise it throws an exception. The cross direction (width for Column or height for Row), must never be unbounded, or it can’t reasonably align its children. <topic_end> |