플러터 Material Swatch Color : Invalid constant value.dart(invalid_constant) Type: int

LeeWonjin·2024년 4월 10일
0

문제해결

목록 보기
21/23

문제

color: Colors.green[100]
Invalid constant value.dart(invalid_constant) Type: int

원인

https://api.flutter.dev/flutter/material/Colors/green-constant.html

Colors.green은 그냥 프라이머리 컬러,
Colors.green[100]같은건 연관색상(스와치 컬러)라고 부른다.

스와치컬러를 사용할 때 유효하지 않은 색상이라고 할 수 있다.
부모 위젯이 const일 때 그렇다.


  Widget build(BuildContext context) {
    return Container(
      decoration: const BoxDecoration(color: Colors.white),
      child: Center(
        child: const Row(
          textDirection: TextDirection.ltr,
          mainAxisSize: MainAxisSize.min,
          children: [
            Icon(
              textDirection: TextDirection.ltr,
              size: 12,
              Icons.star,
              color: Colors.green[100], // <-- [Error] Invalid Constant
            ),
          ],
        ),
      ),
    );
  }

해결

const를 떼주든가

Row( // <-- const 삭제
	...
    color: Colors.green[100],
)

그냥 primary color를 쓰자

const Row(
	...
    color: Colors.green, // <-- primary color로 변경
)
profile
노는게 제일 좋습니다.

0개의 댓글