【自学Flutter】34 自定义Widget(自绘)(1)

canvas.drawArc(

Rect.fromCircle(center: Offset(200.0, 300.0), radius: 100.0),

3.0, 2, true,

customPaint

…color = Colors.yellow

);

canvas.drawRRect(

RRect.fromRectAndRadius(

Rect.fromCircle(center: Offset(90.0, 150.0), radius: 50.0),

Radius.circular(30.0)

),

customPaint

…color = Colors.green

);

RRect outer = RRect.fromRectAndRadius(

Rect.fromCircle(center: Offset(100.0, 400.0), radius: 50.0),

Radius.circular(10.0)

);

RRect inner = RRect.fromRectAndRadius(

Rect.fromCircle(center: Offset(100.0, 400.0), radius: 30.0),

Radius.circular(10.0)

);

canvas.drawDRRect(

outer,

inner,

customPaint

…color = Colors.purpleAccent

);

Path path = new Path()…moveTo(0.0, 300.0);

path.lineTo(100.0, 330.0);

path.lineTo(250, 350);

canvas.drawPath(

path,

customPaint

…color = Colors.brown

);

}

@override

bool shouldRepaint(CustomPainter oldDelegate) {

return true;

}

}

2.解释源代码

import ‘dart:ui’;

import ‘package:flutter/material.dart’;

void main() => runApp(MyApp());

class MyApp extends StatefulWidget {

@override

_MyAppState createState() => _MyAppState();

}

class _MyAppState extends State {

@override

Widget build(BuildContext context) {

return MaterialApp(

home: Scaffold(

body: Center(

child: Column(

mainAxisAlignment: MainAxisAlignment.center,

children: [

CustomPaint(

size: Size(300, 500),

painter: MyPainter(),

)

]

),

)

),

);

}

}

class MyPainter extends CustomPainter {

Paint customPaint = new Paint()

…color = Colors.orange

…strokeCap = StrokeCap.round

…isAntiAlias = true

…strokeWidth = 10.0

…style = PaintingStyle.stroke;

@override

void paint(Canvas canvas, Size size) {

//直线

canvas.drawLine(Offset(0.0, 0.0), Offset(200.0, 0.0), customPaint);

//点

canvas.drawPoints(

//PointMode.xxx

//points(点),lines(线,隔点连接),polygon(线,相邻连接)

PointMode.points,

[

Offset(20.0, 130.0),

Offset(100.0, 180.0),

],

customPaint…color = Colors.redAccent

);

//圆

canvas.drawCircle(

Offset(100.0, 50.0),

30.0,

customPaint

…color = Colors.blue

…style = PaintingStyle.stroke //绘画风格改为stroke

);

canvas.drawColor(Colors.red, BlendMode.color);

//椭圆

canvas.drawOval(

Rect.fromPoints(Offset(170.0, 100.0), Offset(270.0, 150.0)),

customPaint

…color = Colors.black

);

//弧

canvas.drawArc(

Rect.fromCircle(center: Offset(200.0, 5.0), radius: 80.0),

0.0, 1.5, false,

customPaint

…color = Colors.yellow

);

//扇形

canvas.drawArc(

Rect.fromCircle(center: Offset(200.0, 300.0), radius: 100.0),

3.0, 2, true,

customPaint

…color = Colors.yellow

);

//圆角

canvas.drawRRect(

RRect.fromRectAndRadius(

Rect.fromCircle(center: Offset(90.0, 150.0), radius: 50.0),

Radius.circular(30.0)

),

customPaint

…color = Colors.green

);

//双圆角矩形

//外部圆角

RRect outer = RRect.fromRectAndRadius(

Rect.fromCircle(center: Offset(100.0, 400.0), radius: 50.0),

Radius.circular(10.0)

);

//内部圆角

RRect inner = RRect.fromRectAndRadius(

Rect.fromCircle(center: Offset(100.0, 400.0), radius: 30.0),

Radius.circular(10.0)

);

canvas.drawDRRect(

outer,

inner,

customPaint

…color = Colors.purpleAccent

);

//路径

//起点(0.0, 300.0)

//连接(100.0, 330.0)、(250, 350)

Path path = new Path()…moveTo(0.0, 300.0);

path.lineTo(100.0, 330.0);

path.lineTo(250, 350);

canvas.drawPath(

path,

customPaint

…color = Colors.brown

);

}

@override

bool shouldRepaint(CustomPainter oldDelegate) {

// TODO: implement shouldRepaint

最后

给大家送上我成功跳槽复习中所整理的资料,由于文章篇幅有限,所以只是把题目列出来了

image

image

image
《Android学习笔记总结+移动架构视频+大厂面试真题+项目实战源码》点击传送门,即可获取!
}

@override

bool shouldRepaint(CustomPainter oldDelegate) {

// TODO: implement shouldRepaint

最后

给大家送上我成功跳槽复习中所整理的资料,由于文章篇幅有限,所以只是把题目列出来了

[外链图片转存中…(img-zfrMeQwO-1715162249354)]

[外链图片转存中…(img-UzhE2ttC-1715162249355)]

[外链图片转存中…(img-8rRzEhbl-1715162249356)]
《Android学习笔记总结+移动架构视频+大厂面试真题+项目实战源码》点击传送门,即可获取!

  • 17
    点赞
  • 24
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值