Home > Software engineering >  Why i cannot use int value to control my icon color in flutter?
Why i cannot use int value to control my icon color in flutter?

Time:03-31

I wanted to change my fav icon 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 top row.

I mentioned at the above as html tag "this place where i got the mistake". Mistake is Invalid constant value I also tried to declare the variable inside of favoriteButton.Why i cannot use the variable to check condition? Is anyone to help me?

CodePudding user response:

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

  • Related