Created
September 10, 2021 10:08
-
-
Save yeasin50/5e0fca0a208f286facb3b85073b301e6 to your computer and use it in GitHub Desktop.
paint onClick
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: MYWidget(), | |
), | |
), | |
); | |
} | |
} | |
class MyPainter extends CustomPainter { | |
@override | |
void paint(Canvas canvas, Size size) { | |
final paint = Paint()..color = Colors.yellow; | |
var path = Path(); | |
final startPoint = Offset(0, size.height); | |
final endPoint = Offset(size.width, size.height); | |
path.quadraticBezierTo( | |
startPoint.dx, startPoint.dy, endPoint.dx, endPoint.dy); | |
path.close(); | |
canvas.drawPath(path, paint); | |
} | |
@override | |
bool shouldRepaint(CustomPainter oldDelegate) => false; | |
} | |
class MYWidget extends StatefulWidget { | |
MYWidget({Key? key}) : super(key: key); | |
@override | |
_MYWidgetState createState() => _MYWidgetState(); | |
} | |
class _MYWidgetState extends State<MYWidget> { | |
bool _showPaint = false; | |
@override | |
Widget build(BuildContext context) { | |
return Column( | |
children: [ | |
CustomPaint( | |
foregroundPainter: _showPaint ? MyPainter() : null, | |
child: SizedBox( | |
height: 100, | |
width: 100, | |
), | |
), | |
ElevatedButton( | |
onPressed: () { | |
setState(() { | |
_showPaint = true; | |
}); | |
}, | |
child: Text( | |
"show", | |
), | |
), | |
], | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment