banuba_sdk 3.0.3 banuba_sdk: ^3.0.3 copied to clipboard
Banuba SDK for Flutter - full-scale augmented reality SDK | Flutter face filters | Virtual try-on | Background replacement | Face tracking
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'dart:async';
import 'dart:io';
import 'package:permission_handler/permission_handler.dart';
import 'package:banuba_sdk/banuba_sdk.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatefulWidget {
const MyApp({super.key});
@override
State<MyApp> createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> with WidgetsBindingObserver {
final _banubaSdkManager = BanubaSdkManager();
final _epWidget = EffectPlayerWidget(key: null);
@override
void initState() {
super.initState();
initPlatformState();
}
// Platform messages are asynchronous, so we initialize in an async method.
Future<void> initPlatformState() async {
await _banubaSdkManager.initialize([],
<#"Place Token here"#>,
SeverityLevel.info);
// 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;
setState(() {});
// It is required to grant all permissions for the plugin: Camera, Micro, Storage
requestPermissions().then((granted) {
if (granted) {
debugPrint('CameraPage: Thanks! All permissions are granted!');
openCamera();
} else {
debugPrint('CameraPage: WARNING! Not all required permissions are granted!');
// Plugin cannot be used. Handle this state on your app side
SystemNavigator.pop();
}
}).onError((error, stackTrace) {
debugPrint('CameraPage: ERROR! Plugin cannot be used : $error');
// Plugin cannot be used. Handle this state on your app side
SystemNavigator.pop();
});
}
Future<void> openCamera() async {
await _banubaSdkManager.openCamera();
await _banubaSdkManager.attachWidget(_epWidget.banubaId);
await _banubaSdkManager.startPlayer();
await _banubaSdkManager.loadEffect("effects/TrollGrandma", false);
}
@override
Widget build(BuildContext context) {
return MaterialApp(home: _epWidget);
}
@override
void didChangeAppLifecycleState(AppLifecycleState state) {
if (state == AppLifecycleState.resumed) {
_banubaSdkManager.startPlayer();
} else {
_banubaSdkManager.stopPlayer();
}
}
}
// This is a sample implementation of requesting permissions.
// It is expected that the user grants all permissions. This solution does not handle the case
// when the user denies access or navigating the user to Settings for granting access.
// Please implement better permissions handling in your project.
Future<bool> requestPermissions() async {
final requiredPermissions = _getPlatformPermissions();
for (var permission in requiredPermissions) {
var ps = await permission.status;
if (!ps.isGranted) {
ps = await permission.request();
if (!ps.isGranted) {
return false;
}
}
}
return true;
}
List<Permission> _getPlatformPermissions() {
if (Platform.isAndroid) {
return [Permission.camera, Permission.microphone, Permission.storage];
} else if (Platform.isIOS) {
return [Permission.camera, Permission.microphone];
} else {
throw Exception('Platform is not supported!');
}
}