사용하고자 하는 라이브러리를 찾는 방법
pub.dev 에서 사용하고자 하는 라이브러리를 찾고, 해당 문서에서 설치법, 사용법 등등을 참고하여 사용해야 한다.
The official repository for Dart and Flutter packages.
Pub is the package manager for the Dart programming language, containing reusable libraries & packages for Flutter and general Dart programs.
pub.dev
ID 값이란?
어떠한 객체를 구별하는데 사용되는, 각 객체만의 고유한 값을 의미한다.
범용적으로는 Key 라고도 이야기 하며, 각 객체를 구별하여 어떠한 이벤트가 발생했을때 해당 객체에만 적용될 수 있게 할때 용이하다.
UUID 라이브러리
고유 id 값을 자동으로 할당시켜주는 라이브러리
// Terminal 에서 입력
flutter pub add uuid
UUID (Universally Unique Identifier)
UUID는 128비트 숫자로 표현되며, 주로 16진수로 나타내는 고유한 식별자.
// uuid의 기본 형식
xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx
- uuid 라이브러리를 통해, AlarmModel이 새로 생성될때, 고유 id값을 부여하게 해주는 로직
class AlarmModel {
String id;
int hour;
int minute;
bool isOn;
AlarmModel({
required this.hour,
required this.minute,
this.isOn = true,
}) : id = Uuid().v4();
}
## uuid 라이브러리에서 id를 생성할때의 방식들
- 설정된 id로 식별한 객체 삭제 / 수정 로직
// GetX 컨트롤러에서 선언된 삭제, 수정 로직
void removeAlarm(String id) {
alarmList.removeWhere((element) => element.id == id);
update();
}
void isOnAlarm(String id) {
alarmList.forEach((element) {
if(element.id == id){
element.isOn = !element.isOn;
}
});
update();
}
// Getx 컨트롤러에 정의된 삭제 / 수정 함수 사용
Get.find<AlarmController>().removeAlarm(alarm.id);
Get.find<AlarmController>().isOnAlarm(alarm.id);
Image_picker 라이브러리
기기의 라이브러리에 있는 사진에 대한 접근을 가능하게 해주는 라이브러리
안드로이드의 경우 설치 후 별도의 설정 없이 사용이 가능하지만, ios의 경우 접근 권한에 대한 권한 요청이 필요하다.
// Terminal
flutter pub add image_picker
@@ ios 권한 요청하는 법
1. Info.plist 열기
2. 하단부에 라이브러리 접근을 위한 코드 삽입
<dict>
...
<key>NSPhotoLibraryUsageDescription</key>
<string>이미지 컨텐츠를 등록할 목적으로 사진첩에 접근을 합니다.</string>
</dict>
Image_picker 사용하기
Future<void> getImagePickerData() async {
final ImagePicker picker = ImagePicker();
final List<XFile> images = await picker.pickMultiImage();
print(images.length);
}
GestureDetector(
onTap: (){
getImagePickerData()
},
child:
Image.asset('assets/images/photo_icon.png', width: 30),
),
Timeago 라이브러리
시간 표시를 보다 다양하게 만들어주는 라이브러리
// Terminal
flutter pub add timeago
main.dart에서 언어 설정
import 'package:timeago/timeago.dart' as timeago;
timeago.setLocaleMessages('ko', timeago.KoMessages());
사용법
Text(
timeago.format(
DateTime.now().subtract(
DateTime.now().difference(model.createdAt)),
locale: 'ko'),
style: TextStyle(
color: Color(0xff999999),
fontSize: 14,
),
)
intl 라이브러리
날짜 포맷팅을 도와주는 라이브러리
// Terminal
flutter pub add intl
// 날짜 포맷 함수 구현
class DataUtils {
static String formatDate(String format, DateTime date) {
return DateFormat(format).format(date);
}
}
// 날짜 포맷팅 사용
Text(
DataUtils.formatDate(
'yyyy-MM-dd', Get.find<MemoWriteController>().memoDate!),
style: TextStyle(fontSize: 13, color: Color(0xffE3AC34)),
),
'Flutter' 카테고리의 다른 글
파이어베이스(2)_연동 (0) | 2024.10.15 |
---|---|
파이어베이스(1)_백엔드 서비스 (0) | 2024.10.14 |
상태관리 (2) _GetX (0) | 2024.10.11 |
상태관리 (1) _GetX (0) | 2024.10.11 |
예제를 통한 레이아웃 연습 (0) | 2024.10.10 |