packages etiketine sahip kayıtlar gösteriliyor. Tüm kayıtları göster
packages etiketine sahip kayıtlar gösteriliyor. Tüm kayıtları göster

Flutter Package of the Week: image_picker

 

Hello, the second post in our package of the week series is about handling image files in our projects. And our package is image_picker. This package is one of the most common tools used by application developers, because it is a very common need for an application to select photos or videos from users’ devices.

What’s it for?

Image Picker lets you grab media files from the device’s gallery or directly from the camera, essentially making it easier for users to select photos or videos. Let’s say you’re making a profile photo add screen, you want to allow the user to select an image from the gallery or take a photo on the fly. With this package, that can be done with a few lines of code.

How to Use?

First you add the package to your pubspec.yaml file using the command below as usual:

flutter pub add image_picker
dependencies:
image_picker: ^1.1.2 //version number may vary

Then you need to import the package to the dart file you will use with the following line:

import 'package:image_picker/image_picker.dart';

As far as permits are concerned,

As of version 0.8.1 the Android app supports selecting (multiple) images on Android 4.3 or higher. So no configuration is required — the plugin should work out of the box.

But on iOS you need to add the following lines to the Info.plist file:

<key>NSCameraUsageDescription</key>
<string>We need access to your camera to take photos.</string>
<key>NSPhotoLibraryUsageDescription</key>
<string>We need access to your photo library to select photos.</string>

Now comes the fun part. The package provides an ImagePicker class that allows you to easily pick images from the gallery or camera. Here’s a simple example of how to use it:

final ImagePicker _picker = ImagePicker();

// Galeriden resim seçmek
final XFile? image = await _picker.pickImage(source: ImageSource.gallery);

// Kameradan resim çekmek
final XFile? photo = await _picker.pickImage(source: ImageSource.camera);

In the code above, if the user selects an image from the gallery, the file path is assigned to the image variable. Likewise, if you want to use the camera, you can take a shot from the camera with the ImageSource.camera parameter.

It is also possible to select a video in the same way:

final XFile? video = await _picker.pickVideo(source: ImageSource.gallery);

Furthermore, this package offers options to compress both photos and videos. This is a great feature, especially for optimizing large file sizes!

Now let’s see what customizations we can make using this package.

In Image Picker, you can also set resolution settings to control the size and quality of the media files that users upload. For example, if you want to take a low-resolution photo when using the camera, you can set the imageQuality parameter.

final XFile? compressedPhoto = await _picker.pickImage(
source: ImageSource.camera,
imageQuality: 50, // 50% quality
);

This way, you can also preserve the performance of the application. Low-sized images will load faster, especially if you are uploading media files over the internet.

You can also specify the size of the images with maxWidth, maxHeight and the preferredCameraDevice parameter for front or rear camera preference.

Below I am sharing an example of a project made using this package with all the codes.

!!! Since I created the project using iOS Simulator and there is no camera on the virtual device, the Camera button shows Error, but it works fine on the real device.

final ImagePicker _picker = ImagePicker();
Future pickImageFromGallery() async {
final XFile? image = await _picker.pickImage(source: ImageSource.gallery);
}

Future pickImageFromCamera() async {
final XFile? photo = await _picker.pickImage(source: ImageSource.camera);
}

Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
ElevatedButton(
style: ElevatedButton.styleFrom(
backgroundColor: Colors.purple,
padding: const EdgeInsets.all(16.0),
),
child: const Text("Pick Image from Gallery",
style: TextStyle(
color: Colors.white70, fontWeight: FontWeight.bold)),
onPressed: () {
pickImageFromGallery();
},
),
const SizedBox(
height: 22.0,
),
ElevatedButton(
style: ElevatedButton.styleFrom(
backgroundColor: Colors.purple,
padding: const EdgeInsets.all(16.0),
),
child: const Text("Pick Image from Camera",
style: TextStyle(
color: Colors.white70, fontWeight: FontWeight.bold)),
onPressed: () {
pickImageFromCamera();
},
),
],
),
),

I will conclude this article by talking about its advantages and disadvantages.

The biggest advantage of the ImagePicker package is that users can select media files from the gallery or camera with just a few lines of code. You can also control file size and quality.

On the downside, you may need to manage performance when working with large media files. And setting platform-specific permissions manually can be a bit cumbersome.

I hope you found this article useful! If you appreciate the information provided, you have the option to support me by Buying Me A Coffee! Your gesture would be greatly appreciated!


In a nutshell, Image Picker is one of the most popular packages for anyone who wants users to select media or use a camera in Flutter projects. If your app has features like profile photo, gallery selection, or video uploading, this package will give you a lot of peace of mind! It’s a simple and versatile solution.

I hope this was useful.

Happy coding.

Selin.

Haftanın Flutter Paketi: image_picker

 

Merhaba, haftanın paketi serimizin ikinci yazısı projelerimizde resim dosyalarını ele almak ile ilgili. Ve paketimiz image_picker. Bu paket, uygulama geliştirenlerin en sık kullandığı araçlardan biri, çünkü bir uygulamada kullanıcıların cihazlarından fotoğraf ya da video seçmesi çok yaygın bir ihtiyaç.

Ne işe yarıyor?

Image Picker cihazın galerisinden ya da direkt kameradan medya dosyaları almanı sağlıyor yani aslında kullanıcıların fotoğraf ya da video seçmesini kolaylaştırıyor. Diyelim ki bir profil fotoğrafı ekleme ekranı yapıyorsun, kullanıcının galeriden bir resim seçmesine ya da anında fotoğraf çekmesine izin vermek istersin. İşte bu paket sayesinde, o iş birkaç satır kodla hallediliyor.

Nasıl kullanılır?

Önce her zamanki gibi aşağıdaki komutu kullanarak paketin pubspec.yamldosyana eklenmesini sağlıyorsun:

flutter pub add image_picker
dependencies:
image_picker: ^1.1.2 //versiyon numarası değişkenlik gösterebilir

Sonrasında paketi kullanacağın dart dosyasına aşağıdaki satır ile import etmen gerekiyor:

import 'package:image_picker/image_picker.dart';

İzinlere bakacak olursak,

Sürüm 0.8.1'den itibaren Android uygulaması Android 4.3 veya üzeri sürümlerde (birden fazla) görüntü seçmeyi desteklemektedir. Dolayısıyla yapılandırma gerekmez — eklenti kutudan çıkar çıkmaz çalışmalıdır.

Ama iOS’ta Info.plist dosyasına aşağıdaki satırları eklemelisin:

<key>NSCameraUsageDescription</key>
<string>We need access to your camera to take photos.</string>
<key>NSPhotoLibraryUsageDescription</key>
<string>We need access to your photo library to select photos.</string>

Şimdi işin eğlenceli kısmı geliyor. Paketin sunduğu ImagePicker sınıfı sayesinde galeri veya kameradan kolayca resim seçebiliyorsun. İşte nasıl kullanacağına dair basit bir örnek:

final ImagePicker _picker = ImagePicker();

// Galeriden resim seçmek
final XFile? image = await _picker.pickImage(source: ImageSource.gallery);

// Kameradan resim çekmek
final XFile? photo = await _picker.pickImage(source: ImageSource.camera);

Yukarıdaki kodda, kullanıcı galeriden bir resim seçerse image değişkenine dosya yolu atanıyor. Aynı şekilde, kamerayı kullanmak istersen ImageSource.camera parametresiyle kameradan çekim yapabilirsin.

Aynı yöntemle video seçmek de mümkün:

final XFile? video = await _picker.pickVideo(source: ImageSource.gallery);

Ayrıca, bu paket hem fotoğrafları hem de videoları küçültme (compression) seçenekleri sunuyor. Bu, özellikle büyük dosya boyutlarını optimize etmek için harika bir özellik!

Şimdi bu paketi kullanarak ne gibi özelleştirmeler yapabiliriz, bir de ona bakalım.

Image Picker’da ayrıca çözünürlük ayarları yaparak kullanıcıların yükleyeceği medya dosyalarının boyutunu ve kalitesini kontrol edebilirsin. Örneğin, kamera kullanılırken düşük çözünürlüklü bir fotoğraf çekmek istersen imageQualityparametresini ayarlayabilirsin.

final XFile? compressedPhoto = await _picker.pickImage(
source: ImageSource.camera,
imageQuality: 50, // %50 kalite
);

Bu sayede, uygulamanın performansını da koruyabilirsin. Özellikle internet üzerinden medya dosyaları yükleyeceksen, düşük boyutlu resimler daha hızlı yüklenir.

Ayrıca maxWidth, maxHeight ile resimlerin boyutunu ve preferredCameraDevice parametresi ile ön ya da arka kamera tercihini de belirleyebilirsin.

Aşağıda tüm kodları ile bu paket kullanılarak yapılmış proje örneğini paylaşıyorum.

!!! Projeyi iOS Simulator kullanarak oluşturduğum ve sanal cihazda kamera bulunmadığından Camera butonunda Error görünüyor ancak gerçek cihazda sorunsuz çalışır.

final ImagePicker _picker = ImagePicker();
Future pickImageFromGallery() async {
final XFile? image = await _picker.pickImage(source: ImageSource.gallery);
}

Future pickImageFromCamera() async {
final XFile? photo = await _picker.pickImage(source: ImageSource.camera);
}

Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
ElevatedButton(
style: ElevatedButton.styleFrom(
backgroundColor: Colors.purple,
padding: const EdgeInsets.all(16.0),
),
child: const Text("Pick Image from Gallery",
style: TextStyle(
color: Colors.white70, fontWeight: FontWeight.bold)),
onPressed: () {
pickImageFromGallery();
},
),
const SizedBox(
height: 22.0,
),
ElevatedButton(
style: ElevatedButton.styleFrom(
backgroundColor: Colors.purple,
padding: const EdgeInsets.all(16.0),
),
child: const Text("Pick Image from Camera",
style: TextStyle(
color: Colors.white70, fontWeight: FontWeight.bold)),
onPressed: () {
pickImageFromCamera();
},
),
],
),
),

Avantajları ve dezavantajlarından da bahsederek yazımı tamamlıyorum.

ImagePicker paketinin en büyük avantajı kullanıcıların galeriden ya da kameradan medya dosyalarını seçmesinin sadece birkaç satır kodla halledilebiliyor olması. Ayrıca dosya boyutu ve kalitesini de kontrol edebiliyorsun.

Dezavantajına gelince, büyük medya dosyalarıyla çalışırken performans yönetimini iyi yapman gerekebilir. Ve bir de platformlara özgü izinlerin manuel olarak ayarlanması biraz zahmetli olabilir.

Özetle, Image Picker, Flutter projelerinde kullanıcıların medya seçmesini veya kamera kullanmasını isteyen herkesin başvurduğu en popüler paketlerden biri. Eğer uygulamanda profil fotoğrafı, galeri seçimi ya da video yükleme gibi özellikler varsa, bu paket seni epey rahatlatacak! Hem basit, hem de çok yönlü bir çözüm sunuyor.

Umarım faydalı olmuştur.

İyi kodlamalar.

Selin.

The Flutter Package of the Week: url_launcher

 


Hello everyone. With this article, I would like to announce that I have started the ‘Flutter Package of the Week’ series, which I think will be useful when developing Flutter projects. The first week’s package is url_launcher, which I recently used in my own project.

What is url_launcher ?

If you want to redirect your users from your app to an external URL, a web page, an email, or even a phone number, this is the package you need.

  • For example, when a button is pressed, the user is redirected to the default browser so that they can open a specific URL.
  • Enabling a phone number to be copied into the phone’s standard dialling application and displayed.
  • Prepare and send an email to generate the recipient and text when a button is pressed.

Installation

You can start the installation by typing the following line in the terminal:

It will be added to the pubspec.yaml file as follows:

Usage

1- Opening Web Urls

You can use the launch function to open a web URL:

Note: While canLaunch(url) checks whether the given URL can be opened, launch(url)opens the URL in the browser. You can also write the url directly in the code without writing the launch(url) method, but there may be problems in the application in case of a possible inability to open the url related to the user’s device. For this reason, it is especially recommended to use the canLaunch(url)function in the method.

2- Making a Phone Call

Note: A telephone call is made by specifying a telephone number with the prefix tel: .

3- Sending SMS

Note: SMS to a specific number can be initiated using the sms:prefix.

4- Sending E-mail

Note: An e-mail is sent using the mailto:scheme. The subject and body content can be specified in the query section.

Application Permissions

iOS

You must allow internet access by adding the following line to the ios/Runner/Info.plist file:

Android

You must provide internet access permission in android/app/src/main/AndroidManifest.xmlfile:

I hope you found this article useful! If you appreciate the information provided, you have the option to support me by Buying Me A Coffee! Your gesture would be greatly appreciated!


I hope I have been able to present the package in detail, I wish it to be useful. See you in the coming weeks with other packages.

Have a good day.

Selin.