# Flutter and 3D

Flutter and 3D

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

# Pre-Requisite…

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

# Begin…

## 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,

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:

`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…

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 :

## 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

### 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.