[{"data":1,"prerenderedAt":5064},["ShallowReactive",2],{"navigation_docs_en":3,"-en-flutter-for-jobseekers-handling-user-input":95,"-en-flutter-for-jobseekers-handling-user-input-surround":5059},[4,16],{"title":5,"icon":6,"path":7,"stem":8,"children":9,"page":15},"Getting Started","i-lucide-rocket","\u002Fen\u002Fgetting-started","en\u002F1.getting-started",[10],{"title":11,"path":12,"stem":13,"icon":14},"Introduction","\u002Fen\u002Fgetting-started\u002Fintroduction","en\u002F1.getting-started\u002F1.introduction","i-lucide-house",false,{"title":17,"icon":18,"path":19,"stem":20,"children":21,"page":15},"Flutter for Jobseekers","i-lucide-briefcase","\u002Fen\u002Fflutter-for-jobseekers","en\u002F2.flutter-for-jobseekers",[22,27,32,37,42,47,52,57,62,67,72,77,82,86,91],{"title":23,"path":24,"stem":25,"icon":26},"Introduction to Flutter","\u002Fen\u002Fflutter-for-jobseekers\u002Fintroduction-to-flutter","en\u002F2.flutter-for-jobseekers\u002F1.introduction-to-flutter","i-lucide-history",{"title":28,"path":29,"stem":30,"icon":31},"Market Opportunities for Flutter Developers","\u002Fen\u002Fflutter-for-jobseekers\u002Fmarket-opportunities","en\u002F2.flutter-for-jobseekers\u002F2.market-opportunities","i-lucide-trending-up",{"title":33,"path":34,"stem":35,"icon":36},"Installing Flutter and Configuring Your IDE","\u002Fen\u002Fflutter-for-jobseekers\u002Finstalling-flutter","en\u002F2.flutter-for-jobseekers\u002F3.installing-flutter","i-lucide-wrench",{"title":38,"path":39,"stem":40,"icon":41},"Introduction to Widgets","\u002Fen\u002Fflutter-for-jobseekers\u002Fintroduction-to-widgets","en\u002F2.flutter-for-jobseekers\u002F4.introduction-to-widgets","i-lucide-blocks",{"title":43,"path":44,"stem":45,"icon":46},"Handling User Input","\u002Fen\u002Fflutter-for-jobseekers\u002Fhandling-user-input","en\u002F2.flutter-for-jobseekers\u002F5.handling-user-input","i-lucide-text-cursor-input",{"title":48,"path":49,"stem":50,"icon":51},"Using 3rd Party Libraries and External Assets","\u002Fen\u002Fflutter-for-jobseekers\u002Fusing-packages-and-assets","en\u002F2.flutter-for-jobseekers\u002F6.using-packages-and-assets","i-lucide-package",{"title":53,"path":54,"stem":55,"icon":56},"Working with APIs and Asynchronous Operations","\u002Fen\u002Fflutter-for-jobseekers\u002Fworking-with-apis-and-async","en\u002F2.flutter-for-jobseekers\u002F7.working-with-apis-and-async","i-lucide-cloud-download",{"title":58,"path":59,"stem":60,"icon":61},"Navigation and Routing","\u002Fen\u002Fflutter-for-jobseekers\u002Fnavigation-and-routing","en\u002F2.flutter-for-jobseekers\u002F8.navigation-and-routing","i-lucide-route",{"title":63,"path":64,"stem":65,"icon":66},"State Management and the BLoC","\u002Fen\u002Fflutter-for-jobseekers\u002Fstate-management-and-bloc","en\u002F2.flutter-for-jobseekers\u002F9.state-management-and-bloc","i-lucide-boxes",{"title":68,"path":69,"stem":70,"icon":71},"Reactivity and Platform-Specific Considerations","\u002Fen\u002Fflutter-for-jobseekers\u002Freactivity-and-platforms","en\u002F2.flutter-for-jobseekers\u002F910.reactivity-and-platforms","i-lucide-monitor-smartphone",{"title":73,"path":74,"stem":75,"icon":76},"Debugging, Troubleshooting, and Performance Considerations","\u002Fen\u002Fflutter-for-jobseekers\u002Fdebugging-troubleshooting-performance","en\u002F2.flutter-for-jobseekers\u002F911.debugging-troubleshooting-performance","i-lucide-bug",{"title":78,"path":79,"stem":80,"icon":81},"Creating Your First Application","\u002Fen\u002Fflutter-for-jobseekers\u002Fcreating-your-first-application","en\u002F2.flutter-for-jobseekers\u002F912.creating-your-first-application","i-lucide-cloud-sun",{"title":83,"path":84,"stem":85,"icon":18},"Finding Flutter Jobs","\u002Fen\u002Fflutter-for-jobseekers\u002Ffinding-flutter-jobs","en\u002F2.flutter-for-jobseekers\u002F913.finding-flutter-jobs",{"title":87,"path":88,"stem":89,"icon":90},"Preparing for and Succeeding in the Job Interview","\u002Fen\u002Fflutter-for-jobseekers\u002Fpreparing-for-the-job-interview","en\u002F2.flutter-for-jobseekers\u002F914.preparing-for-the-job-interview","i-lucide-messages-square",{"title":92,"path":93,"stem":94,"icon":61},"Your Road Ahead","\u002Fen\u002Fflutter-for-jobseekers\u002Fyour-road-ahead","en\u002F2.flutter-for-jobseekers\u002F915.your-road-ahead",{"id":96,"title":43,"body":97,"description":5052,"extension":5053,"links":5054,"meta":5055,"navigation":5056,"path":44,"seo":5057,"stem":45,"__hash__":5058},"docs_en\u002Fen\u002F2.flutter-for-jobseekers\u002F5.handling-user-input.md",{"type":98,"value":99,"toc":5007},"minimark",[100,115,119,153,158,208,211,222,237,240,245,258,336,343,349,360,378,392,399,416,429,432,452,455,459,472,496,502,507,528,556,562,567,580,603,609,614,626,649,655,660,670,693,699,704,707,711,714,733,751,795,805,808,827,833,838,841,868,886,1015,1022,1043,1049,1054,1057,1071,1074,1086,1095,1114,1121,1128,1131,1137,1146,1164,1168,1185,1267,1279,1289,1310,1316,1354,1357,1367,1373,1402,1408,1411,1414,1418,1431,1453,1478,1487,1519,1531,1534,1591,1597,1603,1608,1630,1636,1641,1645,1653,1665,1668,1751,1758,1823,1829,1835,1840,1852,1885,1891,1896,1903,1953,1968,2011,2017,2023,2028,2034,2042,2046,2061,2253,2260,2280,2286,2292,2297,2300,2303,2307,2310,2317,2473,2484,2516,2529,2535,2540,2544,2553,2666,2675,2681,2687,2692,2707,2809,2818,2824,2829,2833,2842,2853,2859,3036,3043,3049,3054,3058,3070,3195,3209,3215,3220,3240,3246,3251,3254,3261,3277,3283,3377,3383,3388,3401,3480,3493,3499,3504,3507,3517,3534,3538,3562,3589,3592,3613,3649,3664,3680,3712,3716,3722,3729,3732,3769,3800,3836,3854,3857,3860,3882,3885,3889,3897,3913,4007,4011,4030,4039,4042,4075,4081,4119,4126,4132,4137,4141,4153,4162,4165,4218,4224,4252,4255,4259,4269,4279,4289,4296,4326,4329,4372,4386,4393,4397,4400,4419,4438,4441,4460,4466,4485,4489,4492,4511,4514,4602,4609,4613,4616,4638,4672,4675,4683,4714,4718,4721,4733,4736,4740,4849,4853,4863,4867,4886,4899,4903,5003],[101,102,103,107],"u-page-hero",{},[104,105,43],"template",{"v-slot:title":106},"",[104,108,109,110,114],{"v-slot:description":106},"One would be hard pressed to find many applications which did not include some sort of user input. Whether it is a signup or login form, a dropdown list of options, a group of radio buttons, or some checkboxes, handling user input in some way is an ",[111,112,113],"strong",{},"extremely common task",".",[116,117,118],"p",{},"In this chapter, we will explore some of the different input widgets, how to validate user input and provide errors or feedback to the user, and how to submit the data for processing.",[120,121,122,125,126,130,131,140,141,144,145,148,149,152],"note",{},[111,123,124],{},"Companion code:"," the publisher's repository holds this chapter's code as a single file, ",[127,128,129],"code",{},"example dart"," in the ",[132,133,137],"a",{"href":134,"rel":135},"https:\u002F\u002Fgithub.com\u002Fbpbpublications\u002FFlutter-for-Jobseekers\u002Ftree\u002Fmain\u002FChapter%2005",[136],"nofollow",[127,138,139],{},"Chapter 05"," folder; it holds only the complete ",[127,142,143],{},"SignUpForm"," of the last section, so no code block below is labelled. The repository's constructors use the older ",[127,146,147],{},"{Key? key}) : super(key: key)"," form instead of ",[127,150,151],{},"{super.key}",", and it has conversion glitches of its own, so the code on this page follows the book (BPB Publications, n.d.).",[154,155,157],"h2",{"id":156},"chapter-overview","Chapter overview",[159,160,161,191,196],"card-group",{},[162,163,166],"card",{"icon":164,"title":165},"i-lucide-list","Structure",[167,168,169,173,176,179,182,185,188],"ul",{},[170,171,172],"li",{},"Buttons",[170,174,175],{},"Capturing text input",[170,177,178],{},"Pickers, selectors, and dropdowns",[170,180,181],{},"Radios, checkboxes, switches, and segmented controls",[170,183,184],{},"Sliders",[170,186,187],{},"Forms and FormFields",[170,189,190],{},"Building a simple signup form",[162,192,195],{"icon":193,"title":194},"i-lucide-target","Objectives","After completing this chapter, you will be familiar with many of the most common widgets that can be used to capture input from users. Since the applications you will be building can cater to users of different devices with different paradigms, you will learn to spot some of these differences and discover methods of gathering input from users using an appropriate method for the platform they are on. You will be able to ask a user for their birthday by way of a date picker, allow them to toggle options on and off, and select items from a list. You will learn how to validate the input in a text field against one or more rules and how to group several inputs into a form to validate or submit them as a group.",[162,197,200,201,203,204,207],{"icon":198,"title":199},"i-lucide-folder-code","Runnable example","The chapter's widgets and a complete ",[127,202,143],{},", assembled from the chapter's fragments, are also available as a runnable input gallery with widget tests in ",[127,205,206],{},"examples\u002Fflutter_for_jobseekers_ch05\u002F",". The code on this page is shown as the book prints it, with conversion glitches fixed.",[154,209,172],{"id":210},"buttons",[116,212,213,214,217,218,221],{},"There are many different types of buttons in Flutter. Between both the ",[127,215,216],{},"Material"," and ",[127,219,220],{},"Cupertino"," packages, there are several styles from which we can choose. Additionally, we can create our own buttons using whatever widgets we want. Let us look at some of the buttons in their default configurations, then explore how to make our own button.",[120,223,224,225,231,232,236],{},"Buttons, regardless of style, all have one thing in common: each of them has an ",[111,226,227,230],{},[127,228,229],{},"onPressed"," callback function",". But what exactly ",[233,234,235],"em",{},"is"," a callback function? For our purposes, a callback is a function that is passed into a widget as a parameter that can then be invoked from within the widget, but it will be run in the context of the widget's parent.",[116,238,239],{},"If this does not make sense quite yet, do not worry. Let us look at some examples of buttons and how their callbacks work.",[241,242,244],"h3",{"id":243},"cupertinobutton","CupertinoButton",[116,246,247,248,250,251,253,254,257],{},"First, we will start by looking at ",[127,249,244],{},", the ubiquitous iOS-styled button available to us, thanks to the ",[127,252,220],{}," package. Consider the following widgets (",[233,255,256],{},"figure 5.1","):",[259,260,264],"pre",{"className":261,"code":262,"language":263,"meta":106,"style":106},"language-dart shiki shiki-themes material-theme-lighter material-theme material-theme-palenight","const CupertinoButton.filled(\n  onPressed: null,\n  child: Text('Disabled'),\n),\nCupertinoButton(\n  onPressed: () {},\n  child: const Text('Enabled'),\n),\nCupertinoButton.filled(\n  onPressed: () {},\n  child: const Text('Enabled'),\n),\n","dart",[127,265,266,274,280,286,292,298,304,310,315,321,326,331],{"__ignoreMap":106},[267,268,271],"span",{"class":269,"line":270},"line",1,[267,272,273],{},"const CupertinoButton.filled(\n",[267,275,277],{"class":269,"line":276},2,[267,278,279],{},"  onPressed: null,\n",[267,281,283],{"class":269,"line":282},3,[267,284,285],{},"  child: Text('Disabled'),\n",[267,287,289],{"class":269,"line":288},4,[267,290,291],{},"),\n",[267,293,295],{"class":269,"line":294},5,[267,296,297],{},"CupertinoButton(\n",[267,299,301],{"class":269,"line":300},6,[267,302,303],{},"  onPressed: () {},\n",[267,305,307],{"class":269,"line":306},7,[267,308,309],{},"  child: const Text('Enabled'),\n",[267,311,313],{"class":269,"line":312},8,[267,314,291],{},[267,316,318],{"class":269,"line":317},9,[267,319,320],{},"CupertinoButton.filled(\n",[267,322,324],{"class":269,"line":323},10,[267,325,303],{},[267,327,329],{"class":269,"line":328},11,[267,330,309],{},[267,332,334],{"class":269,"line":333},12,[267,335,291],{},[116,337,338],{},[339,340],"img",{"alt":341,"src":342},"Figure 5.1: Various states of a CupertinoButton","\u002Fimages\u002Fflutter-for-jobseekers\u002Ffigure-5-1.png",[344,345,346],"blockquote",{},[116,347,348],{},"Figure 5.1: Various states of a CupertinoButton.",[116,350,351,352,354,355,217,357,114],{},"There are two variants of the ",[127,353,244],{},": ",[127,356,244],{},[127,358,359],{},"CupertinoButton.filled",[159,361,362,366],{},[162,363,365],{"icon":364,"title":244},"i-lucide-square","The second button in the example.",[162,367,369,370,373,374,377],{"icon":368,"title":359},"i-lucide-square-dashed-bottom","As the name implies, this variant comes with a ",[111,371,372],{},"solid background",". The first button is the ",[127,375,376],{},"filled"," variety. The disabled state of the button is grey, whereas the enabled state is blue, such as with the third button.",[116,379,380,381,384,385,217,388,391],{},"The color and disabled color can be changed by passing a ",[127,382,383],{},"Color"," to the ",[127,386,387],{},"color",[127,389,390],{},"disabledColor"," properties, respectively.",[116,393,394,395,398],{},"We can see that there are two states: enabled and disabled, but how are these states controlled, since the ",[127,396,397],{},"enabled"," property is read-only? According to the documentation, the button is disabled by default.",[400,401,402,403,409,410,412,413,114],"tip",{},"A button can be enabled by ",[111,404,405,406,408],{},"setting its ",[127,407,229],{}," property to a non-null value",". So, by providing a callback function, the button is enabled. Look at the code again and note that when the ",[127,411,229],{}," property is null, the button becomes a ",[127,414,415],{},"const",[116,417,418,419,421,422,425,426,428],{},"Let us talk about this ",[127,420,229],{}," callback function again. The default value we have been using is a simple ",[127,423,424],{},"() {}",", which provides an empty function that performs no actions when it is invoked. Any code we want to run when the button is tapped will have to be inside the curly braces. If there was a callback value being passed to the function from the button, we would set that variable inside the parenthesis. However, the ",[127,427,244],{},", like other buttons, does not provide a value, so the parenthesis will be empty.",[116,430,431],{},"Try adding something between the curly braces, then tapping the button. Can you see your code being invoked?",[259,433,435],{"className":261,"code":434,"language":263,"meta":106,"style":106},"() {\n  print(\"The button was pressed\");\n}\n",[127,436,437,442,447],{"__ignoreMap":106},[267,438,439],{"class":269,"line":270},[267,440,441],{},"() {\n",[267,443,444],{"class":269,"line":276},[267,445,446],{},"  print(\"The button was pressed\");\n",[267,448,449],{"class":269,"line":282},[267,450,451],{},"}\n",[116,453,454],{},"This is the mechanism by which we will eventually be able to submit a form. For now, let us briefly look at some of the other buttons available to us, and then we will learn to create our own.",[241,456,458],{"id":457},"material-buttons","Material buttons",[116,460,461,462,464,465,468,469,257],{},"From the ",[127,463,216],{}," package, here is an ",[127,466,467],{},"OutlinedButton"," (",[233,470,471],{},"figure 5.2",[259,473,475],{"className":261,"code":474,"language":263,"meta":106,"style":106},"OutlinedButton(\n  onPressed: () {},\n  child: const Text('OutlinedButton'),\n)\n",[127,476,477,482,486,491],{"__ignoreMap":106},[267,478,479],{"class":269,"line":270},[267,480,481],{},"OutlinedButton(\n",[267,483,484],{"class":269,"line":276},[267,485,303],{},[267,487,488],{"class":269,"line":282},[267,489,490],{},"  child: const Text('OutlinedButton'),\n",[267,492,493],{"class":269,"line":288},[267,494,495],{},")\n",[116,497,498],{},[339,499],{"alt":500,"src":501},"Figure 5.2: Visualizing the state changes of an OutlinedButton when tapping the lower right corner","\u002Fimages\u002Fflutter-for-jobseekers\u002Ffigure-5-2.png",[344,503,504],{},[116,505,506],{},"Figure 5.2: Visualizing the state changes of an OutlinedButton when tapping the lower right corner.",[116,508,509,510,513,514,519,520,523,524,527],{},"There is even a variant which allows us to use an icon, which can be invoked with the ",[127,511,512],{},"OutlinedButton.icon"," constructor and then passing an icon widget to it. Note that the text is moved to a ",[111,515,516],{},[127,517,518],{},"label"," parameter, rather than the ",[127,521,522],{},"child"," parameter (",[233,525,526],{},"figure 5.3",").",[259,529,531],{"className":261,"code":530,"language":263,"meta":106,"style":106},"OutlinedButton.icon(\n  onPressed: () {},\n  icon: const FlutterLogo(),\n  label: const Text('OutlinedButton'),\n)\n",[127,532,533,538,542,547,552],{"__ignoreMap":106},[267,534,535],{"class":269,"line":270},[267,536,537],{},"OutlinedButton.icon(\n",[267,539,540],{"class":269,"line":276},[267,541,303],{},[267,543,544],{"class":269,"line":282},[267,545,546],{},"  icon: const FlutterLogo(),\n",[267,548,549],{"class":269,"line":288},[267,550,551],{},"  label: const Text('OutlinedButton'),\n",[267,553,554],{"class":269,"line":294},[267,555,495],{},[116,557,558],{},[339,559],{"alt":560,"src":561},"Figure 5.3: The icon variant of OutlinedButton","\u002Fimages\u002Fflutter-for-jobseekers\u002Ffigure-5-3.png",[344,563,564],{},[116,565,566],{},"Figure 5.3: The icon variant of OutlinedButton.",[116,568,569,570,573,574,576,577,527],{},"The ",[127,571,572],{},"ElevatedButton"," has a similar look and feel to the ",[127,575,359],{}," with a material design take, including a subtle drop shadow (",[233,578,579],{},"figure 5.4",[259,581,583],{"className":261,"code":582,"language":263,"meta":106,"style":106},"ElevatedButton(\n  onPressed: () {},\n  child: const Text('ElevatedButton'),\n),\n",[127,584,585,590,594,599],{"__ignoreMap":106},[267,586,587],{"class":269,"line":270},[267,588,589],{},"ElevatedButton(\n",[267,591,592],{"class":269,"line":276},[267,593,303],{},[267,595,596],{"class":269,"line":282},[267,597,598],{},"  child: const Text('ElevatedButton'),\n",[267,600,601],{"class":269,"line":288},[267,602,291],{},[116,604,605],{},[339,606],{"alt":607,"src":608},"Figure 5.4: An ElevatedButton","\u002Fimages\u002Fflutter-for-jobseekers\u002Ffigure-5-4.png",[344,610,611],{},[116,612,613],{},"Figure 5.4: An ElevatedButton.",[116,615,616,617,619,620,468,623,527],{},"If you are using the ",[127,618,216],{}," package and need a button that is a bit simpler, try the ",[127,621,622],{},"TextButton",[233,624,625],{},"figure 5.5",[259,627,629],{"className":261,"code":628,"language":263,"meta":106,"style":106},"TextButton(\n  onPressed: () {},\n  child: const Text('TextButton'),\n),\n",[127,630,631,636,640,645],{"__ignoreMap":106},[267,632,633],{"class":269,"line":270},[267,634,635],{},"TextButton(\n",[267,637,638],{"class":269,"line":276},[267,639,303],{},[267,641,642],{"class":269,"line":282},[267,643,644],{},"  child: const Text('TextButton'),\n",[267,646,647],{"class":269,"line":288},[267,648,291],{},[116,650,651],{},[339,652],{"alt":653,"src":654},"Figure 5.5: A simple TextButton","\u002Fimages\u002Fflutter-for-jobseekers\u002Ffigure-5-5.png",[344,656,657],{},[116,658,659],{},"Figure 5.5: A simple TextButton.",[116,661,662,663,666,667,527],{},"Sometimes it doesn't make sense for your button to have text. In those instances, look toward the ",[127,664,665],{},"IconButton"," for a solution (",[233,668,669],{},"figure 5.6",[259,671,673],{"className":261,"code":672,"language":263,"meta":106,"style":106},"IconButton(\n  icon: const Icon(Icons.flutter_dash),\n  onPressed: () {},\n),\n",[127,674,675,680,685,689],{"__ignoreMap":106},[267,676,677],{"class":269,"line":270},[267,678,679],{},"IconButton(\n",[267,681,682],{"class":269,"line":276},[267,683,684],{},"  icon: const Icon(Icons.flutter_dash),\n",[267,686,687],{"class":269,"line":282},[267,688,303],{},[267,690,691],{"class":269,"line":288},[267,692,291],{},[116,694,695],{},[339,696],{"alt":697,"src":698},"Figure 5.6: The default state (left) and hovered state (right) of an IconButton with Dash","\u002Fimages\u002Fflutter-for-jobseekers\u002Ffigure-5-6.png",[344,700,701],{},[116,702,703],{},"Figure 5.6: The default state (left) and hovered state (right) of an IconButton with Dash.",[116,705,706],{},"Whichever button you choose, be sure to explore the button's relevant style option(s) to customize it to your liking.",[241,708,710],{"id":709},"custom-buttons-with-gesturedetector","Custom buttons with GestureDetector",[116,712,713],{},"Let us say that none of these buttons give you quite what you are looking for, though. You have evaluated all of them and your app just needs something a bit more customized.",[400,715,716,717,722,723,726,727,729,730,732],{},"Thankfully, Flutter gives us the flexibility we need to easily accomplish this task. By leveraging the ",[111,718,719],{},[127,720,721],{},"GestureDetector",", we can turn ",[233,724,725],{},"any"," widget into a button. What is more, ",[127,728,721],{}," provides a nearly limitless array of possibilities when it comes to ways you can capture taps, swipes, and other gestures. For devices that support it, ",[127,731,721],{}," even has support for force presses.",[116,734,735,736,738,739,742,743,746,747,750],{},"In fact, ",[127,737,721],{}," is ",[233,740,741],{},"so"," powerful, it can be used for ",[233,744,745],{},"far"," more than just custom buttons. Use it to drag items around the screen, pan and zoom, capture taps, double taps, triple taps, and more with ease. Here is an example where we turn a humble ",[127,748,749],{},"Container"," into a button that only works when you double tap it:",[259,752,754],{"className":261,"code":753,"language":263,"meta":106,"style":106},"GestureDetector(\n  onDoubleTap: () {},\n  child: Container(\n    height: 100,\n    width: 100,\n    color: Theme.of(context).primaryColor,\n  ),\n),\n",[127,755,756,761,766,771,776,781,786,791],{"__ignoreMap":106},[267,757,758],{"class":269,"line":270},[267,759,760],{},"GestureDetector(\n",[267,762,763],{"class":269,"line":276},[267,764,765],{},"  onDoubleTap: () {},\n",[267,767,768],{"class":269,"line":282},[267,769,770],{},"  child: Container(\n",[267,772,773],{"class":269,"line":288},[267,774,775],{},"    height: 100,\n",[267,777,778],{"class":269,"line":294},[267,779,780],{},"    width: 100,\n",[267,782,783],{"class":269,"line":300},[267,784,785],{},"    color: Theme.of(context).primaryColor,\n",[267,787,788],{"class":269,"line":306},[267,789,790],{},"  ),\n",[267,792,793],{"class":269,"line":312},[267,794,291],{},[116,796,797,800,801,804],{},[233,798,799],{},"Runnable copy:"," ",[127,802,803],{},"examples\u002Fflutter_for_jobseekers_ch05\u002Flib\u002Fbuttons\u002Fbuttons_demo.dart"," (all buttons of this section)",[154,806,175],{"id":807},"capturing-text-input",[116,809,810,811,813,814,817,818,820,821,468,824,527],{},"When it comes to capturing text input from users, we have a few options available to us. The ",[127,812,220],{}," package provides the ",[127,815,816],{},"CupertinoTextField"," widget, whereas the ",[127,819,216],{}," package gives us the ",[127,822,823],{},"TextField",[233,825,826],{},"figure 5.7",[116,828,829],{},[339,830],{"alt":831,"src":832},"Figure 5.7: TextField (top) and CupertinoTextField (bottom) with text entered in each","\u002Fimages\u002Fflutter-for-jobseekers\u002Ffigure-5-7.png",[344,834,835],{},[116,836,837],{},"Figure 5.7: TextField (top) and CupertinoTextField (bottom) with text entered in each.",[116,839,840],{},"There are a seemingly infinite number of options available to customize the look and feel of either input widget:",[159,842,843,857],{},[162,844,846,847,852,853,856],{"icon":845,"title":816},"i-simple-icons-apple","Accepts a ",[111,848,849],{},[127,850,851],{},"BoxDecoration"," object in its ",[127,854,855],{},"decoration"," property.",[162,858,860,861,852,866,856],{"icon":859,"title":823},"i-simple-icons-materialdesign","Accepts an ",[111,862,863],{},[127,864,865],{},"InputDecoration",[127,867,855],{},[116,869,870,871,217,874,877,878,217,880,882,883,527],{},"Both styles can have ",[127,872,873],{},"prefix",[127,875,876],{},"suffix"," widgets to display a widget on either the left or right side of the input. This could be useful for specifying a currency symbol, a password visibility toggle, and the like. Here, we have added both a ",[127,879,873],{},[127,881,876],{}," to either field (",[233,884,885],{},"figure 5.8",[259,887,889],{"className":261,"code":888,"language":263,"meta":106,"style":106},"class TextFieldExample extends StatelessWidget {\n  const TextFieldExample({super.key});\n  @override\n  Widget build(BuildContext context) {\n    const Icon _prefixIcon = Icon(Icons.currency_bitcoin);\n    const Icon _suffixIcon = Icon(Icons.lock);\n    return Column(\n      children: const [\n        TextField(\n          decoration: InputDecoration(\n            icon: _prefixIcon,\n            suffixIcon: _suffixIcon,\n          ),\n        ),\n        SizedBox(height: 10),\n        CupertinoTextField(\n          prefix: _prefixIcon,\n          suffix: _suffixIcon,\n        ),\n      ],\n    );\n  }\n}\n",[127,890,891,896,901,906,911,916,921,926,931,936,941,946,951,957,963,969,975,981,987,992,998,1004,1010],{"__ignoreMap":106},[267,892,893],{"class":269,"line":270},[267,894,895],{},"class TextFieldExample extends StatelessWidget {\n",[267,897,898],{"class":269,"line":276},[267,899,900],{},"  const TextFieldExample({super.key});\n",[267,902,903],{"class":269,"line":282},[267,904,905],{},"  @override\n",[267,907,908],{"class":269,"line":288},[267,909,910],{},"  Widget build(BuildContext context) {\n",[267,912,913],{"class":269,"line":294},[267,914,915],{},"    const Icon _prefixIcon = Icon(Icons.currency_bitcoin);\n",[267,917,918],{"class":269,"line":300},[267,919,920],{},"    const Icon _suffixIcon = Icon(Icons.lock);\n",[267,922,923],{"class":269,"line":306},[267,924,925],{},"    return Column(\n",[267,927,928],{"class":269,"line":312},[267,929,930],{},"      children: const [\n",[267,932,933],{"class":269,"line":317},[267,934,935],{},"        TextField(\n",[267,937,938],{"class":269,"line":323},[267,939,940],{},"          decoration: InputDecoration(\n",[267,942,943],{"class":269,"line":328},[267,944,945],{},"            icon: _prefixIcon,\n",[267,947,948],{"class":269,"line":333},[267,949,950],{},"            suffixIcon: _suffixIcon,\n",[267,952,954],{"class":269,"line":953},13,[267,955,956],{},"          ),\n",[267,958,960],{"class":269,"line":959},14,[267,961,962],{},"        ),\n",[267,964,966],{"class":269,"line":965},15,[267,967,968],{},"        SizedBox(height: 10),\n",[267,970,972],{"class":269,"line":971},16,[267,973,974],{},"        CupertinoTextField(\n",[267,976,978],{"class":269,"line":977},17,[267,979,980],{},"          prefix: _prefixIcon,\n",[267,982,984],{"class":269,"line":983},18,[267,985,986],{},"          suffix: _suffixIcon,\n",[267,988,990],{"class":269,"line":989},19,[267,991,962],{},[267,993,995],{"class":269,"line":994},20,[267,996,997],{},"      ],\n",[267,999,1001],{"class":269,"line":1000},21,[267,1002,1003],{},"    );\n",[267,1005,1007],{"class":269,"line":1006},22,[267,1008,1009],{},"  }\n",[267,1011,1013],{"class":269,"line":1012},23,[267,1014,451],{},[116,1016,1017,800,1019],{},[233,1018,799],{},[127,1020,1021],{},"examples\u002Fflutter_for_jobseekers_ch05\u002Flib\u002Ftext_input\u002Ftext_field_example.dart",[120,1023,1024,1027,1028,1030,1031,1034,1035,1038,1039,1042],{},[111,1025,1026],{},"Corrected:"," on the ",[127,1029,823],{},", the book passes the prefix icon to ",[127,1032,1033],{},"icon",", which is shown \"before the input field and outside of the decoration's container\". To place it inside the field, as the ",[127,1036,1037],{},"suffixIcon"," is, use ",[127,1040,1041],{},"prefixIcon",", which \"appears before the prefix or prefixText and before the editable part of the text field, within the decoration's container\" (Flutter, n.d.-a).",[116,1044,1045],{},[339,1046],{"alt":1047,"src":1048},"Figure 5.8: Adding a prefix and suffix icon to each text input style","\u002Fimages\u002Fflutter-for-jobseekers\u002Ffigure-5-8.png",[344,1050,1051],{},[116,1052,1053],{},"Figure 5.8: Adding a prefix and suffix icon to each text input style.",[116,1055,1056],{},"There are plenty of other styling options available, so be sure to explore the various options when you decide to implement these widgets for yourself.",[116,1058,1059,1060,1063,1064,1067,1068,114],{},"Almost more interesting than styling are the various functional options of these fields. Let us explore a few of these useful options, such as ",[127,1061,1062],{},"keyboardType",", ",[127,1065,1066],{},"textInputAction",", and ",[127,1069,1070],{},"inputFormatters",[241,1072,1062],{"id":1073},"keyboardtype",[116,1075,569,1076,1078,1079,1082,1083,1085],{},[127,1077,1062],{}," property accepts a ",[127,1080,1081],{},"TextInputType"," object as its value. ",[127,1084,1081],{}," has several options available to us to pick from:",[259,1087,1089],{"className":261,"code":1088,"language":263,"meta":106,"style":106},"\u003CTextInputType>[text, multiline, number, phone, datetime, emailAddress, url, visiblePassword, name, streetAddress, none]\n",[127,1090,1091],{"__ignoreMap":106},[267,1092,1093],{"class":269,"line":270},[267,1094,1088],{},[120,1096,1097,800,1099,1101,1102,1105,1106,1109,1110,1113],{},[111,1098,1026],{},[127,1100,1081],{}," now has two more constants, ",[127,1103,1104],{},"twitter"," (\"Optimized for social media.\") and ",[127,1107,1108],{},"webSearch"," (\"Optimized for web searches.\"), as well as the ",[127,1111,1112],{},"numberWithOptions"," constructor (Flutter, n.d.-b).",[400,1115,1116,1117,1120],{},"Presenting an ",[111,1118,1119],{},"appropriate keyboard"," to users when they are trying to enter text helps to build trust and goodwill between you, as the developer, and your users, who may have varying levels of expectations.",[116,1122,1123,1124,1127],{},"Think of a time when you were presented with a text input that wanted a numerical value, yet you were presented with a full keyboard, or a time when you were asked to enter your email address, but the ",[127,1125,1126],{},"@"," key was hidden behind a function modifier. These are modern annoyances we all deal with from time to time, but the truth is that the tools to provide a better user experience are available for us to take advantage of, and we should do so whenever possible.",[241,1129,1066],{"id":1130},"textinputaction",[116,1132,1133,1134,1136],{},"While we are on the subject, let us talk about the ",[127,1135,1066],{},". This is the default action of the enter key on the keyboard on mobile devices. Certainly, you have noticed that this button has changed from time to time. Sometimes it will display an arrow, or a carriage return symbol. Other times, it may display a send icon or a search icon. The options available to Android differ slightly from those on iOS, so it's a good idea to read up on the documentation. Here's a list of the available options:",[259,1138,1140],{"className":261,"code":1139,"language":263,"meta":106,"style":106},"\u003CTextInputAction>[continueAction, done, emergencyCall, go, join, newline, next, none, previous, route, search, send, unspecified]\n",[127,1141,1142],{"__ignoreMap":106},[267,1143,1144],{"class":269,"line":270},[267,1145,1139],{},[1147,1148,1149,1150,1063,1153,1063,1156,1159,1160,1163],"warning",{},"Android does not have any concept of the ",[127,1151,1152],{},"continueAction",[127,1154,1155],{},"emergencyCall",[127,1157,1158],{},"join",", or ",[127,1161,1162],{},"route"," options, so ensure that you take this into account when writing your code.",[241,1165,1167],{"id":1166},"texteditingcontroller","TextEditingController",[116,1169,1170,1171,1173,1174,1177,1178,1180,1181,1184],{},"Each text input can optionally have an associated ",[127,1172,1167],{}," associated with it. Generally, you want to create a variable outside of the ",[127,1175,1176],{},"build()"," method where you assign a ",[127,1179,1167],{}," to its value and then pass that variable into the text input's ",[127,1182,1183],{},"controller"," parameter. This could look something like the following:",[259,1186,1188],{"className":261,"code":1187,"language":263,"meta":106,"style":106},"class TextInputDemo extends StatefulWidget {\n  const TextInputDemo({super.key});\n\n  @override\n  State\u003CTextInputDemo> createState() => _TextInputDemoState();\n}\n\nclass _TextInputDemoState extends State\u003CTextInputDemo> {\n  final TextEditingController _textController = TextEditingController();\n\n  @override\n  Widget build(BuildContext context) {\n    return TextField(\n      controller: _textController,\n    );\n  }\n}\n",[127,1189,1190,1195,1200,1206,1210,1215,1219,1223,1228,1233,1237,1241,1245,1250,1255,1259,1263],{"__ignoreMap":106},[267,1191,1192],{"class":269,"line":270},[267,1193,1194],{},"class TextInputDemo extends StatefulWidget {\n",[267,1196,1197],{"class":269,"line":276},[267,1198,1199],{},"  const TextInputDemo({super.key});\n",[267,1201,1202],{"class":269,"line":282},[267,1203,1205],{"emptyLinePlaceholder":1204},true,"\n",[267,1207,1208],{"class":269,"line":288},[267,1209,905],{},[267,1211,1212],{"class":269,"line":294},[267,1213,1214],{},"  State\u003CTextInputDemo> createState() => _TextInputDemoState();\n",[267,1216,1217],{"class":269,"line":300},[267,1218,451],{},[267,1220,1221],{"class":269,"line":306},[267,1222,1205],{"emptyLinePlaceholder":1204},[267,1224,1225],{"class":269,"line":312},[267,1226,1227],{},"class _TextInputDemoState extends State\u003CTextInputDemo> {\n",[267,1229,1230],{"class":269,"line":317},[267,1231,1232],{},"  final TextEditingController _textController = TextEditingController();\n",[267,1234,1235],{"class":269,"line":323},[267,1236,1205],{"emptyLinePlaceholder":1204},[267,1238,1239],{"class":269,"line":328},[267,1240,905],{},[267,1242,1243],{"class":269,"line":333},[267,1244,910],{},[267,1246,1247],{"class":269,"line":953},[267,1248,1249],{},"    return TextField(\n",[267,1251,1252],{"class":269,"line":959},[267,1253,1254],{},"      controller: _textController,\n",[267,1256,1257],{"class":269,"line":965},[267,1258,1003],{},[267,1260,1261],{"class":269,"line":971},[267,1262,1009],{},[267,1264,1265],{"class":269,"line":977},[267,1266,451],{},[116,1268,1269,800,1271,1274,1275,1278],{},[233,1270,799],{},[127,1272,1273],{},"examples\u002Fflutter_for_jobseekers_ch05\u002Flib\u002Ftext_input\u002Ftext_input_demo.dart"," (with ",[127,1276,1277],{},"dispose()"," and the digits-only formatter below)",[116,1280,1281,1284,1285,1288],{},[233,1282,1283],{},"Ok…"," you may be asking yourself, ",[233,1286,1287],{},"but why?"," An excellent question, indeed.",[159,1290,1291,1302],{},[162,1292,1295,1296,1298,1299,114],{"icon":1293,"title":1294},"i-lucide-file-text","Read the text","If you want to retrieve the text that the user has entered in the field, you need a ",[127,1297,1167],{}," to do so, as one example. You would do this by calling ",[127,1300,1301],{},"_textController.value.text",[162,1303,1306,1307,1309],{"icon":1304,"title":1305},"i-lucide-eraser","Clear the field","It is not the only thing you can do with a ",[127,1308,1167],{},". You could also use it to clear the field when a user taps a button.",[116,1311,1312,1313,1315],{},"There are lots of possibilities available to you, so it is worth spending some time thinking about and exploring the ",[127,1314,1167],{}," to see what else it is capable of.",[1147,1317,1318,1325],{},[116,1319,1320,1321,1324],{},"Of course, be sure to ",[111,1322,1323],{},"dispose of it properly"," in your widget:",[259,1326,1328],{"className":261,"code":1327,"language":263,"meta":106,"style":106},"@override\nvoid dispose() {\n  _textController.dispose();\n  super.dispose();\n}\n",[127,1329,1330,1335,1340,1345,1350],{"__ignoreMap":106},[267,1331,1332],{"class":269,"line":270},[267,1333,1334],{},"@override\n",[267,1336,1337],{"class":269,"line":276},[267,1338,1339],{},"void dispose() {\n",[267,1341,1342],{"class":269,"line":282},[267,1343,1344],{},"  _textController.dispose();\n",[267,1346,1347],{"class":269,"line":288},[267,1348,1349],{},"  super.dispose();\n",[267,1351,1352],{"class":269,"line":294},[267,1353,451],{},[241,1355,1070],{"id":1356},"inputformatters",[116,1358,1359,1360,1362,1363,1366],{},"Finally, let us talk about the ",[127,1361,1070],{}," option. This parameter accepts a list of validation and formatting functions, which are applied ",[111,1364,1365],{},"from top to bottom"," each time the text in the field changes (for example, on each keypress). We can use this to do things like limiting the characters which are allowed to be typed into a field or automatically apply formatting to the text, such as entering dashes into phone numbers automatically.",[116,1368,1369,1370,1372],{},"In this example, we only allow numbers to be typed into a ",[127,1371,823],{},":",[259,1374,1376],{"className":261,"code":1375,"language":263,"meta":106,"style":106},"TextField(\n  inputFormatters: [\n    FilteringTextInputFormatter.allow(RegExp(r'[0-9]')),\n  ],\n)\n",[127,1377,1378,1383,1388,1393,1398],{"__ignoreMap":106},[267,1379,1380],{"class":269,"line":270},[267,1381,1382],{},"TextField(\n",[267,1384,1385],{"class":269,"line":276},[267,1386,1387],{},"  inputFormatters: [\n",[267,1389,1390],{"class":269,"line":282},[267,1391,1392],{},"    FilteringTextInputFormatter.allow(RegExp(r'[0-9]')),\n",[267,1394,1395],{"class":269,"line":288},[267,1396,1397],{},"  ],\n",[267,1399,1400],{"class":269,"line":294},[267,1401,495],{},[120,1403,1404,1405,1407],{},"Since the ",[127,1406,1070],{}," property accepts a list of input formatters, you can apply multiple formatters to input. Just keep in mind that the filters are applied from top to bottom.",[154,1409,178],{"id":1410},"pickers-selectors-and-dropdowns",[116,1412,1413],{},"There are more inputs available than just text input. If you want to ask a user for their birthday, text input may not be the best option. Likewise, asking someone to choose an item from a list and type it in would be far less preferable to asking them to pick an item from a dropdown. Let us explore some of the input options which cover these use cases.",[241,1415,1417],{"id":1416},"material-date-and-time-pickers","Material date and time pickers",[116,1419,1420,1421,1423,1424,1430],{},"Let us start by exploring the ",[127,1422,216],{}," style date picker. Rather than being defined as a widget, the Material date picker is invoked using the ",[111,1425,1426,1427],{},"function ",[127,1428,1429],{},"showDatePicker",". Four parameter values are required for this function, but many other options are available. The required parameters are as follows:",[1432,1433,1434,1440,1445,1449],"field-group",{},[1435,1436,1439],"field",{"name":1437,"type":1438},"context","BuildContext","Should have an obvious purpose at this point.",[1435,1441,1444],{"name":1442,"type":1443},"firstDate","DateTime","The earliest selectable date.",[1435,1446,1448],{"name":1447,"type":1443},"lastDate","The latest selectable date.",[1435,1450,1452],{"name":1451,"type":1443},"initialDate","The date the picker opens on.",[120,1454,1455,1457,1458,1063,1460,217,1462,1464,1465,1467,1468,1471,1472,1474,1475,1477],{},[111,1456,1026],{}," only three parameters are required now: ",[127,1459,1437],{},[127,1461,1442],{},[127,1463,1447],{},". ",[127,1466,1451],{}," is optional (",[127,1469,1470],{},"DateTime? initialDate","); \"if ",[127,1473,1451],{}," is not null, it will show the month of ",[127,1476,1451],{},"\" (Flutter, n.d.-c).",[116,1479,1480,1481,1483,1484,1486],{},"Aside from ",[127,1482,1437],{},", the remaining three values expect a ",[127,1485,1443],{}," object. Several ways to get one:",[159,1488,1489,1500,1511],{},[162,1490,1493,1494,1496,1497,114],{"icon":1491,"title":1492},"i-lucide-clock","DateTime.now()","You could simply use ",[127,1495,1492],{}," to get a value, which is, well, ",[233,1498,1499],{},"now",[162,1501,1504,1505,1507,1508,114],{"icon":1502,"title":1503},"i-lucide-text","DateTime.parse()","A ",[127,1506,1443],{}," object will be constructed from a string of text, which represents a given moment in time. The documentation on the method itself provides example strings that it can parse, but one such example could be ",[127,1509,1510],{},"DateTime.parse(\"2000-01-01\")",[162,1512,1514,1515,1518],{"icon":26,"title":1513},"subtract()","Several other options are available, as well. Say you want a point in time that is precisely one year ago from the current time. ",[127,1516,1517],{},"DateTime.now().subtract(const Duration(days: 365))"," would give you what you are looking for.",[120,1520,1521,1523,1524,1527,1528,114],{},[111,1522,1026],{}," 365 days is precisely one year only when no 29 February falls in between; across a leap day it lands one calendar day off. For the same date one year earlier, build the date from its parts: with ",[127,1525,1526],{},"final now = DateTime.now();",", use ",[127,1529,1530],{},"DateTime(now.year - 1, now.month, now.day)",[116,1532,1533],{},"Let us put all this knowledge into practice by creating a button that displays a material date picker.",[259,1535,1537],{"className":261,"code":1536,"language":263,"meta":106,"style":106},"ElevatedButton(\n  child: const Text(\"Press me\"),\n  onPressed: () async {\n    await showDatePicker(\n      context: context,\n      firstDate: DateTime.now().subtract(const Duration(days: 365 * 50)),\n      lastDate: DateTime.now(),\n      initialDate: DateTime.parse(\"2000-01-01\"),\n    );\n  },\n)\n",[127,1538,1539,1543,1548,1553,1558,1563,1568,1573,1578,1582,1587],{"__ignoreMap":106},[267,1540,1541],{"class":269,"line":270},[267,1542,589],{},[267,1544,1545],{"class":269,"line":276},[267,1546,1547],{},"  child: const Text(\"Press me\"),\n",[267,1549,1550],{"class":269,"line":282},[267,1551,1552],{},"  onPressed: () async {\n",[267,1554,1555],{"class":269,"line":288},[267,1556,1557],{},"    await showDatePicker(\n",[267,1559,1560],{"class":269,"line":294},[267,1561,1562],{},"      context: context,\n",[267,1564,1565],{"class":269,"line":300},[267,1566,1567],{},"      firstDate: DateTime.now().subtract(const Duration(days: 365 * 50)),\n",[267,1569,1570],{"class":269,"line":306},[267,1571,1572],{},"      lastDate: DateTime.now(),\n",[267,1574,1575],{"class":269,"line":312},[267,1576,1577],{},"      initialDate: DateTime.parse(\"2000-01-01\"),\n",[267,1579,1580],{"class":269,"line":317},[267,1581,1003],{},[267,1583,1584],{"class":269,"line":323},[267,1585,1586],{},"  },\n",[267,1588,1589],{"class":269,"line":328},[267,1590,495],{},[116,1592,1593,1594,257],{},"This example will give us a date picker from where we can choose from any day in the past 50 years. Here is what that looks like (",[233,1595,1596],{},"figure 5.9",[116,1598,1599],{},[339,1600],{"alt":1601,"src":1602},"Figure 5.9: The Material date picker","\u002Fimages\u002Fflutter-for-jobseekers\u002Ffigure-5-9.png",[344,1604,1605],{},[116,1606,1607],{},"Figure 5.9: The Material date picker.",[116,1609,1610,1611,1616,1617,217,1619,1622,1623,1626,1627,257],{},"There is even a companion method, ",[111,1612,1613],{},[127,1614,1615],{},"showTimePicker",", which will allow you to choose a time of day the same way you would choose a date. The only required parameters are ",[127,1618,1437],{},[127,1620,1621],{},"initialTime",", which takes a ",[127,1624,1625],{},"TimeOfDay"," object. Here is what it looks like in practice (",[233,1628,1629],{},"figure 5.10",[116,1631,1632],{},[339,1633],{"alt":1634,"src":1635},"Figure 5.10: The Material time picker","\u002Fimages\u002Fflutter-for-jobseekers\u002Ffigure-5-10.png",[344,1637,1638],{},[116,1639,1640],{},"Figure 5.10: The Material time picker.",[241,1642,1644],{"id":1643},"cupertino-pickers","Cupertino pickers",[116,1646,569,1647,1649,1650,1652],{},[127,1648,216],{}," pickers are not the only game in town, though. How about the ",[127,1651,220],{}," style?",[120,1654,1655,1656,1658,1659,1664],{},"Unlike the ",[127,1657,216],{}," pickers, the ",[111,1660,1661,1663],{},[127,1662,220],{}," pickers are widgets",". However, they take a bit more setup work.",[116,1666,1667],{},"First, we will need a modal popup that we can pin to the bottom of the display. Inside our widget, we will define it something like this:",[259,1669,1671],{"className":261,"code":1670,"language":263,"meta":106,"style":106},"void _showDialog(BuildContext context, Widget child) {\n  showCupertinoModalPopup\u003Cvoid>(\n    context: context,\n    builder: (BuildContext context) => Container(\n      height: 216,\n      margin: EdgeInsets.only(\n        bottom: MediaQuery.of(context).viewInsets.bottom,\n      ),\n      color: CupertinoColors.systemBackground.resolveFrom(context),\n      child: SafeArea(\n        top: false,\n        child: child,\n      ),\n    ),\n  );\n}\n",[127,1672,1673,1678,1683,1688,1693,1698,1703,1708,1713,1718,1723,1728,1733,1737,1742,1747],{"__ignoreMap":106},[267,1674,1675],{"class":269,"line":270},[267,1676,1677],{},"void _showDialog(BuildContext context, Widget child) {\n",[267,1679,1680],{"class":269,"line":276},[267,1681,1682],{},"  showCupertinoModalPopup\u003Cvoid>(\n",[267,1684,1685],{"class":269,"line":282},[267,1686,1687],{},"    context: context,\n",[267,1689,1690],{"class":269,"line":288},[267,1691,1692],{},"    builder: (BuildContext context) => Container(\n",[267,1694,1695],{"class":269,"line":294},[267,1696,1697],{},"      height: 216,\n",[267,1699,1700],{"class":269,"line":300},[267,1701,1702],{},"      margin: EdgeInsets.only(\n",[267,1704,1705],{"class":269,"line":306},[267,1706,1707],{},"        bottom: MediaQuery.of(context).viewInsets.bottom,\n",[267,1709,1710],{"class":269,"line":312},[267,1711,1712],{},"      ),\n",[267,1714,1715],{"class":269,"line":317},[267,1716,1717],{},"      color: CupertinoColors.systemBackground.resolveFrom(context),\n",[267,1719,1720],{"class":269,"line":323},[267,1721,1722],{},"      child: SafeArea(\n",[267,1724,1725],{"class":269,"line":328},[267,1726,1727],{},"        top: false,\n",[267,1729,1730],{"class":269,"line":333},[267,1731,1732],{},"        child: child,\n",[267,1734,1735],{"class":269,"line":953},[267,1736,1712],{},[267,1738,1739],{"class":269,"line":959},[267,1740,1741],{},"    ),\n",[267,1743,1744],{"class":269,"line":965},[267,1745,1746],{},"  );\n",[267,1748,1749],{"class":269,"line":971},[267,1750,451],{},[116,1752,1753,1754,1757],{},"This will give us a bottom-aligned dialog in which we can display the picker. Next, we can invoke that dialog and add the ",[127,1755,1756],{},"CupertinoDatePicker"," as its child:",[259,1759,1761],{"className":261,"code":1760,"language":263,"meta":106,"style":106},"ElevatedButton(\n  child: const Text(\"Press me\"),\n  onPressed: () => _showDialog(\n    context,\n    CupertinoDatePicker(\n      initialDateTime: DateTime.parse(\"2000-01-01\"),\n      mode: CupertinoDatePickerMode.date,\n      onDateTimeChanged: (DateTime newDate) {\n        print('$newDate');\n      },\n    ),\n  ),\n)\n",[127,1762,1763,1767,1771,1776,1781,1786,1791,1796,1801,1806,1811,1815,1819],{"__ignoreMap":106},[267,1764,1765],{"class":269,"line":270},[267,1766,589],{},[267,1768,1769],{"class":269,"line":276},[267,1770,1547],{},[267,1772,1773],{"class":269,"line":282},[267,1774,1775],{},"  onPressed: () => _showDialog(\n",[267,1777,1778],{"class":269,"line":288},[267,1779,1780],{},"    context,\n",[267,1782,1783],{"class":269,"line":294},[267,1784,1785],{},"    CupertinoDatePicker(\n",[267,1787,1788],{"class":269,"line":300},[267,1789,1790],{},"      initialDateTime: DateTime.parse(\"2000-01-01\"),\n",[267,1792,1793],{"class":269,"line":306},[267,1794,1795],{},"      mode: CupertinoDatePickerMode.date,\n",[267,1797,1798],{"class":269,"line":312},[267,1799,1800],{},"      onDateTimeChanged: (DateTime newDate) {\n",[267,1802,1803],{"class":269,"line":317},[267,1804,1805],{},"        print('$newDate');\n",[267,1807,1808],{"class":269,"line":323},[267,1809,1810],{},"      },\n",[267,1812,1813],{"class":269,"line":328},[267,1814,1741],{},[267,1816,1817],{"class":269,"line":333},[267,1818,790],{},[267,1820,1821],{"class":269,"line":953},[267,1822,495],{},[116,1824,1825,1826,257],{},"This will result in the following date picker being displayed (",[233,1827,1828],{},"figure 5.11",[116,1830,1831],{},[339,1832],{"alt":1833,"src":1834},"Figure 5.11: A Cupertino date picker with the dark theme applied","\u002Fimages\u002Fflutter-for-jobseekers\u002Ffigure-5-11.png",[344,1836,1837],{},[116,1838,1839],{},"Figure 5.11: A Cupertino date picker with the dark theme applied.",[116,1841,1842,1843,1848,1849,257],{},"By swapping out the picker for a ",[111,1844,1845],{},[127,1846,1847],{},"CupertinoTimerPicker",", you could determine a duration of time for, say, a timer. Here is what it would look like to get an hours and minutes duration (",[233,1850,1851],{},"figure 5.12",[259,1853,1855],{"className":261,"code":1854,"language":263,"meta":106,"style":106},"CupertinoTimerPicker(\n  mode: CupertinoTimerPickerMode.hm,\n  onTimerDurationChanged: (Duration duration) {\n    print('$duration');\n  },\n)\n",[127,1856,1857,1862,1867,1872,1877,1881],{"__ignoreMap":106},[267,1858,1859],{"class":269,"line":270},[267,1860,1861],{},"CupertinoTimerPicker(\n",[267,1863,1864],{"class":269,"line":276},[267,1865,1866],{},"  mode: CupertinoTimerPickerMode.hm,\n",[267,1868,1869],{"class":269,"line":282},[267,1870,1871],{},"  onTimerDurationChanged: (Duration duration) {\n",[267,1873,1874],{"class":269,"line":288},[267,1875,1876],{},"    print('$duration');\n",[267,1878,1879],{"class":269,"line":294},[267,1880,1586],{},[267,1882,1883],{"class":269,"line":300},[267,1884,495],{},[116,1886,1887],{},[339,1888],{"alt":1889,"src":1890},"Figure 5.12: A Cupertino timer picker with the dark theme applied","\u002Fimages\u002Fflutter-for-jobseekers\u002Ffigure-5-12.png",[344,1892,1893],{},[116,1894,1895],{},"Figure 5.12: A Cupertino timer picker with the dark theme applied.",[116,1897,1898,1899,1902],{},"Of course, the picker is far more versatile than just getting a date or a duration. In fact, it could be used for ",[111,1900,1901],{},"any arbitrary list of items",". Let us look at an example where you can choose a day of the week. First, we need to define those days. We will do that in a list:",[259,1904,1906],{"className":261,"code":1905,"language":263,"meta":106,"style":106},"static const List\u003CString> daysOfWeek = [\n  \"Monday\",\n  \"Tuesday\",\n  \"Wednesday\",\n  \"Thursday\",\n  \"Friday\",\n  \"Saturday\",\n  \"Sunday\",\n];\n",[127,1907,1908,1913,1918,1923,1928,1933,1938,1943,1948],{"__ignoreMap":106},[267,1909,1910],{"class":269,"line":270},[267,1911,1912],{},"static const List\u003CString> daysOfWeek = [\n",[267,1914,1915],{"class":269,"line":276},[267,1916,1917],{},"  \"Monday\",\n",[267,1919,1920],{"class":269,"line":282},[267,1921,1922],{},"  \"Tuesday\",\n",[267,1924,1925],{"class":269,"line":288},[267,1926,1927],{},"  \"Wednesday\",\n",[267,1929,1930],{"class":269,"line":294},[267,1931,1932],{},"  \"Thursday\",\n",[267,1934,1935],{"class":269,"line":300},[267,1936,1937],{},"  \"Friday\",\n",[267,1939,1940],{"class":269,"line":306},[267,1941,1942],{},"  \"Saturday\",\n",[267,1944,1945],{"class":269,"line":312},[267,1946,1947],{},"  \"Sunday\",\n",[267,1949,1950],{"class":269,"line":317},[267,1951,1952],{},"];\n",[116,1954,1955,1956,1959,1960,1963,1964,1967],{},"Next, we will use a ",[127,1957,1958],{},"CupertinoPicker"," to display each item in the list as an option. The ",[127,1961,1962],{},"itemExtent"," property tells the picker what ",[233,1965,1966],{},"height"," to give each item in the picker list.",[259,1969,1971],{"className":261,"code":1970,"language":263,"meta":106,"style":106},"CupertinoPicker(\n  itemExtent: 30,\n  useMagnifier: true,\n  children: daysOfWeek.map((day) => Text(day)).toList(),\n  onSelectedItemChanged: (int selectedItem) {\n    print(daysOfWeek[selectedItem]);\n  },\n)\n",[127,1972,1973,1978,1983,1988,1993,1998,2003,2007],{"__ignoreMap":106},[267,1974,1975],{"class":269,"line":270},[267,1976,1977],{},"CupertinoPicker(\n",[267,1979,1980],{"class":269,"line":276},[267,1981,1982],{},"  itemExtent: 30,\n",[267,1984,1985],{"class":269,"line":282},[267,1986,1987],{},"  useMagnifier: true,\n",[267,1989,1990],{"class":269,"line":288},[267,1991,1992],{},"  children: daysOfWeek.map((day) => Text(day)).toList(),\n",[267,1994,1995],{"class":269,"line":294},[267,1996,1997],{},"  onSelectedItemChanged: (int selectedItem) {\n",[267,1999,2000],{"class":269,"line":300},[267,2001,2002],{},"    print(daysOfWeek[selectedItem]);\n",[267,2004,2005],{"class":269,"line":306},[267,2006,1586],{},[267,2008,2009],{"class":269,"line":312},[267,2010,495],{},[116,2012,2013,2014,257],{},"And just like that, we have taken an arbitrary list of items and created a picker from them (",[233,2015,2016],{},"figure 5.13",[116,2018,2019],{},[339,2020],{"alt":2021,"src":2022},"Figure 5.13: A Cupertino picker of an arbitrary list of items with the dark theme applied","\u002Fimages\u002Fflutter-for-jobseekers\u002Ffigure-5-13.png",[344,2024,2025],{},[116,2026,2027],{},"Figure 5.13: A Cupertino picker of an arbitrary list of items with the dark theme applied.",[400,2029,2030,2031,2033],{},"Note that in this example we used a list of strings, but in practice you could use ",[233,2032,725],{}," type of data.",[116,2035,2036,800,2038,2041],{},[233,2037,799],{},[127,2039,2040],{},"examples\u002Fflutter_for_jobseekers_ch05\u002Flib\u002Fpickers\u002Fpickers_demo.dart"," (all Material and Cupertino pickers of this section)",[241,2043,2045],{"id":2044},"dropdownbutton","DropdownButton",[116,2047,2048,2049,2051,2052,2054,2055,2057,2058,2060],{},"Finally, let us look at the ",[127,2050,2045],{}," widget, provided by the ",[127,2053,216],{}," package. Like the generic picker from the ",[127,2056,220],{}," package, we can use this widget to allow users to pick from a list of items. Here, we have converted the ",[127,2059,220],{}," weekday picker into a material dropdown:",[259,2062,2064],{"className":261,"code":2063,"language":263,"meta":106,"style":106},"class WeekdayDropdown extends StatefulWidget {\n  static const List\u003CString> daysOfWeek = [\n    \"Monday\",\n    \"Tuesday\",\n    \"Wednesday\",\n    \"Thursday\",\n    \"Friday\",\n    \"Saturday\",\n    \"Sunday\",\n  ];\n\n  const WeekdayDropdown({super.key});\n\n  @override\n  State\u003CWeekdayDropdown> createState() => _WeekdayDropdownState();\n}\n\nclass _WeekdayDropdownState extends State\u003CWeekdayDropdown> {\n  String dropdownValue = WeekdayDropdown.daysOfWeek[0];\n\n  @override\n  Widget build(BuildContext context) {\n    return DropdownButton\u003CString>(\n      value: dropdownValue,\n      icon: const Icon(Icons.arrow_downward),\n      onChanged: (String? newValue) {\n        print(newValue);\n      },\n      items: WeekdayDropdown.daysOfWeek.map\u003CDropdownMenuItem\u003CString>>((String value) {\n          return DropdownMenuItem\u003CString>(\n            value: value,\n            child: Text(value),\n          );\n      }).toList(),\n    );\n  }\n}\n",[127,2065,2066,2071,2076,2081,2086,2091,2096,2101,2106,2111,2116,2120,2125,2129,2133,2138,2142,2146,2151,2156,2160,2164,2168,2173,2179,2185,2191,2197,2202,2208,2214,2220,2226,2232,2238,2243,2248],{"__ignoreMap":106},[267,2067,2068],{"class":269,"line":270},[267,2069,2070],{},"class WeekdayDropdown extends StatefulWidget {\n",[267,2072,2073],{"class":269,"line":276},[267,2074,2075],{},"  static const List\u003CString> daysOfWeek = [\n",[267,2077,2078],{"class":269,"line":282},[267,2079,2080],{},"    \"Monday\",\n",[267,2082,2083],{"class":269,"line":288},[267,2084,2085],{},"    \"Tuesday\",\n",[267,2087,2088],{"class":269,"line":294},[267,2089,2090],{},"    \"Wednesday\",\n",[267,2092,2093],{"class":269,"line":300},[267,2094,2095],{},"    \"Thursday\",\n",[267,2097,2098],{"class":269,"line":306},[267,2099,2100],{},"    \"Friday\",\n",[267,2102,2103],{"class":269,"line":312},[267,2104,2105],{},"    \"Saturday\",\n",[267,2107,2108],{"class":269,"line":317},[267,2109,2110],{},"    \"Sunday\",\n",[267,2112,2113],{"class":269,"line":323},[267,2114,2115],{},"  ];\n",[267,2117,2118],{"class":269,"line":328},[267,2119,1205],{"emptyLinePlaceholder":1204},[267,2121,2122],{"class":269,"line":333},[267,2123,2124],{},"  const WeekdayDropdown({super.key});\n",[267,2126,2127],{"class":269,"line":953},[267,2128,1205],{"emptyLinePlaceholder":1204},[267,2130,2131],{"class":269,"line":959},[267,2132,905],{},[267,2134,2135],{"class":269,"line":965},[267,2136,2137],{},"  State\u003CWeekdayDropdown> createState() => _WeekdayDropdownState();\n",[267,2139,2140],{"class":269,"line":971},[267,2141,451],{},[267,2143,2144],{"class":269,"line":977},[267,2145,1205],{"emptyLinePlaceholder":1204},[267,2147,2148],{"class":269,"line":983},[267,2149,2150],{},"class _WeekdayDropdownState extends State\u003CWeekdayDropdown> {\n",[267,2152,2153],{"class":269,"line":989},[267,2154,2155],{},"  String dropdownValue = WeekdayDropdown.daysOfWeek[0];\n",[267,2157,2158],{"class":269,"line":994},[267,2159,1205],{"emptyLinePlaceholder":1204},[267,2161,2162],{"class":269,"line":1000},[267,2163,905],{},[267,2165,2166],{"class":269,"line":1006},[267,2167,910],{},[267,2169,2170],{"class":269,"line":1012},[267,2171,2172],{},"    return DropdownButton\u003CString>(\n",[267,2174,2176],{"class":269,"line":2175},24,[267,2177,2178],{},"      value: dropdownValue,\n",[267,2180,2182],{"class":269,"line":2181},25,[267,2183,2184],{},"      icon: const Icon(Icons.arrow_downward),\n",[267,2186,2188],{"class":269,"line":2187},26,[267,2189,2190],{},"      onChanged: (String? newValue) {\n",[267,2192,2194],{"class":269,"line":2193},27,[267,2195,2196],{},"        print(newValue);\n",[267,2198,2200],{"class":269,"line":2199},28,[267,2201,1810],{},[267,2203,2205],{"class":269,"line":2204},29,[267,2206,2207],{},"      items: WeekdayDropdown.daysOfWeek.map\u003CDropdownMenuItem\u003CString>>((String value) {\n",[267,2209,2211],{"class":269,"line":2210},30,[267,2212,2213],{},"          return DropdownMenuItem\u003CString>(\n",[267,2215,2217],{"class":269,"line":2216},31,[267,2218,2219],{},"            value: value,\n",[267,2221,2223],{"class":269,"line":2222},32,[267,2224,2225],{},"            child: Text(value),\n",[267,2227,2229],{"class":269,"line":2228},33,[267,2230,2231],{},"          );\n",[267,2233,2235],{"class":269,"line":2234},34,[267,2236,2237],{},"      }).toList(),\n",[267,2239,2241],{"class":269,"line":2240},35,[267,2242,1003],{},[267,2244,2246],{"class":269,"line":2245},36,[267,2247,1009],{},[267,2249,2251],{"class":269,"line":2250},37,[267,2252,451],{},[116,2254,2255,800,2257],{},[233,2256,799],{},[127,2258,2259],{},"examples\u002Fflutter_for_jobseekers_ch05\u002Flib\u002Fpickers\u002Fweekday_dropdown.dart",[120,2261,2262,2264,2265,2268,2269,2272,2273,2276,2277,114],{},[111,2263,1026],{}," as printed, the dropdown always shows ",[233,2266,2267],{},"Monday",", because ",[127,2270,2271],{},"onChanged"," only prints the new value. \"The onChanged callback should update a state variable that defines the dropdown's value. It should also call State.setState to rebuild the dropdown with the new value\" (Flutter, n.d.-d). The runnable copy sets ",[127,2274,2275],{},"dropdownValue = newValue!"," inside ",[127,2278,2279],{},"setState()",[116,2281,2282,2283,257],{},"Here it is in action (",[233,2284,2285],{},"figure 5.14",[116,2287,2288],{},[339,2289],{"alt":2290,"src":2291},"Figure 5.14: A Material dropdown","\u002Fimages\u002Fflutter-for-jobseekers\u002Ffigure-5-14.png",[344,2293,2294],{},[116,2295,2296],{},"Figure 5.14: A Material dropdown.",[154,2298,181],{"id":2299},"radios-checkboxes-switches-and-segmented-controls",[116,2301,2302],{},"Choosing an item from a list is not always the appropriate method, either. Sometimes what we need is to choose multiple items from a small group of similar items or a single item from a small number of items, which would not make sense in a dropdown. That is where this class of inputs comes in handy. Let us delve in.",[241,2304,2306],{"id":2305},"radio","Radio",[116,2308,2309],{},"When it comes to choosing items from a small, mutually exclusive list, the humble radio needs no introduction. Let us look at it in action.",[120,2311,2312,2313,2316],{},"The radio needs to be in a ",[111,2314,2315],{},"stateful widget"," so the value can be updated when the user selects a new option. For brevity's sake, we will forego the stateful widget portion of the code, and instead focus on the state itself.",[259,2318,2320],{"className":261,"code":2319,"language":263,"meta":106,"style":106},"class _MaybeState extends State\u003CMaybe> {\n  String _value = \"Yep\";\n  @override\n  Widget build(BuildContext context) {\n    return Column(\n      children: \u003CWidget>[\n        ListTile(\n          title: const Text('Yep'),\n          leading: Radio\u003CString>(\n            value: \"Yep\",\n            groupValue: _value,\n            onChanged: (String? value) {\n              setState(() {\n                _value = value!;\n              });\n            },\n          ),\n        ),\n        ListTile(\n          title: const Text('Nope'),\n          leading: Radio\u003CString>(\n            value: \"Nope\",\n            groupValue: _value,\n            onChanged: (String? value) {\n              setState(() {\n                _value = value!;\n              });\n            },\n          ),\n        ),\n      ],\n    );\n  }\n}\n",[127,2321,2322,2327,2332,2336,2340,2344,2349,2354,2359,2364,2369,2374,2379,2384,2389,2394,2399,2403,2407,2411,2416,2420,2425,2429,2433,2437,2441,2445,2449,2453,2457,2461,2465,2469],{"__ignoreMap":106},[267,2323,2324],{"class":269,"line":270},[267,2325,2326],{},"class _MaybeState extends State\u003CMaybe> {\n",[267,2328,2329],{"class":269,"line":276},[267,2330,2331],{},"  String _value = \"Yep\";\n",[267,2333,2334],{"class":269,"line":282},[267,2335,905],{},[267,2337,2338],{"class":269,"line":288},[267,2339,910],{},[267,2341,2342],{"class":269,"line":294},[267,2343,925],{},[267,2345,2346],{"class":269,"line":300},[267,2347,2348],{},"      children: \u003CWidget>[\n",[267,2350,2351],{"class":269,"line":306},[267,2352,2353],{},"        ListTile(\n",[267,2355,2356],{"class":269,"line":312},[267,2357,2358],{},"          title: const Text('Yep'),\n",[267,2360,2361],{"class":269,"line":317},[267,2362,2363],{},"          leading: Radio\u003CString>(\n",[267,2365,2366],{"class":269,"line":323},[267,2367,2368],{},"            value: \"Yep\",\n",[267,2370,2371],{"class":269,"line":328},[267,2372,2373],{},"            groupValue: _value,\n",[267,2375,2376],{"class":269,"line":333},[267,2377,2378],{},"            onChanged: (String? value) {\n",[267,2380,2381],{"class":269,"line":953},[267,2382,2383],{},"              setState(() {\n",[267,2385,2386],{"class":269,"line":959},[267,2387,2388],{},"                _value = value!;\n",[267,2390,2391],{"class":269,"line":965},[267,2392,2393],{},"              });\n",[267,2395,2396],{"class":269,"line":971},[267,2397,2398],{},"            },\n",[267,2400,2401],{"class":269,"line":977},[267,2402,956],{},[267,2404,2405],{"class":269,"line":983},[267,2406,962],{},[267,2408,2409],{"class":269,"line":989},[267,2410,2353],{},[267,2412,2413],{"class":269,"line":994},[267,2414,2415],{},"          title: const Text('Nope'),\n",[267,2417,2418],{"class":269,"line":1000},[267,2419,2363],{},[267,2421,2422],{"class":269,"line":1006},[267,2423,2424],{},"            value: \"Nope\",\n",[267,2426,2427],{"class":269,"line":1012},[267,2428,2373],{},[267,2430,2431],{"class":269,"line":2175},[267,2432,2378],{},[267,2434,2435],{"class":269,"line":2181},[267,2436,2383],{},[267,2438,2439],{"class":269,"line":2187},[267,2440,2388],{},[267,2442,2443],{"class":269,"line":2193},[267,2444,2393],{},[267,2446,2447],{"class":269,"line":2199},[267,2448,2398],{},[267,2450,2451],{"class":269,"line":2204},[267,2452,956],{},[267,2454,2455],{"class":269,"line":2210},[267,2456,962],{},[267,2458,2459],{"class":269,"line":2216},[267,2460,997],{},[267,2462,2463],{"class":269,"line":2222},[267,2464,1003],{},[267,2466,2467],{"class":269,"line":2228},[267,2468,1009],{},[267,2470,2471],{"class":269,"line":2234},[267,2472,451],{},[116,2474,2475,800,2477,468,2480,2483],{},[233,2476,799],{},[127,2478,2479],{},"examples\u002Fflutter_for_jobseekers_ch05\u002Flib\u002Fselection\u002Fmaybe.dart",[127,2481,2482],{},"MaybeRadio",")",[120,2485,2486,800,2488,217,2491,2494,2495,2498,2499,217,2502,2504,2505,2507,2508,2511,2512,2515],{},[111,2487,1026],{},[127,2489,2490],{},"Radio.groupValue",[127,2492,2493],{},"Radio.onChanged"," are deprecated after Flutter 3.32: \"Use a RadioGroup ancestor to manage group value instead\" and \"Use RadioGroup to handle value change instead\" (Flutter, n.d.-e). On current Flutter, wrap the radios in a ",[127,2496,2497],{},"RadioGroup\u003CString>"," that holds ",[127,2500,2501],{},"groupValue",[127,2503,2271],{},", and give each ",[127,2506,2306],{}," only its ",[127,2509,2510],{},"value",". The runnable copy keeps the book's form, because the pinned Flutter 3.29 has no ",[127,2513,2514],{},"RadioGroup"," yet.",[116,2517,2518,2519,217,2522,2525,2526,257],{},"This will produce two radios which a user can choose from: ",[233,2520,2521],{},"Yep",[233,2523,2524],{},"Nope",". Here is what it looks like in action (",[233,2527,2528],{},"figure 5.15",[116,2530,2531],{},[339,2532],{"alt":2533,"src":2534},"Figure 5.15: Material radio buttons","\u002Fimages\u002Fflutter-for-jobseekers\u002Ffigure-5-15.png",[344,2536,2537],{},[116,2538,2539],{},"Figure 5.15: Material radio buttons.",[241,2541,2543],{"id":2542},"segmented-controls","Segmented controls",[116,2545,2546,2547,2552],{},"This might feel out of place for an iOS user, though. Here is the same example using the ",[111,2548,2549],{},[127,2550,2551],{},"CupertinoSegmentedControl",", which ought to feel more appropriate on iOS:",[259,2554,2556],{"className":261,"code":2555,"language":263,"meta":106,"style":106},"class _MaybeState extends State\u003CMaybe> {\n  String _value = \"Yep\";\n  @override\n  Widget build(BuildContext context) {\n    return CupertinoSegmentedControl(\n      groupValue: _value,\n      onValueChanged: (String? value) {\n        setState(() {\n            _value = value!;\n        });\n      },\n      children: const {\n        'Yep': Padding(\n          padding: EdgeInsets.symmetric(horizontal: 10),\n          child: Text('Yep'),\n        ),\n        'Nope': Padding(\n          padding: EdgeInsets.symmetric(horizontal: 10),\n          child: Text('Nope'),\n        ),\n      },\n    );\n  }\n}\n",[127,2557,2558,2562,2566,2570,2574,2579,2584,2589,2594,2599,2604,2608,2613,2618,2623,2628,2632,2637,2641,2646,2650,2654,2658,2662],{"__ignoreMap":106},[267,2559,2560],{"class":269,"line":270},[267,2561,2326],{},[267,2563,2564],{"class":269,"line":276},[267,2565,2331],{},[267,2567,2568],{"class":269,"line":282},[267,2569,905],{},[267,2571,2572],{"class":269,"line":288},[267,2573,910],{},[267,2575,2576],{"class":269,"line":294},[267,2577,2578],{},"    return CupertinoSegmentedControl(\n",[267,2580,2581],{"class":269,"line":300},[267,2582,2583],{},"      groupValue: _value,\n",[267,2585,2586],{"class":269,"line":306},[267,2587,2588],{},"      onValueChanged: (String? value) {\n",[267,2590,2591],{"class":269,"line":312},[267,2592,2593],{},"        setState(() {\n",[267,2595,2596],{"class":269,"line":317},[267,2597,2598],{},"            _value = value!;\n",[267,2600,2601],{"class":269,"line":323},[267,2602,2603],{},"        });\n",[267,2605,2606],{"class":269,"line":328},[267,2607,1810],{},[267,2609,2610],{"class":269,"line":333},[267,2611,2612],{},"      children: const {\n",[267,2614,2615],{"class":269,"line":953},[267,2616,2617],{},"        'Yep': Padding(\n",[267,2619,2620],{"class":269,"line":959},[267,2621,2622],{},"          padding: EdgeInsets.symmetric(horizontal: 10),\n",[267,2624,2625],{"class":269,"line":965},[267,2626,2627],{},"          child: Text('Yep'),\n",[267,2629,2630],{"class":269,"line":971},[267,2631,962],{},[267,2633,2634],{"class":269,"line":977},[267,2635,2636],{},"        'Nope': Padding(\n",[267,2638,2639],{"class":269,"line":983},[267,2640,2622],{},[267,2642,2643],{"class":269,"line":989},[267,2644,2645],{},"          child: Text('Nope'),\n",[267,2647,2648],{"class":269,"line":994},[267,2649,962],{},[267,2651,2652],{"class":269,"line":1000},[267,2653,1810],{},[267,2655,2656],{"class":269,"line":1006},[267,2657,1003],{},[267,2659,2660],{"class":269,"line":1012},[267,2661,1009],{},[267,2663,2664],{"class":269,"line":2175},[267,2665,451],{},[116,2667,2668,800,2670,468,2672,2483],{},[233,2669,799],{},[127,2671,2479],{},[127,2673,2674],{},"MaybeSegmented",[116,2676,2677,2678,257],{},"And, of course, here it is in action (",[233,2679,2680],{},"figure 5.16",[116,2682,2683],{},[339,2684],{"alt":2685,"src":2686},"Figure 5.16: A Cupertino segmented control","\u002Fimages\u002Fflutter-for-jobseekers\u002Ffigure-5-16.png",[344,2688,2689],{},[116,2690,2691],{},"Figure 5.16: A Cupertino segmented control.",[116,2693,2694,2695,2700,2701,2703,2704,257],{},"There is even an iOS 13-style version, where the selector slides from option to option. This is, appropriately, a ",[111,2696,2697],{},[127,2698,2699],{},"CupertinoSlidingSegmentedControl",", and it can be used interchangeably with the ",[127,2702,2551],{},", like the following (",[233,2705,2706],{},"figure 5.17",[259,2708,2710],{"className":261,"code":2709,"language":263,"meta":106,"style":106},"class _MaybeState extends State\u003CMaybe> {\n  String _value = \"Yep\";\n  @override\n  Widget build(BuildContext context) {\n    return CupertinoSlidingSegmentedControl(\n      groupValue: _value,\n      onValueChanged: (String? value) {\n        setState(() {\n            _value = value!;\n        });\n      },\n      children: const {\n        'Yep': Padding(\n          padding: EdgeInsets.symmetric(horizontal: 10),\n          child: Text('Yep'),\n        ),\n        'Nope': Padding(\n          padding: EdgeInsets.symmetric(horizontal: 10),\n          child: Text('Nope'),\n        ),\n      },\n    );\n  }\n}\n",[127,2711,2712,2716,2720,2724,2728,2733,2737,2741,2745,2749,2753,2757,2761,2765,2769,2773,2777,2781,2785,2789,2793,2797,2801,2805],{"__ignoreMap":106},[267,2713,2714],{"class":269,"line":270},[267,2715,2326],{},[267,2717,2718],{"class":269,"line":276},[267,2719,2331],{},[267,2721,2722],{"class":269,"line":282},[267,2723,905],{},[267,2725,2726],{"class":269,"line":288},[267,2727,910],{},[267,2729,2730],{"class":269,"line":294},[267,2731,2732],{},"    return CupertinoSlidingSegmentedControl(\n",[267,2734,2735],{"class":269,"line":300},[267,2736,2583],{},[267,2738,2739],{"class":269,"line":306},[267,2740,2588],{},[267,2742,2743],{"class":269,"line":312},[267,2744,2593],{},[267,2746,2747],{"class":269,"line":317},[267,2748,2598],{},[267,2750,2751],{"class":269,"line":323},[267,2752,2603],{},[267,2754,2755],{"class":269,"line":328},[267,2756,1810],{},[267,2758,2759],{"class":269,"line":333},[267,2760,2612],{},[267,2762,2763],{"class":269,"line":953},[267,2764,2617],{},[267,2766,2767],{"class":269,"line":959},[267,2768,2622],{},[267,2770,2771],{"class":269,"line":965},[267,2772,2627],{},[267,2774,2775],{"class":269,"line":971},[267,2776,962],{},[267,2778,2779],{"class":269,"line":977},[267,2780,2636],{},[267,2782,2783],{"class":269,"line":983},[267,2784,2622],{},[267,2786,2787],{"class":269,"line":989},[267,2788,2645],{},[267,2790,2791],{"class":269,"line":994},[267,2792,962],{},[267,2794,2795],{"class":269,"line":1000},[267,2796,1810],{},[267,2798,2799],{"class":269,"line":1006},[267,2800,1003],{},[267,2802,2803],{"class":269,"line":1012},[267,2804,1009],{},[267,2806,2807],{"class":269,"line":2175},[267,2808,451],{},[116,2810,2811,800,2813,468,2815,2483],{},[233,2812,799],{},[127,2814,2479],{},[127,2816,2817],{},"MaybeSliding",[116,2819,2820],{},[339,2821],{"alt":2822,"src":2823},"Figure 5.17: The Cupertino sliding segmented control","\u002Fimages\u002Fflutter-for-jobseekers\u002Ffigure-5-17.png",[344,2825,2826],{},[116,2827,2828],{},"Figure 5.17: The Cupertino sliding segmented control.",[241,2830,2832],{"id":2831},"checkboxes","Checkboxes",[116,2834,2835,2836,820,2838,2841],{},"Finally, let us discuss checkboxes and selecting multiple items. The ",[127,2837,216],{},[127,2839,2840],{},"Checkbox"," widget, which we are all familiar with.",[120,2843,2844,2845,2848,2849,2852],{},"What you might not expect is that there are ",[111,2846,2847],{},"three possible states"," for a checkbox, including checked, unchecked, and null. The third of those is only available when the ",[127,2850,2851],{},"tristate: true"," property is set.",[116,2854,2855,2856,257],{},"Here is an example of the three different states (",[233,2857,2858],{},"figure 5.18",[259,2860,2862],{"className":261,"code":2861,"language":263,"meta":106,"style":106},"class _CheckboxExampleState extends State\u003CCheckboxExample> {\n  bool checkOneIsChecked = true;\n  bool checkTwoIsChecked = false;\n  bool? checkThreeIsChecked;\n  @override\n  Widget build(BuildContext context) {\n    return Row(\n      mainAxisAlignment: MainAxisAlignment.center,\n      children: [\n        Checkbox(\n          value: checkOneIsChecked,\n          onChanged: (bool? value) {\n            setState(() {\n                checkOneIsChecked = value!;\n            });\n          },\n        ),\n        Checkbox(\n          value: checkTwoIsChecked,\n          onChanged: (bool? value) {\n            setState(() {\n                checkTwoIsChecked = value!;\n            });\n          },\n        ),\n        Checkbox(\n          tristate: true,\n          value: checkThreeIsChecked,\n          onChanged: (bool? value) {\n            setState(() {\n                checkThreeIsChecked = value;\n            });\n          },\n        ),\n      ],\n    );\n  }\n}\n",[127,2863,2864,2869,2874,2879,2884,2888,2892,2897,2902,2907,2912,2917,2922,2927,2932,2937,2942,2946,2950,2955,2959,2963,2968,2972,2976,2980,2984,2989,2994,2998,3002,3007,3011,3015,3019,3023,3027,3031],{"__ignoreMap":106},[267,2865,2866],{"class":269,"line":270},[267,2867,2868],{},"class _CheckboxExampleState extends State\u003CCheckboxExample> {\n",[267,2870,2871],{"class":269,"line":276},[267,2872,2873],{},"  bool checkOneIsChecked = true;\n",[267,2875,2876],{"class":269,"line":282},[267,2877,2878],{},"  bool checkTwoIsChecked = false;\n",[267,2880,2881],{"class":269,"line":288},[267,2882,2883],{},"  bool? checkThreeIsChecked;\n",[267,2885,2886],{"class":269,"line":294},[267,2887,905],{},[267,2889,2890],{"class":269,"line":300},[267,2891,910],{},[267,2893,2894],{"class":269,"line":306},[267,2895,2896],{},"    return Row(\n",[267,2898,2899],{"class":269,"line":312},[267,2900,2901],{},"      mainAxisAlignment: MainAxisAlignment.center,\n",[267,2903,2904],{"class":269,"line":317},[267,2905,2906],{},"      children: [\n",[267,2908,2909],{"class":269,"line":323},[267,2910,2911],{},"        Checkbox(\n",[267,2913,2914],{"class":269,"line":328},[267,2915,2916],{},"          value: checkOneIsChecked,\n",[267,2918,2919],{"class":269,"line":333},[267,2920,2921],{},"          onChanged: (bool? value) {\n",[267,2923,2924],{"class":269,"line":953},[267,2925,2926],{},"            setState(() {\n",[267,2928,2929],{"class":269,"line":959},[267,2930,2931],{},"                checkOneIsChecked = value!;\n",[267,2933,2934],{"class":269,"line":965},[267,2935,2936],{},"            });\n",[267,2938,2939],{"class":269,"line":971},[267,2940,2941],{},"          },\n",[267,2943,2944],{"class":269,"line":977},[267,2945,962],{},[267,2947,2948],{"class":269,"line":983},[267,2949,2911],{},[267,2951,2952],{"class":269,"line":989},[267,2953,2954],{},"          value: checkTwoIsChecked,\n",[267,2956,2957],{"class":269,"line":994},[267,2958,2921],{},[267,2960,2961],{"class":269,"line":1000},[267,2962,2926],{},[267,2964,2965],{"class":269,"line":1006},[267,2966,2967],{},"                checkTwoIsChecked = value!;\n",[267,2969,2970],{"class":269,"line":1012},[267,2971,2936],{},[267,2973,2974],{"class":269,"line":2175},[267,2975,2941],{},[267,2977,2978],{"class":269,"line":2181},[267,2979,962],{},[267,2981,2982],{"class":269,"line":2187},[267,2983,2911],{},[267,2985,2986],{"class":269,"line":2193},[267,2987,2988],{},"          tristate: true,\n",[267,2990,2991],{"class":269,"line":2199},[267,2992,2993],{},"          value: checkThreeIsChecked,\n",[267,2995,2996],{"class":269,"line":2204},[267,2997,2921],{},[267,2999,3000],{"class":269,"line":2210},[267,3001,2926],{},[267,3003,3004],{"class":269,"line":2216},[267,3005,3006],{},"                checkThreeIsChecked = value;\n",[267,3008,3009],{"class":269,"line":2222},[267,3010,2936],{},[267,3012,3013],{"class":269,"line":2228},[267,3014,2941],{},[267,3016,3017],{"class":269,"line":2234},[267,3018,962],{},[267,3020,3021],{"class":269,"line":2240},[267,3022,997],{},[267,3024,3025],{"class":269,"line":2245},[267,3026,1003],{},[267,3028,3029],{"class":269,"line":2250},[267,3030,1009],{},[267,3032,3034],{"class":269,"line":3033},38,[267,3035,451],{},[116,3037,3038,800,3040],{},[233,3039,799],{},[127,3041,3042],{},"examples\u002Fflutter_for_jobseekers_ch05\u002Flib\u002Fselection\u002Fcheckbox_example.dart",[116,3044,3045],{},[339,3046],{"alt":3047,"src":3048},"Figure 5.18: Checked, unchecked, and tri-state (null) checkboxes","\u002Fimages\u002Fflutter-for-jobseekers\u002Ffigure-5-18.png",[344,3050,3051],{},[116,3052,3053],{},"Figure 5.18: Checked, unchecked, and tri-state (null) checkboxes.",[241,3055,3057],{"id":3056},"switches","Switches",[116,3059,3060,3061,3066,3067,257],{},"Checkboxes might feel somewhat out of place for iOS users, so consider offering them the option of a ",[111,3062,3063],{},[127,3064,3065],{},"CupertinoSwitch",", instead (",[233,3068,3069],{},"figure 5.19",[259,3071,3073],{"className":261,"code":3072,"language":263,"meta":106,"style":106},"class _SwitchExampleState extends State\u003CSwitchExample> {\n  bool switchOne = true;\n  bool switchTwo = false;\n  @override\n  Widget build(BuildContext context) {\n    return Row(\n      mainAxisAlignment: MainAxisAlignment.center,\n      children: [\n        CupertinoSwitch(\n          value: switchOne,\n          onChanged: (bool? value) {\n            setState(() {\n              switchOne = value!;\n            });\n          },\n        ),\n        CupertinoSwitch(\n          value: switchTwo,\n          onChanged: (bool? value) {\n            setState(() {\n              switchTwo = value!;\n            });\n          },\n        ),\n      ],\n    );\n  }\n}\n",[127,3074,3075,3080,3085,3090,3094,3098,3102,3106,3110,3115,3120,3124,3128,3133,3137,3141,3145,3149,3154,3158,3162,3167,3171,3175,3179,3183,3187,3191],{"__ignoreMap":106},[267,3076,3077],{"class":269,"line":270},[267,3078,3079],{},"class _SwitchExampleState extends State\u003CSwitchExample> {\n",[267,3081,3082],{"class":269,"line":276},[267,3083,3084],{},"  bool switchOne = true;\n",[267,3086,3087],{"class":269,"line":282},[267,3088,3089],{},"  bool switchTwo = false;\n",[267,3091,3092],{"class":269,"line":288},[267,3093,905],{},[267,3095,3096],{"class":269,"line":294},[267,3097,910],{},[267,3099,3100],{"class":269,"line":300},[267,3101,2896],{},[267,3103,3104],{"class":269,"line":306},[267,3105,2901],{},[267,3107,3108],{"class":269,"line":312},[267,3109,2906],{},[267,3111,3112],{"class":269,"line":317},[267,3113,3114],{},"        CupertinoSwitch(\n",[267,3116,3117],{"class":269,"line":323},[267,3118,3119],{},"          value: switchOne,\n",[267,3121,3122],{"class":269,"line":328},[267,3123,2921],{},[267,3125,3126],{"class":269,"line":333},[267,3127,2926],{},[267,3129,3130],{"class":269,"line":953},[267,3131,3132],{},"              switchOne = value!;\n",[267,3134,3135],{"class":269,"line":959},[267,3136,2936],{},[267,3138,3139],{"class":269,"line":965},[267,3140,2941],{},[267,3142,3143],{"class":269,"line":971},[267,3144,962],{},[267,3146,3147],{"class":269,"line":977},[267,3148,3114],{},[267,3150,3151],{"class":269,"line":983},[267,3152,3153],{},"          value: switchTwo,\n",[267,3155,3156],{"class":269,"line":989},[267,3157,2921],{},[267,3159,3160],{"class":269,"line":994},[267,3161,2926],{},[267,3163,3164],{"class":269,"line":1000},[267,3165,3166],{},"              switchTwo = value!;\n",[267,3168,3169],{"class":269,"line":1006},[267,3170,2936],{},[267,3172,3173],{"class":269,"line":1012},[267,3174,2941],{},[267,3176,3177],{"class":269,"line":2175},[267,3178,962],{},[267,3180,3181],{"class":269,"line":2181},[267,3182,997],{},[267,3184,3185],{"class":269,"line":2187},[267,3186,1003],{},[267,3188,3189],{"class":269,"line":2193},[267,3190,1009],{},[267,3192,3193],{"class":269,"line":2199},[267,3194,451],{},[116,3196,3197,800,3199,468,3202,3205,3206,2483],{},[233,3198,799],{},[127,3200,3201],{},"examples\u002Fflutter_for_jobseekers_ch05\u002Flib\u002Fselection\u002Fswitch_example.dart",[127,3203,3204],{},"SwitchExample(material: true)"," swaps in the Material ",[127,3207,3208],{},"Switch",[116,3210,3211],{},[339,3212],{"alt":3213,"src":3214},"Figure 5.19: An example of the CupertinoSwitch","\u002Fimages\u002Fflutter-for-jobseekers\u002Ffigure-5-19.png",[344,3216,3217],{},[116,3218,3219],{},"Figure 5.19: An example of the CupertinoSwitch.",[400,3221,3222,3223,3225,3226,3228,3229,3233,3234,3236,3237,527],{},"Of course, if you like the idea of a ",[127,3224,3065],{}," and you find yourself wanting a ",[127,3227,216],{}," variant, simply swap it out for the ",[111,3230,3231],{},[127,3232,3208],{}," widget to get that ",[127,3235,216],{}," style (",[233,3238,3239],{},"figure 5.20",[116,3241,3242],{},[339,3243],{"alt":3244,"src":3245},"Figure 5.20: The Material style switch","\u002Fimages\u002Fflutter-for-jobseekers\u002Ffigure-5-20.png",[344,3247,3248],{},[116,3249,3250],{},"Figure 5.20: The Material style switch.",[154,3252,184],{"id":3253},"sliders",[116,3255,3256,3257,3260],{},"Occasionally, it might make sense to offer the user the option to pick a number from a range. A common way to do this is by allowing the user to drag a slider. The Material ",[127,3258,3259],{},"Slider"," widget has plenty of options available:",[1432,3262,3263,3268,3273],{},[1435,3264,3267],{"name":3265,"type":3266},"min \u002F max","double","Set the lowest and highest values of the range.",[1435,3269,3272],{"name":3270,"type":3271},"divisions","int","Adds stops along the slider.",[1435,3274,3276],{"name":518,"type":3275},"String","Adds a label above the slider when it is being dragged.",[116,3278,3279,3280,257],{},"Here is the Material slider (",[233,3281,3282],{},"figure 5.21",[259,3284,3286],{"className":261,"code":3285,"language":263,"meta":106,"style":106},"class _SliderExampleState extends State\u003CSliderExample> {\n  double _currentSliderValue = 20;\n  @override\n  Widget build(BuildContext context) {\n    return Center(\n      child: Slider(\n        value: _currentSliderValue,\n        max: 100,\n        divisions: 5,\n        label: _currentSliderValue.round().toString(),\n        onChanged: (double value) {\n          setState(() {\n              _currentSliderValue = value;\n          });\n        },\n      ),\n    );\n  }\n}\n",[127,3287,3288,3293,3298,3302,3306,3311,3316,3321,3326,3331,3336,3341,3346,3351,3356,3361,3365,3369,3373],{"__ignoreMap":106},[267,3289,3290],{"class":269,"line":270},[267,3291,3292],{},"class _SliderExampleState extends State\u003CSliderExample> {\n",[267,3294,3295],{"class":269,"line":276},[267,3296,3297],{},"  double _currentSliderValue = 20;\n",[267,3299,3300],{"class":269,"line":282},[267,3301,905],{},[267,3303,3304],{"class":269,"line":288},[267,3305,910],{},[267,3307,3308],{"class":269,"line":294},[267,3309,3310],{},"    return Center(\n",[267,3312,3313],{"class":269,"line":300},[267,3314,3315],{},"      child: Slider(\n",[267,3317,3318],{"class":269,"line":306},[267,3319,3320],{},"        value: _currentSliderValue,\n",[267,3322,3323],{"class":269,"line":312},[267,3324,3325],{},"        max: 100,\n",[267,3327,3328],{"class":269,"line":317},[267,3329,3330],{},"        divisions: 5,\n",[267,3332,3333],{"class":269,"line":323},[267,3334,3335],{},"        label: _currentSliderValue.round().toString(),\n",[267,3337,3338],{"class":269,"line":328},[267,3339,3340],{},"        onChanged: (double value) {\n",[267,3342,3343],{"class":269,"line":333},[267,3344,3345],{},"          setState(() {\n",[267,3347,3348],{"class":269,"line":953},[267,3349,3350],{},"              _currentSliderValue = value;\n",[267,3352,3353],{"class":269,"line":959},[267,3354,3355],{},"          });\n",[267,3357,3358],{"class":269,"line":965},[267,3359,3360],{},"        },\n",[267,3362,3363],{"class":269,"line":971},[267,3364,1712],{},[267,3366,3367],{"class":269,"line":977},[267,3368,1003],{},[267,3370,3371],{"class":269,"line":983},[267,3372,1009],{},[267,3374,3375],{"class":269,"line":989},[267,3376,451],{},[116,3378,3379],{},[339,3380],{"alt":3381,"src":3382},"Figure 5.21: The default Material slider state (top) and the active state when dragging (bottom)","\u002Fimages\u002Fflutter-for-jobseekers\u002Ffigure-5-21.png",[344,3384,3385],{},[116,3386,3387],{},"Figure 5.21: The default Material slider state (top) and the active state when dragging (bottom).",[116,3389,3390,3391,3393,3394,3397,3398,257],{},"Although the ",[127,3392,220],{}," variant ",[111,3395,3396],{},"lacks the label",", many of the options are the same (",[233,3399,3400],{},"figure 5.22",[259,3402,3404],{"className":261,"code":3403,"language":263,"meta":106,"style":106},"class _SliderExampleState extends State\u003CSliderExample> {\n  double _currentSliderValue = 20;\n  @override\n  Widget build(BuildContext context) {\n    return Center(\n      child: CupertinoSlider(\n        value: _currentSliderValue,\n        max: 100,\n        divisions: 5,\n        onChanged: (double value) {\n          setState(() {\n            _currentSliderValue = value;\n          });\n        },\n      ),\n    );\n  }\n}\n",[127,3405,3406,3410,3414,3418,3422,3426,3431,3435,3439,3443,3447,3451,3456,3460,3464,3468,3472,3476],{"__ignoreMap":106},[267,3407,3408],{"class":269,"line":270},[267,3409,3292],{},[267,3411,3412],{"class":269,"line":276},[267,3413,3297],{},[267,3415,3416],{"class":269,"line":282},[267,3417,905],{},[267,3419,3420],{"class":269,"line":288},[267,3421,910],{},[267,3423,3424],{"class":269,"line":294},[267,3425,3310],{},[267,3427,3428],{"class":269,"line":300},[267,3429,3430],{},"      child: CupertinoSlider(\n",[267,3432,3433],{"class":269,"line":306},[267,3434,3320],{},[267,3436,3437],{"class":269,"line":312},[267,3438,3325],{},[267,3440,3441],{"class":269,"line":317},[267,3442,3330],{},[267,3444,3445],{"class":269,"line":323},[267,3446,3340],{},[267,3448,3449],{"class":269,"line":328},[267,3450,3345],{},[267,3452,3453],{"class":269,"line":333},[267,3454,3455],{},"            _currentSliderValue = value;\n",[267,3457,3458],{"class":269,"line":953},[267,3459,3355],{},[267,3461,3462],{"class":269,"line":959},[267,3463,3360],{},[267,3465,3466],{"class":269,"line":965},[267,3467,1712],{},[267,3469,3470],{"class":269,"line":971},[267,3471,1003],{},[267,3473,3474],{"class":269,"line":977},[267,3475,1009],{},[267,3477,3478],{"class":269,"line":983},[267,3479,451],{},[116,3481,3482,800,3484,468,3487,217,3490,2483],{},[233,3483,799],{},[127,3485,3486],{},"examples\u002Fflutter_for_jobseekers_ch05\u002Flib\u002Fsliders\u002Fslider_example.dart",[127,3488,3489],{},"SliderExample",[127,3491,3492],{},"CupertinoSliderExample",[116,3494,3495],{},[339,3496],{"alt":3497,"src":3498},"Figure 5.22: The Cupertino slider","\u002Fimages\u002Fflutter-for-jobseekers\u002Ffigure-5-22.png",[344,3500,3501],{},[116,3502,3503],{},"Figure 5.22: The Cupertino slider.",[154,3505,187],{"id":3506},"forms-and-formfields",[116,3508,3509,3510,3513,3514,3516],{},"If you would like to collect a bunch of input from a user all at once and submit it to a backend system for processing, it makes sense to group the inputs together in a ",[127,3511,3512],{},"Form",". Wrapping input fields in a ",[127,3515,3512],{}," widget is not required, but doing so has some advantages, for example:",[159,3518,3519,3524,3529],{},[162,3520,3523],{"icon":3521,"title":3522},"i-lucide-rotate-ccw","Reset all inputs","It opens the possibility of resetting all inputs with a single action.",[162,3525,3528],{"icon":3526,"title":3527},"i-lucide-list-checks","Validate together","Validating all the text fields simultaneously.",[162,3530,3533],{"icon":3531,"title":3532},"i-lucide-save","Save and reset state","The state of the form can be both saved and reset.",[241,3535,3537],{"id":3536},"the-form-key","The form key",[116,3539,3540,3541,3544,3545,3550,3551,3554,3555,3557,3558,3561],{},"The first thing you will need for your form is a ",[127,3542,3543],{},"Key",". In this case, you will want to use a ",[111,3546,3547],{},[127,3548,3549],{},"GlobalKey"," for the form key. Assign this to a variable outside of the ",[127,3552,3553],{},"build"," method, then set the ",[127,3556,3512],{},"'s ",[127,3559,3560],{},"key"," property to the variable.",[116,3563,3564,3565,3567,3568,3571,3572,3575,3576,3578,3579,3582,3583,3586,3587,114],{},"Now that you have got a ",[127,3566,3512],{}," with an associated key, you can check to see if all elements in the form are valid using ",[127,3569,3570],{},"_formKey.currentState!.validate()"," or reset the form back to its default state using ",[127,3573,3574],{},"_formKey.currentState!.reset()",". Validating the ",[127,3577,3512],{}," will run the ",[127,3580,3581],{},"validator"," on each ",[127,3584,3585],{},"FormField"," which is a descendant of the ",[127,3588,3512],{},[241,3590,3585],{"id":3591},"formfield",[116,3593,3594,3595,3597,3598,3600,3601,3603,3604,3606,3607,3612],{},"This brings us to the ",[127,3596,3585],{}," and validators. A ",[127,3599,3585],{},", when used inside a ",[127,3602,3512],{},", makes it easier to do things like save the state of the ",[127,3605,3585],{},". When this happens, the ",[111,3608,3609],{},[127,3610,3611],{},"onSaved"," callback is run on the field. If you have ever used an application that automatically saves your changes as soon as you are done editing a field (for example, typing your name into a field and the changes are saved automatically as soon as you tap out of the input), this is the same sort of mechanism.",[400,3614,3615,3616,3618,3619,3621,3622,3624,3625,384,3627,3629,3630,3632,3633,3639,3640,3643,3644,3646,3647,114],{},"The save, reset, and validation of a ",[127,3617,3585],{}," is not bound explicitly to the ",[127,3620,3585],{}," being a descendant of a ",[127,3623,3512],{},". In fact, you could attach a ",[127,3626,3549],{},[127,3628,3585],{}," itself and perform these same actions. What makes the ",[127,3631,3512],{}," powerful is that ",[111,3634,3635,3636,3638],{},"only a single ",[127,3637,3549],{}," is needed"," to save, reset, and validate ",[233,3641,3642],{},"all"," the ancestor ",[127,3645,3585],{}," widgets of that ",[127,3648,3512],{},[120,3650,3651,3653,3654,3656,3657,3659,3660,3663],{},[111,3652,1026],{}," the ",[127,3655,3585],{}," widgets a ",[127,3658,3512],{}," acts on are its ",[111,3661,3662],{},"descendants",", not its ancestors: \"Call methods on FormState to save, reset, or validate each FormField that is a descendant of this Form\" (Flutter, n.d.-f).",[116,3665,3666,3668,3669,3671,3672,3677,3678,114],{},[127,3667,3585],{},", itself, is somewhat abstract since it is not bound to any specific type of input method. There are other widgets that leverage and extend the ",[127,3670,3585],{},", such as ",[111,3673,3674],{},[127,3675,3676],{},"TextFormField",", which you may recognize as being very similar to our earlier example of the ",[127,3679,823],{},[159,3681,3682,3690],{},[162,3683,3684,3685,217,3687,3689],{"icon":46,"title":823},"The standard text input; ",[127,3686,3611],{},[127,3688,3581],{}," are unavailable.",[162,3691,3693,3694,3696,3697,800,3699,3704,3705,217,3707,3709,3710,114],{"icon":3692,"title":3676},"i-lucide-square-check","In fact, these widgets are identical, save that the ",[127,3695,3676],{}," is simply a ",[127,3698,823],{},[111,3700,3701,3702],{},"wrapped in a ",[127,3703,3585],{},". This opens for us the possibility of using the ",[127,3706,3611],{},[127,3708,3581],{}," methods. In addition, we are also able to set a default value inside the field, which we could not have done with a ",[127,3711,823],{},[241,3713,3715],{"id":3714},"validation","Validation",[116,3717,3718,3719,3721],{},"Now that we understand what a ",[127,3720,3676],{}," is let us look at what validation is and how to put it to good use.",[120,3723,3724,3725,3728],{},"Broadly speaking, ",[111,3726,3727],{},"input validation"," is the process of analyzing the input provided by a user and comparing it to a set of rules which will determine if the input is valid.",[116,3730,3731],{},"Let us look at an example. A user has been asked to enter their full legal name into a text field. An assumption you might make is that they will have both a given name and a surname. A validator on the input might check to make sure that at least two distinct words have been entered into the field. Here is what that could look like, in practice:",[259,3733,3735],{"className":261,"code":3734,"language":263,"meta":106,"style":106},"validator: (String? value) {\n  if (value.trim().split(' ').length \u003C 2) {\n    return \"Please enter your full, legal name.\";\n  }\n\n  return null;\n}\n",[127,3736,3737,3742,3747,3752,3756,3760,3765],{"__ignoreMap":106},[267,3738,3739],{"class":269,"line":270},[267,3740,3741],{},"validator: (String? value) {\n",[267,3743,3744],{"class":269,"line":276},[267,3745,3746],{},"  if (value.trim().split(' ').length \u003C 2) {\n",[267,3748,3749],{"class":269,"line":282},[267,3750,3751],{},"    return \"Please enter your full, legal name.\";\n",[267,3753,3754],{"class":269,"line":288},[267,3755,1009],{},[267,3757,3758],{"class":269,"line":294},[267,3759,1205],{"emptyLinePlaceholder":1204},[267,3761,3762],{"class":269,"line":300},[267,3763,3764],{},"  return null;\n",[267,3766,3767],{"class":269,"line":306},[267,3768,451],{},[120,3770,3771,3773,3774,3776,3777,3780,3781,3784,3785,3787,3788,3791,3792,3795,3796,3799],{},[111,3772,1026],{}," this validator does not compile under sound null safety: ",[127,3775,2510],{}," is a ",[127,3778,3779],{},"String?",", so ",[127,3782,3783],{},"value.trim()"," is not allowed until ",[127,3786,2510],{}," has been checked for ",[127,3789,3790],{},"null",". The signup form below adds that check (",[127,3793,3794],{},"if (value == null || value.isEmpty)",") before ",[127,3797,3798],{},"trim()",", which the runnable copy also uses.",[3801,3802,3804,3809,3812,3816,3819,3823,3826,3830],"steps",{"level":3803},"4",[3805,3806,3808],"h4",{"id":3807},"trim","Trim",[116,3810,3811],{},"We start by trimming any whitespace from the input because failing to do so would cause validation to pass as soon as the user enters some string and presses the spacebar.",[3805,3813,3815],{"id":3814},"split","Split",[116,3817,3818],{},"Next, we split the input into words, using a space as a delimiter. This takes each word that is separated by a space and adds it to a list.",[3805,3820,3822],{"id":3821},"count","Count",[116,3824,3825],{},"Then we check to see that there are at least two items in the list.",[3805,3827,3829],{"id":3828},"return","Return",[116,3831,3832,3833,3835],{},"If there are, validation was successful, so we return a ",[127,3834,3790],{}," value. If validation fails, we return a message that describes the validation problem to the user.",[1147,3837,3838,3839,3842,3843,3845,3846,3849,3850,3853],{},"Simply supplying a validator to a field is ",[111,3840,3841],{},"not enough to trigger validation"," of that field. To validate the field, you could call the validate method on the ",[127,3844,3549],{}," when the submit button is pressed, as an example. If you want to automatically validate a field while the user is inputting data, you can supply an ",[127,3847,3848],{},"AutovalidateMode"," object to the field via its ",[127,3851,3852],{},"autovalidateMode"," parameter. Whichever option you choose, just remember that validation does not happen automatically by default.",[154,3855,190],{"id":3856},"building-a-simple-signup-form",[116,3858,3859],{},"Let us use some of what we have learned in the last section to build a simple signup form.",[159,3861,3862,3867,3872,3877],{},[162,3863,3866],{"icon":3864,"title":3865},"i-lucide-user","Name","We want to capture their name.",[162,3868,3871],{"icon":3869,"title":3870},"i-lucide-at-sign","E-mail address","We also want to do some basic validation on the email address to make sure it is properly formatted as an e-mail address.",[162,3873,3876],{"icon":3874,"title":3875},"i-lucide-key-round","Password, twice","We will make sure to have them enter their password twice, so we can confirm that they got it right. Plus, we will want to make sure their password contains at least one special character and is at least eight characters long.",[162,3878,3881],{"icon":3879,"title":3880},"i-lucide-send","Signup button","Finally, we will have a signup button that is disabled until everything has been filled in and validates the form before submitting.",[120,3883,3884],{},"The chapter never shows how to keep the button disabled until every field is filled in; its code validates the form when the button is pressed. The assembled runnable copy follows the chapter's code, and so does the repository's complete form.",[241,3886,3888],{"id":3887},"the-form","The form",[116,3890,3891,3892,3894,3895,114],{},"The first thing we are going to need is a ",[111,3893,2315],{}," for our signup form. This will allow us to validate the input and display any errors without losing the user's input. Let us call our new widget ",[127,3896,143],{},[116,3898,3899,3900,3902,3903,3905,3906,3908,3909,3912],{},"We are going to need a ",[127,3901,3512],{}," that will group together all our inputs so we can validate them all at once. To do that, we will also need our form key. The ",[127,3904,3512],{}," requires a ",[127,3907,522],{},", so let us give it a ",[127,3910,3911],{},"Column"," where we will add our inputs later.",[259,3914,3916],{"className":261,"code":3915,"language":263,"meta":106,"style":106},"class SignUpForm extends StatefulWidget {\n  const SignUpForm({super.key});\n\n  @override\n  State\u003CSignUpForm> createState() => _SignUpFormState();\n}\n\nclass _SignUpFormState extends State\u003CSignUpForm> {\n  final GlobalKey\u003CFormState> _formKey = GlobalKey\u003CFormState>();\n\n  @override\n  Widget build(BuildContext context) {\n    return Form(\n      key: _formKey,\n      child: Column(\n        children: [],\n      ),\n    );\n  }\n}\n",[127,3917,3918,3923,3928,3932,3936,3941,3945,3949,3954,3959,3963,3967,3971,3976,3981,3986,3991,3995,3999,4003],{"__ignoreMap":106},[267,3919,3920],{"class":269,"line":270},[267,3921,3922],{},"class SignUpForm extends StatefulWidget {\n",[267,3924,3925],{"class":269,"line":276},[267,3926,3927],{},"  const SignUpForm({super.key});\n",[267,3929,3930],{"class":269,"line":282},[267,3931,1205],{"emptyLinePlaceholder":1204},[267,3933,3934],{"class":269,"line":288},[267,3935,905],{},[267,3937,3938],{"class":269,"line":294},[267,3939,3940],{},"  State\u003CSignUpForm> createState() => _SignUpFormState();\n",[267,3942,3943],{"class":269,"line":300},[267,3944,451],{},[267,3946,3947],{"class":269,"line":306},[267,3948,1205],{"emptyLinePlaceholder":1204},[267,3950,3951],{"class":269,"line":312},[267,3952,3953],{},"class _SignUpFormState extends State\u003CSignUpForm> {\n",[267,3955,3956],{"class":269,"line":317},[267,3957,3958],{},"  final GlobalKey\u003CFormState> _formKey = GlobalKey\u003CFormState>();\n",[267,3960,3961],{"class":269,"line":323},[267,3962,1205],{"emptyLinePlaceholder":1204},[267,3964,3965],{"class":269,"line":328},[267,3966,905],{},[267,3968,3969],{"class":269,"line":333},[267,3970,910],{},[267,3972,3973],{"class":269,"line":953},[267,3974,3975],{},"    return Form(\n",[267,3977,3978],{"class":269,"line":959},[267,3979,3980],{},"      key: _formKey,\n",[267,3982,3983],{"class":269,"line":965},[267,3984,3985],{},"      child: Column(\n",[267,3987,3988],{"class":269,"line":971},[267,3989,3990],{},"        children: [],\n",[267,3992,3993],{"class":269,"line":977},[267,3994,1712],{},[267,3996,3997],{"class":269,"line":983},[267,3998,1003],{},[267,4000,4001],{"class":269,"line":989},[267,4002,1009],{},[267,4004,4005],{"class":269,"line":994},[267,4006,451],{},[241,4008,4010],{"id":4009},"text-inputs-and-the-button","Text inputs and the button",[116,4012,4013,4014,4016,4017,4020,4021,4023,4024,4026,4027,4029],{},"With that out of the way, we are ready to start adding our text inputs. Each will need its own ",[127,4015,1167],{},", and we can label them with a ",[127,4018,4019],{},"hintText"," by using the ",[127,4022,855],{}," parameter. Create your ",[127,4025,1167],{}," variables, just like you did with the form key, for each field and attach them to the proper field using the ",[127,4028,1183],{}," parameter. The following is an example key:",[259,4031,4033],{"className":261,"code":4032,"language":263,"meta":106,"style":106},"final TextEditingController _nameController = TextEditingController();\n",[127,4034,4035],{"__ignoreMap":106},[267,4036,4037],{"class":269,"line":270},[267,4038,4032],{},[116,4040,4041],{},"And here is an example of how to attach it to the input, then set the hint:",[259,4043,4045],{"className":261,"code":4044,"language":263,"meta":106,"style":106},"TextFormField(\n  controller: _nameController,\n  decoration: const InputDecoration(\n    hintText: 'Name',\n  ),\n),\n",[127,4046,4047,4052,4057,4062,4067,4071],{"__ignoreMap":106},[267,4048,4049],{"class":269,"line":270},[267,4050,4051],{},"TextFormField(\n",[267,4053,4054],{"class":269,"line":276},[267,4055,4056],{},"  controller: _nameController,\n",[267,4058,4059],{"class":269,"line":282},[267,4060,4061],{},"  decoration: const InputDecoration(\n",[267,4063,4064],{"class":269,"line":288},[267,4065,4066],{},"    hintText: 'Name',\n",[267,4068,4069],{"class":269,"line":294},[267,4070,790],{},[267,4072,4073],{"class":269,"line":300},[267,4074,291],{},[116,4076,4077,4078,4080],{},"We will also want a button at the bottom of the list that we can use to submit the form later. Feel free to use any kind of button you want, although this example will use the ",[127,4079,572],{}," with some padding to separate it from the preceding inputs.",[259,4082,4084],{"className":261,"code":4083,"language":263,"meta":106,"style":106},"Padding(\n  padding: const EdgeInsets.only(top: 20),\n  child: ElevatedButton(\n    onPressed: () {},\n    child: const Text('Sign Up'),\n  ),\n),\n",[127,4085,4086,4091,4096,4101,4106,4111,4115],{"__ignoreMap":106},[267,4087,4088],{"class":269,"line":270},[267,4089,4090],{},"Padding(\n",[267,4092,4093],{"class":269,"line":276},[267,4094,4095],{},"  padding: const EdgeInsets.only(top: 20),\n",[267,4097,4098],{"class":269,"line":282},[267,4099,4100],{},"  child: ElevatedButton(\n",[267,4102,4103],{"class":269,"line":288},[267,4104,4105],{},"    onPressed: () {},\n",[267,4107,4108],{"class":269,"line":294},[267,4109,4110],{},"    child: const Text('Sign Up'),\n",[267,4112,4113],{"class":269,"line":300},[267,4114,790],{},[267,4116,4117],{"class":269,"line":306},[267,4118,291],{},[116,4120,4121,4122,4125],{},"We now have a basic form (",[233,4123,4124],{},"figure 5.23","), but it is far from complete:",[116,4127,4128],{},[339,4129],{"alt":4130,"src":4131},"Figure 5.23: Our basic form layout","\u002Fimages\u002Fflutter-for-jobseekers\u002Ffigure-5-23.png",[344,4133,4134],{},[116,4135,4136],{},"Figure 5.23: Our basic form layout.",[241,4138,4140],{"id":4139},"validating-the-name","Validating the name",[116,4142,4143,4144,4146,4147,4149,4150,4152],{},"Since we know the name field is required, let us add a validator to it. As a reminder, the ",[127,4145,3581],{}," is a function that will run whenever we validate the form. It has a ",[127,4148,3779],{}," input and a ",[127,4151,3779],{}," return value.",[1432,4154,4155,4159],{},[1435,4156,4158],{"name":3790,"type":4157},"return value","Returned when the field is valid.",[1435,4160,4161],{"name":3275,"type":4157},"Displayed as the error message when validation fails.",[116,4163,4164],{},"Here is a very simple validator that checks to see if the user has entered any text in the field, then checks to see if they have entered their full name:",[259,4166,4168],{"className":261,"code":4167,"language":263,"meta":106,"style":106},"validator: (value) {\n  if (value == null || value.isEmpty) {\n    return 'Required';\n  }\n\n  if (value.trim().split(' ').length \u003C 2) {\n    return \"Please enter your full, legal name.\";\n  }\n\n  return null;\n},\n",[127,4169,4170,4175,4180,4185,4189,4193,4197,4201,4205,4209,4213],{"__ignoreMap":106},[267,4171,4172],{"class":269,"line":270},[267,4173,4174],{},"validator: (value) {\n",[267,4176,4177],{"class":269,"line":276},[267,4178,4179],{},"  if (value == null || value.isEmpty) {\n",[267,4181,4182],{"class":269,"line":282},[267,4183,4184],{},"    return 'Required';\n",[267,4186,4187],{"class":269,"line":288},[267,4188,1009],{},[267,4190,4191],{"class":269,"line":294},[267,4192,1205],{"emptyLinePlaceholder":1204},[267,4194,4195],{"class":269,"line":300},[267,4196,3746],{},[267,4198,4199],{"class":269,"line":306},[267,4200,3751],{},[267,4202,4203],{"class":269,"line":312},[267,4204,1009],{},[267,4206,4207],{"class":269,"line":317},[267,4208,1205],{"emptyLinePlaceholder":1204},[267,4210,4211],{"class":269,"line":323},[267,4212,3764],{},[267,4214,4215],{"class":269,"line":328},[267,4216,4217],{},"},\n",[116,4219,4220,4221,4223],{},"Of course, this code will never run unless we try to validate the form, so we have no way of testing that it works. Let us fix that now. We want to validate the form whenever we press the button, so let us add some code into the ",[127,4222,229],{}," argument that will call the form validation function.",[259,4225,4227],{"className":261,"code":4226,"language":263,"meta":106,"style":106},"onPressed: () {\n  if (_formKey.currentState!.validate()) {\n    \u002F\u002F Process the form data.\n  }\n},\n",[127,4228,4229,4234,4239,4244,4248],{"__ignoreMap":106},[267,4230,4231],{"class":269,"line":270},[267,4232,4233],{},"onPressed: () {\n",[267,4235,4236],{"class":269,"line":276},[267,4237,4238],{},"  if (_formKey.currentState!.validate()) {\n",[267,4240,4241],{"class":269,"line":282},[267,4242,4243],{},"    \u002F\u002F Process the form data.\n",[267,4245,4246],{"class":269,"line":288},[267,4247,1009],{},[267,4249,4250],{"class":269,"line":294},[267,4251,4217],{},[400,4253,4254],{},"Now you can test the validation of the name field by leaving it blank and pressing the button to see an error or by entering text and watching the error disappear when you push the button. You can also check to make sure the other validator is working by typing just your first name and pressing the button.",[241,4256,4258],{"id":4257},"validating-the-e-mail-address","Validating the e-mail address",[1147,4260,4261,4262,4265,4266,4268],{},"Validating email addresses is a ",[111,4263,4264],{},"far more difficult task"," than simply checking whether the input is empty. We must check for an ",[127,4267,1126],{}," symbol in the middle, with some sort of domain name on the right side. Plus, there are certain characters which are invalid in an e-mail address, like a semicolon. In short, e-mail address validation can become very complex, very quickly.",[116,4270,1504,4271,4274,4275,4278],{},[111,4272,4273],{},"regular expression"," (or ",[233,4276,4277],{},"Regex",", for short) is a pattern used to match character combinations in strings of text. Regular expressions are far outside the scope of this book, so you are encouraged to do some research on your own to understand them. At first glance, they can be terrifying and unapproachable, but with some practice, you will be able to break them down into smaller components that make far more sense.",[400,4280,4281,4282,4288],{},"The website ",[111,4283,4284],{},[132,4285,4286],{"href":4286,"rel":4287},"https:\u002F\u002Fregexr.com\u002F",[136]," has an excellent toolset for writing, testing, and understanding regular expressions. For our purposes, any necessary regular expressions will be provided here, but certainly take time to do research on the subject to set yourself apart. You'll be glad you did.",[116,4290,4291,4292,4295],{},"Using the power of regular expressions, we can create a pattern that ought to match ",[233,4293,4294],{},"most"," valid e-mail addresses:",[259,4297,4299],{"className":261,"code":4298,"language":263,"meta":106,"style":106},"RegExp emailValidator = RegExp(\n  r'^(([^\u003C>()[\\]\\\\.,;:\\s@\"]+(\\.[^\u003C>()[\\]\\\\.,;:\\s@\"]+)*)|(\".+\"))@((\\[[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\])|(([a-zA-Z\\-0-9]+\\.)+[a-zA-Z]{2,}))$',\n  caseSensitive: false,\n  multiLine: false,\n);\n",[127,4300,4301,4306,4311,4316,4321],{"__ignoreMap":106},[267,4302,4303],{"class":269,"line":270},[267,4304,4305],{},"RegExp emailValidator = RegExp(\n",[267,4307,4308],{"class":269,"line":276},[267,4309,4310],{},"  r'^(([^\u003C>()[\\]\\\\.,;:\\s@\"]+(\\.[^\u003C>()[\\]\\\\.,;:\\s@\"]+)*)|(\".+\"))@((\\[[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\])|(([a-zA-Z\\-0-9]+\\.)+[a-zA-Z]{2,}))$',\n",[267,4312,4313],{"class":269,"line":282},[267,4314,4315],{},"  caseSensitive: false,\n",[267,4317,4318],{"class":269,"line":288},[267,4319,4320],{},"  multiLine: false,\n",[267,4322,4323],{"class":269,"line":294},[267,4324,4325],{},");\n",[116,4327,4328],{},"Then, we just need to attach it to our e-mail field's validator function, like so:",[259,4330,4332],{"className":261,"code":4331,"language":263,"meta":106,"style":106},"validator: (value) {\n  if (value == null || value.isEmpty) {\n    return 'Required';\n  }\n  if (!emailValidator.hasMatch(value)) {\n    return 'Invalid email address';\n  }\n  return null;\n},\n",[127,4333,4334,4338,4342,4346,4350,4355,4360,4364,4368],{"__ignoreMap":106},[267,4335,4336],{"class":269,"line":270},[267,4337,4174],{},[267,4339,4340],{"class":269,"line":276},[267,4341,4179],{},[267,4343,4344],{"class":269,"line":282},[267,4345,4184],{},[267,4347,4348],{"class":269,"line":288},[267,4349,1009],{},[267,4351,4352],{"class":269,"line":294},[267,4353,4354],{},"  if (!emailValidator.hasMatch(value)) {\n",[267,4356,4357],{"class":269,"line":300},[267,4358,4359],{},"    return 'Invalid email address';\n",[267,4361,4362],{"class":269,"line":306},[267,4363,1009],{},[267,4365,4366],{"class":269,"line":312},[267,4367,3764],{},[267,4369,4370],{"class":269,"line":317},[267,4371,4217],{},[120,4373,4374,4375,4381,4382,4385],{},"When it comes to e-mail address validation, specifically, we generally would not want to validate by using a regular expression because of how monstrously complex proper e-mail address validation is, instead opting for a ",[111,4376,4377,4378],{},"package such as ",[127,4379,4380],{},"email_validator"," to do the work for us. We will talk more about packages in ",[233,4383,4384],{},"Chapter 6: Using 3rd Party Libraries and External Files",". For the purposes of illustration, however, the regular expression is sufficient.",[120,4387,4388,4390,4391,114],{},[111,4389,1026],{}," Chapter 6 is titled ",[233,4392,48],{},[241,4394,4396],{"id":4395},"validating-the-passwords","Validating the passwords",[116,4398,4399],{},"Next, let us tackle the password fields. We know that both fields need to be the same, so we only need to validate the actual input on one of them and make sure the second field matches it. There are a lot of valid special characters that we want to allow our users to use, so rather than listing all of them, let us use our old friend Regex to solve the problem.",[116,4401,4402,4403,4406,4407,4410,4411,4414,4415,4418],{},"If we consider ",[233,4404,4405],{},"normal"," characters to be all 26 characters of the Latin alphabet, along with the numbers 0 through 9, we can then consider ",[233,4408,4409],{},"special"," characters to be ",[233,4412,4413],{},"anything"," that is not a letter or a number. Luckily, a regular expression that matches all letters and numbers is very easy to write. Then, all we need to do is check that the value of the field ",[233,4416,4417],{},"is not"," matched by that regular expression, at which point we will know that we have a special character:",[259,4420,4422],{"className":261,"code":4421,"language":263,"meta":106,"style":106},"if (!value.contains(RegExp('[^A-Za-z0-9]'))) {\n  return 'Special character required';\n}\n",[127,4423,4424,4429,4434],{"__ignoreMap":106},[267,4425,4426],{"class":269,"line":270},[267,4427,4428],{},"if (!value.contains(RegExp('[^A-Za-z0-9]'))) {\n",[267,4430,4431],{"class":269,"line":276},[267,4432,4433],{},"  return 'Special character required';\n",[267,4435,4436],{"class":269,"line":282},[267,4437,451],{},[116,4439,4440],{},"We can add this check inside our validator alongside any other checks we want to perform, such as making sure the value is not empty. We also want to check our password to make sure it is at least eight characters long. See if you can figure out the test for that and check your work with the following code:",[259,4442,4444],{"className":261,"code":4443,"language":263,"meta":106,"style":106},"if (value.length \u003C 8) {\n  return \"Password is too short\";\n}\n",[127,4445,4446,4451,4456],{"__ignoreMap":106},[267,4447,4448],{"class":269,"line":270},[267,4449,4450],{},"if (value.length \u003C 8) {\n",[267,4452,4453],{"class":269,"line":276},[267,4454,4455],{},"  return \"Password is too short\";\n",[267,4457,4458],{"class":269,"line":282},[267,4459,451],{},[116,4461,4462,4463,4465],{},"It seems like we have got all the validation done for the password complexity requirements, so let us add a validator to the password confirmation field. Like with the other fields, we want to ensure this field has been filled out, so check for that first. Then, we can use the ",[127,4464,1167],{}," that we assigned to the password field to get its current value and compare it to the input on this field.",[259,4467,4469],{"className":261,"code":4468,"language":263,"meta":106,"style":106},"if (value != _passwordController.text) {\n  return \"Passwords do not match\";\n}\n",[127,4470,4471,4476,4481],{"__ignoreMap":106},[267,4472,4473],{"class":269,"line":270},[267,4474,4475],{},"if (value != _passwordController.text) {\n",[267,4477,4478],{"class":269,"line":276},[267,4479,4480],{},"  return \"Passwords do not match\";\n",[267,4482,4483],{"class":269,"line":282},[267,4484,451],{},[241,4486,4488],{"id":4487},"hiding-the-password","Hiding the password",[116,4490,4491],{},"To finish these fields off, we should specify a couple of additional parameters:",[1432,4493,4494,4499,4503,4507],{},[1435,4495,4498],{"name":4496,"type":4497},"obscureText","bool","Obscures the text.",[1435,4500,4502],{"name":4501,"type":4497},"autocorrect","Disables autocorrect.",[1435,4504,4506],{"name":4505,"type":4497},"enableSuggestions","Disables suggestions.",[1435,4508,4510],{"name":4509,"type":4497},"enableIMEPersonalizedLearning","Prevents the keyboard from learning about our password.",[116,4512,4513],{},"If you were so inclined, you could also add an icon to the field to toggle the password visibility. That would look something like the following:",[259,4515,4517],{"className":261,"code":4516,"language":263,"meta":106,"style":106},"TextFormField(\n  decoration: InputDecoration(\n    suffixIcon: IconButton(\n      onPressed: () {\n        setState(() {\n            _hidePassword = !_hidePassword;\n        });\n      },\n      icon: _hidePassword\n      ? const Icon(Icons.visibility)\n      : const Icon(Icons.visibility_off),\n    ),\n  ),\n  obscureText: _hidePassword,\n  autocorrect: false,\n  enableIMEPersonalizedLearning: false,\n  enableSuggestions: false,\n),\n",[127,4518,4519,4523,4528,4533,4538,4542,4547,4551,4555,4560,4565,4570,4574,4578,4583,4588,4593,4598],{"__ignoreMap":106},[267,4520,4521],{"class":269,"line":270},[267,4522,4051],{},[267,4524,4525],{"class":269,"line":276},[267,4526,4527],{},"  decoration: InputDecoration(\n",[267,4529,4530],{"class":269,"line":282},[267,4531,4532],{},"    suffixIcon: IconButton(\n",[267,4534,4535],{"class":269,"line":288},[267,4536,4537],{},"      onPressed: () {\n",[267,4539,4540],{"class":269,"line":294},[267,4541,2593],{},[267,4543,4544],{"class":269,"line":300},[267,4545,4546],{},"            _hidePassword = !_hidePassword;\n",[267,4548,4549],{"class":269,"line":306},[267,4550,2603],{},[267,4552,4553],{"class":269,"line":312},[267,4554,1810],{},[267,4556,4557],{"class":269,"line":317},[267,4558,4559],{},"      icon: _hidePassword\n",[267,4561,4562],{"class":269,"line":323},[267,4563,4564],{},"      ? const Icon(Icons.visibility)\n",[267,4566,4567],{"class":269,"line":328},[267,4568,4569],{},"      : const Icon(Icons.visibility_off),\n",[267,4571,4572],{"class":269,"line":333},[267,4573,1741],{},[267,4575,4576],{"class":269,"line":953},[267,4577,790],{},[267,4579,4580],{"class":269,"line":959},[267,4581,4582],{},"  obscureText: _hidePassword,\n",[267,4584,4585],{"class":269,"line":965},[267,4586,4587],{},"  autocorrect: false,\n",[267,4589,4590],{"class":269,"line":971},[267,4591,4592],{},"  enableIMEPersonalizedLearning: false,\n",[267,4594,4595],{"class":269,"line":977},[267,4596,4597],{},"  enableSuggestions: false,\n",[267,4599,4600],{"class":269,"line":983},[267,4601,291],{},[116,4603,4604,4605,4608],{},"Just make sure you have created a ",[127,4606,4607],{},"_hidePassword"," bool that you can reassign, then tapping the eye icon will toggle visibility of the field and change the icon.",[241,4610,4612],{"id":4611},"submitting-the-values","Submitting the values",[116,4614,4615],{},"To complete our signup form, we should do something with all the values we have collected once the form has been validated. Typically, you will pass these values to some sort of API or backend for processing, but we are just going to print them out to the debug console.",[116,4617,4618,4619,4622,4623,4625,4626,4629,4630,4633,4634,4637],{},"Find the ",[127,4620,4621],{},"if"," statement in your button's ",[127,4624,229],{}," argument, where we call the ",[127,4627,4628],{},"validate"," method on the form. Any values in our form that we try to ",[127,4631,4632],{},"print"," from inside this statement will be guaranteed to be considered ",[233,4635,4636],{},"valid",". Try printing the values from each of the fields, using the fields' controllers to get the value:",[259,4639,4641],{"className":261,"code":4640,"language":263,"meta":106,"style":106},"if (_formKey.currentState!.validate()) {\n  \u002F\u002F Process the form data.\n  debugPrint('Name: ${_nameController.text}');\n  debugPrint('Email Address: ${_emailController.text}');\n  debugPrint('Password: ${_passwordController.text}');\n}\n",[127,4642,4643,4648,4653,4658,4663,4668],{"__ignoreMap":106},[267,4644,4645],{"class":269,"line":270},[267,4646,4647],{},"if (_formKey.currentState!.validate()) {\n",[267,4649,4650],{"class":269,"line":276},[267,4651,4652],{},"  \u002F\u002F Process the form data.\n",[267,4654,4655],{"class":269,"line":282},[267,4656,4657],{},"  debugPrint('Name: ${_nameController.text}');\n",[267,4659,4660],{"class":269,"line":288},[267,4661,4662],{},"  debugPrint('Email Address: ${_emailController.text}');\n",[267,4664,4665],{"class":269,"line":294},[267,4666,4667],{},"  debugPrint('Password: ${_passwordController.text}');\n",[267,4669,4670],{"class":269,"line":300},[267,4671,451],{},[116,4673,4674],{},"Refer to the accompanying repository for the complete example of what we just built.",[116,4676,4677,800,4679,4682],{},[233,4678,799],{},[127,4680,4681],{},"examples\u002Fflutter_for_jobseekers_ch05\u002Flib\u002Fforms\u002Fsign_up_form.dart"," (the complete form, assembled from the fragments above)",[116,4684,4685,4687,4688,4690,4691,4694,4695,4698,4699,4702,4703,4706,4707,4709,4710,4713],{},[233,4686,124],{}," the repository's ",[127,4689,129],{}," has the same button padding as above (",[127,4692,4693],{},"EdgeInsets.only(top: 20.0)","). Where the chapter is silent, the runnable copy differs from it: the repository gives the confirmation field its own visibility toggle (",[127,4696,4697],{},"_hidePasswordConfirmation",") and the hint ",[233,4700,4701],{},"Password (confirmation)",", while the copy shares one toggle and uses ",[233,4704,4705],{},"Confirm Password","; the copy also adds ",[127,4708,1277],{}," and the e-mail keyboard type. The repository's e-mail ",[127,4711,4712],{},"RegExp"," lost its backslashes in conversion, so the copy keeps the pattern shown above.",[154,4715,4717],{"id":4716},"conclusion","Conclusion",[116,4719,4720],{},"Gathering information from users, whether that information is intent (such as by tapping a button) or direct input (such as typing their name into a field), is fundamental to creating any application. Validating that input is just as important, and you now have the tools to do just that.",[116,4722,4723,4724,217,4726,4728,4729,4732],{},"We looked at how ",[127,4725,220],{},[127,4727,216],{}," have different ways of accomplishing similar tasks, and you should now be thinking about building applications which ",[111,4730,4731],{},"feel at home to a user",", regardless of what type of device they are using. Later in the book, we will talk more about this in depth.",[116,4734,4735],{},"In the upcoming chapter, we will learn how to find and use packages in our application that are provided by the community. We will learn how to add assets such as images to our application, then learn how to access them throughout the application. We have got a lot to cover, so take a break: stretch, grab a cup of tea, and when you are ready, let's move on.",[154,4737,4739],{"id":4738},"questions","Questions",[4741,4742,4743,4751,4754,4760,4763,4766,4772,4775,4778,4784,4787,4790,4796,4799,4810,4816,4822,4827,4835,4843,4846],"ol",{},[170,4744,4745,4746,217,4748,4750],{},"What is the difference between ",[127,4747,216],{},[127,4749,220],{}," styled widgets, and when would you use one over the other?",[170,4752,4753],{},"What is a callback function?",[170,4755,4756,4757,4759],{},"What is the best way to add an icon to an ",[127,4758,467],{},"?",[170,4761,4762],{},"How do you style a button, including changing its color?",[170,4764,4765],{},"How do you disable a button?",[170,4767,4768,4769,4771],{},"What widget can you use to convert an arbitrary widget, such as a ",[127,4770,749],{},", into a button?",[170,4773,4774],{},"What widget can be used to detect a swipe gesture?",[170,4776,4777],{},"How do you change the type of keyboard presented to a user on a mobile device when they tap a text field? For example, how would you present the user with a keyboard which is more appropriate for numerical input?",[170,4779,4780,4781,4783],{},"What is a ",[127,4782,1167],{}," used for?",[170,4785,4786],{},"In what order are input formatters applied?",[170,4788,4789],{},"How can you prevent users from typing numbers in a text field and instead only allowing letters?",[170,4791,4792,4793,4795],{},"You have a ",[127,4794,1443],{}," object with a given date. How do you determine what the date will be precisely one year in the future from that date?",[170,4797,4798],{},"What is the key difference between the material date picker and the Cupertino date picker?",[170,4800,4801,4802,4805,4806,4809],{},"What does the ",[127,4803,4804],{},"map"," method of a ",[127,4807,4808],{},"List"," do?",[170,4811,4812,4813,4815],{},"What are some considerations when you want to use a group of ",[127,4814,2306],{}," buttons?",[170,4817,4818,4819,4821],{},"How many states does a ",[127,4820,2840],{}," have?",[170,4823,4824,4825,4759],{},"How can you limit the minimum and maximum values of a ",[127,4826,3259],{},[170,4828,4780,4829,4831,4832,4834],{},[127,4830,3512],{},", and what's the benefit of using one? What are some cases in which a ",[127,4833,3512],{}," might not be necessary?",[170,4836,4837,4838,4840,4841,4759],{},"What's the difference between a ",[127,4839,823],{}," and a ",[127,4842,3676],{},[170,4844,4845],{},"How do you validate an input?",[170,4847,4848],{},"What is a regular expression?",[154,4850,4852],{"id":4851},"key-terms","Key terms",[1432,4854,4855,4859],{},[1435,4856,4858],{"name":4857},"Callback function","A function that is passed into a widget as a parameter that can then be invoked from within the widget, but it will be run in the context of the widget's parent.",[1435,4860,4862],{"name":4861},"Regular expressions (Regex)","A pattern used to match character combinations in strings of text.",[154,4864,4866],{"id":4865},"further-reading","Further reading",[159,4868,4869,4875,4880],{},[162,4870,4874],{"icon":859,"title":4871,"target":4872,"to":4873},"Material Design","_blank","https:\u002F\u002Fmaterial.io\u002F","Learn more about Google's material design principles.",[162,4876,4879],{"icon":845,"title":4877,"target":4872,"to":4878},"Human Interface Guidelines","https:\u002F\u002Fdeveloper.apple.com\u002Fdesign\u002Fhuman-interface-guidelines\u002F","Read up on Apple's Human Interface Guidelines.",[162,4881,4885],{"icon":4882,"title":4883,"target":4872,"to":4884},"i-lucide-regex","RegexOne","https:\u002F\u002Fregexone.com\u002F","Learn regular expressions with simple, interactive exercises.",[120,4887,4888,800,4890,4893,4894,4898],{},[111,4889,1026],{},[132,4891,4873],{"href":4873,"rel":4892},[136]," now redirects to the Material Design 3 site at ",[132,4895,4896],{"href":4896,"rel":4897},"https:\u002F\u002Fm3.material.io\u002F",[136]," (Google, n.d.).",[154,4900,4902],{"id":4901},"references","References",[167,4904,4905,4918,4931,4943,4955,4967,4979,4991],{},[170,4906,4907,4908,4910,4911,800,4915],{},"BPB Publications. (n.d.). ",[233,4909,17],{}," (companion code repository). Retrieved from GitHub: ",[132,4912,4913],{"href":4913,"rel":4914},"https:\u002F\u002Fgithub.com\u002Fbpbpublications\u002FFlutter-for-Jobseekers",[136],[233,4916,4917],{},"(added, not in the book)",[170,4919,4920,4921,4924,4925,800,4929],{},"Flutter. (n.d.-a). ",[233,4922,4923],{},"InputDecoration class",". Retrieved from Flutter API: ",[132,4926,4927],{"href":4927,"rel":4928},"https:\u002F\u002Fapi.flutter.dev\u002Fflutter\u002Fmaterial\u002FInputDecoration-class.html",[136],[233,4930,4917],{},[170,4932,4933,4934,4924,4937,800,4941],{},"Flutter. (n.d.-b). ",[233,4935,4936],{},"TextInputType class",[132,4938,4939],{"href":4939,"rel":4940},"https:\u002F\u002Fapi.flutter.dev\u002Fflutter\u002Fservices\u002FTextInputType-class.html",[136],[233,4942,4917],{},[170,4944,4945,4946,4924,4949,800,4953],{},"Flutter. (n.d.-c). ",[233,4947,4948],{},"showDatePicker function",[132,4950,4951],{"href":4951,"rel":4952},"https:\u002F\u002Fapi.flutter.dev\u002Fflutter\u002Fmaterial\u002FshowDatePicker.html",[136],[233,4954,4917],{},[170,4956,4957,4958,4924,4961,800,4965],{},"Flutter. (n.d.-d). ",[233,4959,4960],{},"DropdownButton class",[132,4962,4963],{"href":4963,"rel":4964},"https:\u002F\u002Fapi.flutter.dev\u002Fflutter\u002Fmaterial\u002FDropdownButton-class.html",[136],[233,4966,4917],{},[170,4968,4969,4970,4924,4973,800,4977],{},"Flutter. (n.d.-e). ",[233,4971,4972],{},"Radio class",[132,4974,4975],{"href":4975,"rel":4976},"https:\u002F\u002Fapi.flutter.dev\u002Fflutter\u002Fmaterial\u002FRadio-class.html",[136],[233,4978,4917],{},[170,4980,4981,4982,4924,4985,800,4989],{},"Flutter. (n.d.-f). ",[233,4983,4984],{},"Form class",[132,4986,4987],{"href":4987,"rel":4988},"https:\u002F\u002Fapi.flutter.dev\u002Fflutter\u002Fwidgets\u002FForm-class.html",[136],[233,4990,4917],{},[170,4992,4993,4994,4997,4998,800,5001],{},"Google. (n.d.). ",[233,4995,4996],{},"Material Design 3",". Retrieved from Material Design: ",[132,4999,4896],{"href":4896,"rel":5000},[136],[233,5002,4917],{},[5004,5005,5006],"style",{},"html .light .shiki span {color: var(--shiki-light);background: var(--shiki-light-bg);font-style: var(--shiki-light-font-style);font-weight: var(--shiki-light-font-weight);text-decoration: var(--shiki-light-text-decoration);}html.light .shiki span {color: var(--shiki-light);background: var(--shiki-light-bg);font-style: var(--shiki-light-font-style);font-weight: var(--shiki-light-font-weight);text-decoration: var(--shiki-light-text-decoration);}html .default .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}html .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}html .dark .shiki span {color: var(--shiki-dark);background: var(--shiki-dark-bg);font-style: var(--shiki-dark-font-style);font-weight: var(--shiki-dark-font-weight);text-decoration: var(--shiki-dark-text-decoration);}html.dark .shiki span {color: var(--shiki-dark);background: var(--shiki-dark-bg);font-style: var(--shiki-dark-font-style);font-weight: var(--shiki-dark-font-weight);text-decoration: var(--shiki-dark-text-decoration);}",{"title":106,"searchDepth":276,"depth":276,"links":5008},[5009,5010,5015,5021,5026,5032,5033,5038,5047,5048,5049,5050,5051],{"id":156,"depth":276,"text":157},{"id":210,"depth":276,"text":172,"children":5011},[5012,5013,5014],{"id":243,"depth":282,"text":244},{"id":457,"depth":282,"text":458},{"id":709,"depth":282,"text":710},{"id":807,"depth":276,"text":175,"children":5016},[5017,5018,5019,5020],{"id":1073,"depth":282,"text":1062},{"id":1130,"depth":282,"text":1066},{"id":1166,"depth":282,"text":1167},{"id":1356,"depth":282,"text":1070},{"id":1410,"depth":276,"text":178,"children":5022},[5023,5024,5025],{"id":1416,"depth":282,"text":1417},{"id":1643,"depth":282,"text":1644},{"id":2044,"depth":282,"text":2045},{"id":2299,"depth":276,"text":181,"children":5027},[5028,5029,5030,5031],{"id":2305,"depth":282,"text":2306},{"id":2542,"depth":282,"text":2543},{"id":2831,"depth":282,"text":2832},{"id":3056,"depth":282,"text":3057},{"id":3253,"depth":276,"text":184},{"id":3506,"depth":276,"text":187,"children":5034},[5035,5036,5037],{"id":3536,"depth":282,"text":3537},{"id":3591,"depth":282,"text":3585},{"id":3714,"depth":282,"text":3715},{"id":3856,"depth":276,"text":190,"children":5039},[5040,5041,5042,5043,5044,5045,5046],{"id":3887,"depth":282,"text":3888},{"id":4009,"depth":282,"text":4010},{"id":4139,"depth":282,"text":4140},{"id":4257,"depth":282,"text":4258},{"id":4395,"depth":282,"text":4396},{"id":4487,"depth":282,"text":4488},{"id":4611,"depth":282,"text":4612},{"id":4716,"depth":276,"text":4717},{"id":4738,"depth":276,"text":4739},{"id":4851,"depth":276,"text":4852},{"id":4865,"depth":276,"text":4866},{"id":4901,"depth":276,"text":4902},"Buttons, text fields, pickers, radios, checkboxes, switches and sliders in Material and Cupertino, plus validating and submitting input with a Form.","md",null,{},{"icon":46},{"title":43,"description":5052},"dAGyZVw7fjmbKtOSDhWbdy6W37H3gZz4PC0gdfzTJR8",[5060,5062],{"title":38,"path":39,"stem":40,"description":5061,"icon":41,"children":-1},"How Flutter turns widgets into pixels, the difference between stateless, stateful and inherited widgets, and what BuildContext is.",{"title":48,"path":49,"stem":50,"description":5063,"icon":51,"children":-1},"The pubspec.yaml file, adding images and fonts as assets, the Pub package ecosystem, judging package quality, playing audio with a package, and popular packages.",1795696849804]