Created
January 18, 2023 14:35
-
-
Save jxstxn1/b1d8ed7283b9dd1eba92a8b052fa437e to your computer and use it in GitHub Desktop.
Flutter 3D Cube Widget
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'package:flutter/material.dart'; | |
const double boxSize = 65; | |
class CubeAnimation extends StatefulWidget { | |
const CubeAnimation(); | |
@override | |
_CubeAnimationState createState() => _CubeAnimationState(); | |
} | |
class _CubeAnimationState extends State<CubeAnimation> { | |
@override | |
Widget build(BuildContext context) { | |
return Center( | |
child: Stack( | |
alignment: Alignment.center, | |
children: <Widget>[ | |
Transform( | |
alignment: Alignment.center, | |
transform: Matrix4.identity()..translate(0.0, 0.0, boxSize), | |
child: Container( | |
color: Colors.blue, | |
height: boxSize, | |
width: boxSize, | |
), | |
), | |
Transform.translate( | |
offset: const Offset(-boxSize / 2, 0), | |
child: Transform( | |
alignment: Alignment.center, | |
transform: Matrix4.rotationY(-270.toRad()) | |
..translate(-boxSize / 2), | |
child: Container( | |
color: Colors.red, | |
height: boxSize, | |
width: boxSize, | |
), | |
), | |
), | |
Transform.translate( | |
offset: const Offset(-boxSize / 2, 0), | |
child: Transform( | |
alignment: Alignment.center, | |
transform: Matrix4.rotationY(-270.toRad()) | |
..translate(-boxSize / 2), | |
child: Container( | |
color: Colors.yellow, | |
height: boxSize, | |
width: boxSize, | |
), | |
), | |
), | |
Transform.translate( | |
offset: const Offset(0, boxSize / 2), | |
child: Transform( | |
alignment: Alignment.center, | |
transform: Matrix4.rotationX(270.toRad()) | |
..translate(0.0, -boxSize / 2), | |
child: Container( | |
color: Colors.green, | |
height: boxSize, | |
width: boxSize, | |
), | |
), | |
), | |
Transform.translate( | |
offset: const Offset(0, -boxSize / 2), | |
child: Transform( | |
alignment: Alignment.center, | |
transform: Matrix4.rotationX(270.toRad()) | |
..translate(0.0, -boxSize / 2), | |
child: Container( | |
color: Colors.orange, | |
height: boxSize, | |
width: boxSize, | |
), | |
), | |
), | |
Container( | |
color: Colors.black, | |
height: boxSize, | |
width: boxSize, | |
child: Container( | |
color: Colors.cyan, | |
height: boxSize, | |
width: boxSize, | |
), | |
), | |
], | |
), | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment