Home > Enterprise >  Adding extra option to tenerary operator flutter
Adding extra option to tenerary operator flutter

Time:11-25

   Container(
              decoration: BoxDecoration(
                  color: (page == _currentPage)
                      ? Colors.blue.shade800
                      : Colors.green.shade600
                      ),

I am trying to add another option to the tenerary operator since i have three pages that will change color. How can i add a third option

CodePudding user response:

you can do

color: page == _currentPage
       ? anotherCheck == _currentPage? 
         Colors.blue.shade500: Colors.blue.shade800 
       : Colors.green.shade600       

Creating a method will be better for more.

CodePudding user response:

List colors = [Colors.blue, Colors.amber, Colors.pink]; ...

Container( decoration: BoxDecoration( color: colors[ _currentPage],

CodePudding user response:

First you have to understand how tenerary operator works Here is the simple nested example of tenerary operator

(foo==1)? something1():(foo==2)? something2():(foo==3)?something3():something4();

and this is equal to

if(foo == 1){
    something1();
}
else if(foo == 2){
    something2();
}
else if (foo == 3){
    something3();
}
else{
    something4();}
  • Related