Flutter’da Titreşim Çalma: Vibration Alert

Bu yazımda, Flutter’da Titreşim Uyarısının nasıl yapılacağını göstereceğim. Cep telefonu, çağrı cihazı gibi iletişim cihazlarına mesaj veya çağrı geldiğinde kullanıcıyı bilgilendirmek için titreşimli uyarı kullanılmaktadır.

Flutter’da, çeşitli titreşim türlerini uygulamak için Vibration paketini kullanacağız. Bu uygulamayı android cihazda test ettim ve sorunsuz çalıştı. Ancak iOS cihaza sahip olmadığım için bu projeyi iOS cihazında kontrol edemedim.

Bu yazımda Flutter Android ve iOS Uygulamasında Titreşim Uyarısı’nın nasıl verileceğini göstereceğim.

Flutter Android / iOS Titreşim Uyarısı Nasıl Yapılır ?

#1 Öncelikle Flutter projemizde Vibration Pub paketini kurmamız gerekiyor. Bunu yapmak için Flutter projenizin pubspec.yaml dosyasını açın ve dependencies: bloğunun altına vibration: ^1.7.3 ekleyin ve Terminali açın ve flutter pub get komutunu çalıştırın.

dependencies:
  vibration: ^1.7.3
  flutter:
    sdk: flutter

#2 Şimdi bu adım yalnızca Android Kullanıcıları içindir. Titreşim’i Android cihazlarda kullanmak için AndroidManifest.xml dosyasına Titreşim iznini manuel olarak eklemeliyiz.

Flutter_projesi (Benim projemin adı alert_vibra) -> android -> app -> src -> main -> AndroidManifest.xml dosyasını açın ve aşağıdaki kodu ekleyin.

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.alert_vibra">
    
    <uses-permission android:name="android.permission.VIBRATE" />

   <application
        android:label="alert_vibra"
        .
        .
        .
        .

#3 Kurulum işlemini yaptık. Şimdi, uygulamayı kodlamaya başlayabiliriz. Projenizin main.dart dosyasını açın ve paketleri içeri aktarın.

import 'package:flutter/material.dart';
import 'package:vibration/vibration.dart';

#4 void main runApp() metodunu oluşturup MyApp sınıfını çağırıyoruz.

void main() => runApp(MyApp());

#5  Daha sonra StatelessWidget oluşturuyoruz.

class MyApp extends StatelessWidget {



}

#6  Sırasıyla Widget Build -> MaterialApp -> Scaffold widget -> Body area -> Column widget, oluşturun.

Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: const Text('Flutter Titreşim Uyarısı'),
        ),
        body: Builder(
          builder: (BuildContext context) {
            return Center(
              child: Column(
                children: <Widget>[
                 

                ],
              ),
            );
          },
        ),
      ),
    );
  }

#7 3 tane Container widgetı oluşturun. Her container widgetına ElevatedButton.icon() oluşturun ve onPressed özelliklerine sırasıyla ;

  1. Vibration.vibrate()
  2.  Vibration.vibrate(duration: 1000)
  3. Vibration.vibrate( pattern: [ 500, 1000, 500, 2000, 500, 3000, 500, 500 ], ) fonksiyonlarını verin.

Not: 1000 =  1 saniye

Container(
      margin: const EdgeInsets.all(5),
      child: ElevatedButton.icon(
        onPressed: () {
          Vibration.vibrate();
        },
        label: Text('0.5 saniye titret. (Varsayılan)'),
        icon: Icon(Icons.vibration),
        style: ElevatedButton.styleFrom(
          primary: Colors.green,
          textStyle: TextStyle(
            color: Colors.white,
            fontSize: 24,
          ),
        ),
      )),
  Container(
      margin: const EdgeInsets.all(5),
      child: ElevatedButton.icon(
        onPressed: () {
          Vibration.vibrate(duration: 1000);
        },
        label: Text('1 saniye titret'),
        icon: Icon(Icons.vibration),
        style: ElevatedButton.styleFrom(
          primary: Colors.green,
          textStyle: TextStyle(
            color: Colors.white,
            fontSize: 24,
          ),
        ),
      )),
  Container(
      margin: const EdgeInsets.all(5),
      child: ElevatedButton.icon(
        onPressed: () {
          final snackBar = SnackBar(
            content: Text(
              'Pattern: 0.5s bekle, 1s titret, 0.5s bekle, 2s titret, 0.5s bekle, 3s titret, 0.5s bekle, 0.5s titret',
            ),
          );
          Scaffold.of(context).showSnackBar(snackBar);
          Vibration.vibrate(
            pattern: [
              500,
              1000,
              500,
              2000,
              500,
              3000,
              500,
              500
            ],
          );
        },
        label: Text('Modellli titret'),
        icon: Icon(Icons.vibration),
        style: ElevatedButton.styleFrom(
          primary: Colors.green,
          textStyle: TextStyle(
            color: Colors.white,
            fontSize: 24,
          ),
        ),
      )),

#8 Tam Kod (main.dart)

import 'package:flutter/material.dart';
import 'package:vibration/vibration.dart';

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: const Text('Flutter Titreşim Uyarısı'),
        ),
        body: Builder(
          builder: (BuildContext context) {
            return Center(
              child: Column(
                children: <Widget>[
                  Container(
                      margin: const EdgeInsets.all(5),
                      child: ElevatedButton.icon(
                        onPressed: () {
                          Vibration.vibrate();
                        },
                        label: Text('0.5 saniye titret. (Varsayılan)'),
                        icon: Icon(Icons.vibration),
                        style: ElevatedButton.styleFrom(
                          primary: Colors.green,
                          textStyle: TextStyle(
                            color: Colors.white,
                            fontSize: 24,
                          ),
                        ),
                      )),
                  Container(
                      margin: const EdgeInsets.all(5),
                      child: ElevatedButton.icon(
                        onPressed: () {
                          Vibration.vibrate(duration: 1000);
                        },
                        label: Text('1 saniye titret'),
                        icon: Icon(Icons.vibration),
                        style: ElevatedButton.styleFrom(
                          primary: Colors.green,
                          textStyle: TextStyle(
                            color: Colors.white,
                            fontSize: 24,
                          ),
                        ),
                      )),
                  Container(
                      margin: const EdgeInsets.all(5),
                      child: ElevatedButton.icon(
                        onPressed: () {
                          final snackBar = SnackBar(
                            content: Text(
                              'Pattern: 0.5s bekle, 1s titret, 0.5s bekle, 2s titret, 0.5s bekle, 3s titret, 0.5s bekle, 0.5s titret',
                            ),
                          );
                          Scaffold.of(context).showSnackBar(snackBar);
                          Vibration.vibrate(
                            pattern: [
                              500,
                              1000,
                              500,
                              2000,
                              500,
                              3000,
                              500,
                              500
                            ],
                          );
                        },
                        label: Text('Modellli titret'),
                        icon: Icon(Icons.vibration),
                        style: ElevatedButton.styleFrom(
                          primary: Colors.green,
                          textStyle: TextStyle(
                            color: Colors.white,
                            fontSize: 24,
                          ),
                        ),
                      )),
                ],
              ),
            );
          },
        ),
      ),
    );
  }
}

#9 Kod Çıktısı

vibration Alert flutter

Not: Çalıştırırken hata alırsanız terminalde flutter run --no-sound-null-safety komutunu çalıştırın.

 

 

1 Comment

Yorum Yap