Home > Net >  How to different Widget depending on paramater Flutter
How to different Widget depending on paramater Flutter

Time:05-23

I'm trying to show different Icon depending on my paramater, I manage to show it using If statement but I have to back my screen first and reload it again, I tried to use setState but I got some error

error: This expression has a type of 'void' so its value can't be used. (use_of_void_result at [tiket_kerja] lib\screens\main_pages\main_detail_ticket_page.dart:2915)

How can I fix this ?

Here's the code:

Column[
TextButton();
setState(() {
 if (progress ==0) 
  if (fileExistList[index] ==true)
      Icon(Icons.done);
  else
      Icon(Icons.close);
  else
      Text('$progress');
})
]

CodePudding user response:

create a function and call it with value you want

int progress = 0;
bool fileExist = true;
Widget getIcon(int progress, bool fileExist) {

 if (progress == 0) 
  if (fileExist)
      return Icon(Icons.done);
  else
      return Icon(Icons.close);
  else
      return Text('$progress');
}

Column(
    childreen: [
             TextButton(),
             getIcon(progress, fileExist),
            ]
)

now when ever you want to change the icon just call

setState(() {
 //change value of the paramaters
})

CodePudding user response:

If you just want to show two icons, create a relatable Boolean variable and set it to false initially, here, _fileExists = false initially.

Overtime you could change the value to true using setState() so that flutter knows that something has changed and it needs to rebuild the widget.

To show icons depending on the value of _fileExists, you can use ternary operation. It's generally you pass a true or false statement followed by question mark (?) and output in each case.

Here this could be used as _fileExists(as it only returns true or false) ? Icon(Icons.done) : Icon(Icons.close)

Here, Icon(Icons.done) is returned only if _fileExists is true, else returns Icon(Icons.close)

Ternary operators is in the form of

`condition ? return_this_if_true : return_this_if_false`

CodePudding user response:

you can try this

Column(childreen: [
    TextButton(),
    (progress == 0 && fileExistList[index] == true)
        ? Icon(Icons.done)
        : (progress == 0 && fileExistList[index] == false)
            ? Icon(Icons.close)
            : Text('$progress'),
  ]);

  • Related