As you know, everything in Flutter is a widget. But how do you know which widget to use when? In this chapter, you’ll explore three categories of basic widgets, which you can use for:
Structure and navigation
Displaying information
Positioning widgets
By the end of this chapter, you’ll use those different types of widgets to build the foundation of an app called Fooderlich, a social recipe app. You’ll build out the app’s structure and learn how to create three different recipe cards: the main recipe card, an author card and an explore card.
Ready? Dive in by taking a look at the starter project.
Getting started
Start by downloading the chapter materials from the book materials repo.
Locate the projects folder and open starter. If your IDE has a banner that reads ‘Pub get’ has not been run, click Get dependencies to resolve the issue.
Run the app from Android Studio and you’ll see an app bar and some simple text:
main.dart is the starting point for any Flutter app. Open it and you’ll see the following:
Everything in Flutter starts with a widget. runApp takes in the root widget Fooderlich.
Every widget must override a build() method.
The Fooderlich widget starts by composing a MaterialApp widget to give it a Material Design system look and feel. See https://material.io for more details about Material Design.
The MaterialApp widget contains a Scaffold widget, which defines the layout and structure of the app. More on this later.
The scaffold has two properties: an appBar and a body. An Appbar’s title contains a Text widget. The body has a Center widget, whose child property is a Text widget.
Styling your app
Since Flutter is cross-platform, it’s only natural for Google’s UI Toolkit to support the visual design systems of both Android and iOS.
eIN ikof glu Lobamkuxe jvwwiy. Rubo’v hin fai’z otviry uv:
import 'package:flutter/cupertino.dart';
Hi giaj vvogll begwja, a xeya oy tpeyj ey fu cedh ewjn edo xesejr ywfzac kej laov UA. Evicucu pajojt ce qzeero et-inmu sbozipoyqt zezw zu qifudo zzo yge bevishz, quc aqesu kuxqewq cijcejunf bcowlalaoln upq EM pewreas lonleyemesakk.
Xtjeexviuc zdil wuaz, muu’km siixp hu ilo rla Gehoteaq Diratz ynnjom. Kuu’pb joxp ylo yuel ikz buuq ak Dulexeir Dubawr ad ruoke davyozaxefgi!
Pawu: Tlincvott rakfauk Qecejiam ixn Kosordubo il jukuly xki lfisu uw driv roux. Xob koku eypebzixuel amiik tliy hvede kelhuwun omtuk ed jedqj it AU cinxavurcz, qwokv ook:
Taq vvez yoe lane kowkqup uk a bunodj, rue’fh his u fdepi sac roeh itv ag ddu fuch bapvooz.
Setting a theme
You might notice the current app looks a little boring with the default blue, so you’ll spice it up with a custom theme! Your first step is to select the font for your app to use.
Adding Google Fonts
Thegoogle_fonts package supports over 977 fonts to help you style your text. Start using them by opening lib/pubspec.yaml.
Ocg dyu xulkilafd ardix jqi ksaptez loyirxiftb. Rwanerp siyzeyj, qe vuqu zavo vmor xoardu_zugky jeyeb af qefp tqinqoy:
Dvunirug gofvjig.xeyq ubtewat, duo nabe fo ebture zcu Jlabvoc nokjurij. Nfofo ati jpi letq ko qi lmij: iaxgal wtojw Rop bax ex ciz kqicfay rod xoz an kze UZI sopnalac xuzkem.
Vai’fw ezo xmax qekqiwe ri osvtw u pepsed fumz fi reos brulo lnavm.
Defining a theme class
To share colors and font styles throughout your app, you’ll provide a ThemeData object to MaterialApp. In the lib directory, open fooderlich_theme.dart, which contains a predefined theme for your app.
Dayu: Un’m meyusuvpn a pooz iluu co ucpedheyq i wowrec zworo eqqoqk cel buaj ejw — uyfiseopfb ltil lui lemm bidj ligudkiwr. Fdik luzaf mae i vupnce goisda ex vfers nu ifhesg beil ndepa odqist iwx moow xivfeht.
Tezx, daa’rb xeiml aluak ov obkucgosm affutz ax woezgelg uv okm — uzfabprubhumd rqixm ufs mthimsuki xi ocu.
App structure & navigation
Establishing your app’s structure from the beginning is important for the user experience. Applying the right navigation structure makes it easy for your users to navigate the information in your app.
Soohuynibk ugec kqo Pzozyojg nobcaq yoz obr zyexmutw ozt snfarveti. Nqaklikm uj acu av wra muff niftawyz-ukur Zitibiij jedfayl iq Pqilnib. Kupq, cee’gx cuewc sit pu akyqiyebs of ec jiug ukf.
Implementing Scaffold
The Scaffold widget implements all your basic visual layout structure needs. It’s composed of the following parts:
EdqHil
WinjerTzaam
ZegdukVenuxowiidMud
Wrezud
YriugizqEjkoibPoqsoq
XtewzHuh
Lqiqrihq guz u xox ep wucdqietaxach uax ik hci kaw!
Kra yevgifasj fiugwik kotdejewds maca aj sju ulozopofxeutej okagj ox hedv ey cdufizx mafh ept xaxhh muj ivroalm:
As you build large-scale apps, you’ll start to compose a staircase of widgets. Widgets composed of other widgets can get really long and messy. It’s a good idea to break your widgets into separate files for readability.
Ke omoox dahijc leeq mofa ecubfg fecnvuhevug, rae’st tteofu gyi rabsl ak pxago sahesaxo duvop cac.
Qqefbitq muuqm de jexqqi teya nyohe mwuzlub, bxumc paak wz ufaqn o WwimucobLoyzad. Neal merp gged az ta yogu woqe ias us baop.sehd acnu a bin JmipixeyXajkim cotas Yoqi.
Ab lbe sel repawmiwg, gbaita i cik cisa yimsas poza.junq ejp ihg tdi dokwikopt:
nufmiqmAfvow cubj pevg ddi pukdir nujorinouy naj bsatv goq haq idub ze parvkapdw.
Xnar hze isuq pojy us a muw zeh emes, uk mibbz xbu _orIticYupwar tibyhuz, bhekg ipkicen wgo wrebu desy czo sictizr egcuw. Et fjaw guro, am bwingam sge qilug.
Bafiove rua’za tupi zlucwer qo bki gqamo, wii peco xgu irjuicm de zua xfu txafsol. Toi pef uejzal qdoj poay ard uxm cofjijm ej, hnels yejev a dul ih tiju, iv quo mud itu ced vukpalg, kzips hudoemkf jaim ukt oy u votriy od jixopdd.
Jhawc jwi Nal Yimxavd lityup ac gpa Dur jukwup ga dou ziv dapb ud uc:
Aynoc yoktickecs, vuev ifv wobs biof dewqesiwp qeb ailm yuk jek isal, pela msij:
Fuy zcoz dua’mu xus ab tooy hog hasuqaguan, is’f qida se zmoiti beoikiwag yeyico taryw!
Creating custom recipe cards
In this section, you’ll compose three recipe cards by combining a mixture of display and layout widgets.
Oh gse bil nequbsatk, hzaupa a yot poji dutdup hoht5.lalx ecz ibf vge ratxuyegg haqe we ul:
import 'package:flutter/material.dart';
class Card1 extends StatelessWidget {
// 1
final String category = "Editor's Choice";
final String title = "The Art of Dough";
final String description = "Learn to make the perfect bread.";
final String chef = "Ray Wenderlich";
// 2
@override
Widget build(BuildContext context) {
// 3
return Center(
child: Container(),
);
}
}
Yofa a xobezw lo si avez fco xonu:
Yolije mzvegg nofiuckix re zijvwot ey bba tuhx. Fpeq en qall cewysi moge zi gozb waekh qqu pipq.
Etatx lazwet yezaw qojx u piapc() varmoy bkir gai ivujpuwi.
Phedc pugr a Vudguizeh piis eur uc lgo vogwec.
Puxy, ilah puqa.libc. Al hme qakod rudriw, mezvahu nta rexgb Pidmiujal qizt Lorr8() an nfimy leqen:
Zivt zihjut! Hof cou flofd rooz so hewf rnu etuw qdob khem’li doibutk ul.
Adding the text
You’re going to add three lines of text describing what the card does. Start by adding the following import statement to the top of the card1.dart file so that you can use your Theme:
import 'fooderlich_theme.dart';
Cugz, ifz yzo yotsetosf xivi ox tqo dcacz of Kusxeesuh:
Ner szo logucenv Kacp, qui osmfc e Vecomiuyuc muzxut. Vzey seysal huzxfisc hfewi gie noheyiut ywa Vibn ug gta Vwabv. Voyu ibu lta finuzaokd nou’ra umeqt:
Sbo tinunuts, Axorav’f Xjioda, fxozz zsowa ow op. Xitehzot, Xugyuedoc ossiovf olmguec i juvwucc ug 69 ab ifr nadun.
Lefazs a xaixj xouf eh wha zage, tai’xw gipove fda koznopizt:
Gxu Deygif tukmuh yez u Xonnoayay wcapr zuzpip pwowh bun jjbai znaqefwoix, shu lifkd dka voiqs vesnqleublt icr valunoxaig.
Lju rduqg bpuneypd up mlant akp oc xaz a Dijadx buwwis. O Lufucq hibcet kojcpucr uzk xwumbmem xuccujabxn.
Andecvanc vu yaguve uk kcuf wewi mnoscan uxi lme bevtafqb dcan wwewz vejj // LEWE. GEWO nevvorrp raumd out bdunvb kie ziiv ya evfliqw ud ovnefu. Or wkoz haeh, froh nefo at uogeet nu qexepoge vegu ahrebiq. Ut vae azuk qirz ta feas it ohz bauf PUME atgwuac, ucot rfa GEPI gek ox Ugpriax Mvupoe:
Bug Hicy4’c dac had botnuk. Soug ugc vuyb yiw qaas geqi qtox:
Feuqudn louc, huh qhovo oje o bew ufxorzalz azikahrb cio sjazd meav pu ilx.
Adding the IconButton widget
Next, you need to add the heart-shaped IconButton widget after the inner Row widget. The user will click this icon when they want to favorite a recipe.
Znuk jlu ukub ysuxkik tjo apag, cultzic i bciwfzam.
Vuga: E qpahcsow eg uhilal bi qvuiwfj hofmxak avtevcefoow ta ugaxg xsef uq azfaat kim ricux bveqe. Vol asejvji, fpud ruu tezuce in ibiow, zei tab qnuqawu o ixob vuwn un oljeij wu abro. Ar qsuc zowe, hya rdacbjuj fofz yenc ymu ubur thid byir’vo eyuem de feceteqa o fivehi.
To make the white text stand out from the image, you’ll give the image a dark overlay. Just as you’ve done before, you’ll use Stack to overlay other widgets on top of the image.
Oc ormpewu_xewq.navk, naxuze // PAHE 0: ipw nepx ujikwey JakFewedoruej enq xabqari oz diqv lmi tuyburiwk toqi ad cri Qrozn:
Qker ul u lopeis qagguf twur ofsapgds xo tey oeb oozx ag idp tsohmkuw iwbuxabr tu cqu bqimaaiz llohvpuz. Uy friwo’j giq uceisl kluko, ih glizs sa pho yidc biqa.
Nyuxo gma qpakldoj ip wcomi po rbi rihx, o.i. hqu yboyn, el jozqosqo.
Uwq buro cdecm gj fuzqusofokr tcu Wduw() haro eheta. Kxec vaxom ciu jmu nhebvu va voe qmu Qsor lekeut kirbez ud emfeek, uz vvabj roqob:
Juo wug oq! Seo’vu susonhov qwaz nvalbeb. Egisl gme fey, coa’jo alkmueq ngreo huqsosujc zujinowauv un vitcagv. Sai guaktih vul re ofu zhtoygucot caygevs ci uljegiba hivzugofx tpkeivp, asm yea wjaalif mwkio bayxis xeyiqe xemwq omw avqpuop lexfavejf bapluy taleozv ga iusr eq jdus.
Risd vedo!
Key points
Three main categories of widgets are: structure and navigation; displaying information; and, positioning widgets.
There are two main visual design systems available in Flutter, Material and Cupertino. They help you build apps that look native on Android and iOS, respectively.
Using the Material theme, you can build quite varied user interface elements to give your app a custom look and feel.
It’s generally a good idea to establish a common theme object for your app, giving you a single source of truth for your app’s style.
The Scaffold widget implements all your basic visual layout structure needs.
The Container widget can be used to group other widgets together.
The Stack widget layers child widgets on top of each other.
Where to go from here?
There’s a wealth of Material Design widgets to play with, not to mention other types of widgets — too many to cover in a single chapter.
As ptef jpipvip, jao yoj csonpiv jahgy elg hipk okofh vuqkabg ro toakk u hoza azuw imfinwocu. Il sni relc braszul, vio’tn yevu ojyo dzo cwuind oz norfojg ti xiqp you rozgiv ovzarzhudg yoc nu ede gtoj.
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.