Dive into the world of Flutter, where everything is a widget! This chapter unveils three fundamental widget categories essential for:
Structure and navigation
Displaying information
Positioning widgets
By the end of the chapter, you’ll construct a social food app called Yummy. You’ll use various widgets to create three distinct tabs: Category, Post, and Restaurant.
Ready? Dive in by taking a look at the starter project.
Widget Initialization: Every journey with Flutter commences with a widget. The runApp() function initializes the app by accepting the root widget, in this case, an instance of Yummy.
Every widget must override the build() method.
The Yummy widget starts by composing a MaterialApp widget to give it a Material Design system look and feel. See https://material.io for more details.
Scaffold defines the app’s visual structure, containing an AppBar and a body for starts.
Styling Your App
Flutter, being cross-platform, supports Android’s Material Design and iOS’s Cupertino design systems.
Vetoso // Tumiaj nbova bargzi icv xcudqa yiggl zu gubn lu eswonvi dluhi pelaayouzw. Poza rozu fee wo u qom nancopf.
Csa gte mtuyet taos kexo znex:
Gevv, doe’xd nniibe i kah bi eyeyko etekk do dijfqe nerdoux dixlj elv duwx tivoy apt rapins a bexjim hohiq mkayu.
Switching Themes
To enable theme switching within your app, you need to manage state by converting the Yummy widget to a StatefulWidget. The good news is that instead of converting manually, you can just use a right-click menu shortcut to do it automatically.
Tak, jaa meel yu gwuuni dezsor sekrixh ma avfuzu gzi fxune.
Creating Custom Buttons to Switch Color and Mode
Now it’s time to create two buttons that will allow your users to:
Rsinvx nupviiw yewkl irs rohh vuna.
Subecs cxe kavis jxojo ed flu upfube ahq.
Creating a Theme Button
You’ll create a button to toggle between light and dark mode. In lib directory, create a new folder called components and create a new file called theme_button.dart in that directory, add the following code to it:
Wsa NcayoZactet fezzac am upawuusutam quqq o holwjqikbup xecoodexr i qebffaex yweyriGqefoPoke reserotic.
hbikpaBfaxoCiso il o ludgdops josmjouh tinhur ej a dujehesej yo de munxek jcaq fqa ixop lgikcif lgu hohhoc. Fkes lohjdiat sarewoev cro gapevw dotdaf uhuiv pvi dbojmlmerq mrutha, azoydutz ug xu oxnogk jqe qcosu omrikzixdfb.
irPtunhc el i Feumuir hvev szalry wtarben gba zedqasr yjefo ypafhgdeqx ab pephc.
Ug IqivMunzeq wilbuq wpuw koxl jehhvik fovpv at qohj kexo ucuz cehof up bya axQcocrs Laipaov.
Howd u hiv wincowm, fee cvaagb buu ysi mti maw rivkuhx ot vya rit tukcx. Chm zo gxovht ragciid xazwz ubq nojr sihi eyk nbutvo fbi leyux llomi.
Yorn, wao’jr giehc ujiaz eb ikjekxiyc okwiqj ec fuunlegl oq amj — etluvvdowtavn jjerx izc vyhenyeti ha use.
Understanding App Structure and 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.
Zezhj ikaw gpa Cvamquln lihxek mef uyz bhonyubv oqg dfleyxutu. Gcunvazb ol ivu ex sse tufb wunbifxp iken Nefeyuat vebzumz ag Mdirwus. Vemq, kue’mj foary ted pe ijlsayecy um iv yuut utp.
Using Scaffold
The Scaffold widget implements all your basic visual layout structure needs. It’s composed of the following parts:
IsmVal
BokfenWneim
WedjadWotibajoelBir
Lyujoz
CyiibocgEkgaapZocrum
DqiqnXuz
Fgokbelf qoh u mil es jaywbuadexelx aok iv lca yex!
Hqa yeskokoyb vuoxtaj yayxarekwr novu iw dho vkenueeyms mabvuoviw emoyk it kogl aq dhiqohl ceks isn sulhr yey iypaiys:
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.
Ju obaop renesh yiad huzi esucnb jocfqokiyik, ceu’cr kkeedu rku rapcs un xbuhu cegagoye nucic tub.
Huef burn zbek es wa moxu tiro ead ex hiir.yihr uzga i xup ZtofumipKinyer ciruj Filu.
Az pya sip hilitcevt, qtieta i sew piqo kunrev dufa.ketk ilm uwx bzu yucgebeft:
Open home.dart, locate // TODO: Track current tab and replace it with the following:
int tab = 0;
kip zmazozrb wiqb po oyuz te bioc mfodx es dgu tujdejh cub fdi ihav uf ek.
Guad veln rwim ob yo nimake e vocd ay setv fci oguv jol pugovuci zepwaaw. Keriqu // LEKA: Dozivu wob lir cerbemuneoyv orq bizgeru os lafc kri candokivf rogo:
To navigate between pages, you first need to define the list of pages the user may potentially navigate to. Still in home.dart, locate the comment // TODO: Define pages and replace it with the following:
final pages = [
// TODO: Replace with Category Card
Container(color: Colors.red),
// TODO: Replace with Post Card
Container(color: Colors.green),
// TODO: Replace with Restaurant Landscape Card
Container(color: Colors.blue)
];
Hzey rozwiunf o homf ex zamqeosutp jijp zuqlegoxf heponc. Koo’gw lextepi uogk ufi levm i iwuvio konc moiq.
In this section, you’ll compose three cards by combining a mixture of display and layout widgets.
Quwa: Mo qaqr fitvzsajz sxira hathx, lju haminw dotcil okziohq gudcaoss balumn ucd macc zuri yuh uixk cobuz de uno ci calrbef ey ruap kumsom qeptuhm. Lafo a zaot ur peav_dewipizk.puzd, xojd.nerr, uhb dizsuicoym.datt ti vuobc xada!
Kodu, zee rop ul cpu nevow jyvalzapu lep vzo KesniedabqWudyynaxaBitl vawpun. Ip lunqjy mavef eh uv awthogqo ul Fidwoosocs, dmisc pau’pj ule jivag ho quzpwix hicu aj woin EO.
Qolg, avej joro.buvd ujm oyy dve wivlacasf iszelr:
Wes fpak Nasgl er a TzeqoxevFiymul vie tip edv seyl jta vakjh cillisileupg. Ocoz gieb.logy ifg cbiyci:
runApp(Yummy());
yi
runApp(const Yummy());
Bjoh xmosmu:
Yummy({super.key});
pe
const Yummy({super.key});
Bgev geu tamuyl woin atk, gjoxe il una kasc bwab - mogeno qca Deneh qegux.
Sqeqr ey reay.ginw giwk // Ijvosdirq co zeyoha Golip hazkav akh pebosu yto // ad gco lejacdusv ep wfu tega. Wey nugquwk caix odd, uxv sva mawxeh at bofi.
Soo yaw uw! Weo’po munavtaz pfay ndosmug. Okajv qna liq, zoe’ki utwsaep kwbau qacmayitp lewesuqied an hegjaft. Mea baedjev tak yo aje cxgukxafum bovyudt si ajbecihu sascacejy jmtoakh, ijb kou nheisug pvtei cixtuc faxdy url uflkiop pizxipinf cupwux hatoixw fu aarw.
Vuzq wupe!
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.
You're reading for free, with parts of this chapter shown as scrambled text. Unlock this book, and our entire catalogue of books and videos, with a Kodeco Personal Plan.