class _MyHomePageState extends State<MyHomePage>{ @override Widget build(BuildContext context) { return Scaffold( backgroundColor: Color.fromARGB(255, 255,255,255), appBar: AppBar( title: Text('App Name', style: TextStyle(fontSize: 30.0),), ), body:Container( child: CustomPaint( painter: MyPainter(), ), ), ); } } class MyPainter extends CustomPainter { @override void paint(Canvas canvas, Size size){ Paint p = Paint(); p.style = PaintingStyle.fill; p.color = Color.fromARGB(150, 0, 200, 255); Rect r = Rect.fromLTWH(50.0, 50.0, 150.0, 150.0); canvas.drawRect(r, p); p.style = PaintingStyle.stroke; p.color = Color.fromARGB(150, 200, 0, 255); p.strokeWidth = 10.0; r = Rect.fromLTWH(100.0, 100.0, 150.0, 150.0); canvas.drawRect(r, p); } @override bool shouldRepaint(CustomPainter oldDelegate) => true; }
楕円の描画
void paint(Canvas canvas, Size size){ Paint p = Paint(); p.style = PaintingStyle.fill; p.color = Color.fromARGB(150, 0, 200, 255); Offset ctr = Offset(100.0, 100.0); canvas.drawCircle(ctr, 75.0, p); p.style = PaintingStyle.stroke; p.color = Color.fromARGB(150, 200, 0, 255); p.strokeWidth = 10.0; Rect r = Rect.fromLTWH(100.0, 50.0, 200.0, 150.0); canvas.drawRect(r, p); r = Rect.fromLTWH(50.0, 100.0, 150.0, 200.0); canvas.drawOval(r, p); }
直線の描画
p.style = PaintingStyle.stroke; p.strokeWidth = 5.0; p.color = Color.fromARGB(150, 0, 200, 255); for (var i = 0; i <= 10; i++) { Rect r = Rect.fromLTRB( 50.0 + 20 * i, 50.0, 50.0, 250.0 - 20 * i); canvas.drawLine(r.topLeft, r.bottomRight, p); } }
テキストの描画
@override void paint(Canvas canvas, Size size){ Paint p = Paint(); ui.ParagraphBuilder builder = ui.ParagraphBuilder( ui.ParagraphStyle(textDirection: TextDirection.ltr), ) ..pushStyle(ui.TextStyle(color: Colors.red, fontSize: 48.0)) ..addText('Hello!') ..pushStyle(ui.TextStyle(color: Colors.blue[700], fontSize: 30.0)) ..addText('This is a sample of paragraph text.') ..pushStyle(ui.TextStyle(color: Colors.blue[200], fontSize: 30.0)) ..addText('You can draw Multi-font text!.'); ui.Paragraph paragraph = builder.build() ..layout(ui.ParagraphConstraints(width: 300.0)); Offset off = Offset(50.0, 50.0); canvas.drawParagraph(paragraph, off); }