Skip to content

Instantly share code, notes, and snippets.

@yeasin50
Created September 10, 2021 10:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yeasin50/5e0fca0a208f286facb3b85073b301e6 to your computer and use it in GitHub Desktop.
Save yeasin50/5e0fca0a208f286facb3b85073b301e6 to your computer and use it in GitHub Desktop.
paint onClick
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