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

文章详细介绍了使用Flutter中的CanvasAPI进行各种图形绘制的方法,包括直线、点、圆、颜色填充、弧形等,并提及了与Android开发相关的学习资源,如自定义Painter和一套完整的移动开发学习资料包。
摘要由CSDN通过智能技术生成

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

canvas.drawPoints(

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

);

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

);

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)

);

先自我介绍一下,小编13年上海交大毕业,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里一直到现在。

深知大多数初中级Android工程师,想要提升技能,往往是自己摸索成长或者是报班学习,但对于培训机构动则近万的学费,着实压力不小。自己不成体系的自学效果低效又漫长,而且极易碰到天花板技术停滞不前!

因此收集整理了一份《Android移动开发全套学习资料》送给大家,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。

img

img

img

img

由于文件比较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频

如果你觉得这些内容对你有帮助,可以扫码领取!!!!

Android高级架构师

由于篇幅问题,我呢也将自己当前所在技术领域的各项知识点、工具、框架等汇总成一份技术路线图,还有一些架构进阶视频、全套学习PDF文件、面试文档、源码笔记。

  • 330页PDF Android学习核心笔记(内含上面8大板块)

  • Android学习的系统对应视频

  • Android进阶的系统对应学习资料

  • Android BAT部分大厂面试题(有解析)

好了,以上便是今天的分享,希望为各位朋友后续的学习提供方便。觉得内容不错,也欢迎多多分享给身边的朋友哈。
《Android学习笔记总结+移动架构视频+大厂面试真题+项目实战源码》点击传送门,即可免费领取!

8大板块)**

[外链图片转存中…(img-FzGqyoBb-1711253027819)]

[外链图片转存中…(img-vB3bvv8i-1711253027819)]

  • Android学习的系统对应视频

  • Android进阶的系统对应学习资料

[外链图片转存中…(img-T9qmmoBf-1711253027819)]

  • Android BAT部分大厂面试题(有解析)

[外链图片转存中…(img-wx0K8wTq-1711253027819)]

好了,以上便是今天的分享,希望为各位朋友后续的学习提供方便。觉得内容不错,也欢迎多多分享给身边的朋友哈。
《Android学习笔记总结+移动架构视频+大厂面试真题+项目实战源码》点击传送门,即可免费领取!

  • 10
    点赞
  • 27
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值