Last active
November 4, 2024 08:16
-
-
Save slightfoot/e35e8d5877371417e9803143e2501b0a to your computer and use it in GitHub Desktop.
Flutter Squircle Shape
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/cupertino.dart'; | |
import 'package:flutter/material.dart'; | |
class MyApp extends StatelessWidget { | |
@override | |
Widget build(BuildContext context) { | |
return new MaterialApp( | |
title: 'Squircle', | |
home: new Scaffold( | |
appBar: new AppBar(title: new Text('Squircle')), | |
body: new Center( | |
child: new Container( | |
width: 56.0, | |
height: 56.0, | |
child: new Material( | |
color: Colors.blueGrey[400], | |
shape: new SquircleBorder( | |
side: BorderSide(color: Colors.blueGrey[800], width: 3.0), | |
), | |
child: new Image.network('https://raw.githubusercontent.com/ScottS2017/local_package/master/lib/image/bipi/placeholder_icon.jpg'), | |
), | |
), | |
), | |
) | |
); | |
} | |
} | |
class SquircleBorder extends ShapeBorder { | |
final BorderSide side; | |
final double superRadius; | |
const SquircleBorder({ | |
this.side: BorderSide.none, | |
this.superRadius: 5.0, | |
}) | |
: assert(side != null), | |
assert(superRadius != null); | |
@override | |
EdgeInsetsGeometry get dimensions => EdgeInsets.all(side.width); | |
@override | |
ShapeBorder scale(double t) { | |
return new SquircleBorder( | |
side: side.scale(t), | |
superRadius: superRadius * t, | |
); | |
} | |
@override | |
Path getInnerPath(Rect rect, {TextDirection textDirection}) { | |
return _squirclePath(rect.deflate(side.width), superRadius); | |
} | |
@override | |
Path getOuterPath(Rect rect, {TextDirection textDirection}) { | |
return _squirclePath(rect, superRadius); | |
} | |
static Path _squirclePath(Rect rect, double superRadius) { | |
final c = rect.center; | |
final dx = c.dx * (1.0 / superRadius); | |
final dy = c.dy * (1.0 / superRadius); | |
return new Path() | |
..moveTo(c.dx, 0.0) | |
..relativeCubicTo(c.dx - dx, 0.0, c.dx, dy, c.dx, c.dy) | |
..relativeCubicTo(0.0, c.dy - dy, -dx, c.dy, -c.dx, c.dy) | |
..relativeCubicTo(-(c.dx - dx), 0.0, -c.dx, -dy, -c.dx, -c.dy) | |
..relativeCubicTo(0.0, -(c.dy - dy), dx, -c.dy, c.dx, -c.dy) | |
..close(); | |
} | |
@override | |
void paint(Canvas canvas, Rect rect, {TextDirection textDirection}) { | |
switch (side.style) { | |
case BorderStyle.none: | |
break; | |
case BorderStyle.solid: | |
var path = getOuterPath(rect.deflate(side.width / 2.0), textDirection: textDirection); | |
canvas.drawPath(path, side.toPaint()); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment