Flutter 颤振:画布不显示圆弧
我正在尝试创建一个加载器。因此,使用Flutter 颤振:画布不显示圆弧,flutter,flutter-animation,Flutter,Flutter Animation,我正在尝试创建一个加载器。因此,使用CustomPaint我创建了一个圆弧: class ArcPainter extends CustomPainter { final double radius; ArcPainter({ this.radius = 5, }); @override void paint(Canvas canvas, Size size) { Paint paint = Paint() ..color = Colors.bl
CustomPaint
我创建了一个圆弧:
class ArcPainter extends CustomPainter {
final double radius;
ArcPainter({
this.radius = 5,
});
@override
void paint(Canvas canvas, Size size) {
Paint paint = Paint()
..color = Colors.black
..strokeWidth = 5.0
..strokeCap = StrokeCap.round
..style = PaintingStyle.stroke;
Rect rect = Rect.fromCenter(
center: size.center(Offset.zero),
width: size.width / radius,
height: size.height / radius);
canvas.drawArc(rect, pi / 4, pi / 2, false, paint);
canvas.drawArc(rect, -pi / 4, -pi / 2, false, paint);
}
@override
bool shouldRepaint(ArcPainter old) {
return false;
}
}
我是这样用的:
Widget build(BuildContext context) {
return Material(
child: Container(
child: AnimatedBuilder(
animation: _animationController,
builder: (context, child) {
return Transform.rotate(
angle: _animationController.value * 4.0 * pi,
child: Stack(
children: [
Align(
alignment: Alignment.center,
child: CustomPaint(
painter: ArcPainter(radius: _animation.value),
),
)
],
),
);
},
),
),
);
这是initState
方法:
@override
void initState() {
_animationController = AnimationController(
vsync: this, duration: Duration(milliseconds: 3000));
_animation = Tween(begin: 5.0, end: 3.0).animate(
CurvedAnimation(parent: _animationController, curve: Curves.easeIn),
);
_animationController.repeat();
super.initState();
}
但是当我运行代码时什么都没有发生?我希望在我的画布上显示一个弧!!!我的代码有什么问题?您需要为CustomPaint提供一个画布,该画布具有要绘制的大小。请参阅下面的工作代码或直接在上运行代码:
导入“包装:颤振/材料.省道”;
void main()=>runApp(MyApp());
类MyApp扩展了无状态小部件{
@凌驾
小部件构建(构建上下文){
返回材料PP(
标题:“颤振演示”,
debugShowCheckedModeBanner:false,
主题:主题数据(
主样本:颜色。蓝色,
),
主页:MyHomePage(标题:“颤振演示主页”),
);
}
}
类MyHomePage扩展StatefulWidget{
MyHomePage({Key,this.title}):超级(Key:Key);
最后的字符串标题;
@凌驾
_MyHomePageState createState()=>\u MyHomePageState();
}
类_MyHomePageState扩展状态
使用SingleTickerProviderStateMixin{
AnimationController _AnimationController;
动画(动画),;
@凌驾
void initState(){
_animationController=animationController(
vsync:this,duration:duration(毫秒:3000));
_动画=Tween(开始:5.0,结束:3.0)。设置动画(
曲线动画(父对象:_animationController,曲线:Curves.easeIn),
);
_animationController.repeat();
super.initState();
}
@凌驾
小部件构建(构建上下文){
退货(
子对象:动画生成器(
动画:_animationController,
生成器:(上下文,子对象){
返回Transform.rotate(
角度:_animationController.value*4.0*3.14,
子:堆栈(
儿童:[
居中(
子:容器(
宽度:150,
身高:150,
孩子:定制油漆(
画师:ArcPainter(半径:_animation.value),
),
),
),
],
),
);
},
),
);
}
}
类ArcPainter扩展了CustomPainter{
最终双半径;
电弧画家({
这个半径=5,
});
@凌驾
空心油漆(帆布,尺寸){
油漆油漆=油漆()
…颜色=颜色
..冲程宽度=5.0
..strokeCap=strokeCap.round
..风格=绘画风格.笔划;
Rect Rect=Rect.fromCenter(
中心:大小。中心(偏移。零),
宽度:大小。宽度/半径,
高度:尺寸、高度/半径);
画布。drawArc(矩形,3.14/4,3.14/2,假,油漆);
帆布画弧(矩形,-3.14/4,-3.14/2,假,油漆);
}
@凌驾
布尔应重新喷漆(旧){
返回true;
}
}
import 'package:flutter/material.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
debugShowCheckedModeBanner: false,
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: MyHomePage(title: 'Flutter Demo Home Page'),
);
}
}
class MyHomePage extends StatefulWidget {
MyHomePage({Key key, this.title}) : super(key: key);
final String title;
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage>
with SingleTickerProviderStateMixin {
AnimationController _animationController;
Animation _animation;
@override
void initState() {
_animationController = AnimationController(
vsync: this, duration: Duration(milliseconds: 3000));
_animation = Tween(begin: 5.0, end: 3.0).animate(
CurvedAnimation(parent: _animationController, curve: Curves.easeIn),
);
_animationController.repeat();
super.initState();
}
@override
Widget build(BuildContext context) {
return Material(
child: AnimatedBuilder(
animation: _animationController,
builder: (context, child) {
return Transform.rotate(
angle: _animationController.value * 4.0 * 3.14,
child: Stack(
children: [
Center(
child: Container(
width: 150,
height: 150,
child: CustomPaint(
painter: ArcPainter(radius: _animation.value),
),
),
),
],
),
);
},
),
);
}
}
class ArcPainter extends CustomPainter {
final double radius;
ArcPainter({
this.radius = 5,
});
@override
void paint(Canvas canvas, Size size) {
Paint paint = Paint()
..color = Colors.black
..strokeWidth = 5.0
..strokeCap = StrokeCap.round
..style = PaintingStyle.stroke;
Rect rect = Rect.fromCenter(
center: size.center(Offset.zero),
width: size.width / radius,
height: size.height / radius);
canvas.drawArc(rect, 3.14 / 4, 3.14 / 2, false, paint);
canvas.drawArc(rect, -3.14 / 4, -3.14 / 2, false, paint);
}
@override
bool shouldRepaint(ArcPainter old) {
return true;
}
}