본문 바로가기
Flutter

[Flutter] Vibration 라이브러리로 진동효과 주기

by 김무스비 2025. 1. 22.
728x90
반응형

오늘은 Vibration 패키지로 특정 상황에서 진동 효과를 줄 수 있게끔 구현해보겠습니다.

우선, pub.dev 링크는

https://pub.dev/packages/vibration

 

vibration | Flutter package

A plugin for handling Vibration API on iOS, Android, web and OpenHarmony.

pub.dev


유의사항

android의 경우 AndroidManifest.xml에 manifest 와 application 태그 사이에 아래의 태그를 삽입해주셔야 합니다.

<uses-permission android:name="android.permission.VIBRATE"/>

<manifest xmlns:android="http://schemas.android.com/apk/res/android">
    <uses-permission android:name="android.permission.VIBRATE"/>
    <application  ~~ 중략

요런 식으로 말이죠.

 

사용법

사용법은 매우 간단합니다.

특정 버튼을 눌렀을때 진동이 오게끔 하려면, 아래의 코드처럼 onPressed 메서드에 Vibration.vibrate 메서드만 추가해주시면 됩니다. 

더보기




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

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

class VibratingApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: const Text('Vibration Plugin example app'),
        ),
        body: Builder(
          builder: (BuildContext context) {
            return Center(
              child: Column(
                children: <Widget>[
                  ElevatedButton(
                    child: Text('Vibrate for default 500ms'),
                    onPressed: () {
                      Vibration.vibrate();
                    },
                  ),
                  ElevatedButton(
                    child: Text('Vibrate for 1000ms'),
                    onPressed: () {
                      Vibration.vibrate(duration: 1000);
                    },
                  ),
                  ElevatedButton(
                    child: Text('Vibrate with pattern'),
                    onPressed: () {
                      final snackBar = SnackBar(
                        content: Text(
                          'Pattern: wait 0.5s, vibrate 1s, wait 0.5s, vibrate 2s, wait 0.5s, vibrate 3s, wait 0.5s, vibrate 0.5s',
                        ),
                      );
                      ScaffoldMessenger.of(context).showSnackBar(snackBar);
                      Vibration.vibrate(
                        pattern: [500, 1000, 500, 2000, 500, 3000, 500, 500],
                      );
                    },
                  ),
                  ElevatedButton(
                    child: Text('Vibrate with pattern and amplitude'),
                    onPressed: () {
                      final snackBar = SnackBar(
                        content: Text(
                          'Pattern: wait 0.5s, vibrate 1s, wait 0.5s, vibrate 2s, wait 0.5s, vibrate 3s, wait 0.5s, vibrate 0.5s',
                        ),
                      );

                      ScaffoldMessenger.of(context).showSnackBar(snackBar);
                      Vibration.vibrate(
                        pattern: [500, 1000, 500, 2000, 500, 3000, 500, 500],
                        intensities: [0, 128, 0, 255, 0, 64, 0, 255],
                      );
                    },
                  )
                ],
              ),
            );
          },
        ),
      ),
    );
  }
}

 

진동 상세 옵션 설정하기

진동의 상세 옵션을 조정할 수도 있는데요,

Vibration.vibrate(duration: 1000); 로 하면 1초, 

Vibration.vibrate(duration: 1000, amplitude: 128); 로 하면 1초, 진폭 128

Vibration.vibrate(pattern: [500, 1000, 500, 2000]); 로 하면 0.5초 대기, 1초 진동, 0.5초 대기, 2초 진동

과 같은 방식입니다.

만약 진동을 취소하고 싶다면, Vibration.cancel(); 메서드를 추가해주시면 됩니다.


참고로, android 일부 태블릿 기기에서는 진동 기능을 지원하지 않으니, 진동을 지원하는 기기로 테스트 해보시길 추천드립니다!(제가 galaxy tab s6 lite로 테스트하다가 진동이 안와서 한참 헤맸다는...)

728x90
반응형