skip to Main Content
   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

3

Answers


  1. 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.

    Login or Signup to reply.
  2. List<Color> colors = [Colors.blue, Colors.amber, Colors.pink];
    ...
    
    
    Container(
                  decoration: BoxDecoration(
                      color: colors[ _currentPage],
    
    Login or Signup to reply.
  3. 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();}
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search