sized_box_shrink_expand 린트 규칙: SizedBox.shrink·expand 생성자 사용하기
sized_box_shrink_expand 린트 규칙: SizedBox.shrink·expand 생성자 사용하기
SizedBox(...)를 만들 때 height와 width를 직접 0이나 double.infinity로 지정할 수 있는데요, 이럴 때는 더 뜻이 분명한 SizedBox.shrink(...)나 SizedBox.expand(...) 같은 네임드 생성자를 쓰는 게 좋아요. 이 린트 규칙이 그런 상황을 잡아주고요.
본문
설명
SizedBox.shrink(...)와 SizedBox.expand(...) 생성자를 상황에 맞게 사용해요.
네임드 생성자 중 하나가 코드의 의도를 더 간결하게 표현할 수 있다면, 일반적인 SizedBox(...) 생성자 대신 SizedBox.shrink(...) 또는 SizedBox.expand(...)를 쓰는 게 좋아요.
BAD:
Widget buildLogo() {
return SizedBox(
height: 0,
width: 0,
child: const MyLogo(),
);
}
Widget buildLogo() {
return SizedBox(
height: double.infinity,
width: double.infinity,
child: const MyLogo(),
);
}
GOOD:
Widget buildLogo() {
return SizedBox.shrink(
child: const MyLogo(),
);
}
Widget buildLogo() {
return SizedBox.expand(
child: const MyLogo(),
);
}
height: 0, width: 0은 SizedBox.shrink(...)로, height: double.infinity, width: double.infinity는 SizedBox.expand(...)로 표현하면 의도가 더 분명하게 드러나요.
활성화 방법
sized_box_shrink_expand 규칙을 활성화하려면 analysis_options.yaml 파일의 linter > rules 아래에 규칙을 추가하면 돼요.
linter:
rules:
- sized_box_shrink_expand
대신 YAML 맵 문법으로 린트 규칙을 설정하고 있다면, linter > rules 아래에 sized_box_shrink_expand: true를 추가하면 돼요.
linter:
rules:
sized_box_shrink_expand: true