我试图在轻按时突出显示 GestureDetector 的颜色,我只想更改轻按的容器颜色,但是我的 GestureDetector 是从数组创建的,所以当我更改一个时,一切都会改变。
我如何达到我的目标?数组中的文本不是固定的,会增长或缩小。
import 'package:flutter/material.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: false,
home: MyFloat(),
);
}
}
class MyFloat extends StatefulWidget {
@override
_MyFloatState createState() => _MyFloatState();
}
class _MyFloatState extends State<MyFloat> {
List<BoxShadow> shadow = customShadow;
Color color = Colors.green;
@override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: primaryColor,
body: SafeArea(
child: Column(
children: [
Wrap(
children: [
...(["hello", "hi", "hey"]
.map(
(val) => GestureDetector(
onTap: () {
setState(() {
this.color == Colors.green
? this.color = Colors.cyan
: this.color = Colors.green;
});
},
child: AnimatedContainer(
duration: Duration(milliseconds: 250),
height: 100,
width: 100,
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(20),
color: this.color == Colors.green
? Colors.cyan
: Colors.green,
),
child: Center(
child: Text(val),
),
),
),
)
.toList())
],
)
],
),
),
);
}
}