sorisdk_flutter 0.3.1 copy "sorisdk_flutter: ^0.3.1" to clipboard
sorisdk_flutter: ^0.3.1 copied to clipboard

Add SORI-powered audio recognition, campaign discovery, and action-link handling to Flutter apps on Android, iOS, and Web.

example/lib/main.dart

import 'dart:async';

import 'package:cached_network_image/cached_network_image.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:sorisdk_flutter/sorisdk_flutter.dart';

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

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

  @override
  State<ExampleApp> createState() => _ExampleAppState();
}

class _ExampleAppState extends State<ExampleApp> {
  static const applicationId = String.fromEnvironment('SORI_APP_ID');
  static const secretKey = String.fromEnvironment('SORI_SECRET_KEY');
  static const ephemeralKeyEndpoint = String.fromEnvironment(
    'SORI_EPHEMERAL_KEY_ENDPOINT',
  );

  late final SORIAudioRecognizer recognizer;
  StreamSubscription<SORIRecognitionEvent>? subscription;
  final campaigns = <RecognizedCampaign>[];
  var isStarting = false;
  var isRunning = false;
  String? statusMessage;

  bool get configured =>
      applicationId.isNotEmpty &&
      (kIsWeb ? ephemeralKeyEndpoint.isNotEmpty : secretKey.isNotEmpty);

  @override
  void initState() {
    super.initState();
    recognizer = kIsWeb
        ? SORIAudioRecognizer.web(
            applicationId: applicationId,
            webAuth: SORIWebAuthOptions.ephemeralKeyEndpoint(
              Uri.parse(ephemeralKeyEndpoint),
            ),
          )
        : SORIAudioRecognizer(
            applicationId: applicationId,
            secretKey: secretKey,
          );
    subscription = recognizer.events.listen(handleEvent);
  }

  @override
  void dispose() {
    subscription?.cancel();
    super.dispose();
  }

  void handleEvent(SORIRecognitionEvent event) {
    setState(() {
      if (event.type == SORIRecognitionEventType.stateChanged) {
        final state = event.payload['state'];
        isStarting = state == 'STARTING';
        isRunning = state == 'STARTING' || state == 'STARTED';
      }

      if (addOrReplaceCampaign(campaigns, event)) {
        statusMessage = null;
        return;
      }

      if (event.type == SORIRecognitionEventType.error ||
          event.type == SORIRecognitionEventType.networkError) {
        statusMessage = event.message ?? 'Recognition failed.';
      }
    });
  }

  Future<void> toggleRecognition() async {
    if (!configured || isStarting) {
      return;
    }

    if (isRunning) {
      await recognizer.stopRecognition();
      setState(() => isRunning = false);
      return;
    }

    setState(() {
      isStarting = true;
      statusMessage = null;
    });
    try {
      await recognizer.configure();
      await recognizer.startRecognition(
        notification: const SORIAndroidNotificationOptions(
          title: 'SORI recognition',
          body: 'Listening for SORI audio signals',
        ),
      );
      setState(() {
        isStarting = false;
        isRunning = true;
      });
    } on PlatformException catch (error) {
      setState(() {
        isStarting = false;
        statusMessage = error.message ?? error.code;
      });
    } catch (error) {
      setState(() {
        isStarting = false;
        statusMessage = error.toString();
      });
    }
  }

  Future<void> openCampaign(SORICampaign campaign) async {
    final url = campaign.actionUrl;
    if (url == null || url.isEmpty) {
      return;
    }

    try {
      await recognizer.handleActionUrl(url);
    } on PlatformException catch (error) {
      if (!mounted) {
        return;
      }
      ScaffoldMessenger.of(
        context,
      ).showSnackBar(SnackBar(content: Text(error.message ?? error.code)));
    }
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(title: const Text('SORI SDK Flutter')),
        body: CampaignList(
          campaigns: campaigns,
          configured: configured,
          statusMessage: statusMessage,
          onTapCampaign: openCampaign,
        ),
        floatingActionButton: FloatingActionButton(
          onPressed: configured && !isStarting ? toggleRecognition : null,
          tooltip: isRunning ? 'Stop recognition' : 'Start recognition',
          child: Icon(isRunning ? Icons.mic_off : Icons.mic),
        ),
      ),
    );
  }
}

class CampaignList extends StatelessWidget {
  const CampaignList({
    required this.campaigns,
    required this.configured,
    required this.onTapCampaign,
    this.statusMessage,
    super.key,
  });

  final List<RecognizedCampaign> campaigns;
  final bool configured;
  final String? statusMessage;
  final ValueChanged<SORICampaign> onTapCampaign;

  @override
  Widget build(BuildContext context) {
    final itemCount = campaigns.isEmpty ? 1 : campaigns.length;

    return ListView.builder(
      padding: const EdgeInsets.fromLTRB(16, 16, 16, 96),
      itemCount: itemCount,
      itemBuilder: (context, index) {
        if (campaigns.isEmpty) {
          return EmptyState(configured: configured, message: statusMessage);
        }
        return CampaignCard(
          campaign: campaigns[index].campaign,
          onTap: () => onTapCampaign(campaigns[index].campaign),
        );
      },
    );
  }
}

class EmptyState extends StatelessWidget {
  const EmptyState({required this.configured, this.message, super.key});

  final bool configured;
  final String? message;

  @override
  Widget build(BuildContext context) {
    return Padding(
      padding: const EdgeInsets.symmetric(vertical: 48),
      child: Center(
        child: Text(
          message ??
              (configured
                  ? 'No recognition results yet.'
                  : kIsWeb
                  ? 'Missing SORI_APP_ID or '
                        'SORI_EPHEMERAL_KEY_ENDPOINT.'
                  : 'Missing SORI_APP_ID or SORI_SECRET_KEY.'),
          textAlign: TextAlign.center,
        ),
      ),
    );
  }
}

class CampaignCard extends StatelessWidget {
  const CampaignCard({required this.campaign, required this.onTap, super.key});

  final SORICampaign campaign;
  final VoidCallback onTap;

  @override
  Widget build(BuildContext context) {
    return Card(
      clipBehavior: Clip.antiAlias,
      margin: const EdgeInsets.only(bottom: 12),
      child: InkWell(
        onTap: campaign.actionUrl == null ? null : onTap,
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.stretch,
          children: [
            CampaignImage(url: campaign.imageUrl),
            Padding(
              padding: const EdgeInsets.all(16),
              child: Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                children: [
                  Text(
                    campaign.name.isEmpty ? campaign.id : campaign.name,
                    style: Theme.of(context).textTheme.titleMedium,
                  ),
                  if (campaign.trait?.marker case final marker?) ...[
                    const SizedBox(height: 4),
                    Text('Marker: $marker'),
                  ],
                ],
              ),
            ),
          ],
        ),
      ),
    );
  }
}

class CampaignImage extends StatelessWidget {
  const CampaignImage({required this.url, super.key});

  final String? url;

  @override
  Widget build(BuildContext context) {
    if (url == null || url!.isEmpty) {
      return const SizedBox(
        height: 180,
        child: ColoredBox(
          color: Color(0xFFE0E0E0),
          child: Icon(Icons.image_not_supported_outlined, size: 40),
        ),
      );
    }

    return CachedNetworkImage(
      imageUrl: url!,
      height: 180,
      fit: BoxFit.cover,
      placeholder: (context, url) => const SizedBox(
        height: 180,
        child: Center(child: CircularProgressIndicator()),
      ),
      errorWidget: (context, url, error) => const SizedBox(
        height: 180,
        child: ColoredBox(
          color: Color(0xFFE0E0E0),
          child: Icon(Icons.broken_image_outlined, size: 40),
        ),
      ),
    );
  }
}

Map<String, Object?>? stringKeyedMap(Object? value) {
  if (value is Map<String, Object?>) {
    return value;
  }
  if (value is Map) {
    return value.map((key, value) => MapEntry(key.toString(), value));
  }
  return null;
}

class RecognizedCampaign {
  const RecognizedCampaign({required this.campaign, this.activityId});

  final SORICampaign campaign;
  final String? activityId;
}

/// Adds a legacy campaign as a new row, or replaces the row with the same
/// server activity identity when a refinement or duplicate delivery arrives.
bool addOrReplaceCampaign(
  List<RecognizedCampaign> campaigns,
  SORIRecognitionEvent event,
) {
  final result = recognizedCampaignFromEvent(event);
  if (result == null) {
    return false;
  }

  final activityId = result.activityId;
  final existingIndex = activityId == null
      ? -1
      : campaigns.indexWhere((item) => item.activityId == activityId);
  if (existingIndex < 0) {
    campaigns.insert(0, result);
  } else {
    final existing = campaigns[existingIndex];
    final existingMarker = existing.campaign.trait?.marker;
    final replacementMarker = result.campaign.trait?.marker;
    if (existingMarker == null || replacementMarker != null) {
      campaigns[existingIndex] = result;
    }
  }
  return true;
}

RecognizedCampaign? recognizedCampaignFromEvent(SORIRecognitionEvent event) {
  if (event.type != SORIRecognitionEventType.campaignFound &&
      event.type != SORIRecognitionEventType.recognitionResult) {
    return null;
  }

  var campaign = event.campaign;
  final payloadCampaign = stringKeyedMap(event.payload['campaign']);
  campaign ??= payloadCampaign == null
      ? null
      : SORICampaign.fromMap(payloadCampaign);
  campaign ??= SORICampaign.fromMap(event.payload);
  if (campaign.id.isEmpty && campaign.name.isEmpty) {
    return null;
  }

  return RecognizedCampaign(
    campaign: campaign,
    activityId: event.activityId ?? campaign.activityId,
  );
}
0
likes
130
points
307
downloads

Documentation

API reference

Publisher

verified publisheriplateia.com

Weekly Downloads

Add SORI-powered audio recognition, campaign discovery, and action-link handling to Flutter apps on Android, iOS, and Web.

Homepage

License

unknown (license)

Dependencies

flutter, flutter_web_plugins, plugin_platform_interface

More

Packages that depend on sorisdk_flutter

Packages that implement sorisdk_flutter