아래 코드와 같이 컨테이너 안 어느 요소든 탭했을때 textfield의 focus를 null로 만들어 키보드를 내리는 기능을 구현하려고 했다.

 

문제의 코드

 

컨테이너 안 빈공간을 눌러도 키보드가 닫히지 않았다.

 

하지만 신기하게 컨테이더 안에 circle 요소나 text를 클릭하면 기능이 동작을 했다.

그래서 container가 할당되는 공간이 없는 건가 싶어서 color를 넣어 확인했지만 이미 container를 가득 채운 상태였다.

더 이상한 것은 color를 넣으니 원래 의도하던대로 기능이 잘 작동한다는 것이었다.

 

혼란에 빠져 구글링을 하다가 내가 겪은 문제를 다룬 깃헙이슈를 발견했다.

 

[GestureDetector with Container not working without color]

https://github.com/flutter/flutter/issues/32364

 

GestureDetector with Container not working without color · Issue #32364 · flutter/flutter

Hey, I don't know if this is the expected behavior but a GestureDetector with a Container in it's child, the onTap callback is not executed when the Container doesn't have a color. For ...

github.com

 

 

결론은 container의 color를 따로 지정하지 않으면 container의 decoration이 null로 설정되어서 tap을 인식하지 못한다고 한다.

 

해결방법

1. color를 넣어준다.

2. color를 넣지 않아야하는 경우라면 Colors.transparent(투명)를 넣는다.

 onTap이 잘 작동할 것이다.

+ Recent posts