Skip to content

Instantly share code, notes, and snippets.

@pingbird
Created September 17, 2022 18:51
Show Gist options
  • Save pingbird/18cfbcc696b43c7c002a5ac3c94dd520 to your computer and use it in GitHub Desktop.
Save pingbird/18cfbcc696b43c7c002a5ac3c94dd520 to your computer and use it in GitHub Desktop.
import 'dart:math';
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 const MaterialApp(
debugShowCheckedModeBanner: false,
home: MyHomePage(),
);
}
}
class MyHomePage extends StatelessWidget {
const MyHomePage({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return Scaffold(
body: Center(
child: CustomPaint(
painter: InvertedRoundedRectanglePainter(
color: Colors.blue,
radius: 32.0,
),
child: const SizedBox(width: 300, height: 200),
),
),
);
}
}
class InvertedRoundedRectanglePainter extends CustomPainter {
InvertedRoundedRectanglePainter({
required this.radius,
required this.color,
});
final double radius;
final Color color;
@override
void paint(Canvas canvas, Size size) {
final cornerSize = Size.square(radius * 2);
canvas.drawPath(
Path()
..addArc(
Offset(0, -radius) & cornerSize,
pi,
-pi / 2,
)
..arcTo(
Offset(size.width - cornerSize.width, -radius) & cornerSize,
pi / 2,
-pi / 2,
false,
)
..lineTo(size.width, size.height)
..lineTo(0, size.height),
Paint()..color = color,
);
}
@override
bool shouldRepaint(InvertedRoundedRectanglePainter oldDelegate) =>
oldDelegate.radius != radius || oldDelegate.color != color;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment