티스토리 뷰

반응형

 

   왜 Stack의 첫번째 child로 비어있는 SizedBox를 깔아주는 것일까?

현재 참고 하고 있는 Time table 패키지의 코드를 보면 Stack 위젯의  children의 첫번째로 SizedBox를 사용하고 있다. 내부에 의미있는 child도 없는, 빈 box 를 왜 처음에 깔아두는지 몰라서 검색해 보았다. 

 

참고자료1 (링크)

Stack 내에서 자리표시자(a syntactic placeholder)로 SizedBox를 사용한다고 한다. SizedBox는 const constructor라서 그렇지 않은 Container에 비해 컴파일러가 더 효율적인 코드를 생성할 수 있다고 한다. 

또한 Container는 SizedBox에 비해 많은 properties 와 methods를 가지고 있어서 초기화 하는데 비용이 상대적으로 크다. 따라서 SizedBox를 사용하는 것이 더 나은 방법이라고 한다. 

 

여기서 한걸음 더 들어가서 그럼 const constructor는 뭔가?

Const objects are final/immutable and created in compile time. So you don't waste time creating them. Also, all const objects of the same type with the same parameters are the same instance. So you don't waste memory creating more than one of them. In other words, const objects make your program faster and more memory efficient.

Flutter knows if an element is const, it can skip over that in a rebuild.

 

Placeholder 위젯이 있는데 이것을 SizedBox를 대신해서 사용할 수 있는지에 대해서는 답변이 없다. 

 

 

반응형
댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2024/07   »
1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30 31
글 보관함