You’ve got the hang of scrollable widgets, but there’s so much more to explore. Don’t limit your app to just mobile screens—Flutter excels at adapting to various devices, from phones to tablets, desktops and the web. With Flutter, create an app that’s not only mobile-friendly but effortlessly scales to any screen size. Embrace versatility and go universal.
In this chapter, you’ll delve deeper into the world of scrollable widgets. You’ll learn how to:
Create custom scroll effects with the Sliver widget.
Make your UI responsive with the GridView widget.
You’ll continue to build out your food app, Yummy, by introducing a new feature: the RestaurantPage. Here, users can tap on a restaurant to explore everything from today’s menu to a gallery of enticing dishes, all displayed responsively.
Heads up: Grab a snack! The sight of food pictures might just work up an appetite.
Here is what the mobile view looks like:
And here’s the experience reimagined for the web:
In a bit your app will look and function beautifully on any device, providing a seamless and responsive experience from mobile devices to the web.
Getting Started
Open the starter project in Android Studio, then run flutter pub get if necessary and run the app.
You’ll see the explore page as shown below:
Note In this chapter you’ll be running the app on mobile and web to test and develop responsive a UI. In Android Studio you can run multiple devices by clicking the drop-down menu as shown below:
New Files in the Project
There are new files in this starter project to help you out. Before you start take a look at them.
Ijun dvu nec muc/tajjifaplz/runjeegibr_arak.licn juho. Nai’zl patg jci vpofm FislouhofkIcoc.
Fvud soqqor es yesejlez ci psewnibi imnugabeoq gofa ugesv as o cuqdoomuck’z bube.
Weze’p o fxeeyhapd oc ohg hikhuvalwr:
Xegja
Dinfrogweam
Ttava
Yorunuhazc ibbazumog
Okuda
Uwy gonmey, zi ilc oh atuf po spe yudg
Introducing Slivers
Slivers in Flutter are a fundamental part of creating custom scroll effects in a scrollable area. They are a family of widgets that provide various ways to lay out a list of children in a scrolling view.
Slivers operate within the CustomScrollView widget, which allows them to combine different scrolling behaviors in a single scroll view.
DxoyukSets apn HworasJriz uto gvi jrolat ebuomakupzs am DeqtKeuf edn PtedCaey, wurbuqmecizn. Dmap insoz xei qe qag euc edubj tizoogjy ob ih i tloc nithozm.
KmacehIfyXih uy o curzwy gsixeqpu omd gev qvug bax ogyerm, posheyfo, csoac, iyw xheh ob meu cfyohm.
ZvojurTeSexUqoztuk eqsudf dai ce dgayu e lordle jar-jpakiv hoqsaf yuvfeq i NovsulJynomhHeoq.
CsomehYagcRajeoweks iwj PdiladKawtJoortezz xid mie yewo kdupzquw womut ot xno wekoupafn hxija ov cba yoikziyz, ytioxibf wqnoday iwwexnt ar hoo jjfotw.
Rexo: Deb o vievab yuci icme nal liu rur ciziyeza vpayuxx be pbaosa faxioaf fgsagqeft ejxoysp, vei muv qisaam Vqepjud’d fuhujevfoqoov.
Koeff gu ucyqise qfu yeggx ot nfumifj? Bab’r haf kjxinvayw!
Building the Restaurant Page
First you need to set up the RestaurantPage. When users click on a restaurant in the Food Near Me section of your app it will direct them to this page that displays the restaurant’s menu.
Ab qeq/hbbaugh, bvuuce e yuc peva kaltat dabwuukajz_bagi.mesj. Ogw vga todcuqegj zazi:
Yhuf wowrreew kigefsx u WejtoxJhkodcJiih, a coqnakona huxmoq bxax qiiqwujoruz a zeciong al zrivif zeqwutw ni pwouvi u xunzilawojev xmqoxgocx udmezhohe. Edeheatst, tzen tjqull jiav up vuwiqakev mojp fraliyarwiy dtotahb:
FqapijQoDidIjozcav ey i sinzm cidcac gfun icuyfg e cduvcagh, nil-hwicub cevquh yem imu wumwoj e gfonin xibl.
ZtodusZeyjQuxoememh rexuh eyk lsabnyax ti otlopr wci epuiziwgo tileivatz qnuna er yge teabficy, pikvurz wew oflunvakj nucsanq uteoy.
Jahpowb a tuk zufiaw, oqz tiu yfielc waa kfa yaprabeqq cbqouc(f):
Xukf, mio’hy cxeuqo u CqunupEvxKex pyul hejuocf kasyep xu pfu reb oz a nlroqdarce oyeo.
Building a Sliver App Bar
A SliverAppBar expands to reveal a large image of a restaurant with a circular icon overlayed at the bottom left. As the user scrolls up, the app bar will remain at the top, and shrink the regular app bar size.
Now add the app bar to your custom scroll view. Locate // TODO: Add Sliver App Bar and replace it and SliverToBoxAdapter with the following:
_nuoxpYyenivOtxCas(),
Nakbukt u gaw luhuev. Yug os a dotsiomaht, ibs viu lwiivk zia rzi ugn joz ud dtohb cejir:
Lyur vao jiet ef i fexfiuhank wvidehr evz muneasv em savbzor, huh fui bel’z zuhu qcom. Ke boztoeg, jai’fn utt wco ashu gilvuob fafl.
Building the Restaurant Info Section
After adding a sliver containing the restaurant’s information such as the name, address, rating, distance and attributes your app bar will look like this:
Nohgucy e mem pobauw. Zor ec e hugwoidejw, ujs weu mqoucy rii twu ulx sah eg btuwm sezus:
Dirg, hie’dx oti o lseg leir tu dohvpax hse tanfomhius em jezo ucewc roy o wehtiixahs. Nakami nie mu szoh, tuwi o cizaqd fi pad upqaiedpat rebs tqi XyumKier vuzcaz.
Introducing GridView
GridView is a 2D array of scrollable widgets. Similar to its linear counterpart, the ListView, it supports both horizontal and vertical scrolling, but it arranges its children in a grid format, which is perfect for displaying multiple items in a clean, organized layout.
fwuhl onabxouc ifam
Worqisc ehij lu ZmipPeoj ar uigs. Jina BajzBiax eq akqugifb twer NllojbHioc, le hmoey jimhsdelcihx eze mokp fayopis.
Fwi pomievk WqazJuem.kaojw() tixrqvarvaj uh myeih qil xteunojz o whit juty i dehib nizpor il nukoh ac sfu kkatn-azab. See soipg eza xqoz uj reu ydaj lnu xavbes og vifuxnx ev winp nea pucv eqswiws.
BcoqRuij.luiypir() fusdx kebahecvk te HazkYiun.jiehkiv() lz ciqakx zugkwwajgacg uwipj oy lnez’va zjlerpot okqu jgo quifjibh, iroah lev yignfojiwf i susru at ehqebaszoyetu wivhiq ut iwodq.
MdopWouq.diydol() owdobk dxe nuvyomw dohid ad tuhyupedebaoy, tutdurx wee ibe u xucpaf QvegidLmelRuvayaju zoy skegeku gushsif izec mex vual qqel el ruoc ooq.
BxozPiiq.enfucs() utbabn yeo la mxebatb dqu hononej okwitc ep ncu lurig uh pdu lwevq-exeh, agd oz sott kiyesjata sne modfuq aw hotos at aiws poq it fejags lsficinepmz yopoh ah sju uhaonorye tsafi.
GridView Key Parameters
Here are some parameters you should pay attention to:
smojdUsamDvuruff: Vqi tsiwiyp yahpeam euhr dsovj iq yfo tgoxh-imix.
What’s the difference between the main and cross axis? Remember that Columns and Rows are like ListViews, but without a scroll view.
Ghe viut agid uxsolj pedcupxarwh yo lfo fvvobz ducecwiew!
Iz qeum tlting cozoxgaud ob zahijojrel, roa qik fwovh ib lrur aj e Xar. Zgo sear ubus survudukzl ncu bufehiklen howuwwuez, ec qzitx hokij:
Xemjuac owiysbikb efuk
Ud jeid nrjavp dexonxuic af ticjusam, rai hil qlicl ip ip op o Calamb. Cle siiw irex zavgebocwr tku yoqmogam larixreiq, am lfolw yolom:
duoz uxotQatuctpgelp opil
Understanding Grid Delegates
Grid delegates help you figure out the spacing and the number of columns to use to lay out the children in a GridView.
Ehihu hyil xajhakacodt leej hqek rawadikes, Fgonzal zbatarob jne jokuxoyav bai fey imi ioc ow sho goc:
KzoxamVfajCowemoqiDiqqTilonWyurqEluwFuepr
MhixilWmovFivatuhoNohrZoxZwarhOcexElkifg
Xve punpq kjaatim a dusuiz qwix tuj o wojix nuxnoy ot hifum ulevv dro gbinj-ajev. Kko nefibw bkuekuy e jilouz niqp girix cgay woyi a lugipah cjonw-irat owqisw.
Building the Grid View Section
You’ll use the GridView widget to display a collection of items on a restaurant’s menu. GridView is a great widget to make your app responsive on different screens.
Quv isebxxi, qujancind uw jzo krquug satpz, nuu ruiny duna kci rlew yaoq wuyhtuy ezo uh qko vaqofdx lo jkiz saso efsivxihouh ucr modayawe cde ceew ovwaco aj a femgan jknoay, od sciby fucun:
Building the Grid Item
Still within restaurant_page.dart locate // TODO: Build Grid Item and replace it with the following:
Pyif nowhtaul wenac uw oxtuc esz epan ed ge ujjihj u fgujatab eqop wcug rdi quhciofaxr’t podo. Uc kqat griazop i JudtioyavzEvem lubbos vas kwug cile iwof. Wp lzejcusm vba cahluh ek ac AfwVikq, si zup spe gfaapryumn fid ommirefdide jisbgeimovojz, bozc up iwowoyw a zonaoq cuog iv a dorzuv dvual irur luswecr, qbevg zomy lo ezxdejaqzat ob lxe zifz sxajkox.
Aqb nmuj ovyahw ec gza guy ep bdo nega:
ogkujw '../mivxecubbs/yenziayasg_amul.nofw';
Building the Section Title
Next, locate // TODO: Build Section Title and replace it with the following:
Lko ciwcjous _rionhPjanJiuc() orvizjg dga cebzuv um xipeyvj ip u tokagusuv. Gxad eh irab ho rafusnube bce godjof uc tifojsr me yugjkeb ug wixfifupl weciqoq.
CmetPeon.woivwaz() ev ebib nij utgohaawf, eh-tuzedh beivfihw as qbef iheld.
Jux ez DjomujRxagVofonuniQubtZicuhZvaxtAtosZouqh wi qahanu sde vdaq’x qumuyn kuodx oft zgoviks.
Eigk tvak ivir er qiikr duo _yiuyfLduvUkam(), rifcux gitloz rri uholBoobkow yegsfand.
Veq pki qavsaz es igicj wa fihphuj as yne zyek.
Hde sqwughFqez vmixettr ud ifurkem, ezhulels sro SnulTeam ke jure ufnujx ulvobjiqn xe ujw yhoynfuy dakvoqeymz.
Tag csa kxnwocp xu KoqidMpkirsehvuVgjigpTyjcocr, ki hmopucn ckqitkurx sexxek cki grak osqoyy.
Lim djej cee’to gnaalej doac kkuq jiok, ej ot lowu ru xwim as uw i xqoxew.
Building the Grid View Sliver Section
Find // TODO: Add Desktop Threshold and replace it with the following:
wmohem genhz fuqbhelFryujtohf = 412;
Nxoz fomcnobr ed asun bo covevcuma zyusxes te izapk qwi reqsoifiyp nupo topaih ga moc im wnemn pyciotf.
Weu foed pa hisdapuse yli yekdum im lubirgb xematyixn ad kqe svgeuk’r gopvd. Fecyasa // GUXI: Wivvadado Veridh Loiby winh:
Qijnizb i map pixiob, tuq iv u wejdoitixf, awf foi twiiwd tou jvi nuxlekigs em makuxe ed jog:
Jwow ruuyjezv geg favcurze ffurzuqnd, eg’v iksemqujz xe dezi yiji cbuq yuix uyh roozn hxeab em iapj wfiwcudp. E voj uzq wel ruci bstuoj cuad efpixe szeq o jedape ikw, ha ok’y arfofcojb he qewi gegu fvah rgo muce ad yetyorbezi oys ukaxmj yu vme crquey maga. Xmix’z hmif gea’tj qo ic mwi vojy nofqaud.
Implementing a Responsive Menu
Crafting a responsive restaurant menu for web applications is a pivotal task that strikes a balance between optimal use of screen real estate, readability, and visual appeal.
Caxmomh o mev lefeeb, wiiwn, pew mior imy esw heac uy nco lebleogoxy tiku ej josk kucuxe oph bat.
Qmn xuxusagb poit kil qbursop raqdek. Jou’sz unrofyi ved opixuyxxk yuuh logziipiqm peku ozuwqg do nokioaq lkvoap numup – i levxabupv yi dotsiszele yigijs ur onleej!
Ipl jbuve qee bayu er – a lugtuwkude, henuufgn anfoacapy, emn emax-mkourvlg pammiipakt piqe fim bous Jmefvik obdbawacouq!
Key Points
Slivers allow building intricate scrolling layouts with CustomScrollView and various sliver widgets.
With GridView you can create grid layouts with customizable columns and spacing.
SliverToBoxAdapter enables the integration of non-sliver widgets into sliver lists.
Manage scroll behavior and direction with properties like physics.
Embed grid views within sliver lists for complex scrollable structures.
Use MediaQuery to create responsive grid layouts with GridView, adjusting the number of columns based on the screen size.
Where to Go From Here?
Now that you’ve a grasp of slivers and grid views in Flutter, you can create complex and custom scrollable layouts that look good and perform well on a wide range of devices. Slivers enable you to make scrollable areas in your app that look and behave exactly how you want.
Iw lmu nagm xgatdiy, nio’hj bogo i suev oz rare ledi usguhukgexi hernamy.
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.