Skip to content

Instantly share code, notes, and snippets.

@yeasin50
Created September 16, 2021 04:35
Show Gist options
  • Save yeasin50/05fc29d2dddaa8e6daa19bf3e8099530 to your computer and use it in GitHub Desktop.
Save yeasin50/05fc29d2dddaa8e6daa19bf3e8099530 to your computer and use it in GitHub Desktop.
customPaint vs BoxPaint
import 'package:flutter/material.dart';
const Color darkBlue = Color.fromARGB(255, 18, 32, 47);
void main() {
runApp(MaterialApp(
home: MyApp(),
));
}
class MyApp extends StatefulWidget {
@override
_MyAppState createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
@override
Widget build(BuildContext context) {
return SafeArea(
child: Scaffold(
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
SizedBox(
height: 300,
width: 300,
child: CustomPaint(
painter: MyCustomPainter(frameSFactor: .1, padding: 20),
child: const Center(
child: Text(
'Text',
style: TextStyle(
color: Colors.black,
fontSize: 30,
),
),
),
),
),
const SizedBox(
height: 20,
),
SizedBox(
width: 300,
height: 300,
child: Container(
height: 200,
width: 200,
alignment: Alignment.center,
decoration: const CustomDecoration(
frameSFactor: .1,
gap: 20,
),
child: const Text("a"),
),
),
],
),
),
),
);
}
}
class CustomDecoration extends Decoration {
final Color? backgroundColor;
final double frameSFactor;
//defalut padding _Need to check
final double gap;
const CustomDecoration({
this.backgroundColor = Colors.transparent,
required this.frameSFactor,
required this.gap,
});
@override
BoxPainter createBoxPainter([VoidCallback? onChanged]) {
return CustomDecorationPainter(
backgroundColor: backgroundColor!,
frameSFactor: frameSFactor,
padding: gap);
}
}
class CustomDecorationPainter extends BoxPainter {
final Color backgroundColor;
final double frameSFactor;
final double padding;
CustomDecorationPainter({
required this.backgroundColor,
required this.frameSFactor,
required this.padding,
});
@override
void paint(Canvas canvas, Offset offset, ImageConfiguration configuration) {
print(configuration.size!.height);
final Rect bounds = offset & configuration.size!;
final frameHWidth = configuration.size!.width * frameSFactor;
Paint paint = Paint()
..color = backgroundColor
..strokeCap = StrokeCap.round
..style = PaintingStyle.fill
..strokeWidth = 4;
/// background
canvas.drawRRect(
RRect.fromRectAndRadius(
bounds,
const Radius.circular(18),
),
paint..color = Colors.redAccent);
paint.color = Colors.black;
/// top left
canvas.drawLine(
bounds.topLeft + Offset(padding, padding),
Offset(bounds.topLeft.dx + frameHWidth, bounds.topLeft.dy) +
Offset(padding, padding),
paint,
);
canvas.drawLine(
bounds.topLeft + Offset(padding, padding),
Offset(bounds.topLeft.dx, bounds.topLeft.dy + frameHWidth) +
Offset(padding, padding),
paint,
);
//top Right
canvas.drawLine(
Offset(bounds.topRight.dx - padding, bounds.topRight.dy + padding),
Offset(bounds.topRight.dx - padding - frameHWidth,
bounds.topRight.dy + padding),
paint,
);
// canvas.drawLine(
// bounds.topLeft + Offset(padding, padding),
// Offset(bounds.topLeft.dx, bounds.topLeft.dy + frameHWidth) +
// Offset(padding, padding),
// paint,
// );
}
}
class MyCustomPainter extends CustomPainter {
final double padding;
final double frameSFactor;
MyCustomPainter({
required this.padding,
required this.frameSFactor,
});
@override
void paint(Canvas canvas, Size size) {
final frameHWidth = size.width * frameSFactor;
Paint paint = Paint()
..color = Colors.redAccent
..strokeCap = StrokeCap.round
..style = PaintingStyle.fill
..strokeWidth = 4;
/// background
canvas.drawRRect(
RRect.fromRectAndRadius(
Rect.fromLTRB(0, 0, size.width, size.height),
Radius.circular(18),
),
paint);
/// top left
canvas.drawLine(
Offset(0 + padding, padding),
Offset(
padding + frameHWidth,
padding,
),
paint..color = Colors.black,
);
canvas.drawLine(
Offset(0 + padding, padding),
Offset(
padding,
padding + frameHWidth,
),
paint..color = Colors.black,
);
/// top Right
canvas.drawLine(
Offset(size.width - padding, padding),
Offset(size.width - padding - frameHWidth, padding),
paint..color = Colors.black,
);
canvas.drawLine(
Offset(size.width - padding, padding),
Offset(size.width - padding, padding + frameHWidth),
paint..color = Colors.black,
);
/// Bottom Right
canvas.drawLine(
Offset(size.width - padding, size.height - padding),
Offset(size.width - padding - frameHWidth, size.height - padding),
paint..color = Colors.black,
);
canvas.drawLine(
Offset(size.width - padding, size.height - padding),
Offset(size.width - padding, size.height - padding - frameHWidth),
paint..color = Colors.black,
);
/// Bottom Left
canvas.drawLine(
Offset(0 + padding, size.height - padding),
Offset(0 + padding + frameHWidth, size.height - padding),
paint..color = Colors.black,
);
canvas.drawLine(
Offset(0 + padding, size.height - padding),
Offset(0 + padding, size.height - padding - frameHWidth),
paint..color = Colors.black,
);
}
@override
bool shouldRepaint(covariant CustomPainter oldDelegate) {
return false;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment