Picker
1. Date Picker
DateTime now = DateTime.now();
showDatePicker(
context: context,
initialDate: now,
firstDate: DateTime(2020),
lastDate: DateTime(now.year + 1),
).then((value) {
print('datePicked: ${value?.toIso8601String()}');
});
2. Date Range Picker
Sama seperti Text Field namun dengan tambahan beberapa property seperti validator yang dikombinasikan dengan widget Form.
DateTime now = DateTime.now();
showDateRangePicker(
context: context,
firstDate: DateTime(2020),
lastDate: DateTime(now.year + 1),
initialDateRange: DateTimeRange(
start: now.subtract(const Duration(days: 3)),
end: now,
),
).then((value) {
print('duration: ${value?.duration.inDays} days');
print('start: ${value?.start.toIso8601String()}');
print('end: ${value?.end.toIso8601String()}');
});
3. Time Picker
DateTime now = DateTime.now();
showTimePicker(
context: context,
initialTime: TimeOfDay.fromDateTime(now),
).then((value) {
print('timePicked: ${value?.toString()}');
});
4. Image Picker
Tambahkan package image_picker di pubspec.yaml. Karena cara penggunaan setiap versi kemungkinan berbeda, silahkan cek ke Doc: image_picker | Flutter Package (pub.dev).
- setup iOS
- setup Android
pickImage(ImageSource source) async {
XFile? xFile = await ImagePicker().pickImage(source: source);
if (xFile == null) {
print('cancel pick');
return;
}
print('''
Mime type: ${xFile.mimeType}
Name: ${xFile.name}
Path: ${xFile.path}
Last modified: ${(await xFile.lastModified()).toIso8601String()}
''');
}
Gallery | Camera |
---|---|
![]() | ![]() |
5. File Picker
Untuk setup seperti Image Picker diatas. Namun disini akan dicontohkan untuk custom picker, dengan format file berupa document biasanya. Karena cara penggunaan setiap versi kemungkinan berbeda, silahkan cek ke Doc: file_picker | Flutter Package (pub.dev)
pickFile() async {
FilePickerResult? result = await FilePicker.platform.pickFiles(
type: FileType.custom,
allowedExtensions: ['txt', 'pdf', 'doc', 'docx'],
);
if (result != null) {
PlatformFile file = result.files.first;
print(file.name);
print(file.bytes);
print(file.size);
print(file.extension);
print(file.path);
} else {
print('User canceled the picker');
}
}