[Flutter] Button style이 정상적으로 적용되지 않는경우 해결

코딩하는계란·2022년 3월 27일
0

Flutter

목록 보기
4/6
post-thumbnail
  Widget build(BuildContext context) {
    return SizedBox(
      width: width,
      height: height,
      child: ElevatedButton(
        onPressed: action,
        style: ElevatedButton.styleFrom(
          primary: color,
          elevation: 0,
          shape: RoundedRectangleBorder(
            borderRadius: BorderRadius.circular(16.0),
          ),
        ),
        child: 생략
    );
  }
}

Button의 색상, 함수 등을 인자로 받아 버튼을 생성하는 코드의 일부이다.
여기서 action 인자가 함수를 안 받으면 null이 들어가게 되는데
이상하게 null 이면ElevatedButton.styleFrom이 작동을 안 한다.
ButtonStyle에서는 잘 작동하는데 Button.styleFrom 여기에서만 작동이 안 된다.

onPressed: action ?? () {}
이렇게 처리하면 정상적으로 잘 실행된다.

안되는 이유를 한참을 찾아봤지만 찾지를 못했다.
나중에 다시 한번 버튼 속성들을 더 딥한 부분부터 파악해봐야 알 수 있을 거 같다.

profile
코딩💻 고양이😺

0개의 댓글