Created
December 5, 2024 17:45
-
-
Save huykgit98/08b62f8c57a9b93a605edde8ae0b21f5 to your computer and use it in GitHub Desktop.
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({Key? key}) : super(key: key); | |
@override | |
Widget build(BuildContext context) { | |
return MaterialApp( | |
debugShowCheckedModeBanner: false, | |
home: Scaffold( | |
body: Center( | |
child: SizedBox( | |
width: 900, // Adjust the width to accommodate more columns | |
height: 500, // Adjust the height for the columns | |
child: CustomPaint( | |
painter: SpacedMultiShadowPainter(), | |
), | |
), | |
), | |
), | |
); | |
} | |
} | |
class SpacedMultiShadowPainter extends CustomPainter { | |
@override | |
void paint(Canvas canvas, Size size) { | |
final barSpacing = size.width / 4; // Divide space for 3 columns | |
const barWidth = 30.0; | |
const spacerHeight = 4.0; // Vertical space between base bars and shadows | |
const spaceWidth = 2.0; // Horizontal space between base bar and shadow | |
const borderRadius = Radius.circular(8.0); // Radius for rounded corners | |
// Data for all columns | |
const columnHeights = [ | |
[50.0, 15.0, 30.0], // First column (Green, Pink, Blue) | |
[60.0, 25.0, 35.0], // Second column | |
[70.0, 30.0, 40.0], // Third column | |
]; | |
// Colors for each base bar | |
final baseBarColors = [ | |
Colors.green.withOpacity(0.8), | |
Colors.pink.withOpacity(0.8), | |
Colors.blue.withOpacity(0.8), | |
]; | |
// Draw shadows dynamically for each base bar | |
for (int i = 0; i < columnHeights.length - 1; i++) { | |
final currentColumn = columnHeights[i]; | |
final nextColumn = columnHeights[i + 1]; | |
double currentHeight1 = size.height; | |
double currentHeight2 = size.height; | |
for (int j = 0; j < currentColumn.length; j++) { | |
final shadowPaint = Paint() | |
..color = baseBarColors[j] | |
.withOpacity(0.3) // Match shadow color with base bar color | |
..style = PaintingStyle.fill; | |
final currentBaseBottom1 = currentHeight1; | |
final currentBaseTop1 = currentHeight1 - currentColumn[j]; | |
final nextBaseBottom2 = currentHeight2; | |
final nextBaseTop2 = currentHeight2 - nextColumn[j]; | |
if (currentColumn[j] > 0 || nextColumn[j] > 0) { | |
// Draw shadow for this base bar | |
final shadowPath = Path() | |
..moveTo( | |
barSpacing * (i + 1) + | |
barWidth / 2 + | |
spaceWidth, // Bottom-right with space | |
currentBaseBottom1, | |
) | |
..lineTo( | |
barSpacing * (i + 1) + | |
barWidth / 2 + | |
spaceWidth, // Top-right with space | |
currentBaseTop1, | |
) | |
..lineTo( | |
barSpacing * (i + 2) - | |
barWidth / 2 - | |
spaceWidth, // Top-left with space | |
nextBaseTop2, | |
) | |
..lineTo( | |
barSpacing * (i + 2) - | |
barWidth / 2 - | |
spaceWidth, // Bottom-left with space | |
nextBaseBottom2, | |
) | |
..close(); | |
canvas.drawPath(shadowPath, shadowPaint); | |
} | |
// Update heights for the next base bar | |
currentHeight1 -= (currentColumn[j] + spacerHeight); | |
currentHeight2 -= (nextColumn[j] + spacerHeight); | |
} | |
} | |
// Draw all columns | |
for (int i = 0; i < columnHeights.length; i++) { | |
final column = columnHeights[i]; | |
double currentHeight = size.height; | |
for (int j = 0; j < column.length; j++) { | |
final baseBarPaint = Paint() | |
..color = baseBarColors[j] | |
..style = PaintingStyle.fill; | |
final barRect = Rect.fromLTWH( | |
barSpacing * (i + 1) - barWidth / 2, | |
currentHeight - column[j], | |
barWidth, | |
column[j], | |
); | |
final barRRect = RRect.fromRectAndRadius(barRect, borderRadius); | |
canvas.drawRRect(barRRect, baseBarPaint); | |
currentHeight -= | |
(column[j] + spacerHeight); // Add space between base bars | |
} | |
} | |
} | |
@override | |
bool shouldRepaint(covariant CustomPainter oldDelegate) => false; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment