Home > Enterprise >  The argument type 'Color' can't be assigned to the parameter type 'String'
The argument type 'Color' can't be assigned to the parameter type 'String'

Time:12-05

The argument type 'Color' can't be assigned to the parameter type 'String' , I expect to change the color of the text stored in the variable 'status' when the status is set to closed. please help me to fix this..

class _todaysMealState extends State<todaysMeal> {
  String itemName = "<item_name>",
      time = "<time>",
      dayTime = "<time?>",
      status = "status",
      itemImage = "lib/images/default.png";
  Color statusColor = Colors.green;

  _todaysMealState({required this.itemName,
    required this.itemImage,
    required this.time,
    required this.dayTime,
    required this.status});

  @override
  Widget build(BuildContext context) {
    double height = MediaQuery
        .of(context)
        .size
        .height;
    double width = MediaQuery
        .of(context)
        .size
        .width;
    return Card(
      color: const Color(0xFF2B2E3F),
      shape: const RoundedRectangleBorder(
        borderRadius: BorderRadius.all(Radius.circular(10)),
      ),
      .....
              trailing: Text(
                status == "Closed" ? statusColor = const Color(0xFFFF0000):statusColor=Colors.white,
                style:  TextStyle(
                  color: statusColor,
                  fontWeight: FontWeight.bold,
                  fontSize: 16,
                ),
              ),
              horizontalTitleGap: -15,
            ),
          ],
        ),
      )
      ,
    );
  }
}

the problem is here

trailing: Text(
                status == "Closed" ? statusColor = const Color(0xFFFF0000):statusColor=Colors.white,
                style:  TextStyle(
                  color: statusColor,
                  fontWeight: FontWeight.bold,
                  fontSize: 16,
                ),
              ),

i tried my best the rest is up to you, Thanks

CodePudding user response:

        status == "Closed" ? statusColor = const Color(0xFFFF0000):statusColor=Colors.white,

creates the error inside your Text widget. It expect's a string, but you provide a color object.

CodePudding user response:

Text(status,
  style:TextStyle(
              color:status == "Closed" ?const Color(0xFFFF0000):Colors.white,
              fontWeight: FontWeight.bold,
              fontSize: 16,
            ),
          )

CodePudding user response:

trailing: Text('Your Text here',
                
                style:  TextStyle(
                  color: status == "Closed" ? const Color(0xFFFF0000):Colors.white,
                  fontWeight: FontWeight.bold,
                  fontSize: 16,
                ),
              ),


or if you go with your statuscolor variable

assign it in ur build method like

statuscolor=status == "Closed" ? statusColor = const Color(0xFFFF0000):statusColor=Colors.white;
  • Related