Flutter and 3D

What if I want to display something in 3D ? Hmmm…

Flutter and 3D

Pre-Requisite…

The application created is for Flutter Desktop…In case you are new to it, we got you covered, follow the article here

Flutter and 3D

Begin…

This article lays focus on the following widgets :

Transform…

As per the official Flutter documentation,

“A widget that applies a transformation before painting its child.”

This is an important widget if you aim for 3d transformation…

Our code using Transform Widget,

Flutter and 3D

Parameters for Transform Widget :

  • alignment : Alignment of the origin (Our case FractionalOffset.centerLeft)
  • transform : Matrix to transform…
  • child : Widget on which Transformation occurs

Transform Matrix…

Transform takes a 3D transformation matrix, which is a Matrix4.

Steps :

  1. Create the matrix:

Start with an identity matrix, and apply transformations to it.

Matrix4.identity()

2. Set the perspective:

According to an article from Flutter

Perspective makes objects that are farther away appear smaller. Setting row 3, column 2 of the matrix to 0.001 scales things down based on their distance.

Now we know,

setEntry(3, 2, 0.0011)

3 means the Row 3, 2 means the Column 2

The number 0.0011 : is the perspective number…This number can only be achieved by playing around…

For our case, 0.0011 (fits fine)

3. Set the rotation / Tilting the axis….

..rotateX(_offset.dy)
..rotateY(_offset.dx),

rotateX and rotateY take in angle as parameters. For our case, we have defined as offset as :

Offset _offset = Offset(0.3, -0.9);

Again, these values were achieved by playing around….

4. Specify child : RotationTransition in our case

RotationTransition….

As per the official Flutter documentation,

Animates the rotation of a widget.

This is the child widget for our 3d transformation…

Flutter and 3D

Parameters for RotationTransition Widget :

  • turns : animation (our case animation controller)
  • child : Widget on which rotation occurs…

Note : As your turns need a controller, hence the widget should be a Stateful widget having animation controller….

The image for the solar-system is rotated using the animation controller, also the animation is kept in repeat mode with the duration of 50 seconds (for one animation cycle)….

How does the 3D effect looks like :

Flutter and 3D

Slowing down animations…

This can be achieved using (timeDilation)

timeDilation = 2.0; // Will slow down animations by a factor of two

This property is present inside the package

import 'package:flutter/scheduler.dart';

Founder of Flatteredwithflutter.com | YouTuber | Writer | FlutterWeb: web.flatteredwithflutter.com | Flutter Desktop

Sign up for CodeChai Publication 

By CodeChai

The CodeChai email digest is a summary of the most popular and interesting code content from CodeChai publication. We curate the best, so you can stay continually informed and inspired. Take a look.

By signing up, you will create a Medium account if you don’t already have one. Review our Privacy Policy for more information about our privacy practices.

Check your inbox
Medium sent you an email at to complete your subscription.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store