Skip to content

Instantly share code, notes, and snippets.

@Kurogoma4D
Created March 13, 2022 17:16
Show Gist options
  • Save Kurogoma4D/f7b936c9e54c38fcb065a44fcd2d4654 to your computer and use it in GitHub Desktop.
Save Kurogoma4D/f7b936c9e54c38fcb065a44fcd2d4654 to your computer and use it in GitHub Desktop.
import 'dart:async';
import 'dart:ui' as ui;
import 'package:flutter/material.dart';
import 'package:graphx/graphx.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: const Home(),
);
}
}
class Home extends StatelessWidget {
const Home({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return Scaffold(
body: Center(
child: SceneBuilderWidget(
builder: () => SceneController(
back: SimpleShapes(MediaQuery.of(context).size),
),
autoSize: true,
child: const SizedBox.expand(),
),
),
);
}
}
const _colorLevels = [
50,
100,
200,
300,
400,
500,
600,
700,
// 800,
// 900,
];
class Triangle {
final Path path;
Triangle(this.path);
}
class TriangleShape {
final Triangle triangle;
final GShape shape;
TriangleShape(this.triangle, this.shape);
void setup({required MaterialColor color}) {
final level = [..._colorLevels]..shuffle();
shape.graphics.beginFill((color[level.first] ?? color).withOpacity(0.87));
shape.graphics.drawPath(triangle.path);
shape.graphics.endFill();
}
}
Future<ui.Image> getImage(String path) async {
final byteData = await rootBundle.load(path);
return await decodeImageFromList(byteData.buffer.asUint8List());
}
const windowOffset = 30.0;
const shapeSize = 60.0;
class SimpleShapes extends GSprite {
final Size size;
final points = <GShape>[];
final triangles = <TriangleShape>[];
SimpleShapes(this.size);
@override
void addedToStage() {
super.addedToStage();
double currentX = -windowOffset;
double currentY = -windowOffset;
bool useOffset = false;
for (; currentY < size.height + windowOffset; currentY += shapeSize) {
final offset = useOffset ? shapeSize / 2 : 0;
useOffset = !useOffset;
for (currentX = -windowOffset;
currentX < size.width + windowOffset;
currentX += shapeSize) {
final underTriangle = Triangle(Path()
..moveTo(currentX + offset, currentY)
..relativeLineTo(shapeSize, 0)
..relativeLineTo(-shapeSize / 2, shapeSize)
..close());
final underTriangleShape = TriangleShape(underTriangle, GShape())
..setup(color: Colors.amber);
triangles.add(underTriangleShape);
addChild(underTriangleShape.shape);
final overTriangle = Triangle(Path()
..moveTo(currentX + offset, currentY)
..relativeLineTo(shapeSize / 2, shapeSize)
..relativeLineTo(-shapeSize, 0)
..close());
final overTriangleShape = TriangleShape(overTriangle, GShape())
..setup(color: Colors.yellow);
triangles.add(overTriangleShape);
addChild(overTriangleShape.shape);
}
}
}
@override
void update(double delta) {
final mx = (mouseX / size.width - 0.5) * 50;
pivotX = mx;
final my = (mouseY / size.height - 0.5) * 50;
pivotY = my;
super.update(delta);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment