0

i am trying to change the primary color based on a string value , meaning if the string value is this then main color = this , else main color =that color i have a color.dart file with this code:

import 'package:flutter/material.dart';
import 'package:flutter/painting.dart';

Color mainColor = Color(0xfff04592);

and i have a string value stored in a class.

what i dont understand is how can i use conditional statement "if" in the colors.dart file ? like this :

if (value=='asd') 
{mainColor = Color(0xfff04592);}
else
{mainColor = Color(0xfff09245);}
0

3 Answers 3

1

You can use ternary operator for assigning different colors like this:

Color mainColor = value == 'asd' ? Color(0xfff04592) : Color(0xfff09245);
Sign up to request clarification or add additional context in comments.

1 Comment

What if there is more then one String?
1

You can declare a variable to store actual state for condition:

String value = 'default';

Color mainColor = value == 'default' ? Colors.green : Colors.red;

Comments

1

i think its bette rto do in material app, you can do...

primaryColor: value == 'asd' ? colorOne : colorTwo

you can define your colorOne and colorTwo somewhere else

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.