ar_tryon_view 0.0.10
ar_tryon_view: ^0.0.10 copied to clipboard
Flutter AR try-on for face, hands, wrists, fingers and feet, with Android and Web GLB placement and remote model loading.
import 'package:ar_tryon_view/ar_tryon_view.dart';
import 'package:flutter/material.dart';
import 'package:flutter/foundation.dart';
import 'package:permission_handler/permission_handler.dart';
void main() {
WidgetsFlutterBinding.ensureInitialized();
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: false,
title: 'AR Try-on View',
theme: ThemeData(useMaterial3: true),
home: const DemoScreen(),
);
}
}
class DemoScreen extends StatefulWidget {
const DemoScreen({super.key});
@override
State<DemoScreen> createState() => _DemoScreenState();
}
class _DemoScreenState extends State<DemoScreen> {
ArTryOnController? _controller;
bool _starting = false;
ArTarget _target = ArTarget.leftWrist;
ArTryOnCamera _bodyCamera = ArTryOnCamera.rear;
bool _bodyBusy = false;
Future<void> _startCamera() async {
if (!mounted || _starting) return;
_starting = true;
try {
if (!kIsWeb) {
final status = await Permission.camera.request();
if (!status.isGranted) {
if (!mounted) return;
ScaffoldMessenger.of(context).showSnackBar(
const SnackBar(content: Text('Camera permission denied')),
);
return;
}
}
if (!mounted) return;
await _controller?.start();
} catch (e) {
if (!mounted) return;
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(content: Text('Camera start failed: $e')),
);
} finally {
_starting = false;
}
}
Future<void> _showPngFace() async {
try {
await _controller?.setEffectAsset('assets/glasses_01.png');
} catch (e) {
if (!mounted) return;
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(content: Text('PNG overlay failed: $e')),
);
}
}
Future<void> _placeGlb() async {
try {
final file = await ArGlbPlacement.pickGlb();
if (file == null) return;
await ArGlbPlacement.open(
modelUri: file.uri,
modelSizeM: 0.55,
);
} catch (e) {
if (!mounted) return;
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(content: Text('AR placement failed: $e')),
);
}
}
Future<void> _placeFaceGlb() async {
try {
final file = await ArFacePlacement.pickGlb();
if (file == null || !mounted) return;
await ArFacePlacement.open(modelUri: file.uri);
} catch (e) {
if (!mounted) return;
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(content: Text('Face AR failed: $e')),
);
}
}
Future<void> _openRemote({required bool face}) async {
var url = '';
final selected = await showDialog<String>(
context: context,
builder: (context) => AlertDialog(
title: Text(face ? 'Face AR model URL' : 'Floor AR model URL'),
content: TextField(
autofocus: true,
keyboardType: TextInputType.url,
onChanged: (value) => url = value,
decoration: const InputDecoration(hintText: 'https://example.com/model.glb'),
),
actions: [
TextButton(onPressed: () => Navigator.pop(context), child: const Text('Cancel')),
FilledButton(onPressed: () => Navigator.pop(context, url.trim()), child: const Text('Open')),
],
),
);
if (selected == null || selected.isEmpty || !mounted) return;
try {
if (face) {
await ArFacePlacement.openUrl(url: selected);
} else {
await ArGlbPlacement.openUrl(url: selected);
}
} catch (error) {
if (!mounted) return;
ScaffoldMessenger.of(context).showSnackBar(SnackBar(content: Text('GLB URL error: $error')));
}
}
Future<void> _bodyTryOn({required bool remote}) async {
if (_bodyBusy) return;
setState(() => _bodyBusy = true);
try {
final target = _target;
final camera = _bodyCamera;
if (remote) {
var url = '';
final selected = await showDialog<String>(
context: context,
builder: (context) => AlertDialog(
title: const Text('Body AR model URL'),
content: TextField(
keyboardType: TextInputType.url,
onChanged: (value) => url = value,
decoration: const InputDecoration(hintText: 'https://example.com/model.glb'),
),
actions: [
TextButton(onPressed: () => Navigator.pop(context), child: const Text('Cancel')),
FilledButton(onPressed: () => Navigator.pop(context, url.trim()), child: const Text('Open')),
],
),
);
if (selected == null || selected.isEmpty || !mounted) return;
await ArVirtualTryOn.openUrl(url: selected, target: target, camera: camera);
} else {
await ArVirtualTryOn.pickAndOpen(target: target, camera: camera);
}
} catch (error) {
if (mounted) ScaffoldMessenger.of(context).showSnackBar(
SnackBar(content: Text('Body AR: $error')),
);
} finally {
if (mounted) setState(() => _bodyBusy = false);
}
}
@override
void dispose() {
_controller?.dispose();
super.dispose();
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('AR Try-on View'),
centerTitle: true,
),
body: Column(
children: [
Expanded(
child: ArTryOnView(
trackFace: ArTryOnView.supportsFaceTracking,
onCreated: (controller) {
_controller = controller;
WidgetsBinding.instance.addPostFrameCallback((_) {
if (!kIsWeb) _startCamera();
});
},
),
),
Flexible(
child: SafeArea(
top: false,
child: SingleChildScrollView(
padding: const EdgeInsets.all(12),
child: Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
Text(
ArTryOnView.supportsFaceTracking
? '1. Experimental eyewear tracking'
: '1. PNG camera overlay',
style: const TextStyle(
fontWeight: FontWeight.w700,
),
),
const SizedBox(height: 8),
Row(
children: [
Expanded(
child: FilledButton(
onPressed: _showPngFace,
child: const Text('Show PNG'),
),
),
const SizedBox(width: 8),
Expanded(
child: OutlinedButton(
onPressed: () => _controller?.clearEffect(),
child: const Text('Clear PNG'),
),
),
],
),
const SizedBox(height: 14),
const Text(
'2. GLB floor placement',
style: TextStyle(fontWeight: FontWeight.w700),
),
const SizedBox(height: 8),
FilledButton.icon(
onPressed: ArGlbPlacement.isSupported ? _placeGlb : null,
icon: const Icon(Icons.view_in_ar),
label: const Text('Choose .glb and place in AR'),
),
OutlinedButton.icon(
onPressed: ArGlbPlacement.isSupported ? () => _openRemote(face: false) : null,
icon: const Icon(Icons.link),
label: const Text('Open Floor AR from URL'),
),
const SizedBox(height: 14),
const Text('3. 3D Face AR',
style: TextStyle(fontWeight: FontWeight.w700)),
FilledButton.icon(
onPressed: ArFacePlacement.isSupported ? _placeFaceGlb : null,
icon: const Icon(Icons.face_retouching_natural),
label: const Text('Choose .glb and apply to face'),
),
OutlinedButton.icon(
onPressed: ArFacePlacement.isSupported ? () => _openRemote(face: true) : null,
icon: const Icon(Icons.link),
label: const Text('Open Face AR from URL'),
),
const SizedBox(height: 14),
const Text('4. Hand, Foot & Body AR (experimental)',
style: TextStyle(fontWeight: FontWeight.w700)),
DropdownButton<ArTarget>(
isExpanded: true,
value: _target,
items: ArTarget.values.where((t) =>
t != ArTarget.face && t != ArTarget.nose &&
t != ArTarget.floor && t != ArTarget.wall).map((t) =>
DropdownMenuItem(value: t, child: Text(t.name))).toList(),
onChanged: _bodyBusy ? null : (value) {
if (value != null) setState(() => _target = value);
},
),
DropdownButton<ArTryOnCamera>(
isExpanded: true,
value: _bodyCamera,
items: ArTryOnCamera.values.map((c) =>
DropdownMenuItem(value: c, child: Text('${c.name} camera'))).toList(),
onChanged: _bodyBusy ? null : (value) {
if (value != null) setState(() => _bodyCamera = value);
},
),
FilledButton.icon(
onPressed: ArVirtualTryOn.isSupported && !_bodyBusy
? () => _bodyTryOn(remote: false) : null,
icon: const Icon(Icons.back_hand),
label: const Text('Choose GLB for selected body part'),
),
OutlinedButton.icon(
onPressed: ArVirtualTryOn.isSupported && !_bodyBusy
? () => _bodyTryOn(remote: true) : null,
icon: const Icon(Icons.link),
label: const Text('Open body AR from URL'),
),
OutlinedButton(
onPressed: ArTryOnView.isSupported ? _startCamera : null,
child: const Text('Resume camera preview'),
),
],
),
),
),
),
],
),
);
}
}