- flutter

선택적 매개변수 / Visibility / INTL라이브러리

나둥식 2022. 11. 21. 11:33

✅ 선택적 매개변수로 받기 _buildIcons

@override
  Widget build(BuildContext context) {
    return Column(
      children: [
        Text(product.title, style: textTheme().bodyText1),
        SizedBox(height: 4.0),
        Text("${product.address} * ${product.publishedAt}"),
        Text("${product.price}원", style: textTheme().headline2),
        Row(
          children: [
            _buildIcons(iconDate: CupertinoIcons.chat_bubble_2, count: product.commentsCount),
            _buildIcons(iconDate: CupertinoIcons.heart, count: product.heartCount),
          ],
        )
      ],
    );
  }

  Widget _buildIcons({required IconData iconDate, required int count}) {
    //선택적 매개변수로 받기
    return Row(
      children: [
        Icon(iconDate),
        Text("${count}"),
      ],
    );
  }

➡ {required IconData iconDate, required int count}로 받아서 사용

 

 

 

 

✅ Visibility :  댓글 & 좋아요가 0이면 화면에 그리지 않기

➡ if-else문으로 해도 됨!

return Visibility(
      visible: count > 0, //조건문
)

⭐ 컴포넌트화 할때는 순수하게 사용하기! (패딩도 사용 x)

 

 

 

 

✅ 숫자 관련된 util 파일 만들어서 사용하기 - INTL 라이브러리 사용

import 'package:intl/intl.dart';

String numberPriceFormat(String price) {
  final formatter = NumberFormat("#,###원"); // INTL 라이브러리
  return formatter.format(int.parse(price)); // 문자열을 숫자로 변경하는 함수
}