求助:在Flutter 3.3.9(空安全)中实现ar_flutter_plugin 0.7.3
在Flutter 3.3.9中实现ar_flutter_plugin插件
1. 环境前置检查
- 验证Flutter版本:执行命令
flutter --version,确保版本为3.3.9 - Android配置:在
android/app/build.gradle中将minSdkVersion设置为24(ARCore最低要求) - iOS配置:在
ios/Runner/Info.plist添加AR权限声明:<key>NSCameraUsageDescription</key> <string>需要相机权限以运行AR功能</string> <key>NSLocationWhenInUseUsageDescription</key> <string>需要位置权限以实现AR场景定位</string>
2. 添加插件依赖
在项目根目录的pubspec.yaml中添加依赖:
dependencies: ar_flutter_plugin: ^0.4.0 # 选择适配Flutter3.3.9的稳定版本
执行命令 flutter pub get 完成依赖安装
3. 基础AR视图实现
创建一个最简AR页面示例:
import 'package:flutter/material.dart'; import 'package:ar_flutter_plugin/ar_flutter_plugin.dart'; import 'package:ar_flutter_plugin/managers/ar_session_manager.dart'; import 'package:ar_flutter_plugin/managers/ar_object_manager.dart'; import 'package:ar_flutter_plugin/models/ar_node.dart'; class ARDemoPage extends StatefulWidget { const ARDemoPage({Key? key}) : super(key: key); @override State<ARDemoPage> createState() => _ARDemoPageState(); } class _ARDemoPageState extends State<ARDemoPage> { late ARSessionManager _arSessionManager; late ARObjectManager _arObjectManager; @override void dispose() { _arSessionManager.dispose(); super.dispose(); } @override Widget build(BuildContext context) { return Scaffold( appBar: AppBar(title: const Text('AR演示')), body: ARView( onARViewCreated: _onARViewCreated, planeDetectionConfig: PlaneDetectionConfig.horizontal, ), ); } void _onARViewCreated( ARSessionManager sessionManager, ARObjectManager objectManager, _, __, ) { _arSessionManager = sessionManager; _arObjectManager = objectManager; // 初始化AR会话 _arSessionManager.onInitialize( showPlanes: true, showFeaturePoints: false, showWorldOrigin: false, ); _arObjectManager.onInitialize(); } }
4. 加载3D模型到AR场景
加载本地glTF模型
先在pubspec.yaml中声明模型资源:
flutter: assets: - assets/models/your_model.gltf - assets/models/your_model.bin # 若模型包含此文件需一并声明
然后在_onARViewCreated中添加模型加载逻辑:
final localModelNode = ARNode( type: NodeType.localGLTF2, uri: "assets/models/your_model.gltf", scale: const Vector3(0.5, 0.5, 0.5), position: const Vector3(0, 0, -1), ); _arObjectManager.addNode(localModelNode);
加载网络glTF模型
final webModelNode = ARNode( type: NodeType.webGLTF2, uri: "https://example.com/your_model.gltf", scale: const Vector3(1.0, 1.0, 1.0), ); _arObjectManager.addNode(webModelNode);
5. 常见问题排查
- Android运行失败:确认设备支持ARCore,且
minSdkVersion不低于24 - iOS运行失败:确保设备为iPhone 6s及以上,且已正确配置Info.plist权限
- 模型加载失败:检查模型路径是否正确,仅支持glTF 2.0格式,网络模型需确保设备能正常访问对应地址
内容的提问来源于stack exchange,提问作者saied foyez
相关产品推荐
相关产品推荐

