Skip to content

Instantly share code, notes, and snippets.

@jxstxn1
Created January 18, 2023 14:35
Show Gist options
  • Save jxstxn1/b1d8ed7283b9dd1eba92a8b052fa437e to your computer and use it in GitHub Desktop.
Save jxstxn1/b1d8ed7283b9dd1eba92a8b052fa437e to your computer and use it in GitHub Desktop.
Flutter 3D Cube Widget
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