Last active
May 9, 2022 11:04
-
-
Save akshansh2000/5d732a6ff62a952ee17e0d05e8da9cf2 to your computer and use it in GitHub Desktop.
Fractal Tree
This file contains 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'; | |
import 'package:flutter/services.dart'; | |
import 'dart:math'; | |
main() { | |
runApp( | |
MaterialApp( | |
debugShowCheckedModeBanner: false, | |
home: MyApp(), | |
), | |
); | |
} | |
class MyApp extends StatelessWidget { | |
@override | |
Widget build(BuildContext context) { | |
SystemChrome.setSystemUIOverlayStyle( | |
SystemUiOverlayStyle( | |
systemNavigationBarColor: Colors.white, | |
statusBarColor: Colors.grey[900], | |
statusBarIconBrightness: Brightness.light, | |
systemNavigationBarIconBrightness: Brightness.dark, | |
), | |
); | |
SystemChrome.setPreferredOrientations([ | |
DeviceOrientation.portraitUp, | |
]); | |
return SafeArea( | |
child: AppBody(), | |
); | |
} | |
} | |
class AppBody extends StatefulWidget { | |
@override | |
_AppBodyState createState() => _AppBodyState(); | |
} | |
class _AppBodyState extends State<AppBody> { | |
double angle = pi / 4, height = 10, scale = 0.5; | |
Size size = Size(0, 0); | |
@override | |
Widget build(BuildContext context) { | |
size = MediaQuery.of(context).size; | |
return Scaffold( | |
backgroundColor: Colors.grey[900], | |
body: Transform.scale( | |
scale: scale, | |
child: CustomPaint( | |
painter: FractalPainter(height, angle), | |
child: Container(), | |
), | |
), | |
bottomNavigationBar: BottomAppBar( | |
elevation: 0, | |
color: Colors.white, | |
child: Column( | |
mainAxisAlignment: MainAxisAlignment.center, | |
mainAxisSize: MainAxisSize.min, | |
children: <Widget>[ | |
customSlider(), | |
customSlider(isHeight: true), | |
customSlider(isScale: true), | |
], | |
), | |
), | |
); | |
} | |
Widget customSlider({bool isHeight = false, bool isScale = false}) { | |
return Row( | |
mainAxisSize: MainAxisSize.max, | |
mainAxisAlignment: MainAxisAlignment.center, | |
children: <Widget>[ | |
Padding( | |
padding: EdgeInsets.symmetric(horizontal: 10, vertical: 20), | |
child: Text( | |
isHeight ? "depth" : isScale ? "zoom" : "angle", | |
style: TextStyle( | |
fontWeight: FontWeight.bold, | |
fontSize: 18, | |
), | |
), | |
), | |
Slider( | |
min: 0, | |
max: isHeight ? 20 : isScale ? 1 : pi, | |
activeColor: Colors.red[400], | |
inactiveColor: Colors.grey[200], | |
value: isHeight ? height : isScale ? scale : angle, | |
onChanged: (newValue) => setState( | |
() => isHeight | |
? height = newValue | |
: isScale ? scale = newValue : angle = newValue, | |
), | |
), | |
], | |
); | |
} | |
} | |
int counter = 0; | |
class FractalPainter extends CustomPainter { | |
double _height, _angle; | |
FractalPainter(this._height, this._angle); | |
@override | |
void paint(Canvas canvas, Size size) { | |
Canvas _canvas = canvas; | |
Paint _paint = Paint() | |
..color = Colors.white | |
..strokeWidth = 3 | |
..strokeJoin = StrokeJoin.bevel | |
..style = PaintingStyle.stroke; | |
canvas.translate(size.width / 2, size.height - 30); | |
drawBranch(_height * _height, _canvas, _paint); | |
} | |
void drawBranch(double length, Canvas _canvas, Paint _paint) { | |
_canvas.drawLine(Offset(0, 0), Offset(0, -length), _paint); | |
_canvas.translate(0, -length); | |
if (counter == length.round()) return; | |
if (length > 4) { | |
_canvas.save(); | |
_canvas.rotate(_angle); | |
++counter; | |
drawBranch(length * 0.7, _canvas, _paint); | |
_canvas.restore(); | |
_canvas.save(); | |
_canvas.rotate(-_angle); | |
++counter; | |
drawBranch(length * 0.7, _canvas, _paint); | |
_canvas.restore(); | |
} | |
} | |
@override | |
bool shouldRepaint(FractalPainter oldDelegate) => false; | |
@override | |
bool shouldRebuildSemantics(FractalPainter oldDelegate) => false; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment