hapticlabs_player 0.1.0 copy "hapticlabs_player: ^0.1.0" to clipboard
hapticlabs_player: ^0.1.0 copied to clipboard

Cross-platform custom haptics in Flutter.

example/lib/main.dart

import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'dart:async';

import 'package:hapticlabs_player/hapticlabs_player.dart';
import 'package:hapticlabs_player/hapticlabs_player_android.dart';
import 'package:hapticlabs_player/hapticlabs_player_ios.dart';
import 'package:hapticlabs_player/hapticlabs_player_platform_interface.dart';

void main() {
  runApp(const MyApp());
}

class MyApp extends StatefulWidget {
  const MyApp({super.key});

  @override
  State<MyApp> createState() => _MyAppState();
}

class PlayButton extends StatelessWidget {
  final VoidCallback onPressed;
  const PlayButton({required this.onPressed, super.key});

  @override
  Widget build(BuildContext context) {
    return ElevatedButton(onPressed: onPressed, child: const Text('Play'));
  }
}

class _MyAppState extends State<MyApp> {
  final _hapticlabsPlayer = HapticlabsPlayer();

  @override
  void initState() {
    super.initState();
    initPlatformState();
  }

  // Platform messages are asynchronous, so we initialize in an async method.
  Future<void> initPlatformState() async {
    // If the widget was removed from the tree while the asynchronous platform
    // message was in flight, we want to discard the reply rather than calling
    // setState to update our non-existent appearance.
    if (!mounted) return;
  }

  // Local states for iOS mute status and Android haptic support level
  bool hapticsMuted = false;
  bool audioMuted = false;
  int androidHapticSupportLevel = 0;

  // Example: Query Android haptic support level (if supported by your plugin)
  Future<void> updateAndroidHapticSupportLevel() async {
    if (Theme.of(context).platform == TargetPlatform.android) {
      int level =
          (await _hapticlabsPlayer.getAndroidConstants()).hapticSupportLevel;
      setState(() {
        androidHapticSupportLevel = level;
      });
    }
  }

  // Example: Query iOS mute state (if supported by your plugin)
  Future<void> updateMuteStates() async {
    // These methods are placeholders; replace with actual plugin calls if available
    bool hMuted = await _hapticlabsPlayer.isHapticsMuted();
    bool aMuted = await _hapticlabsPlayer.isAudioMuted();
    setState(() {
      hapticsMuted = hMuted;
      audioMuted = aMuted;
    });
  }

  @override
  void didChangeDependencies() {
    super.didChangeDependencies();
    if (defaultTargetPlatform == TargetPlatform.android) {
      updateAndroidHapticSupportLevel();
    } else if (defaultTargetPlatform == TargetPlatform.iOS) {
      updateMuteStates();
    }
  }

  Widget _buildIOSControls() {
    return Column(
      children: [
        Text(
          'Haptics muted: ${hapticsMuted ? 'Yes' : 'No'}',
          style: TextStyle(
            fontSize: 18,
            fontWeight: FontWeight.bold,
            color: hapticsMuted ? Colors.red : Colors.green,
            height: 2,
          ),
        ),
        ElevatedButton(
          onPressed: () async {
            await _hapticlabsPlayer.setHapticsMute(!hapticsMuted);
            updateMuteStates();
          },
          child: const Text('Toggle Haptics Mute'),
        ),
        Text(
          'Audio muted: ${audioMuted ? 'Yes' : 'No'}',
          style: TextStyle(
            fontSize: 18,
            fontWeight: FontWeight.bold,
            color: audioMuted ? Colors.red : Colors.green,
            height: 2,
          ),
        ),
        ElevatedButton(
          onPressed: () async {
            await _hapticlabsPlayer.setAudioMute(!audioMuted);
            updateMuteStates();
          },
          child: const Text('Toggle Audio Mute'),
        ),
        ElevatedButton(
          onPressed: () async {
            await _hapticlabsPlayer.playAHAP('AHAP/CatPurring.ahap');
          },
          child: const Text('Play AHAP'),
        ),
      ],
    );
  }

  Widget _buildAndroidControls() {
    return Column(
      children: [
        Text('Haptic support level: $androidHapticSupportLevel'),
        ElevatedButton(
          onPressed: () async {
            await _hapticlabsPlayer.preload('Android samples/Purring cat.hac');
          },
          child: const Text('Preload Android Haptics'),
        ),
        ElevatedButton(
          onPressed: () async {
            await _hapticlabsPlayer.unload('Android samples/Purring cat.hac');
          },
          child: const Text('Unload Android Haptics'),
        ),
        ElevatedButton(
          onPressed: () async {
            await _hapticlabsPlayer.unloadAll();
          },
          child: const Text('Unload all Android Haptics'),
        ),
        ElevatedButton(
          onPressed: () async {
            await _hapticlabsPlayer.playHLA('Android samples/8bit/main.hla');
          },
          child: const Text('Play HLA'),
        ),
        ElevatedButton(
          onPressed: () async {
            await _hapticlabsPlayer.playHAC('Android samples/Purring cat.hac');
          },
          child: const Text('Play HAC'),
        ),
        ElevatedButton(
          onPressed: () async {
            await _hapticlabsPlayer.preloadOGG('Android samples/8bit/main.ogg');
          },
          child: const Text('Preload OGG'),
        ),
        ElevatedButton(
          onPressed: () async {
            await _hapticlabsPlayer.unloadOGG('Android samples/8bit/main.ogg');
          },
          child: const Text('Unload OGG'),
        ),
        ElevatedButton(
          onPressed: () async {
            await _hapticlabsPlayer.playOGG('Android samples/8bit/main.ogg');
          },
          child: const Text('Play OGG'),
        ),
      ],
    );
  }

  Widget _buildPredefinedButton() {
    return ElevatedButton(
      onPressed: () {
        _hapticlabsPlayer.playPredefinedHaptics(
          iosEffect: IOSPredefinedHapticEffect.heavy,
          androidEffect: AndroidPredefinedHapticEffect.heavyClick,
        );
      },
      child: const Text('Cross-Platform: Predefined "Heavy"'),
    );
  }

  Widget _buildCrossPlatformButton() {
    return ElevatedButton(
      onPressed: () {
        _hapticlabsPlayer.playHaptics(
          iosPath: 'AHAP/Button.ahap',
          androidPath: 'Android samples/Button.hac',
        );
      },
      child: const Text('Cross-Platform: Play Haptics'),
    );
  }

  @override
  Widget build(BuildContext context) {
    final isIOS = Theme.of(context).platform == TargetPlatform.iOS;
    final isAndroid = Theme.of(context).platform == TargetPlatform.android;

    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(title: const Text('Plugin example app')),
        body: Center(
          child: SingleChildScrollView(
            child: Column(
              mainAxisAlignment: MainAxisAlignment.center,
              children: [
                _buildCrossPlatformButton(),
                _buildPredefinedButton(),
                if (isIOS) _buildIOSControls(),
                if (isAndroid) _buildAndroidControls(),
              ],
            ),
          ),
        ),
      ),
    );
  }
}