Skip to content

Instantly share code, notes, and snippets.

@Barttje
Last active October 25, 2022 19:20
Show Gist options
  • Save Barttje/04bda72c42df08c39330021fbdd6aaa5 to your computer and use it in GitHub Desktop.
Save Barttje/04bda72c42df08c39330021fbdd6aaa5 to your computer and use it in GitHub Desktop.
Hexagon CustomPaint Example
import 'package:flutter/material.dart';
import 'dart:math' as math;
void main() => runApp(HexagonGridDemo());
class HexagonGridDemo extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: Text('Hexagon Grid Demo'),
),
body: Center(
child: Container(
width: 200,
height: 200,
child: CustomPaint(
painter: HexagonPainter(Offset(100, 100), 100),
),
),
),
),
);
}
}
class HexagonPainter extends CustomPainter {
static const int SIDES_OF_HEXAGON = 6;
final double radius;
final Offset center;
HexagonPainter(this.center, this.radius);
@override
void paint(Canvas canvas, Size size) {
Paint paint = Paint()..color = Colors.blue;
Path path = createHexagonPath();
canvas.drawPath(path, paint);
}
Path createHexagonPath() {
final path = Path();
var angle = (math.pi * 2) / SIDES_OF_HEXAGON;
Offset firstPoint = Offset(radius * math.cos(0.0), radius * math.sin(0.0));
path.moveTo(firstPoint.dx + center.dx, firstPoint.dy + center.dy);
for (int i = 1; i <= SIDES_OF_HEXAGON; i++) {
double x = radius * math.cos(angle * i) + center.dx;
double y = radius * math.sin(angle * i) + center.dy;
path.lineTo(x, y);
}
path.close();
return path;
}
@override
bool shouldRepaint(CustomPainter oldDelegate) => false;
}
@mhmzdev
Copy link

mhmzdev commented Oct 25, 2022

Screen.Recording.2022-10-26.at.12.19.08.AM.mov

Hey there,
I want to create hexgonal effect for this ripple effect
I've already done this , but I'm not sure how to handle this for hexagone shape

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment