flutter - sweepGradient 终点没有动画
问题描述
我想动画扫描渐变角度,这样我就可以实现移动颜色效果。
我将起始角度设置为 0,并将该角度设置为 2*math.pi。而我在 2*math.pi 处声明结束角度并将其动画化为 4*math.pi;
当我这样做时,开始角度正在动画,但结束角度没有动画。
import 'package:flutter/material.dart';
import 'dart:math' as math;
class Delete extends StatefulWidget {
Delete({Key key}) : super(key: key);
@override
_DeleteState createState() => _DeleteState();
}
class _DeleteState extends State<Delete> with SingleTickerProviderStateMixin {
Animation<double> _animation;
AnimationController _controller;
@override
void initState() {
super.initState();
_controller =
AnimationController(vsync: this, duration: Duration(seconds: 5));
_animation = Tween<double>(begin: 0, end: 2 * math.pi).animate(_controller)
..addListener(() {
print(_animation.value);
setState(() {});
});
_controller.repeat();
}
@override
void dispose() {
_controller.dispose();
super.dispose();
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('hello'),
),
body: Container(
width: 180,
height: 180,
padding: EdgeInsets.all(20.0),
decoration: BoxDecoration(
shape: BoxShape.circle,
gradient: SweepGradient(
colors: [
Colors.blue,
Colors.green,
Colors.yellow,
Colors.red,
Colors.blue
],
// stops: [0.0, 0.25, 0.5, 0.75, 1],
center: Alignment(-0.35, -0.35),
startAngle: _animation.value,
endAngle: _animation.value + (2 * math.pi),
),
),
),
);
}
}
解决方案
基本上你做对了。
您所缺少的(并且在文档中没有完全解释清楚)是您需要TileMode
在您的设备上设置 aSweepGradient
以获得预期的结果。
这是一个工作示例:
class Delete extends StatefulWidget {
Delete({Key key}) : super(key: key);
@override
_DeleteState createState() => _DeleteState();
}
class _DeleteState extends State<Delete> with SingleTickerProviderStateMixin {
Animation<double> _animation;
AnimationController _controller;
@override
void initState() {
super.initState();
_controller = AnimationController(vsync: this, duration: Duration(seconds: 5));
_animation = Tween<double>(begin: 0, end: 2 * math.pi).animate(_controller)
..addListener(() {
setState(() {});
});
_controller.repeat();
}
@override
void dispose() {
_controller.dispose();
super.dispose();
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('hello'),
),
body: Container(
width: 180,
height: 180,
padding: EdgeInsets.all(20.0),
decoration: BoxDecoration(
shape: BoxShape.circle,
gradient: SweepGradient(
colors: [Colors.blue, Colors.green, Colors.yellow, Colors.red, Colors.blue],
// stops: [0.0, 0.25, 0.5, 0.75, 1],
center: Alignment(-0.35, -0.35),
startAngle: _animation.value,
endAngle: _animation.value + (2 * math.pi),
tileMode: TileMode.repeated,
),
),
),
);
}
}
TileMode
您可以在此处找到文档。
推荐阅读
- sql - SQL 窗口函数 order by vs partition by。有什么区别?
- discord.js - 需要 Discord.JS 嵌入命令帮助
- angular - 无法读取未定义的属性“endsWith”
- amazon-web-services - 有没有办法确保按顺序运行 lambda
- javascript - SignalR $.connection 未定义 - MVC 5 进度条
- excel - 基于Value Loop的VBA Open Workbook
- python - 如何确定 mysql.connector 游标类型?
- r - 提取回归类型输出的一部分
- javascript - React Native 如何在 AsyncStorage 中存储语言并更改应用程序语言?
- three.js - 如何让 mtlLoader 和 objloader 等待材质纹理被加载,然后再继续程序的其余部分?