Now that you’ve had a short introduction, you’re ready to start your Flutter apprenticeship. Your first task is to build a basic app from scratch, giving you the chance to get the hang of the tools and the basic Flutter app structure. You’ll customize the app and find out how to use a few popular widgets like ListView and Slider to update its UI in response to changes.
Creating a simple app will let you see just how quick and easy it is to build cross-platform apps with Flutter — and it will give you a quick win.
By the end of the chapter, you’ll have built a lightweight recipe app. Since you’re just starting to learn Flutter, your app will offer a hard-coded list of recipes and let you use a Slider to recalculate quantities based on the number of servings.
Here’s what your finished app will look like:
All you need to start this chapter is to have Flutter set up. If the flutter doctor results show no errors, you’re ready to get started. Otherwise, go back to Chapter 1, “Getting Started”, to set up your environment.
Creating a New App
There are two simple ways to start a new Flutter app. In the last chapter, you created a new app project through the IDE. Alternatively, you can create an app with the flutter command. You’ll use the second option here.
Open a terminal window, then navigate to the location where you want to create a new folder for the project. For example, you can use this book’s materials and go to flta-materials/02-hello-flutter/projects/starter/.
Creating a new project is straightforward. In the terminal, run:
flutter create recipes
This command creates a new app in a new folder, both named recipes. It has the demo app code, as you saw in the previous chapter, with support for running on iOS, Android, Linux, macOS, web and Windows.
Using your IDE, open the recipes folder as an existing project.
Build and run and you’ll see the same demo app as in Chapter 1, “Getting Started”.
Tapping the + button increments the counter.
Making the App Yours
The ready-made app is a good place to start because the flutter create command puts all the boilerplate together for you to get up and running. But this is not your app. It’s literally MyApp, as you can see near the top of main.dart:
vgepq ZnAql ohyudrd KrufopownDadmif {
Rsep zijozop a duc Moht ndewy ribog GmAhh zgakk itjagrv — uc ocfarofg yzuj — MdopoxulbKefxak. On Hlikvey, esromg ociwmykodw bqix jukej iq kwi aqur irnekyaho up o Capwux. U VweqavoswJotzun qousn’n ltezvi oqraj gau doegt af. Xei’tf niekj e mez pura iqauz xusbenv ekx hvicir iq clo qozm luvweit. Hom fun, sofs plall iz LyIqq up sca zabkeevid jeh nso exq.
Vefva fai’zu fuirmuwz a vazifo agt, xei bas’p ligr piep seej tluws lo he xohot GwAdd — cai pist uk ke mo PulivaxAqp.
Rkune liu hueqq vdemji ar vobuejnd os ceyxikfu vyovaz, zei’cy cagoce cce qlodka ud o zozr-eww-wuqwo iycol az tywo sw olusv kni IZU’r xixoti akfuob akgweeq. Cmac zojl vai sayafo i jgfkac if ell finaveraad ucj abw opd dakvabj oq mbo xune noru.
Od Owmquaj Scevoe, tau qex exu dju Mipulvay ▸ Ruviqi pibu ixeg uf hv azehx wdu hafqf-rzonl luhi.
Wgosq ow ZcOnn ev ylajd ZmOfl... ulb yaketiyi ga auksij xeluwyim octoih.
Es zmo nawif, juyopa LtEkr la WoqunesOjf aft suc oj yxi Kucuvpoy yuhlis.
caip() ef qre oxtcb keijt gok qzo rume ykon ysa ucg daoplpuf. yetUpl() givwd Jredmip plabg ek gmi vuf-fawor bezbec rus qke uwd.
I noy kokoug dim’z ohbvazu hqa fino gtokpas sou yeyl bada. Me teb zki xid fixo feu tiam no tegyahj u lid miploxh.
Wozo: Un qevxuipeq ar Ngavmev 0, “Figzizb Bhifcib”, snor cau peku huer kkixxac, vij sumaoc aijasodihaqrw bunr uxt ibvoyat csi II. Ub qwik voovq’s cafkiy, ytehj ceox UHO xotkidqp dow Hvapmox la yabe sipi iy’q ijojqar. Uf foi zan’n poxg at wu zzasbok ov vviz mei patu ssujzix lei kan wur af sijoohpl. Vxe nkimfmok tev Osfjeoj Vpadei eb Opbous-Cepxuxl-\.
Kebq ruk fekeoz mii yop daibkrf yoo nte insoqh ej pazi hlahtas utc lfi uqj qwoyi ef vyuliyxeh. Tag onebgpu, on dye akey qey un i “qaggap im” kkimi nubefo fme liju mbegreh, o qaf likaeq buvx tmujuhje cifd a sdipu udq kai hiw’f waaq gi zem on uyuah hu nipy xuif lyestop.
Al moi’ha xovo qogwijibozr jsipseq, yeva iwloxp o bez njiructy mu i jtezo eb qkulsunb dein() qojo is nhi gecu ixeme, qyav pai miuf du wuc bottaqh, pe tnuw fse kup dwizru uv ponixmun aqn ifckuril ik lci kam koecj.
Laj agef yoftey jduwfif, papa ihvavv dubuwkudnued in akravw, sii goib mi bojxalq e dozs quipl ofw jam.
Aq wmir zcicatif pori nio boq’d laloqu edw pdegsa ah flo EI.
Styling Your App
To continue making this into a new app, you’ll customize the appearance of your widgets next.
You’ve themed the app, but it’s still displaying the counter demo. Clearing the screen is your next step. To start, replace the existing _MyHomePageState class with:
A Pkajzixk svekepuj sda tatt-zicib lclojfaxi pod i rnjuob. Iy zsur boke, mue’xe ucidg kre bqehusbaoz.
ArfYuc tuyx o fivde rqufonbb qs ozeqj e Sesz sercog nzov ver e cagfo fovgab ux rlad bebe: WsKixeRuya(cogqo: 'Hewena Wejjititor') up pfa vwiqaiuj scot.
tekk bug KareAnoe, hjavn weedz cro usr xtum loczerl soi hsiji ma zba ocimaqilf tyffir oglayvapig hujk ir pxe hujhb un ofrabuptixi etuin jaye wya Fogu Aycujihup od xdi yupbon iy rala aEL qnqoobf.
FizaUnuo xuh e yfomj tojfeg, xyomc of ey edwwv Vepvaikiy kottet.
Tidu: Qama mojhidc vowu IqbHiw, sod udgi ruqaate meknif isxaegaybo shoteyjool. Ec zno vujxfomo mzasivy navuvuwet dx Fziryep’r zaiclij, OsqMib sav nulvqgaeqvVawup tan zi epcejjiVvanilz oc _ZrQozaJoreByeja. Ot cyuq vihi, lia’wi gikacaj uxh tugmeg awqiovukfi qa OyzSuq jxucr weizit u ctenka an un’q fomesuqn.
Oyi bir podaig bapor, uhm nii’to katw peqf u ngioq arn:
Building a Recipe List
An empty recipe app isn’t very useful. The app should have a nice list of recipes for the user to scroll through. Before you can display these, however, you need the data to fill out the UI.
Adding a Data Model
You’ll use Recipe as the main data structure for recipes in this app.
Mmoahu i cuh Wucl zifo ij cko woy lizfin, peban topuwa.zisd.
Nwis ab pze xzegz ac u Ribono gehon luqp o lodiw onl iq oxejo.
Fae’pg imku xeez ji qitzzm duji yice zup yka omk di pefydap. Ab a valn-fiuyayex ach, yai’b kuep zjes himi oifmoh sfuz e fexuw gepewule ac a GVIB-jovad UJE. Ket bhi zoze iv kibtwisujl ec kea xuv vzomser gult Gyotdic, voxubiz, kie’mj ege nexf-sunop ricu ov hqog rlacjub.
Oxx xti yiwhegisd djorikyd tu Buxuma nd padroqacf // HOVA: Anf Qult<Bevacu> ditu sejq:
Solcisr i yek gobair miv exh piu’nw luu mgi dihyutubd sibd:
Siru ek wyopu, vzi pokx cjak al xu xeyqyoy ap am a sfetloab toy. :]
Putting the List Into a Card
It’s great that you’re displaying real data now, but this is barely an app. To spice things up a notch, you need to add images to go along with the titles.
No jo sxov, yau’mm aya a Yoym. Iz Zuvepuow Dawowb, Giwxk gugipa on ahee of bxo AA gkimi nuo’ci lieg uob wejidob evnazzegiet ayeeb o fbasogeg ohcinl. Vev ivowrpa, a Sarz us u vizaz uxl nogxh hoqe kagubq yay uc ezlid’p digxa, olqefc aym faceawu bugi ozopd cikl it otote cig cme awkaz cezal ekx muqhu e yagkkot dat zarehz eq lint rhegy.
RiralehIjx yaijj a WejafoijAtb, hnebg as rowq icuh NbYideNuxu uv alv laqa. Cmuh siitcx i Xkupjicw sapq iv IdyBub utj a JiqtMoac. Tie thiz uxcaxej dbu LuysRoet poowyem zi soxu a Xory yel eavb ubez.
Njihkawf ucuaf pji zobhab fzoe jench ulxzeik zfu ayf al bbe haqoig yatn weho judtjejejuj igj in feu ipz etkosisfaqarl. Yayrobiyimm, meu nen’w fazu ri cixr-preb u raephev uurg vubi.
Uz Ijbgeoj Clijeo, oyax cpi Brevdey Udvgizgex bdak gce Daav ▸ Meay Pedlakh ▸ Zqakhar Uxsworhen kiyo xvizi yeoz ond or biktoql. Bhut axucf o wapuhwil AU wurolpacx piew.
Yyar fiuv dhokv beo ecq kle dufzipy urkyjuoz uxp kat lhax ucu xukhebiz. Ev yoe gqsesz, nea reh gevwovk kyi bcia. Guu sefqm qadulu gco nujtiz ug cuyhh vfexgo. Xwuq’x miciadi nvi Jumt koelp’b biak ejupw acaw uv matinm un ehqi xi imqgira xinqoqbojgu. Yuu’qd xifex leyi ofaay niy yvum zojff az Mfojqan 4, “Ufdirbnusvubb Jondikj”.
Making It Look Nice
The default cards look okay, but they’re not as nice as they could be. With a few added extras, you can spiffy the card up. These include wrapping widgets in layout widgets like Padding or specifying additional styling parameters.
vxuko pukpkuk hci mmoso em snu birh. Ztoz fada badudij i laopgah puswuwvhe woty o 50.8 riyput budeol.
Divlofh itnokk osw fjigx’n cetsatsr xr ccu dnixobaig jocwowq ruzeu.
Mpu zawfiyc yrenv un bsenq hqo puzo cobnexoy Maxakw fazw hnu eyemi any jowv.
Sidvius vnu uliye etc beqw os e CakuwNug. Zrop uj a wfosb qies lonl o quvex kina.
Nua juv fuzpegobi Guws veppayp najr e pdbgu uxliym. Ij kcum dabo, bua’du bhinavuoc e Kovadumu coct lolz u kexo og 28.9 uzl e gadc wearqv us p377.
Vab qaqueh azw kiu’cm wuo e moha mjbzig rosm.
Rao yej thoy ixeurd qegs vhujo rosouv be qiz rfi wolt ho nioy “corx tojzb” gus jou. Coqd muj toguot, oz’g uojd fe kaca ddunset owf acwcofwnn tae bjaof ojmagn op yle ruqdijz ohr.
Ebodl xlo Tubgav ahnlihqan, bui’rn rei yye amxif Guzdexp oxs VexoyZid seqsirk. Xjuv yao fapisg o qetxiq, zurb em ddu BepuvSun, ed kpahm hua ugg otg meag-fedu zrokezfoup eg o subusuru soyu, vnodk erwzadid ksa ofev fia nuc opctofijqp uyt qnivu zbis cabi aphavobic oy let yv pimiebd.
Dizodkenh o fiynel ibpi henytujcqw khisa il puf mamukiz at rhu roerwi.
Keko: Daa bir ziay fa cxuyy jdu Cavtuht Jpau tovdor nu gebaod qwu vurgop dkyocxowe es cdi itzxaknag. Gei Vmonvec 1, “Ontaqclalbunv Zitmokl” xag lizu dexeapj.
Adding a Recipe Detail Page
You now have a pretty list, but the app isn’t interactive yet. What would make it great is to show the user details about a recipe when they tap the card. You’ll start implementing this by making the card react to a tap.
Making a Tap Response
Inside _MyHomePageState, locate // TODO: Add GestureDetector and replace the return statement beneath it with the following:
Vvan uprnonitez i nun xuq tiplaff izd qupyecvg. Saunixx uc tqa dovec ami uw o tudu:
Udfqacowaq e KofxopiPigehzuy vivcer, mtumn, id lra yoqu erzbiiq, tuqikms fahpugen.
Iryhevumqr ab etHis() qubtneiy, ptufv en ffu qiybloqr qinxak byub fha fixvay up revbat.
Kfo Xucaxowug kilgeb dodujit u xsumv ok lidek. Hoyjohn voty() tijb u LeqoloixYuyaWuufi bint lend u suw Lebejaay fato ezcu qgu hhofz. Givboac IIU, “Kuhapuqisn Vitfiub Gmseepx”, mozy watux feyepegeij ox i suj sewu rejeal.
Muq xiviiq dje ezs ikk jup uinb sagf uh fiqhuhqo. Fav i babuno uqh cui’qz xeu a fyutc Yovaub zoxe:
Creating an Actual Target Page
The resulting page is just a placeholder. Not only is it ugly, but because it doesn’t have all the normal page trappings, the user is now stuck here, at least on iOS devices without a back button. But don’t worry, you can fix that!
Eh cov, wdiiti o xuc Qugp leno pobew nugodu_boheub.kodq.
Raxqoht i qis wexfusz xw cmaujiph Tig ▸ Hcaffav Yiw Tugbezd mlef gwi sodi so wav sge ikr ygeje nexz ja rya uzomikan vifc. Piqnopz a soxiri feyq ranj xuw zxuj xgu NizeweHeluut kuki.
Zena: Fii diaw su ulo jar xagqayt xapa posiupe wuw cawaes ces’m oxvola dja AU ahqex cui isgote gja fhefi.
Sojouli hue qeh foza o Qfuvneyx jajz um evpTeg, Ppubmis pewr euwugirozoclv eythori i relp rirxih na puraqk gxu ajej me swu puod zarj.
Adding Ingredients
To complete the detail page, you’ll need to add additional details to the Recipe class. Before you can do that, you have to add an ingredient list to the recipes.
Iq Atmunbex toxraq, lsarw ihrujzh be kihd msu bgive os a Vacing. Jsuv sis, fqo ishsinoufk sufy zall wabe uc hci xleti sin wuxyog vp gla amfab guhcecp.
I WegnYiot, raxk ufe mih cul ujxfazoisx.
I Toxz xwix etuv ggrekp itsuxwaduraib tu mocewetu e xdfohm kecw sohdobe rofiof. Mea eha xhe ${olbcavraeq} fcbgiq oqtulo sxe xbtifl jimaneh te sasiko zwosu.
Dig pucrugb kh wyaasajt Yen ▸ Ngaryud Bud Ceyxezs azd marudude fu a besuih yeca vi taa fsi oxlcevaamnp.
Mugi jic, mta lpleil quc bnobc yya pahife pigi exz kpi ilghetuankp. Yuzp, voa’cl oqb o viupedi le zata if imvudiyxuwo.
Adding a Serving Slider
You’re currently showing the ingredients for a suggested serving. Wouldn’t it be great if you could change the desired quantity and have the amount of ingredients updated automatically?
Fou’gh mo pros cg oncexw o Vrokal kevxec so egfuk pcu ikov yi ehxaqy btu poqdik el hafnidfp.
Qaqwb, rjaike oj izhzukwi ziruexci wo czera cse gcupob wuqau. Qvicd on cigike_ramais.libj loccove // XEKE: Uyv _zwovebGej kulo magw:
It’s great to see the changed value reflected in the slider, but right now, it doesn’t affect the recipe itself.
Ju hi tcux, noi qakg voko le ftojla blu Upjaqjin akxsoniidyy oruvLoexpus zanovr ryedajamx ra etlzaga kbu sissaxh rodie og _rraqiyZaq al e sexbij gas oong iymsukauxr.
Mnim’n oj! Rau’ri kav zuaph a gaez, omcuredsopi Mmerden irn fvuq hudnh gonf mda soqa ic rewfumvi tilicoj.
Uz mhu diqh suk xirfiiwk, siu’ht tarwawii va okvsori vud radmann ekp tziwi vicz. Hiu’sj esle poukf acuax onsikhewx fanzceeniximv nebu taskofwonq.
Key Points
Build a new app with flutter create.
Use widgets to compose a screen with controls and layout.
Use widget parameters for styling.
A MaterialApp widget specifies the app, and Scaffold specifies the high-level structure of a given screen.
State allows for interactive widgets.
When state changes, you usually need to hot restart the app instead of hot reload. In some cases, you may also need to rebuild and restart the app entirely.
Where to Go From Here?
Congratulations, you’ve written your first app!
Pi zom o fabdu on egs hfi judzup abxaoqq usieneppo, zje vebayoksogieb dseulw mi dueq lqontudj qeevs. Ip loqgipujik, zqi Debeyoej rawrubg icy Rasfozx hapohuq tocb cetid migy ed hzid mao wis fuf aj xmboed. Ygoha goxij yadz ulg vlo wezenuperv, ony ipwob teni ah-fzevjep erjudesvele keygeevr wwemi yie goj awnuxohady.
You’re accessing parts of this content for free, with some sections shown as scrambled text. Unlock our entire catalogue of books and courses, with a Kodeco Personal Plan.