How to Implement a 3D Model Viewer in Flutter

Aug 26, 2021 | Programming

In today’s digital landscape, user interactivity plays a vital role in applications, especially when showcasing 3D models. If you’re venturing into Flutter development, you’re in for a treat, as you can easily integrate a 3D Model Viewer widget that supports glTF and GLB formats. Let’s dive into how to set this up!

Prerequisites

Compatibility

This widget is compatible with both Android and iOS, provided your system browser is up to date. Check out the browser support for more details.

Installation Steps

Follow these steps to include the model viewer in your Flutter app:

1. Update your pubspec.yaml

dependencies:
  model_viewer: ^0.8.1

2. Configure AndroidManifest.xml (For Android 9+)

If you are deploying on Android 9 or higher, allow your app to make HTTP connections. Modify the AndroidManifest.xml like this:

android:usesCleartextTraffic=true

This change ensures that your model viewer will work correctly. Ensure the snippet is placed within the application tag.

3. Update Info.plist (For iOS)

For iOS applications, you need to embed views. Add this to your Info.plist:

io.flutter.embedded_views_preview

Using the Model Viewer Widget

Let’s see how to utilize the Model Viewer widget to display a 3D model.

Basic Example

Here’s how you can create a basic Model Viewer in your app:

import 'package:model_viewer/model_viewer.dart';

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 Models from Different Sources

You can load models not just from the web, but also from the file system and bundled assets:

// Loading from bundled assets
src: 'assets/MyModel.glb',

// Loading from the file system
src: 'file:path/to/MyModel.glb',

// Loading from the web
src: 'https://modelviewer.dev/shared-assets/models/Astronaut.glb',

Do remember to serve model files with the appropriate Access-Control-Allow-Origin: * HTTP header to avoid issues.

Troubleshooting Common Issues

Here are some tips to troubleshoot common problems you might encounter:

  • Issue: Error net::ERR_CLEARTEXT_NOT_PERMITTED.
    Solution: Ensure your AndroidManifest.xml is configured properly to allow clear text traffic.
  • Issue: The app shows a blank screen.
    Solution: This could be due to an outdated browser version on your device. Update the browser to a recent version.
  • Issue: The 3D model fails to load.
    Solution: Verify that your model URL adheres to CORS security requirements, validate the model file, and ensure the browser supports the necessary features.

For more insights, updates, or to collaborate on AI development projects, stay connected with fxis.ai.

Conclusion

With just a few steps, you can elegantly showcase 3D models in your Flutter application using the Model Viewer widget. By integrating it seamlessly, you advance your app’s interactivity and user experience.

At fxis.ai, we believe that such advancements are crucial for the future of AI, as they enable more comprehensive and effective solutions. Our team is continually exploring new methodologies to push the envelope in artificial intelligence, ensuring that our clients benefit from the latest technological innovations.

Stay Informed with the Newest F(x) Insights and Blogs

Tech News and Blog Highlights, Straight to Your Inbox