Last active
May 14, 2023 11:46
-
-
Save silvernoo/61cfe2e55492a3d50cb127b7d75dbdde to your computer and use it in GitHub Desktop.
Flutter CustomPainter
This file contains hidden or 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'; | |
| void main() { | |
| runApp(const MyApp()); | |
| } | |
| class MyApp extends StatelessWidget { | |
| const MyApp({super.key}); | |
| @override | |
| Widget build(BuildContext context) { | |
| return MaterialApp( | |
| title: 'Flutter Demo', | |
| theme: ThemeData( | |
| primarySwatch: Colors.blue, | |
| ), | |
| home: const MyHomePage(title: 'Flutter Demo Home Page'), | |
| ); | |
| } | |
| } | |
| class MyHomePage extends StatefulWidget { | |
| const MyHomePage({super.key, required this.title}); | |
| final String title; | |
| @override | |
| State<MyHomePage> createState() => _MyHomePageState(); | |
| } | |
| class _MyHomePageState extends State<MyHomePage> with SingleTickerProviderStateMixin { | |
| late AnimationController _controller; | |
| @override | |
| void initState() { | |
| _controller = AnimationController(vsync: this, duration: const Duration(seconds: 1)); | |
| _controller.repeat(); | |
| super.initState(); | |
| } | |
| @override | |
| Widget build(BuildContext context) { | |
| return Scaffold( | |
| appBar: AppBar( | |
| title: Text(widget.title), | |
| ), | |
| body: Center( | |
| child: CustomPaint( | |
| willChange: true, | |
| size: const Size(100, 100), | |
| painter: ShapePainter(color: Colors.black, listenable: _controller), | |
| ), | |
| ), | |
| ); | |
| } | |
| } | |
| class ShapePainter extends CustomPainter { | |
| Color color; | |
| AnimationController listenable; | |
| ShapePainter({required this.color, required this.listenable}):super(repaint: listenable); | |
| @override | |
| void paint(Canvas canvas, Size size) { | |
| Paint paint = Paint()..color = color; | |
| canvas.drawCircle(Offset(size.width / 2, size.height / 2), (size.width / 2) * listenable.value, paint); | |
| } | |
| @override | |
| bool shouldRepaint(covariant ShapePainter oldDelegate) { | |
| return true; | |
| } | |
| } |
This file contains hidden or 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 'dart:math' show pi, cos, sin; | |
| import 'package:flutter/material.dart'; | |
| void main() { | |
| runApp(const MyApp()); | |
| } | |
| class MyApp extends StatelessWidget { | |
| const MyApp({super.key}); | |
| @override | |
| Widget build(BuildContext context) { | |
| return MaterialApp( | |
| title: 'Flutter Demo', | |
| theme: ThemeData( | |
| primarySwatch: Colors.blue, | |
| ), | |
| home: const MyHomePage(title: 'Flutter Demo Home Page'), | |
| ); | |
| } | |
| } | |
| class MyHomePage extends StatefulWidget { | |
| const MyHomePage({super.key, required this.title}); | |
| final String title; | |
| @override | |
| State<MyHomePage> createState() => _MyHomePageState(); | |
| } | |
| class _MyHomePageState extends State<MyHomePage> with SingleTickerProviderStateMixin { | |
| @override | |
| Widget build(BuildContext context) { | |
| return Scaffold( | |
| appBar: AppBar( | |
| title: Text(widget.title), | |
| ), | |
| body: Center( | |
| child: CustomPaint( | |
| willChange: true, | |
| size: const Size(100, 100), | |
| painter: ShapePainter(color: Colors.black.withOpacity(0.1)), | |
| ), | |
| ), | |
| ); | |
| } | |
| } | |
| class ShapePainter extends CustomPainter { | |
| Color color; | |
| ShapePainter({required this.color}); | |
| @override | |
| void paint(Canvas canvas, Size size) { | |
| Paint paint = Paint()..color = color; | |
| Offset center = Offset(size.width / 2, size.height / 2); | |
| canvas.drawCircle(center, (size.width / 2), paint); | |
| var angle = (2 * pi) / 12; | |
| for (var i = 0; i < 12; i++) { | |
| var xAngle = cos(angle * i - pi / 2); | |
| var yAngle = sin(angle * i - pi / 2); | |
| var pivotOffset = Offset(center.dx + size.width / 2 * xAngle, center.dy + size.width / 2 * yAngle); | |
| canvas.drawCircle(pivotOffset, 1, paint..color = Colors.red.withOpacity(1)); | |
| canvas.drawLine(center, pivotOffset, paint); | |
| } | |
| } | |
| @override | |
| bool shouldRepaint(covariant ShapePainter oldDelegate) { | |
| return true; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment