How to change ListView.builder background image based on certain condition in flutter?

1,834 views
Skip to first unread message

Niyazi Toros

unread,
Jul 17, 2018, 8:02:38 AM7/17/18
to Flutter Dev
Hi,

How to change  ListView.builder background image based on certain condition in flutter?


I need to change background image with '${_TempData[i][2]}' condition



body: new ListView.builder(

  itemCount: _TempData.length,

  itemBuilder: (context, i) => new Column(

        children: <Widget>[

          new Divider(

            height: 10.0,

          ),

          new ListTile(

            leading: new CircleAvatar(

              backgroundImage: AssetImage('assets/dollar.png'),


Joost Lekkerkerker

unread,
Jul 17, 2018, 8:19:05 AM7/17/18
to Flutter Dev
A lot of magic can be done with:

statement ? (if true) : (if false)

so if you want a backgroundImage only if _TempData[i][2] is a certain condition you end up with:

_TempData[i][2] ? AssetImage() : null,

if _TempData[i][2] isn't a boolean, just replace that statement with like:

_TempData[i][2] == 'dollar' ? AssetImage() : null,

cheers

Op dinsdag 17 juli 2018 14:02:38 UTC+2 schreef Niyazi Toros:

Niyazi Toros

unread,
Jul 17, 2018, 8:46:21 AM7/17/18
to Flutter Dev
thanks @Joost, my problem is that _TempData[i][2] == has 4 condition. if use than load use image, if euro than load euro image, if birth pond than load sterling image

Eugenio Tesio

unread,
Jul 17, 2018, 9:42:49 AM7/17/18
to Niyazi Toros, Flutter Dev
Generate a method:

AssetImage loadImage(int condition) {
if (condition == 1) return AssetImage('assets/dollar.png');
if (condition == 2) return AssetImage('assets/euro.png');
if (condition == 4) return AssetImage('assets/blabla.png');
}
Then use it in backgroundImage propertie:

new ListTile(

            leading: new CircleAvatar(

              backgroundImage: loadImage(${_TempData[i][2]}),


El mar., 17 jul. 2018 a las 9:46, Niyazi Toros (<niyazi...@gmail.com>) escribió:
thanks @Joost, my problem is that _TempData[i][2] == has 4 condition. if use than load use image, if euro than load euro image, if birth pond than load sterling image

--
You received this message because you are subscribed to the Google Groups "Flutter Dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email to flutter-dev...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
--
Eugenio Tesio
Zetus - Soluciones Empresariales
División Sistemas
(+549) 3564-15-599945
San Francisco - 2400
Pcia de Córdoba - Argentina



Reply all
Reply to author
Forward
0 new messages