Flutter 此代码有什么问题?为什么控制器上的速度不工作?-颤振
我发现这个代码有问题,动画控制器的速度不正常。当它必须从上一个值传递到下一个值时,它会使速度过快,我如何解决它? 它是一个径向计数器,当你添加一项任务时,它会计算全天完成任务的百分比Flutter 此代码有什么问题?为什么控制器上的速度不工作?-颤振,flutter,dart,radial,Flutter,Dart,Radial,我发现这个代码有问题,动画控制器的速度不正常。当它必须从上一个值传递到下一个值时,它会使速度过快,我如何解决它? 它是一个径向计数器,当你添加一项任务时,它会计算全天完成任务的百分比 class RadialProgress extends StatefulWidget { final porcentaje; final Color colorprimario; final Color colorsecundario; final double grosorsecundario
class RadialProgress extends StatefulWidget {
final porcentaje;
final Color colorprimario;
final Color colorsecundario;
final double grosorsecundario;
final double grosorprimario;
RadialProgress({
@required this.porcentaje,
this.colorprimario = Colors.blue,
this.colorsecundario = Colors.grey,
this.grosorsecundario = 4,
this.grosorprimario = 10
});
@override
_RadialProgressState createState() => _RadialProgressState();
}
double porcentaje;
class _RadialProgressState extends State<RadialProgress> with SingleTickerProviderStateMixin{
AnimationController controller;
double porcentajeAnterior;
@override
void initState() {
porcentajeAnterior = widget.porcentaje;
controller = new AnimationController(vsync: this, duration: Duration( milliseconds: 1000 ));
super.initState();
}
@override
void dispose() {
controller.dispose();
super.dispose();
}
@override
Widget build(BuildContext context) {
controller.forward( from: 0.0 );
final diferenciaAnimar = widget.porcentaje - porcentajeAnterior;
porcentajeAnterior = widget.porcentaje;
return AnimatedBuilder(
animation: controller,
builder: (BuildContext context, Widget child) {
return StreamBuilder<List<Todo>>(
stream: DatabaseService().trueItems(),
builder: (context, snapshot) {
// List<Todo> trueitems = snapshot.data;
if (!snapshot.hasData) return SizedBox();
return Container(
padding: EdgeInsets.all(10),
width: double.infinity,
height: double.infinity,
child: StreamBuilder<List<Todo>>(
stream: DatabaseService().listTodos(),
builder: (context, snapshot) {
// List<Todo> listtodo = snapshot.data;
// final porcentaje = ((trueitems.length/listtodo.length)).toDouble();
return CustomPaint(
painter: _MiRadialProgress(
( ((widget.porcentaje*100) - (diferenciaAnimar))) + (((diferenciaAnimar) * controller.value)),
widget.colorprimario,
widget.colorsecundario,
widget.grosorsecundario,
widget.grosorprimario)
);
}
),
// child: Text('${widget.porcentaje}'),
);
}
);
}
);
}
}
您不应将
控制器向前(从:0.0开始)代码>在构建方法内部,因为每次重新渲染(构建方法执行)时都会启动动画。可能是渲染时动画重新开始,所以它似乎不起作用。只需将动画开始代码放入initState
方法中:
@override
void initState() {
super.initState();
porcentajeAnterior = widget.porcentaje;
controller = new AnimationController(vsync: this, duration: Duration( milliseconds: 1000 ));
controller.forward();
}
我认为这是CustomPaint的问题,但我不知道为什么只有在动画完成后才能通过流收到待办事项列表?我如何解决?这取决于您是否能够确认情况是否如此。如果是这样,您可以尝试调用StreamBuilder
中的动画。不知道是否是这样,但如果是这样,我如何调用StreamBuilder中的动画?它不允许我做这件事。
@override
void initState() {
super.initState();
porcentajeAnterior = widget.porcentaje;
controller = new AnimationController(vsync: this, duration: Duration( milliseconds: 1000 ));
controller.forward();
}