搜索插件、代码仓与 APP 分类

插件详情v0.8.1

model_viewer

一个用于渲染 glTF 和 GLB 格式交互式 3D 模型的 Flutter 小部件。

近 30 天下载
32
喜欢数
142
Pub 分数
30

安装插件

flutter pub add model_viewer

插件介绍

基于插件公开信息、社区数据与项目使用情况整理的选型参考。

一个 Flutter 小部件,可启用 glTF 和 GLB 格式的 3D 模型的渲染和交互。它提供了一种简单的方法,可在 Flutter 应用中嵌入和操作 3D 内容,支持旋转、缩放和拖动等功能。非常适合需要沉浸式视觉体验的应用程序,例如产品展示或教育工具。该小部件利用原生平台功能以实现最佳性能和流畅的交互。

适合场景

  • 电子商务应用中的产品可视化
  • 互动3D教育内容
  • 虚拟试穿服装或配饰
  • 增强现实集成
  • 设计工具中的3D模型预览

主要特点

  • 支持 glTF 和 GLB 3D 模型格式
  • 内置的触摸手势用于旋转和缩放
  • 使用原生后端的高性能渲染
  • 与 Flutter 小部件轻松集成
  • 轻量级且专注于核心3D查看

适合对象

  • 开发支持3D的Flutter应用的开发者
  • 展示产品的电子商务平台
  • 教育或培训应用程序
  • 带有3D资产的AR/VR体验

使用建议

  • 仅支持 glTF/GLB 格式
  • 无内置动画或物理处理
  • 需要对3D模型进行优化以获得最佳性能
  • 平台特定的渲染行为可能会有所不同

原始 README

以下为关联源码仓库的 README 原文快照,最新内容请访问源码仓库。

3D Model Viewer for Flutter

Project license Dart compatibility Pub package Dartdoc reference

This is a Flutter widget for rendering interactive 3D models in the glTF and GLB formats.

The widget embeds Google's <model-viewer> web component in a WebView.

Screenshot

Screenshot of astronaut model

Prerequisites

Compatibility

Android and iOS, with a recent system browser version.

Installation

pubspec.yaml
dependencies:
  model_viewer: ^0.8.1
AndroidManifest.xml (Android 9+ only)

To use this widget on Android 9+ devices, your app must be permitted to make an HTTP connection to http://localhost:XXXXX. Android 9 (API level 28) changed the default for android:usesCleartextTraffic from true to false, so you will need to configure your app's android/app/src/main/AndroidManifest.xml as follows:

--- a/example/android/app/src/main/AndroidManifest.xml
+++ b/example/android/app/src/main/AndroidManifest.xml
@@ -8,7 +8,8 @@
     <application
         android:name="io.flutter.app.FlutterApplication"
         android:label="model_viewer_example"
-        android:icon="@mipmap/ic_launcher">
+        android:icon="@mipmap/ic_launcher"
+        android:usesCleartextTraffic="true">
         <activity
             android:name=".MainActivity"
             android:launchMode="singleTop"

This does not affect Android 8 and earlier. See #7 for more information.

Info.plist (iOS only)

To use this widget on iOS, you need to opt-in to the embedded views preview by adding a boolean property to your app's ios/Runner/Info.plist file, with the key io.flutter.embedded_views_preview and the value YES:

<key>io.flutter.embedded_views_preview</key>
<true/>

Features

  • Renders glTF and GLB models. (Also, USDZ models on iOS 12+.)

  • Supports animated models, with a configurable auto-play setting.

  • Optionally supports launching the model into an AR viewer.

  • Optionally auto-rotates the model, with a configurable delay.

  • Supports a configurable background color for the widget.

Examples

Importing the library
import 'package:model_viewer/model_viewer.dart';
Creating a ModelViewer widget
class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(title: Text("Model Viewer")),
        body: ModelViewer(
          src: 'https://modelviewer.dev/shared-assets/models/Astronaut.glb',
          alt: "A 3D model of an astronaut",
          ar: true,
          autoRotate: true,
          cameraControls: true,
        ),
      ),
    );
  }
}
Loading a bundled Flutter asset
class MyApp extends StatelessWidget {
// ...
          src: 'assets/MyModel.glb',
// ...
}
Loading a model from the file system
class MyApp extends StatelessWidget {
// ...
          src: 'file:///path/to/MyModel.glb',
// ...
}
Loading a model from the web
class MyApp extends StatelessWidget {
// ...
          src: 'https://modelviewer.dev/shared-assets/models/Astronaut.glb',
// ...
}

Note that due to browsers' CORS security restrictions, the model file must be served with a Access-Control-Allow-Origin: * HTTP header.

Frequently Asked Questions

Q: Why do I get the error net::ERR_CLEARTEXT_NOT_PERMITTED?

You didn't configure your AndroidManifest.xml as per the installation instructions earlier in this document. See also #7.

Q: Why does the example app just show a blank screen?

A: Most likely, the platform browser version on your device or emulator is too old and does not support the features that Model Viewer needs.

For example, the stock Chrome version on the Android 10 emulator is too old and will display a blank screen; it must be upgraded from the Play Store in order to use this package. (The stock Chrome version on the Android 11 emulator works fine, however.) See google/model-viewer#1109.

Q: Why doesn't my 3D model load and/or render?

A: There are several reasons why your model URL could fail to load and render:

  1. It might not be possible to load the model URL due to CORS security restrictions. The server hosting the model file must send appropriate CORS response headers for Model Viewer to be able to load the file. See google/model-viewer#1015.

  2. It might not be possible to parse the provided glTF or GLB file. Some tools can produce invalid files when exporting glTF. Always run your model files through the glTF Validator to check for this.

  3. The platform browser might not support the features that Model Viewer needs. See google/model-viewer#1109.

所属分类

依赖

依赖清单 3 项

源码仓库

使用此插件的项目