Created
May 11, 2022 09:46
-
-
Save sumate-sdk/6e47defc71af2caee3004b15c673b30f to your computer and use it in GitHub Desktop.
Rounded any acute corner
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 'dart:ui'; | |
import 'package:flutter/material.dart'; | |
import 'dart:math' as math; | |
void main() => runApp(MyApp()); | |
class MyApp extends StatelessWidget { | |
@override | |
Widget build(BuildContext context) { | |
return MaterialApp( | |
title: 'Flutter Custom Painter', | |
theme: ThemeData( | |
primarySwatch: Colors.pink, | |
), | |
home: MyPainter(), | |
); | |
} | |
} | |
class MyPainter extends StatefulWidget { | |
@override | |
_MyPainterState createState() => _MyPainterState(); | |
} | |
class _MyPainterState extends State<MyPainter> { | |
var _sides = 3.0; | |
var _radius = 100.0; | |
var _radians = 0.0; | |
@override | |
Widget build(BuildContext context) { | |
return Scaffold( | |
appBar: AppBar( | |
title: Text('Polygons'), | |
), | |
body: SafeArea( | |
child: Column( | |
crossAxisAlignment: CrossAxisAlignment.start, | |
children: <Widget>[ | |
Expanded( | |
child: CustomPaint( | |
painter: ShapePainter(_sides, _radius, _radians), | |
child: Container(), | |
), | |
), | |
Padding( | |
padding: const EdgeInsets.only(left: 16.0), | |
child: Text('Sides'), | |
), | |
Slider( | |
value: _sides, | |
min: 3.0, | |
max: 10.0, | |
label: _sides.toInt().toString(), | |
divisions: 7, | |
onChanged: (value) { | |
setState(() { | |
_sides = value; | |
}); | |
}, | |
), | |
Padding( | |
padding: const EdgeInsets.only(left: 16.0), | |
child: Text('Size'), | |
), | |
Slider( | |
value: _radius, | |
min: 10.0, | |
max: MediaQuery.of(context).size.width / 2, | |
onChanged: (value) { | |
setState(() { | |
_radius = value; | |
}); | |
}, | |
), | |
Padding( | |
padding: const EdgeInsets.only(left: 16.0), | |
child: Text('Rotation'), | |
), | |
Slider( | |
value: _radians, | |
min: 0.0, | |
max: math.pi, | |
onChanged: (value) { | |
setState(() { | |
_radians = value; | |
}); | |
}, | |
), | |
], | |
), | |
), | |
); | |
} | |
} | |
// FOR PAINTING POLYGONS | |
class ShapePainter extends CustomPainter { | |
final double sides; | |
final double radius; | |
final double radians; | |
ShapePainter(this.sides, this.radius, this.radians); | |
@override | |
void paint(Canvas canvas, Size size) { | |
var paint = Paint() | |
..color = Colors.teal | |
..strokeWidth = 5 | |
..style = PaintingStyle.stroke | |
..strokeCap = StrokeCap.round; | |
var path = new Path(); | |
path.lineTo(0.0, size.height - 20); | |
path.quadraticBezierTo(0.0, size.height, 20.0, size.height); | |
path.lineTo(size.width - 20.0, size.height); | |
path.quadraticBezierTo(size.width, size.height, size.width, size.height - 20); | |
// path.lineTo(size.width, 50.0); | |
// path.quadraticBezierTo(size.width, 30.0, size.width - 20.0, 30.0); | |
// path.lineTo(20.0, 5.0); | |
// path.quadraticBezierTo(0.0, 0.0, 0.0, 20.0); | |
path.close(); | |
canvas.drawPath(path, paint); | |
} | |
@override | |
bool shouldRepaint(CustomPainter oldDelegate) { | |
return true; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment