Canvas 颤振:开始使用画布
我以前从未使用过canvas,但我必须这样做: 如果你知道一些好的教程来开始学习画布,这将是一个很大的帮助 如果您知道如何轻松创建此UI,请随时共享您的代码Canvas 颤振:开始使用画布,canvas,dart,flutter,Canvas,Dart,Flutter,我以前从未使用过canvas,但我必须这样做: 如果你知道一些好的教程来开始学习画布,这将是一个很大的帮助 如果您知道如何轻松创建此UI,请随时共享您的代码 太多了 无需画布即可制作所需内容,请查看下面的我的代码: @override Widget build(BuildContext context) { return Scaffold( body: Column( children: <Widget>[
太多了 无需画布即可制作所需内容,请查看下面的我的代码:
@override
Widget build(BuildContext context) {
return Scaffold(
body: Column(
children: <Widget>[
SizedBox(
height: 100.0,
child: Stack(
children: <Widget>[
Align(
alignment: Alignment.topCenter,
child: Container(
decoration: BoxDecoration(
color: Colors.green,
borderRadius: BorderRadius.only(
bottomLeft: Radius.elliptical(150.0, 50.0),
bottomRight: Radius.elliptical(150.0, 50.0))),
),
),
Align(
alignment: Alignment.center,
child: Text(
"My app title!",
style: TextStyle(color: Colors.white, fontSize: 25.0),
))
],
),
)
],
),
);
@覆盖
小部件构建(构建上下文){
返回脚手架(
正文:专栏(
儿童:[
大小盒子(
高度:100.0,
子:堆栈(
儿童:[
对齐(
对齐:alignment.topCenter,
子:容器(
装饰:盒子装饰(
颜色:颜色。绿色,
borderRadius:仅限borderRadius(
左下角:椭圆半径(150.0,50.0),
右下角:椭圆半径(150.0,50.0)),
),
),
对齐(
对齐:对齐.center,
子:文本(
“我的应用程序标题!”,
样式:TextStyle(颜色:Colors.white,fontSize:25.0),
))
],
),
)
],
),
);
这是一个简单的画布示例,它创建了一个圆形进度条
import 'package:flutter/material.dart';
import 'dart:ui' as ui;
import 'dart:core';
class CircularProgress extends StatefulWidget{
final double size;
final Color backgroundColor;
final Color color;
CircularProgress({@required this.size, this.backgroundColor = Colors.grey,
this.color = Colors.blue});
@override
_CircularProgress createState() => new _CircularProgress();
}
class _CircularProgress extends State<CircularProgress> with
SingleTickerProviderStateMixin{
AnimationController controller;
Animation animation;
@override
void initState() {
// TODO: implement initState
controller = new AnimationController(vsync: this, duration: const
Duration(milliseconds: 10000))..repeat();
animation = Tween(begin: 0.0, end: 360.0).animate(controller);
controller.addListener((){
setState(() {
});
});
}
@override
Widget build(BuildContext context) {
return new Stack(
alignment: Alignment.center,
children: <Widget>[
new CustomPaint(
painter: new CircularCanvas(progress: animation.value, backgroundColor:
widget.backgroundColor, color: widget.color),
size: new Size(widget.size, widget.size),
),
new Text('${(animation.value/360*100).round()}%',
style: new TextStyle(color: widget.color, fontSize: widget.size/5, fontWeight:
FontWeight.bold),)
],);
}
}
class CircularCanvas extends CustomPainter{
final double progress;
final Color backgroundColor;
final Color color;
CircularCanvas({this.progress, this.backgroundColor = Colors.grey, this.color =
Colors.blue});
@override
void paint(Canvas canvas, Size size) {
var paint = new Paint();
paint..color = backgroundColor
..strokeWidth = size.width/20
..style = PaintingStyle.stroke;
canvas.drawCircle(new Offset(size.width/2, size.height/2), size.width/2, paint);
Rect rect = new Offset(0.0, 0.0)&size;
// paint..shader = new LinearGradient(colors: [Colors.white, color]
// ,begin: Alignment.topRight, end: Alignment.bottomLeft).createShader(rect);
canvas.drawArc(new Offset(0.0, 0.0)
&new Size(size.width, size.width), -90.0*0.0174533, progress*0.0174533,
false, paint..color = color);
}
@override
bool shouldRepaint(CircularCanvas oldDelegate) {
return oldDelegate.progress !=progress;
}
}
导入“包装:颤振/材料.省道”;
将“dart:ui”导入为ui;
导入“省道:核心”;
类CircularProgress扩展StatefulWidget{
最终双倍尺寸;
最终颜色背景色;
最终颜色;
CircularProgress({@required this.size,this.backgroundColor=Colors.grey,
this.color=Colors.blue});
@凌驾
_CircularProgress createState()=>new_CircularProgress();
}
类_CircularProgress使用扩展状态
SingleTickerProviderStateMixin{
动画控制器;
动画;
@凌驾
void initState(){
//TODO:实现initState
控制器=新的AnimationController(vsync:this,duration:const
持续时间(毫秒:10000))…重复();
动画=Tween(开始:0.0,结束:360.0)。动画(控制器);
controller.addListener(){
设置状态(){
});
});
}
@凌驾
小部件构建(构建上下文){
返回新堆栈(
对齐:对齐.center,
儿童:[
新定制涂料(
画师:新循环动画(进度:animation.value,背景色:
widget.backgroundColor,颜色:widget.color),
大小:新大小(widget.size,widget.size),
),
新文本(“${(animation.value/360*100.round()}%”,
样式:新文本样式(颜色:widget.color,fontSize:widget.size/5,fontwweight:
FontWeight.bold),)
],);
}
}
类CircularCanvas扩展了CustomPainter{
最终双倍进展;
最终颜色背景色;
最终颜色;
CircularCanvas({this.progress,this.backgroundColor=Colors.grey,this.color=
颜色:蓝色});
@凌驾
空心油漆(帆布,尺寸){
var paint=新的paint();
油漆..颜色=背景色
..冲程宽度=尺寸.宽度/20
..风格=绘画风格.笔划;
画布.画圈(新偏移量(size.width/2,size.height/2),size.width/2,绘制);
Rect Rect=新偏移量(0.0,0.0)和大小;
//paint..shader=new LinearGradient(颜色:[colors.white,color]
//,开始:Alignment.topRight,结束:Alignment.bottomLeft).createShader(rect);
画布.drawArc(新偏移量(0.0,0.0)
&新尺寸(尺寸.宽度,尺寸.宽度),-90.0*0.0174533,进度*0.0174533,
假,油漆..颜色=颜色);
}
@凌驾
bool应重新绘制(循环NVAS旧委托){
返回oldDelegate.progress!=进度;
}
}
查看以下UI检查的完整代码
示例链接已断开