Home > Mobile >  if else in Listview Builder
if else in Listview Builder

Time:10-12

I have two design widgets for the main page is the first one

child: FutureBuilder<Kategori>(
          future: _futureArticles,
          builder: (BuildContext context, AsyncSnapshot<Kategori> snapshot) {
            if (snapshot.hasData) {
              final articles = snapshot.data?.data;

              return ListView.builder(
                  itemCount: articles!.length,
                  itemBuilder: (BuildContext context, int index) =>
                      customListTile(articles, index, context));

the second one is customListTile2(articles, index, context));

I am trying to put a if-then and launch the first design and second-time second design and again first design...

is it possible to do that?

CodePudding user response:

return ListView.builder(
          itemCount: articles!.length,
          itemBuilder: (BuildContext context, int index) =>
   (index.isEven())?           customListTile(articles, index, context): customListTile2(articles, index, context));

This works like so:

(if condition)?(do if true):(do if false);

CodePudding user response:

Use if and else condition for diffrent Ui like this:

 ListView.builder(
       itemCount: articles!.length,
          itemBuilder: (BuildContext context, int index) {
           
              if(your condition){

            customListTile(articles, index, context));

      }else{
           
              customListTile2(articles, index, context));

              }

           )

i hope this is helpfull.

  • Related