How to shape the container? - dart

I want to make different shape of a container in flutter.
For example, shaping the container to have a shape of octagonal, etc.
Thank you in advance.

You can extend CustomClipper and define a custom path to be used with ClipPath. There are other pre-made clip widgets like ClipOval and ClipRRect (a rectangle with rounded corners). The below is an example of a star-shaped Container.
class _MyHomePageState extends State<MyHomePage> {
#override
Widget build(BuildContext context) {
return Scaffold(
body: ClipPath(
child: Container(
color: Colors.amber,
),
clipper: _MyClipper(),
),
);
}
}
class _MyClipper extends CustomClipper<Path> {
#override
Path getClip(Size size) {
final path = Path();
path.lineTo(size.width * 0.5, size.height * 0.15);
path.lineTo(size.width * 0.35, size.height * 0.4);
path.lineTo(0.0, size.height * 0.4);
path.lineTo(size.width * 0.25, size.height * 0.55);
path.lineTo(size.width * 0.1, size.height * 0.8);
path.lineTo(size.width * 0.5, size.height * 0.65);
path.lineTo(size.width * 0.9, size.height * 0.8);
path.lineTo(size.width * 0.75, size.height * 0.55);
path.lineTo(size.width, size.height * 0.4);
path.lineTo(size.width * 0.65, size.height * 0.4);
path.lineTo(size.width * 0.5, size.height * 0.15);
path.close();
return path;
}
#override
bool shouldReclip(CustomClipper<Path> oldClipper) => false;
}

Related

react-konva How to set Shape width and height to fill it's Group container?

I want to have the same animation movement on both a Text and a Rect. I put them into a group and apply a tween on the group. However, the x and y props are inherited, width and height are not, which means the Rect's width and height don't have smooth tween. Is there any way to set Rect's width and height to equal to 100% of its Group container's width and height all the time and along with their change?
const { scaleFactor, DynamicData, isPause } = props;
const groupRef = useRef<Konva.Group>(null);
const start = DynamicData.startData;
const end = DynamicData.endData;
useEffect(() => {
if (start && end && groupRef.current) {
const animation = new Tween({
node: groupRef.current,
duration: DynamicData.endTime - DynamicData.startTime,
x: scaleFactor * end.x,
y: scaleFactor * end.y,
width: scaleFactor * end.width,
height: scaleFactor * end.height,
easing: Easings.Linear,
});
if (isPause) {
animation.pause();
} else {
animation.play();
}
}
});
return (
<>
{!_.isUndefined(end) && (
<Group
ref={groupRef}
x={scaleFactor * start.x}
y={scaleFactor * start.y}
width={scaleFactor * start.width}
height={scaleFactor * start.height}
>
<Text
text={start.concept}
/>
<Rect
stroke={start.stroke}
/>
</Group>
)}
</>
);
};
update: for now I add a new Tween on Rect to track the width and height changes:
const sizeAnimation = new Tween({
node: rectRef.current,
duration: DynamicData.endTime - DynamicData.startTime,
width: scaleFactor * end!.width,
height: scaleFactor * end!.height,
easing: Easings.Linear,
});
if (isPause) {
sizeAnimation.pause();
} else {
sizeAnimation.play();
}
width and height properties of Konva.Group do nothing. They don't affect rendering.
You have to add another tween for rectangle.

I have an area on which i have to draw something like signature how can i do this in appium version 1.15 java,Image link is attached

I have an area on which i have to draw something like signature how can I do this in appium version 1.15 java,Image link is attached.
I have tried actions and Touch Actions but it does not worked.
TouchActions action= new TouchActions(driver)
.longPress((WebElement) PointOption.point(464, 727)).(PointOption.point(977, 7)).release().perform();
Image of signature location
Swipe left using -
Dimension size = appiumDriver.manage().window().getSize();
int startx = (int) (size.width * 0.8);
int endx = (int) (size.width * 0.20);
int starty = size.height / 2;
appiumDriver.swipe(startx, starty, endx, starty, 1000);
Swipe right-
appiumDriver.context("NATIVE_APP");
Dimension size = appiumDriver.manage().window().getSize();
int endx = (int) (size.width * 0.8);
int startx = (int) (size.width * 0.20);
int starty = size.height / 2;
appiumDriver.swipe(startx, starty, endx, starty, 1000);
OR
new TouchAction(driver).longPress(250, 1200).moveTo(900, 1200).release().perform();

How can I simulate CSS like Object-Fit Cover in Konvajs React?

This is really easy in DOM because I can just do object-fit: cover in css and the image would crop and fill in the width and height. But in Konvajs the default seems to be to fill it.
Here is what I've tried:
const [image] = useImage(content.img);
if (image) {
image.setAttribute('style', `object-
fit:cover;width:${content.width};height:${content.height}`);
}
This doesn't work.
Just in case anyone else is looking for a solution.
The only way I could achieve this is doing calculations on where to crop the original image.
const crop = () => {
if (content.width > content.height) {
const [cropX, cropY, cropW, cropH] = cropBasedOnWidth();
if (cropY < 0) {
const [cropX, cropY, cropW, cropH] = cropBasedOnHeight();
return {x: cropX, y: cropY, height: cropH, width: cropW};
}
return {x: cropX, y: cropY, height: cropH, width: cropW};
} else if (content.width < content.height) {
const [cropX, cropY, cropW, cropH] = cropBasedOnHeight();
if (cropX < 0) {
const [cropX, cropY, cropW, cropH] = cropBasedOnWidth();
return {x: cropX, y: cropY, height: cropH, width: cropW};
}
return {x: cropX, y: cropY, height: cropH, width: cropW};
} else {
return undefined;
}
}
const cropBasedOnWidth = () => {
const cropW = content.naturalWidth;
const cropH = cropW / content.width * content.height;
const cropX = content.naturalWidth / 2 - cropW / 2;
const cropY = content.naturalHeight / 2 - cropH / 2;
return [cropX, cropY, cropW, cropH];
}
const cropBasedOnHeight = () => {
const cropH = content.naturalHeight;
const cropW = cropH / content.height * content.width;
const cropX = content.naturalWidth / 2 - cropW / 2;
const cropY = content.naturalHeight / 2 - cropH / 2;
return [cropX, cropY, cropW, cropH];
}
...
return <Image crop={crop()} ... />
I don't know if there is a better way.

Not able to draw text on top or arc in Flutter

I have drawn a custom arc using the draw arc method of the canvas library, now I want to write some text on top of this arc using TextPainter but when I paint the text on canvas I do not see anything. I just know the arc is drawn, and the text is invisible.
Is there some concept of Z-index or layering which I have to give to text paint to draw on top of the arc?
Below is my sample code
import 'dart:math' as math;
import 'package:flutter/material.dart';
class ChartPainter extends CustomPainter {
#override
void paint(Canvas canvas, Size size) {
print(size.width);
print(size.height);
final Paint paint = new Paint()..color = Colors.blue;
// Draw a circle that circumscribes the arrow.
paint.style = PaintingStyle.stroke;
paint.strokeWidth = 50.0;
paint.isAntiAlias = true;
//canvas.drawCircle(new Offset(centerX, centerY), r, paint);
canvas.drawArc(new Rect.fromLTWH(0.0, 0.0, size.width, size.height), 0.0,
-math.pi / 4, false, paint);
paint.color = Colors.orange;
canvas.drawArc(new Rect.fromLTWH(0.0, 0.0, size.width, size.height),
-math.pi / 4, -math.pi / 4, false, paint);
paint.color = Colors.green;
canvas.drawArc(new Rect.fromLTWH(0.0, 0.0, size.width, size.height),
-math.pi / 2, -math.pi / 4, false, paint);
paint.color = Colors.purple;
canvas.drawArc(new Rect.fromLTWH(0.0, 0.0, size.width, size.height),
-math.pi * 3 / 4, -math.pi / 4, false, paint);
TextSpan span = new TextSpan(
style: new TextStyle(color: Colors.black, fontSize: 24.0,
fontFamily: 'Roboto'), text: "Ajay Singh");
TextPainter tp = new TextPainter(
text: span, textAlign: TextAlign.left);
tp.layout();
tp.paint(canvas, new Offset(100.0, 100.0));
}
#override
bool shouldRepaint(ChartPainter oldDelegate) {
return true;
}
}

Arc gradients in Flutter?

I have a Paint object and I'm trying to use it to paint an Arc Gradient using canvas.drawArc, but the only way to do this (at least according to my research) is to use a Shader, but to get a Shader from a Gradient object, you have to use Gradient.createShader(Rect rect), which takes a rectangle. My question is, is there any way to create a shader for an Arc and not a Rectangle? Here's what I have so far for reference:
Paint paint = new Paint()
..color = bgColor
..strokeCap = StrokeCap.round
..strokeWidth = 3.0
..style = PaintingStyle.stroke
..shader = new Gradient.radial(size.width / 2.0, size.height / 2.0, size.height / 3.0, Colors.transparent, timerColor, TileMode.mirror).createShader(/* I don't have a rect object */);
canvas.drawArc(..., paint);
The Rectangle that you need is actually a square into which the circle that you are drawing would fit. The arc is just a slice of pie from that circle swept through so many radians. Create this square using Rect.fromCircle, using the centre and radius. You then use this square when creating the gradient and drawing the arc.
Here's an example
import 'dart:math';
import 'package:flutter/material.dart';
class X1Painter extends CustomPainter {
#override
void paint(Canvas canvas, Size size) {
// create a bounding square, based on the centre and radius of the arc
Rect rect = new Rect.fromCircle(
center: new Offset(165.0, 55.0),
radius: 180.0,
);
// a fancy rainbow gradient
final Gradient gradient = new RadialGradient(
colors: <Color>[
Colors.green.withOpacity(1.0),
Colors.green.withOpacity(0.3),
Colors.yellow.withOpacity(0.2),
Colors.red.withOpacity(0.1),
Colors.red.withOpacity(0.0),
],
stops: [
0.0,
0.5,
0.7,
0.9,
1.0,
],
);
// create the Shader from the gradient and the bounding square
final Paint paint = new Paint()..shader = gradient.createShader(rect);
// and draw an arc
canvas.drawArc(rect, pi / 4, pi * 3 / 4, true, paint);
}
#override
bool shouldRepaint(X1Painter oldDelegate) {
return true;
}
}
class X1Demo extends StatelessWidget {
#override
Widget build(BuildContext context) {
return new Scaffold(
appBar: new AppBar(title: const Text('Arcs etc')),
body: new CustomPaint(
painter: new X1Painter(),
),
);
}
}
void main() {
runApp(
new MaterialApp(
theme: new ThemeData.dark(),
home: new X1Demo(),
),
);
}
My SweepGradient version.
Complete example:
import 'dart:math' as math;
import 'package:flutter/material.dart';
class GradientArcPainterDemo extends StatefulWidget {
const GradientArcPainterDemo({
Key key,
}) : super(key: key);
#override
GradientArcPainterDemoState createState() => GradientArcPainterDemoState();
}
class GradientArcPainterDemoState extends State<GradientArcPainterDemo> {
double _progress = 0.9;
#override
Widget build(BuildContext context) {
return new Scaffold(
appBar: AppBar(title: const Text('GradientArcPainter Demo')),
body: GestureDetector(
onTap: () {
setState(() {
_progress += 0.1;
});
},
child: Center(
child: SizedBox(
width: 200.0,
height: 200.0,
child: CustomPaint(
painter: GradientArcPainter(
progress: _progress,
startColor: Colors.blue,
endColor: Colors.red,
width: 8.0,
),
child: Center(child: Text('$_progress')),
),
),
),
),
);
}
}
class GradientArcPainter extends CustomPainter {
const GradientArcPainter({
#required this.progress,
#required this.startColor,
#required this.endColor,
#required this.width,
}) : assert(progress != null),
assert(startColor != null),
assert(endColor != null),
assert(width != null),
super();
final double progress;
final Color startColor;
final Color endColor;
final double width;
#override
void paint(Canvas canvas, Size size) {
final rect = new Rect.fromLTWH(0.0, 0.0, size.width, size.height);
final gradient = new SweepGradient(
startAngle: 3 * math.pi / 2,
endAngle: 7 * math.pi / 2,
tileMode: TileMode.repeated,
colors: [startColor, endColor],
);
final paint = new Paint()
..shader = gradient.createShader(rect)
..strokeCap = StrokeCap.butt // StrokeCap.round is not recommended.
..style = PaintingStyle.stroke
..strokeWidth = width;
final center = new Offset(size.width / 2, size.height / 2);
final radius = math.min(size.width / 2, size.height / 2) - (width / 2);
final startAngle = -math.pi / 2;
final sweepAngle = 2 * math.pi * progress;
canvas.drawArc(new Rect.fromCircle(center: center, radius: radius),
startAngle, sweepAngle, false, paint);
}
#override
bool shouldRepaint(CustomPainter oldDelegate) => false;
}
Result:

Resources