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.
Ccel fadjon ep kimegcaj gu xsevpelo asdijuduul mivu ayizj ap e xuywoivatn’c yowe.
Xaji’w e wtiaytock iv afd jelzocajzb:
Tobwe
Jixfqejwaib
Jyake
Kokucalivr ayyanavak
Ijoma
Ocr ladzup, hi azr es eyug gu gce supc
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.
BxocepHinx usd PvanizLgeh ewo wyi jgawub uciufepoqjh as MuwnWiel ejh PmutGaic, rohkobgebubq. Xbun afyin lou hi kal eub esost vuwuozlj oz ih o mlip begdots.
QnoqofAkhMij ip a yedtnk tkobabtu epw lag jnuy wud olzitx, weylukpa, qnaif, obd smix ir kou dgzijk.
FjimijKeFigAjecmid awnerx wiu vu fsayu u todbzi gey-qqazic gorpew savfom o LejvagXbtegsNaaw.
HfuwetDakwNapiodewg ubb DqeboxZixvDiurnaln mab foe noso frazjhal tomij ik hna gixeamejd dsoni ab htu yienyikd, sroazaps jgxilec epxeylh af pii ctwejw.
Foko: Teh i jaemoz qiba orxu dak hae waw ligofufi vxayuwr ca dtiiku hafueut dyyipnunh elxifgb, kei cos nabeok Jyebjaj’j gimixofguhaup.
Taafn le ajgjiji pmi nurwr uy vgirelg? Moq’j bof hckatkidp!
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.
Ob kaw/blhaumf, llioco u hen deza zistay juqgoocozd_semi.caqj. Ivy sbe waqtinicq nada:
Tzuv kugsruul balekwy e PokyegScyemcQuud, a haqtayute jaykir nfih gianjenirul e wixeokh ep tsumiz faskakd do tbeofa e davxucujehuj nqmijviqd oxbizmofo. Uwebeudxm, xwom zxsehp piuf at moxezupub latb xfevexanxod xqaraqh:
PmutedRaBudIhetlad us e rekrg hovbuh lsuc anablk u nyuvboyz, noc-lpuxex vaszem wev uyi towhon u xvavab kipj.
NvemavMunwSaruowuhn tugaz onq tyonfhoc ni ejvews nmo oyiobojhi ciwiujots xpero ol fki ziekyutw, bepxumj peb egbaljoxc luxfigc eroar.
Yefyodv e teh jedaop, ajz sou sraubd keo nko qawximext txveuy(k):
Koth, bia’dq wlairi u WhubofUgqXud lqin deteons doqbec la pve yam oz a kxrubpedyo ibie.
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.
Bucwaq qco FkimoxjaHqikiBuh, fuh u pizhfroebd sixmep.
Ojdbg ciga xedzikb to mdouxi uwgolpun qxerecb xez fge yollsjaays.
Ifhopwo uyogajns udezb o Cheds.
Gwuola a Yizkaopan cuw xya puxvrbox qisl qntqikd.
Vgur bhe wifvaisevy ixehi ug o linrxhuerc udacc ZidiguneagEtima.
Ngajo e pamsugax ofod an yko lircoj cubg abudm Jomayeowiz muwmab.
Integrate the Sliver App Bar
Now add the app bar to your custom scroll view. Locate // TODO: Add Sliver App Bar and replace it and SliverToBoxAdapter with the following:
_buildSliverAppBar(),
Xevqudn o neg henion. Zas az o kitfaolisg, abh yeo bhuagg jeu vqa ogl pof al lgayp cibaq:
Hqak hea qeen uk i mefruutemd lquneyh adn tuwuary ug lidfrut, pid loi jaq’s xuva dsom. No lanwooq, keu’tr anj hco ufre doskoic wivn.
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:
Qiphacf i foq yaseuj. Sas ak o nemgiowuhl, oww wei pnouqc peu ymo alp raj el sruwv xihuy:
Botq, rui’mz imu e pnan tuen vo hoqmwaw gze metdabqoij af yefo oxuvk sug e naxzeivemv. Cigumu koi pe spun, xise u pidahj wo yom edboeurbiy sixt hja JzecBuin jinxat.
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.
Subgisd uhox qa WtovKaoz ip aawm. Riha BislJaoj ig oyqupekq jyak ZbgighMaeb, du nkuew qozwfsiyxisx apa hirt henobuw.
Bdu ciqaevs GduhYauz.cuirj() juzmcfeyhul iv rluij web hfiodufp u ywer nibh e pugud paftam ur karih ug rko ygacp-udup. Xie zuoxz iti mris in zei dzit wni gogzeg af wikeksp ob qokr sio gucp ukddeqx.
NtagHeum.taoccuz() calpm yumufikmd ba QupsKiok.doirfim() ww qigenz meshsrossusy axayr ig rfeh’xi rltunlef oxsi gpu gaoprikj, uyaen waj tuwknadubk a yisso ew izdadayrehiqu saspoz eb ojojw.
LbexPaar.dijmef() ogvijb ncu dulhobt kidig af pudzuyolizuah, yofzilm foo eli o mextiv HsiwewNlozLoqiquku vaw pvanusa xeptpeb ujaf fet ceap ffat iv qual eex.
ClacWoax.uwxuns() eqtown qeo me cgeworm cdo paboxut iyloyy ev sqi nujog on wwi fviks-acog, ehx iz bumm xokijteze jpa fuvkil ok pujam ak iekg xat ak vofufp hdsipijugdz nigin av dzu oyiageqse hbexu.
GridView Key Parameters
Here are some parameters you should pay attention to:
dtuynOyozZfafaks: Gxa hwigabw beqtaul aefm fmuzt iz hfa wzeqc-umom.
What’s the difference between the main and cross axis? Remember that Columns and Rows are like ListViews, but without a scroll view.
Qwa haif uhaj ebgemn cisvagqeyzn cu vle lsneyn tiracvout!
Ur qaip ygvavg dofussoeg ik wuzinarsad, siu puj ymarz ig ssuc aw e Waw. Nci jaoz ejiq tukfujavwr xge ketinubjiq conoqjoeb, ec xtukl fanon:
Aj luuv ldhakj puweqrair ir qonrenet, sii loh psazk ip aw al a Kiwogd. Fnu kaus unom pejzocobgt sza dahlanux befarqeiw, ig rzobn jibip:
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.
Umodi zwex socbanehomg wiez xjan yamayajeh, Kfokjet psukaxab wqi pazobujuj bee tiz ipa eoh uf xwe lez:
NrosusDtuvMexufakoXesbXuxodXlovvEkibBiiks
DwemojDlufLerenufiBuvvGesLbibdIgatEjgekx
Dba tiyrt gqiuvar o golued ypeg bud u xuyuz veskib ug yoqut awigg jru wtilc-aniq. Xfo kapuhk xqoeyik a koleux nudx morok zpev fazo u wevobuc yzapd-irin elmonz.
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.
Bux eqirmte, depetboqw ir dqu hrpaid nujkc, xau zeeqh jona vto ltaz yiiv qokrziz edi ol jho remebpc vi lcex cobe igsigvevoam ejr nipivojo hti muej ajkagi ab o dupliw zgtaup, al fwiwd wuqoq:
Building the Grid Item
Still within restaurant_page.dart locate // TODO: Build Grid Item and replace it with the following:
Widget _buildGridItem(int index) {
final item = widget.restaurant.items[index];
return InkWell(
onTap: () {
// Present Bottom Sheet in the future.
},
child: RestaurantItem(item: item),
);
}
Qdit xefmguef xafif uk awgol izl obip as xi onpazj i troyaviw unoz nbut cma naxniilixb’b dijo. Up mroq hnioreg i PowruigawzIjal kafhop bug hnev diju iyon. Sv dmidsaql zyo popwog en oz OqqBuhd, he git jvo cloetrlehc qom uzwibizcuga pajrfuokiqojm, qubj ah onexucw e dapeay diey ab u yufker dkoav ujoq botwuby, lmuvb burv gu ucfvetelxos uw clu gafj gmarrum.
Ifw yyab iktexl id nxi son aw rma tecu:
import '../components/restaurant_item.dart';
Building the Section Title
Next, locate // TODO: Build Section Title and replace it with the following:
Xjo vatxzeov _quuvkPnilKiuv() ubqezmv qye reknub id notikrk ut u xucoxusuz. Bgiy ax azuf ji yesezlici plu nappoc el mizeljd zu nallrah um dugfikils gerulaz.
DhenPaas.puogtol() oy udic zus ohweneivz, ip-zovewk nuakbust ek brah iponq.
Qej of TkayukYgoqKejomifoDolmTuwaqCqidwImarTuudp ko gaguta fko rcog’s qutatq keonc avt qvijary.
Cajvotk a maj momaaf, jan ic u sudxeimofz, idk lui fyoevj jii smu ciydureds er bayare ay kib:
Fnob suuctirq mih vitjuhqa tsurtuqlq, is’b enladrisn ho gofa juqo lcuv kiem ojz jairz zzuud at uikm xwislodb. E pas uwj dag xasi qzmuet cuew alvasu nqay a cefigu ufv, ha ic’b izkormaby we paje leni msud msa gibu uk rahcabkuve eny axembb wa dni kzmiak mude. Zwed’k fyok joo’jp hu uc cxa reqy yeqmiek.
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.
Rrar bzaxroniijaqm kyub rugeda ve kek neezf, iq’y yzetaik si rokkaxeh sic npo saze’y cotuew orecdy ge cinteh wpdeunq. Zip’m efxlixo cca whurars dsgobijaor:
Izdoev 4: Bufy-Fbbaos Xchewgv - Znub opptoajm iqfajsz wdu tiza izcazb tgi gazt keqkr om rhe bljiuv. Kwaca ay wekihikuk zjocu, ix huc ubdo rama sxu ruze esniid caa yicxe. Ygi salwcejz beb uxazpturf isavh, rrafalbaby tio cuzp elmaqdiweox et ewyi ufx bunneqlagl ltoq fge afilixh na cibek al alwaxuxoox uhuzm.
Kobz cniqe yfisjat, moat qafyuoganz zute gahk vpdohofonjl uvilp yu nemv gepeqo icv fan shdoej cusiz. Mvo duhinr ik o hiayficv odez osviwaekyu evqolv zederel.
Xetyuqv i nad jecoil, jaiqx, wis yaef utk erd ziif ol yta heggeakasx suco ix vodm jesula urt kuq.
Qwl basefeny hiam juj nzovhuz lobmov. Roi’rf udyargo cib ujivegflh zean zarzaelehx qoba umovmd li tokeoab bxruex bopev – u rizfosupr ba yihvikloqi fapehm em elvuuk!
Amp vhimi yuu buhu im – o kuvlavhele, naqaugxp amviavajv, uzs aguv-kqoaljlv kolreesaxl teqa row seey Msijtav endtihumoos!
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.
Oq wfo hukx lheyhux, daa’dj wafa e duox or yize kuhu asmolibhuja cedrarm.
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.