'Why i cannot use int value to control my icon color in flutter?
I wanted to change my favicon whether pushing in it but it is not happening because I cannot use the int value to control it. Here is my code example=>
class _WebViewExampleState extends State<WebViewExample> {
final Completer<WebViewController> _controller = Completer<WebViewController>();
int ctr=0;
...
floatingActionButton: favoriteButton(),
...
Widget favoriteButton() {
return FutureBuilder<WebViewController>(
future: _controller.future,
builder: (BuildContext context,
AsyncSnapshot<WebViewController> controller) {
if (controller.hasData) {
return FloatingActionButton(
backgroundColor: Colors.indigo,
onPressed: () async {
final String url = (await controller.data!.currentUrl())!;
// ignore: deprecated_member_use
Scaffold.of(context).showSnackBar(
SnackBar(content: Text('Favorited $url')),
);
setState(() {
ctr++;
});
},
child: const Icon(Icons.favorite,
<this place where i got the mistake>
color: this.ctr%2==1 ? Colors.red : Colors.white),
</this place where i got the mistake>
);
}
return Container();
});
}
}//end of the top row.
I mentioned the above as HTML tag "this place where I got the mistake". The mistake is Invalid constant value I also tried to declare the variable inside of favoriteButton.Why cannot use the variable to check conditions? Is anyone to help me?
Solution 1:[1]
please remove const from icons widget
please check my code
child: Icon(Icons.favorite,
<this place where i got the mistake>
color: this.ctr%2==1 ? Colors.red : Colors.white),
</this place where i got the mistake>
);
doing by that you can use mutable values
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Jinto Joseph |
