【发布时间】:2021-10-22 20:38:41
【问题描述】:
AnimationController? controller;
Animation<double>? animation;
@override
void initState() {
super.initState();
controller =
AnimationController(duration: const Duration(seconds: 3), vsync: this)
..addListener(() => setState(() {}));
animation = Tween(begin: -500.0, end: 500.0).animate(controller!);
controller!.forward();
}
List tile = [
[true, true, true, true],
[true, true, true, true],
[true, true, true, true],
[true, true, true, true],
];
String? gettw;
Container(
color: Colors.white,
child: ListView.builder(
reverse: false,
itemCount: tile[0].length,
itemBuilder: (BuildContext context, int index) {
String? gettw;
if (tile[index][0]) {
if (gettw != 'transparent') {
gettw = 'black';
}
} else if (tile[index][0] == false) {
gettw = 'transparent';
}
print('gettw $gettw');
return GestureDetector(
behavior: HitTestBehavior.translucent,
onTap: () {
setState(() {
if (tile[index][0] == true) {
print('working');
gettw = 'transparent';
print('gettw $gettw');
} else if (tile[index][0] == false) {
gettw = 'red';
}
});
},
child: Transform.translate(
offset: Offset(0.0, animation!.value),
child: Container(
height: MediaQuery.of(context).size.height / 4,
color: (gettw == 'black')
? Colors.black
: (gettw == 'transparent')
? Colors.transparent
: (gettw == 'red')
? Colors.red
: Colors.green,
child: Text('${tile[index][0]}',
style: const TextStyle(color: Colors.purple)),
),
),
);
},
)),
我从上到下为 4 个容器设置动画。如果用户单击容器。容器颜色需要将黑色变为白色。但所有 4 个容器的颜色都发生了变化。 gettw 变量包含需要在容器中显示的颜色。请有人帮我解决这个问题。
【问题讨论】: