My goal is to draw a line, however, it is not working.
I find out that the size parameter in paint function is always 0
why ? what should I change in order to make it works
class CurvePainter extends CustomPainter {
#override
void paint(Canvas canvas, Size size) {
var paint = Paint();
paint.color = Colors.green[800];
paint.style = PaintingStyle.stroke;
paint.strokeWidth = 2.0;
var path = Path();
path.lineTo(size.width, size.height);
}
#override
bool shouldRepaint(CustomPainter oldDelegate) {
return true;
}
}
class Home extends StatelessWidget{
#override
Widget build(BuildContext context){
return Scaffold(
appBar: AppBar(
title: Text('Test'),
),
body: Center(child: CustomPaint(painter: CurvePainter)),
);
You can copy paste run full code beow
Step 1 : You can wrap with Container to provide size
Step 2 : You need path.close() and canvas.drawPath()
Step 3 : CurvePainter typo need CurvePainter(), you loss ()
code snippet
void paint(Canvas canvas, Size size) {
var paint = Paint();
paint.color = Colors.green[800];
paint.style = PaintingStyle.stroke;
paint.strokeWidth = 2.0;
var path = Path();
print(size.height);
path.lineTo(size.width, size.height);
path.close();
canvas.drawPath(path, paint);
}
...
body: Center(
child: Container(
width: 100,
height: 100,
child: CustomPaint(painter: CurvePainter())))
working demo
full code
import 'package:flutter/material.dart';
class CurvePainter extends CustomPainter {
#override
void paint(Canvas canvas, Size size) {
var paint = Paint();
paint.color = Colors.green[800];
paint.style = PaintingStyle.stroke;
paint.strokeWidth = 2.0;
var path = Path();
print(size.height);
path.lineTo(size.width, size.height);
path.close();
canvas.drawPath(path, paint);
}
#override
bool shouldRepaint(CustomPainter oldDelegate) {
return true;
}
}
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
#override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: Home(),
);
}
}
class Home extends StatelessWidget {
#override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Test'),
),
body: Center(
child: Container(
width: 100,
height: 100,
child: CustomPaint(painter: CurvePainter()))),
);
}
}
Related
The container has an image as a child. I need a line on top of that (diagonally from top right to bottom left).
Use a CustomPainter
import 'package:flutter/material.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
#override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
body: Container(
color: Colors.white,
padding: EdgeInsets.symmetric(horizontal: 40, vertical: 80),
child: Container(
width: 300,
height: 400,
color: Colors.yellow,
child: CustomPaint(painter: LinePainter()),
),
),
),
);
}
}
class LinePainter extends CustomPainter {
#override
void paint(Canvas canvas, Size size) {
final p1 = Offset(size.width, 0);
final p2 = Offset(0, size.height);
final paint = Paint()
..color = Colors.black
..strokeWidth = 4;
canvas.drawLine(p1, p2, paint);
}
#override
bool shouldRepaint(LinePainter oldDelegate) => false;
}
You need to use clippath To do that
like this
class CustomClipPath extends CustomClipper<Path> {
var radius=10.0;
#override
Path getClip(Size size) {
Path path = Path();
path.lineTo(0, 200);
path.lineTo(200,200);
path.lineTo(260,0);
path.lineTo(30, 0);
return path;
}
#override
bool shouldReclip(CustomClipper<Path> oldClipper) => false;
}
I am creating a flutter application where I need to detect clicks from a CustomPaint widget with my own custom painter. The paint objects are being painted based on a given Path object. My CustomPainter is overriding hitTest with return path.contains(position).
The problem I am having is the method returns inconsistent results.
Two point path: Path()..moveTo(40, 80)..lineTo(100, 20);
The hitTest will always return false, even when I click within the line.
Three point path: Path()..moveTo(40, 80)..lineTo(100, 20)..lineTo(300, 60); The hitTest works as expected.
Complex path: Path()..moveTo(50, 100)..lineTo(50, 150)..lineTo(200, 150)..lineTo(200, 250)..lineTo(400, 250)..lineTo(400, 100); The hitTest returns true more than expected. Takes clicks in between paths.
Here is my sample code:
import 'package:flutter/material.dart';
import 'package:flutter/rendering.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
// This widget is the root of your application.
#override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: MyHomePage(title: 'Flutter Demo Home Page'),
);
}
}
class MyHomePage extends StatelessWidget {
MyHomePage({Key key, this.title}) : super(key: key);
final String title;
int boxCount = 0;
int pathCount = 0;
final Path path1 = Path()
..moveTo(40, 80)
..lineTo(100, 20);
final Path path2 = Path()
..moveTo(40, 80)
..lineTo(100, 20)
..lineTo(300, 60);
final Path path3 = Path()
..moveTo(50, 100)
..lineTo(50, 150)
..lineTo(200, 150)
..lineTo(200, 250)
..lineTo(400, 250)
..lineTo(400, 100);
#override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(title),
),
body: GestureDetector(
onTap: () => print("path clicked ${++pathCount}"),
child: CustomPaint(
size: MediaQuery.of(context).size,
painter: MyCustomPainter(path3),
)));
}
}
class MyCustomPainter extends CustomPainter {
final Path path;
final Paint myPaint = Paint()
..strokeWidth = 2
..strokeCap = StrokeCap.round
..style = PaintingStyle.stroke;
int clickCount = 0;
MyCustomPainter(this.path);
#override
void paint(Canvas canvas, Size size) {
canvas.drawPath(path, myPaint);
}
#override
bool shouldRepaint(covariant CustomPainter oldDelegate) {
return true;
}
#override
bool hitTest(Offset position) {
return path.contains(position);
}
}
How make this styling for a button in flutter. Since I'm a newbie, I have no Idea on how to do it.
In image the triangle is somewhat irregular, I want that to be matched with the button box.
You can use the CustomPaint widget to get that effect. You can easily auto-generate the CustomPaint code from Flutter Shape Maker. Please see the code below. You can also run the code on DartPad at the following URL https://dartpad.dev/0171e4b838b740ea23f896f3b0be1f8e :
import 'package:flutter/material.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
#override
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: false,
home: MyWidget(),
);
}
}
class MyWidget extends StatelessWidget {
#override
Widget build(BuildContext context) {
return Scaffold(
body: Center(
child: Container(
width: 360,
height: 100,
color: Colors.black,
child: CustomPaint(
size: Size(1200, 700),
painter: FlagPainter(),
child: Text(
"text",
style: TextStyle(color: Colors.black, fontSize:24),
),
),
),
),
);
}
}
class FlagPainter extends CustomPainter {
#override
void paint(Canvas canvas, Size size) {
Paint paint_0 = new Paint()
..color = Color.fromARGB(255, 255, 255, 255)
..style = PaintingStyle.fill
..strokeWidth = 1;
Path path_0 = Path();
path_0.moveTo(0, 0);
path_0.lineTo(0, size.height * 2.40);
path_0.lineTo(0, size.height * 0.72);
path_0.lineTo(size.width * 0.28, 0);
canvas.drawPath(path_0, paint_0);
}
#override
bool shouldRepaint(covariant CustomPainter oldDelegate) {
return true;
}
}
I was experimenting with custom clipper. I am trying to draw this curvy blue container. Somehow I cannot seem to figure out(currently a noob). Can I help with the code snippet of the paths of custom clipper? Most probably I am writing the paths wrong.
Here is the image:
using custompaint calss
class _MyHomePageState extends State<MyHomePage> {
#override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
body: CustomPaint(
painter: BackgroundPaint(),
child: Container(
child: Center(
child: Text("hello"),
),
),
),
backgroundColor: Colors.blue,
),
);
}
}
class BackgroundPaint extends CustomPainter {
#override
void paint(Canvas canvas, Size size) {
final paint = Paint();
Path path = Path();
paint.color = Colors.white;
path.lineTo(0, size.height *0.3);
path.quadraticBezierTo(size.width*0.70, size.height*0.60, size.width*1.2, 0);
path.close();
canvas.drawPath(path, paint);
}
#override
bool shouldRepaint(CustomPainter oldDelegate) {
return oldDelegate != this;
}
}
My UI draw like this picture:
So I write some code by using CodePainter in Flutter. Here's my code:
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
class BackgroundShape extends CustomPainter {
#override
void paint(Canvas canvas, Size size) {
final Paint paint = Paint();
paint.color = Colors.black;
var smallRect = Alignment.bottomCenter.inscribe(Size(100, 50), Rect.fromLTWH(size.width/2 -35, size.height-40, 40, 30));
var path = Path();
path.fillType = PathFillType.evenOdd;
path.addRRect(RRect.fromRectAndCorners(Rect.fromLTWH(0, 0, size.width, size.height), bottomLeft: Radius.circular(10), bottomRight: Radius.circular(10)));
path.addRRect(RRect.fromRectAndCorners(smallRect, topLeft: Radius.circular(10), topRight: Radius.circular(10)));
path.lineTo(0, size.height);
path.lineTo(size.width/2 - 35, size.height);
path.lineTo(size.width/2 - 35, size.height-40);
path.lineTo(size.width/2 + 35, size.height-40);
path.lineTo(size.width/2 + 35, size.height);
path.lineTo(size.width, size.height);
path.lineTo(size.width, 0);
path.close();
canvas.drawPath(path, paint);
}
#override
bool shouldRepaint(CustomPainter oldDelegate) => false;
}
Here is my result:
How to add border radius? please help me :D thanks!
For some reason, I'm not able to run completely the code you've provided, but taking consideration your current requirements and from the comments. I've created the following samples for your references:
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
#override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
home: MyHomePage(),
);
}
}
class MyHomePage extends StatefulWidget {
#override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
#override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Flutter Demo'),
),
body: Center(
child: Container(
width: 200,
height: 200,
child: CustomPaint(
painter: PainterTwo(),
),
),
),
);
}
}
class PainterOne extends CustomPainter {
#override
void paint(Canvas canvas, Size size) {
double w = size.width;
double h = size.height;
double r = 15;
var paint1 = Paint()
..color = Color(0xff888888)
..style = PaintingStyle.fill;
RRect fullRect = RRect.fromRectAndRadius(
Rect.fromCenter(center: Offset(w / 2, h / 2), width: w, height: h),
Radius.circular(r),
);
canvas.drawRRect(fullRect, paint1);
}
#override
bool shouldRepaint(CustomPainter oldDelegate) => true;
}
class PainterTwo extends CustomPainter {
#override
void paint(Canvas canvas, Size size) {
Paint paint = Paint()
..color = Colors.blue
..style = PaintingStyle.stroke
..strokeWidth = 8.0;
Path path = Path();
path.addRRect(RRect.fromRectAndRadius(
Rect.fromLTWH(
size.width / 2, size.height / 2, size.width / 4, size.height / 4),
Radius.circular(15)));
canvas.drawPath(path, paint);
}
#override
bool shouldRepaint(CustomPainter oldDelegate) => true;
}
Output of PainterOne():
Output of PainterTwo():