sized_box_shrink_expand 린트 규칙: SizedBox.shrink·expand 생성자 사용하기

sized_box_shrink_expand 린트 규칙: SizedBox.shrink·expand 생성자 사용하기

SizedBox(...)를 만들 때 heightwidth를 직접 0이나 double.infinity로 지정할 수 있는데요, 이럴 때는 더 뜻이 분명한 SizedBox.shrink(...)SizedBox.expand(...) 같은 네임드 생성자를 쓰는 게 좋아요. 이 린트 규칙이 그런 상황을 잡아주고요.

출처: sized_box_shrink_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: 0SizedBox.shrink(...)로, height: double.infinity, width: double.infinitySizedBox.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

더 알아보기