Created with <3 with dartpad.dev.
Last active
December 24, 2022 19:49
-
-
Save isaacadariku/7b92fe91cba753b75a42a67bd44c43d4 to your computer and use it in GitHub Desktop.
Custom Shapes in flutter with enhanced enums
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'; | |
const Color darkBlue = Color.fromARGB(255, 18, 32, 47); | |
void main() { | |
runApp(MyApp()); | |
} | |
class MyApp extends StatelessWidget { | |
@override | |
Widget build(BuildContext context) { | |
return MaterialApp( | |
theme: ThemeData.dark().copyWith( | |
scaffoldBackgroundColor: darkBlue, | |
), | |
debugShowCheckedModeBanner: false, | |
home: Scaffold( | |
body: Center( | |
child: CustomShape( | |
shapeType: ShapeType.circle, | |
child: Container(), | |
), | |
), | |
), | |
); | |
} | |
} | |
enum ShapeType { | |
circle('circle', Size(12, 12)), | |
square('square', Size(100, 100)), | |
star('star', Size(50, 15)), | |
; | |
const ShapeType(this.shape, this.size); | |
final String shape; | |
final Size size; | |
} | |
class CustomShape extends StatelessWidget { | |
const CustomShape({ | |
Key? key, | |
required this.shapeType, | |
required this.child, | |
}) : super(key: key); | |
final ShapeType shapeType; | |
final Widget child; | |
@override | |
Widget build(BuildContext context) { | |
return Scaffold( | |
backgroundColor: Colors.white, | |
body: Center( | |
child: Semantics( | |
label: shapeType.shape, | |
child: SizedBox( | |
width: shapeType.size.width, | |
height: shapeType.size.height, | |
child: CustomPaint( | |
painter: _ShapePainter(shapeType), | |
size: shapeType.size, | |
child: child, | |
), | |
), | |
), | |
), | |
); | |
} | |
} | |
class _ShapePainter extends CustomPainter { | |
_ShapePainter(this.shapeType); | |
final ShapeType shapeType; | |
@override | |
void paint(Canvas canvas, Size size) { | |
final paint = Paint() | |
..color = Colors.black | |
..style = PaintingStyle.stroke | |
..strokeWidth = 2 | |
..strokeJoin = StrokeJoin.round; // Round the corners | |
switch (shapeType) { | |
case ShapeType.circle: | |
canvas.drawCircle(size.center(Offset.zero), size.width / 2, paint); | |
break; | |
case ShapeType.square: | |
canvas.drawRect(Offset.zero & size, paint); | |
break; | |
case ShapeType.star: | |
final path = Path() | |
..moveTo(size.width / 2, 0) | |
..lineTo(size.width * 0.75, size.height * 0.5) | |
..lineTo(size.width, size.height * 0.5) | |
..lineTo(size.width * 0.8, size.height * 0.75) | |
..lineTo(size.width * 0.9, size.height) | |
..lineTo(size.width / 2, size.height * 0.85) | |
..lineTo(size.width * 0.1, size.height) | |
..lineTo(size.width * 0.2, size.height * 0.75) | |
..lineTo(0, size.height * 0.5) | |
..lineTo(size.width * 0.25, size.height * 0.5) | |
..close(); | |
canvas.drawPath(path, paint); | |
break; | |
} | |
} | |
@override | |
bool shouldRepaint(CustomPainter oldDelegate) => false; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment