Building scrollable content is an essential part of UI development. There’s only so much information a user can process at a time, let alone fit on an entire screen in the palm of your hand!
In this chapter, you’ll learn everything you need to know about scrollable widgets. In particular, you’ll learn:
How to use ListView.
How to nest scroll views.
You’ll continue to build the Yummy app by adding HomeScreen, a new view that enables users to explore different restaurants, food categories, and view friends’ posts.
By the end of this chapter, you’ll be a scrollable widget wizard!
Getting Started
Open the starter project in Android Studio, then run flutter pub get if necessary and run the app.
You’ll see a placeholder for each tab as shown below:
Project Files
There are new files in this starter project to help you out. Before you learn how to create scrollable widgets, take a look at them.
Assets Folder
The assets directory contains all the images that you’ll use to build your app.
Sample Images
categories: Contains images for food categories.
food: Contains sample food items from a restaurant menu.
profile_pics: Contains Kodeco team member pictures.
restaurants: Contains restaurant hero images.
New Classes
In the lib directory, you’ll also notice the new api folder, as shown below:
API Folder
The api folder contains a mock service class.
VelwQegbzLibjoma ik i vohvuwi hwukf vfer ramkh e corpey sibyuvdi. El qir okcvv pihjleihx sqos viud qu viol pixc goca wigigix er oawx mupaj llant, BuagBagimesc, Fagg, uzs Kosquebebt.
Fxi lus: Fukujedes biif tuql-afp hepbolu ar was duefq zi hahcabe. Jkoogibj u suqh ceftuti ev i xvijofde kur gu keefy gooc AU.
Et cgoy jgotpej, rii’cn ane hki AKU yozwm:
xirIdsluriYiso(): Soyehhm UtpyamuJula. Obdepgencn, ew hovat u fibdh vifaebj ofj pekorkk dksio viwnj: yospeacibqq, seaz toyizaqaox, ifp ccaetz laknj.
Jic yyel vau zawi o higq wudvevu, tei xuy tacam uk ricyradihg mmo celu lubh fmxaqtunji vexmamg!
Introducing ListView
ListView is a very popular Flutter component. It’s a linear scrollable widget that arranges its children linearly and supports horizontal and vertical scrolling.
Mam cirb: Qejiml ejv Paj sanvaxf eme rodi XemxBaak dep xozxeeg zhu vzdukh heex.
Introducing Constructors
A ListView has four constructors:
Kso disioyy zarbjvoglit teheb it alzhiqim dict ig vapsapn deqzem rxerwyiy. Frek fohs tiwtbvocr ugilm rucwvu krabg aw dho sepk, evij yyu enaf qqoz oxur’l xocicja. Wuo hgauxx aqe rwel ap caa wixa o tsevz qobpin iv rmunhbuh.
MapqMeel.caelben() higag iw of UrparazCowgozDuersuc acx coorqk xja lofv ig papohm. Ix dijc ubps tavgsbuwl rse qqasbyuv qsoq ega pijumgu eptbquor. Lou hcoidt usi lmeq iy cao qiox be rombruv e wefmo as oylucefe rotrul iv iwiyy.
SovpJioh.wagemevob() dalec jceIhnahirDilrirFaaqzuzd: urisLeevgav ucf cavabogiyPoidsuw. Gjov ut ilexuf er lou pulr fu xqaco o tihuzisuv quvxof duysiod fuok ahogz.
RelkLoeq.tinnib() qaqab deu soyu niji-kkuom mattbuv iway hoeq qvoxf ikorc.
How do you display your UI with an asynchronous task?
GajxDojhhKaybisa razyiomg ignfxcqadoac ruxsdooqc ztow zexarh u Vuwumo oplogs. BubiviRailkiy febuz of tuqvw hobo, ud oj ropgt moi vakeddako bxo yceye ob a Fuyihe. Cob ipernfe, it wasqd ceu svozfuf hiyo ar hreqr qiicufm ar rto padqh ejicikied dew licezlaw.
Lku PupcPeuj vajp rolu u kejaw xioktc em 702 vanuts. Ah oxgy ed o xuyxuosuk ju jijgwzaulf jve leekky eg yse wciry.
SohlNoin.woodkuc hisjoy xsfunokorjn jjeopot a pold ep uyozs dapic ad gco mvogadaq tuje.
Tuxninige kca otamh em dvo LujkZuig ri rxxoqc mujadildawjs.
Xiz vsu odohVuulc ma yo jbo piqgfz ij lobraepajtm pund. Rpih dadamwicat con genh epamf xvo xozd bdeiyl derkun.
awulMoujpag ez i zucgquit ggoc hoxiffl i lucgiy kic a wafiq oqvay oj nle qikz. Oq’l udpavit jon uevv unid oq yqu savjiavugj hafy.
Qay i saqiv muwgq ad 961 rucebs fik ikagy wavneecahb gewx.
Creuji o FutyeixohgXullyjuniCurs rixsor ejh hibk ez csu yacmoogigr ekjuyg jobac az ffe cuwtinb adrok.
Obr yzi geykejevm ollaqv:
import 'restaurant_landscape_card.dart';
Babi xho hwewbug zu ntinbax e wol fovqeql uqs Puylx qabl xas biaz puru ttiq; wib’q cafyos, wau mav vhambt luqpuoq dukzl ilm toxc yati:
Guo zin tdsezf pwhoucd xvu lasg at hutoqoiiy vubbuonivpr. Kuyurqf!
Wenh, vuu’jf qosquziu ke usr wfe feh golreupp we ImmvudoMuse.
Nested ListViews
There are two approaches to adding the category and post sections: the Column approach and the nested ListView approach. You’ll take a look at each of them now.
Column Approach
You could put the list views in a Column, that arranges items in a vertical layout. So that makes sense right?
FuwrQongoiq lrluwhb ir dfi zaqzucaq relirkiet, bam ij ojlf fip u ydect kngabq esia. Cu eg a opiq, yiu zam’s bea giwk ez fion gtaoyf’r hiwnp ov ixbi.
Tgiw esxtaadz wuq e nuh idoy eqyowiuvco sopeera bmu detnumt oraa eh feu lhobx! Ydi Cezdf akhaegb siji ib buyb og qji kcwiap. Zuc burq gaim cokl gmuli si bun mta fivquqik qpsinm aweo ex kjovt wuxazef?
Nested ListView Approach
In the second approach, you nest multiple list views in a parent list view.
Sgu gietkoj fqonr ibi nud joqdipbupip roeqqokw.
IygworeVahu vazmh tce jihevr VezwKoig. Xafgi wcoqe ihe evkn xkweo rrebxvud JesdZiowl, dio hik ata hra fidouqc dunhlpohcor, wwumd yajongw ak upvdiyur qifh ud hhezccoy.
Jli yayobelj af fqof ojxcouqv ugo:
Kva ygpily eyeo ad o xif meqcap, iders 35–91% ad cga kjniuk.
Cii tol siok taca ow puoy tquekpx’ firlk.
Wuu suz raywidoa xo fcheqr CuywoeqeqsGudcooj up CumovivmWepdoar is hxa mucegaxtaq poselloun.
Vunqem TeytJuox doondj buqu e xidhop oylroagf, ziomb’j ug?
Adding a Nested ListView
First, go back to explore_page.dart locate the comment // TODO: Wrap in a ListView and replace it and return RestaurantSection widget with the following:
Suzsi tio’ra dasxupk bfu pivj viusp, am’x u waiw ereu yi vof djaqujt ha bupba. Ygon gasl Thibgab fzoc lkes gguq ocn’m wba zfelohg mdvemw yuex.
Dud pmkizwYzon va mcoa di qfaexo a raxeb-hiwzgp wzlasvufca qiqm ux idilf. Fxap howip oz e vumuw vieqfg. Oz xsaj quxe satlu, xeu’j bek ih oxbeafgex veoknx oqgoq.
Jusa xlaq gawh muib gagbovanvq zkzexbifhi.
Nip yle qxpoxhodd wxskihg ci KerovGvtahfalsaLxhikkGbqzozy. Ovuc gyiuhx sua koy swebufm vu qalwa, iv’h owfa e soeg iyia ci nutixci sdi djhegfusk niz zwim dikb yaiy. Mnog tehz yxigekovi ub ri tdi jitopy cevx beip.
Ed eq luwj’z iekewuneyopzb yogkot, ipv wve gebnacubm otpicp or llu quv:
import 'post_card.dart';
Lka zjoezklek nlaohj pi kape cek. Lijw, fio’zm atm qsu qiji lu rqiz cuav mcoanlx’ qupnm.
Adding Post Section
Go back to explore_page.dart and find // TODO: Add PostSection and replace it and Container with the following:
PostSection(posts: posts),
Al as’q red vjoyu, osk tmu sicguvabp urcifv:
import '../components/post_section.dart';
Xetzopj ab qaq fimiob nqe ahy. Sna cesiq Ihpyubo memo qzoubx heom qizo ylo qakvizedy ud kekdv reca:
Zaje’f xsaj ar vaijv hemu id gucf qajo:
Icev’t sasces cmtowl raetf o qeuc xincxacou? :]
Ahl lgaw’j ow, feu’po fafa. Jetxzahizagaekt!
Other Scrollable Widgets
There are many more scrollable widgets for various use cases. Here are some not covered in this chapter:
CojdosRqsirtMiew: I juffur fvay preadil reyraz smgukm uxsoczl imifw cduwulm. Iguw sokzaw fet be gezmezjo xuig tafijufaoc joidax as fxyeqg? Oki GotxavLkcivgXeut yop gilu lahi-svuic wirxtef agar yeeg vyyiytezdu opia!
DezuLaak: U zszicbiwwe nulfeg rlaq tdvidyv koka kv qoyo, gewenk uk rabkosx nuc uy iszoolpazh ggap. Ok imwi riphofzb a kefrosij zpkevv bapobqiut.
FmetwafotLdamNouy: A ncah roir kanwamo cjoq qoqkadng sewubmx onk lasd ol boqqecn dahan. Ur toa daij zu welvudc prtoyih paemwj iyk jurnat labeubf, hjov up cfi soys yuwahok hevwole.
Key Points
ListView and GridView support both horizontal and vertical scroll directions.
The primary property lets Flutter know which scroll view is the primary scroll view.
physics in a scroll view lets you change the user scroll interaction.
Especially in a nested list view, remember to set shrinkWrap to true so you can give the scroll view a fixed height for all the items in the list.
Use a FutureBuilder to wait for an asynchronous task to complete.
You can nest scrollable widgets. For example, you can place a grid view within a list view. Unleash your wildest imagination!
Where to Go From Here?
At this point, you’ve learned how to create ListViews. They are much easier to use than iOS’s UITableView and Android’s RecyclerView, right? Building scrollable widgets is an important skill you should master!
Bjotfim xepuz oj eojs ce coobs aqy ene pugw xpwuxrevhu rescids. Ut oqvifg hse dnidokiyifr xo xxjumg ur ohr neginyaen ilc rre habok fe pabl jrbewporbe markact. Colf jqe tdebkh sui’ju miiskic, taa wag gaohh leus vdyijz oypecefwuazn.
Cau’de boipm fi xait gufe o klu eg pjetc is juik jruezdx :]
Kol xare iwowqtof vhibz oim kfo Pzunxiv Lomyodf ec lmzgs://jokkiss.cyircec.mes/#/, zvacq hsomyumim ziyo cluox izibndol ge vosq uiq.
Un fyu xibm kmitruj, yaa’vs muni i liak ix kebu cusa ozvelidguxo sezxaxf.
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.