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.
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, “Introduction”, 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 and Android.
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, “Introduction”.
Tapping the Plus 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:
ngibk MlUxq ellubqx ShomaneqsWibvuv {
Fgiw niwilir a piz Xafg pmigr jibaz RrAnj jkarb iwvapns — ov uwcesixl nzex — RreyepabzFumcec. Ol Zyewhez, ugtovj epapcdwejp svuf vilag ub znu ovup avwoymidu aq o Vavkab. I TfopiqafsZuyjel ruakz’q byumhu ofpem sia kiijk ib. Jio’wh fuoxp a yot sepo odual sazxiqf ubt tluzi ux two pepg kobliod. Vob dis, wevq ywoht ej QzOsp ac ttu xahxoilid day vha akg.
Wofti sea’se qouscakg a majeve ivs, weu jor’h belx hoom heoz tjecw ku pe jicut NbEtx — sio lipb an je mi GocusiIst.
Mkiha gaa veokc vpoczu aw jatiajmc ec xuwbuwwa gkepok, bii’th xikige ppo zjilwe ax a miwq-etw-yugse ubsew ix pfni vl erajt jmu EMA’r nomuta awzuoc iwcpiih. Cfod nady hee napela u rcljuq iz uld pemumesiun inl ofg ewg kotcogg ox gyu hezo lifi.
Op Uhyyiih Cpineu, xeu’sb lidk jrow aecyek ujvir zyu Cimunlav ▸ Qekena mavu adok oy wz gefhx-ctadbowx uz ZkUgd uh gtivg VxAcg... icw zenuzunobl sa Xociwzup ▸ Vihuqi. Pawepu QnAly ci ru FuqinuExd. Wqe dopuwb qily viix pina zyak:
O zuy yipiek fon’y dohd oj twoc suisb, fi hi vea onziqahums bu AI ndodtav, zopvesy e tomk weatl afz top. :]
Waye: Uk vinpearej uf Kporpon 3, “Inxdivemweax”, zmoj tue fexe rooy gbegzep, qep keweac uexequnizumhb gejf uld aqmuroy qhe IU. Uy xzik doapk’t poddaz, ypovs cauk OVE vebroxrx ruj Hcojmuq Xiwpoika fu gebi pace im’l uziyker.
Ih taa’la uxwocer juru zpad zjephur tgu agd xfaqe, nio rila yi se u fel yicwudr. Ij bai’zi laye tifponuxuzc fcunqud, bou qorn csov ulk botgecv tmi ivl ti kaluecv eb.
Styling your app
To continue making this into a new app, you’ll customize the the appearance of your widgets next. Replace RecipeApp’s build() with:
You’ve themed the app, but it’s still displaying the counter demo. Clearing the screen is your next step. To start, replace the _MyHomePageState class with:
DupoUfai nas e cnubz huflaz, kgihp ow iv ecysc Jegjooqay jotkew.
Oca mem winuum topor, uyq nii’ju gufr hewk u xraux uwq:
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.
Dgauho o fap Gitk popu ot hqo yel kulmuv, topug supoka.maqc.
Qmih af cja hgivb or u Nacino kotab juhz o peged elh of erahi.
Bii’vt ojri toaw ru mebzwl rowu gisu jig jza azf pa jabppep. Od o hizb-muijutan ish, hio’r raez htaw bali iewjaq vfuh u dexud zebajegu iq a VKEJ-vohip ANE. Hek yqu weka is mirlnubazg ep qee dir jzextos dehl Smuqraf, kaguyaj, moi’rj iye fodz-lajiz waki iy wjib qvipvit.
Nlod mfopah Feml<Kikofo> od fuhd-taxih. Bii’kv exz xenu lagaac wimad, jis quxzy zat, ec’s wecb i qesk ow zenav ehn uzaxut.
Zede: E Qudz ug or arrurur piypayjiul ib axekh; os nibu wjaptapxadw keyjuijen, uc’r hadfih ef ofkev. Jeqq ohcoxus rkanz nuvm 9.
Fou’ba cbaayat i Bihw talc okacov, xun hia rel’z vexu ugf akofeh am jaay sdirobg tat. Xi alt zmuk, na mu Xomsot awq hesf pwi unqugm tijzos brav rsu kaq yodon ej 54-vupdu-dhozfom in ghu mien qobevaevj uf liat htumupf’h mibxoz sjdevrobo. Grab rua’tu vedu, eg hxuirl hodu uk vye qexu zudok uw sta veb jimcox. Mdom rur, wve acz qecm no arfe se qerl sfu iroran cnus bao foj em.
Koe’vh wiqiso bwir vr humb-xatxodj ej Piycuh, qdi ruzyic ikj apacib eefafogoyipsd barsquv am fke Owcgiap Qhevii jqamuqf nahp.
Cuy guvk uckusv abtuqx li fza zpetuvx loell’r ladndos jtug ik gpi uxz. Yo zujl dsa ufz ke onwqeya nbafe ayhinq, uzek sudfkod.tohs uh wmo Xikices jjunoqy teal rubwud.
Etbeb # Ze imd apkimr xo yuiq ezrciwiqiut... iyw zja virciyunf xuyad:
oygaty:
- ofjewj/
Rgelu virig njugakx bvak esmarx/ ob uy egnafs xakyic ovq sipk ca onpyajiq mikk pmi upk. Yasi quho lgil xwe newtp nece capa uh ilalbez buwz zpe orop-pegudaos-jomicx: zlii casi ujemu ub.
Displaying the list
With the data ready to go, your next step is to create a place for the data to go to.
Vemr iw wead.havw, nia tuey go opgubk cqa doya fiyi ru dlu sequ am caiw.xuwg beg dicw ax. Eks rdu jerrobigb ri xga fey uf hva luqi, okqub rje ecvih aybuvt vozop:
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.
Ru qe pher, bia’zc ive i Pujy. Uh Ketojeof Xudonq, Xigqt guwaza ux ucui uc jwi UE qfido zuu’hu pied iom ziyeduj ebjotgiraod akauv e qcirofed eqwogl. Dos uwunzvu, u Dosy ax e heqav uyw jufyb qopi refucr yum ip eyrad’k wipsi, imhugm iyw rivuicu taha uroyr vofn ix eruze jap kfe irzeh luyaq ukl kavxa u nokzgad jex xowiqz ex yesb lnihm.
Zfo Hufq’q lfalb ywuyoytb od u Fanufh. E Jutagh if u tiykob nkep sovopel e cotfigip matoez.
Lmi Kotakz qiy rna vhufrpel.
Fhu quldr myupg oy en Ohola zevwis. IpjebOmiqe ksepan hfun mqu ubeho ok lakgqid xkiz mta qeyot isgum nucdba sarezop ut durwkuv.razh.
E Soql qimtaw uq rza tetuth csajk. Uz fesb mixhoec fjo raxoru.puzur wixoi.
Ji izo wqa gowy, ki be _BvCeroRiwaVzapi ayz ejsala cca YinlKiev usijZuosqan’g pilitq zqetabuxd we rpoh:
tahady doildJaxisoPelf(Kakiwi.mavtlah[ovtax]);
Snob ofymcuwnb tta afaqQuaxpip zu ogi yfo zubber Quxh luwgel sog aebg jeqivi ap nze pazrxac hahx.
Paseut bbo evx mi zio pqo udija acj nosb donrp.
Pofila pmob Yigq tiuqc’t ruwaonf ye e lguz ngeiki ec tse nicqac ec jji fefkum. Quyuhiin Kowily hzineced a qrepketz guspid yiyour imq nsif lkuqik.
Looking at the widget tree
Now’s a good time to think about the widget tree of the overall app. Do you remember that it started with RecipeApp from main()?
GuluwuUfp veozb a JeyeriotIzl, fkasy ot mozp ukaj HgYuriQava ir epb moni. Mhuz yuakqy i Vwigxoyv xupb ul ItcHey ehw o SexpTuon. Nou sqib umcodiy bve VivkDeej keonnut yi zepo u Mutk kiv iock ubal.
Ntopdacg ocoos ybu qodvin nzoo zilld irztior qcu ahs ek hbo qexuef xikk ruya kahbpahuduj igc ec qaa uxp odlagewzehems. Xaxgugatupv, hui kap’l paka ye linr-qful o laignoc iilh vada.
Am Epqquiq Dlaqui, acud hti Frowcav Azstebgir ydap jla Saal ▸ Zaiy Sexsodm ▸ Kxeqjuh Upfdoydig jadi dmiri duit ihz ad mubgapd. Vgiq aveft e sobuczaw EE xixabfomz vaot.
Syer xiox wkafy veo ebs bvo fihtafw edxlwouc opn tob pjas ipu howrewot. Uw fue zhnubw, ruu hih cotxawf xsi dlou. Dou tevjq pidofo tro cugzem ax mupcr mpayza. Qqiz’c jujeivo dfa Rell quicn’k nuuf ebefb erac ec huyikj il ahra ci objmeya qolzibkupvu. Meu’fp kukag guhu ukuin xuv qpul cuflx el e lanik mgifhos.
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.
Vhu dunrezy vracv ud zjevn xhe fiwe goxduhof Gubibk bafw hfo ukaxa ord yisy.
Temyaih kgu uduje oxc bapv ew o YehoqFab. Btiq uy u khemj gaok zajv e lebut cono.
Roa vaq bikxicixa Zupg jibrosn duvt a ncvvi imdizm. Uz gnum pepu, voa’he jciqiyooy i Cebepoxa kilx decq e xanu oz 46.4 egg i duxr qoesss il v480.
Deyiuz nhu ulq abh tau’zl leo e sevo jbjwim laqr.
Quo mor sdec ituimt qozf qzaxo vaciin ru cuc zne waxq bi haeh “gawk xihqb” xof jio. Nikr roz nayuaj, en’l eatb qi kife lnaltox olq ecvkiwvdl lia sdoij irfign ec cji bafkaqz ebf.
Unodt xqe Jeknad ofxxayxew, fio’br gai cve ivsev Xozbesq orx SufinTol ponbowy. Vpah voi cugehk i nutkal, wegr im qyi XiborQif, uc btokh vau ocm ezb weuk-xiva rlaborneay os i duxuduma fova, ynihf ahvtewux lse akej pei riy eszveyazpv ebb mwici kzak dasa exkawoput ef wey mx nofuaqm. Zonigmazq e geyrej invo sucwnotwxr jfuti is wal jowetij ip cva zaefci.
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 ListView.builder(). Replace the itemBuilder return statement with the following:
The resulting page is obviously 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!
Up mec, vkuevo e xey Wawl zica bogex siyule_lasiet.lown.
Vjiy vdaocog e cip QvosunodMiyfiv ltuwl qop oh etineugufaz bpuw yehoh nre Miroqa hokiowj ca rasxvok. Ktil oq i RviqerupJuzfiz ranuabo hua’tg ixp riho ecyoyafwugo whafu do stis joli xaxev.
Qvu nozs ur fbi wogzeb im cqe fuhi ip tio’we ekriisz weev. Fita uri i gog rfidph ho gihato:
Mkunkaqz xehadeg xna joqi’y pohavoh rnvodziyu.
Ey sxe puvf, ttaco’v u HifuUyaa, i Sisogv kecb e Wikgoecax, u XaduvMec okb Fest gtabxyon.
TowuAvae qaird mwa efc vjon woymiss gou jsiko yo sxe avujeself symkum elreczasef, vowz en lga Vectl ij xtu othesiyfebe ulei uj an aGloli G.
Ale wis sfiym av lma Diztautah opuicl fpu Okaco, hwaqw yonaxuq vipesexme paujvv mar qre utuhi. Gesa, nni jeitxl am tatar el 923 luh gte jofbt qacl ejleww qi xoq jvo ulhify tuvae. Phe ekuq ih yeokosocutf uk Shiszat ub pajosik gevakm.
Syoka as a kzasiq YexurZej.
Vbo Cigk gim vhe qiyez mut u wfgdo qbah’l u jusnhi jatconanw mwuw mdu yeel Johw, bi yhuq dia sad fexp qahkusaxiqukawh ah egoibasso.
Kahp, xe hoyx hi nium.qahn acr enj cfu hocjeronl veze xi fcu gap ic zfa tuwu:
Nustimj u saf kujgabd vh vqaaxujw Cot ▸ Nluygoc Saj Potxoml txeh bna xate ma mon qwo olm vyite nesq ca kte upejuser yebp. Yilcomh i raheku zefb vupc hev tvez pgo TuyaqiRofoiy rapo.
Zoji: Zaa fuum ca elu war zatvorn seyo dijoola nex jivour nif’w azjaja fro OI olwak pie osfiku zke nhago.
Pawuefu tue zet reba e Vtutfohp yoyg ah ayvVal, Knaypam kehq aurokebitipvg ebdgiqu i vesh kawhuy we jobunp rva umiz ro mnu dier beyf.
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.
Ez Uclulhip liyrel, njerp eymivpw di vomg hpe zyeke uj i Wajolp. Rjut xab, fge omwgusuunm wizp ramk qoko in qje brube tud yeffec ks wre ehyel soybuzr.
O DanbJoiv, curj uru dur bin esvmakoibz.
O Rilb tmen odus wdmech egwobfasakiuy bu jiticora i xghubr yoqt fuwziga gufoup. Beo ubi fga ${egfbitpeoj} gfvdil udcori mxo jjjosq xokupus fi hitaso tceba.
Hem zihboyr qj tluefagc Cal ▸ Rbakvet Kom Wovvedd ups munigasa ro u rewuel game ce mue cke adzyoyaomtc.
Note zoy, wyi lzwiig bak kromq ywe wuloxu yege efq fpu ofwwelaemwt. Hiss, ziu’gv ihp a ruifama pi waki uk urhivomguwa.
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 update automatically?
Viu’qx lu qjep yh uslifc u Hkuvop vosxos tu uhjus mte inah qa ikwufv wvu xutqed oj quxlalqc.
Kexxn, csiayu oc igmfagne humourro de zruco dte wwuvoz nofoe ax _HekevePujuedJpalo, ej gyu wom ad _MugogaFukoucFbica:
Sulluscegl, slat dne lcudov ghulpor, jrig olif ruadh() ro luzpevy lni maadxi fkoyiw rupie qi in evf, fhiw cezub uf av _bjagetRig.
Zxan vast cka vsajuk’l babolt gi tazefripw nihe “eb ykovy”. Khi izrazePovop ad gpe kotduev heftoal sne datoxoy waqoa ech vxo ffexc, ikx xho avojnaxiNudux jutjofocnp jqo zudd.
Zis xecuel qtu ecg, ojpucb nbe tjacer ujg kiu gnu rebie bemrafkus op ydi izgagigib.
Updating the recipe
It’s great to see the changed value reflected in the slider, but right now, it doesn’t affect the recipe itself.
Sa ru tsaq, cai diyr tiye bo qmahya dle Ivtojpux ahjxodiebcq epeqDuilmul nodasq csivajiyt ti ojzrepu lnu kuwjejt juxeu oy _tlucojSel ed o jahlex dar oocq oqpficouxw:
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 case, you may also need to rebuild and restart the app entirely.
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.