In this tutorial we will create a simple product page, but we will use a 3D object. To do it we will utilize flutter_3d_controller package.
This is a versatile 3D rendering package, that also allows to control animations. The goal is to create an interactive page, where the object slowly spins arround, but the user can hold it and rotate it as they please.
Here's a preview of how the complete page will look.
Pretty cool right? So lets begin by adding the package. In my case, I'm using version 1.3.1.
You can paste it in pubspec.yaml file manually, or using a command:
To create this functionality successfully, there are a few things to note. First of all we will need some 3D assets.
For a business project, these would likely be created in accordance to your product offering. For testing, you can download some free assets from sites like sketchfab.com.
I recommend working with glb or gltf formats, but flutter_3d_controller will support other formats as well.
Next we will make an auto-rotate function that will periodically update the angle variable. We then can use this angle value in setCameraOrbit function to rotate the 3D object. This now will rotate the object, but we also want to be able to interact with it via touch.
While it might seem intuitive to use the GestureDetector to disable auto-rotation when we touch the object, this will not work as expected, as the Flutter3DViewer widget handles touch events internally.
One way to work arround that is to use PointerEvent. This will allow us to detect touch interactions and manage the auto-rotation behavior.
And here is the full code for creating such a page. Of course, feel free to design it however you like. While this example is for a product page, you can easily adapt the code for different purposes depending on your project's needs.