需要此行的“周日,周一,周二,...周六”按钮才能单独点击。有人知道该怎么做吗?下面是我当前的代码:
import 'package:flutter/material.dart';
class WeeklyRepeat extends StatefulWidget {
@override
_WeeklyRepeatState createState() => _WeeklyRepeatState();
}
class _WeeklyRepeatState extends State<WeeklyRepeat> {
List<String> _label = [
'S',
'M',
'T',
'W',
'T',
'F',
'S',
];
bool pressed = false;
_pressed() {
setState(() {
pressed = !pressed;
});
}
Widget _buildIcon(int index) {
return GestureDetector(
onTap: () => _pressed(),
child: pressed
? Container(
alignment: Alignment.center,
height: 42.0,
width: 42.0,
decoration: BoxDecoration(
color: Colors.black87,
borderRadius: BorderRadius.circular(21.0),
border: Border.all(color: Colors.white, width: 0.0),
),
child: Text(
'${_label[index]}',
style: TextStyle(
color: Colors.white,
fontSize: 20.0,
fontWeight: FontWeight.bold,
),
),
)
: Container(
alignment: Alignment.center,
height: 42.0,
width: 42.0,
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(21.0),
border: Border.all(color: Colors.black87, width: 1.0),
),
child: Text(
'${_label[index]}',
style: TextStyle(
fontSize: 20.0,
fontWeight: FontWeight.bold,
),
),
),
);
}
@override
Widget build(BuildContext context) {
return Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: <Widget>[
_buildIcon(0),
_buildIcon(1),
_buildIcon(2),
_buildIcon(3),
_buildIcon(4),
_buildIcon(5),
_buildIcon(6),
],
);
}
}
最佳答案
您可以在_onPressed
方法中添加一个参数,以便知道按下了哪个图标。
为了在实际的小部件(现在正在检查pressed
)中反射(reflect)出来,您需要更改代码,以便可以知道应该检查所有图标。
您正在将索引传递到_buildIcon
方法中,也许您也可以使用它来跟踪已按下哪个图标。
您可以在状态pressedIndex
中添加一个字段,并将当前按下的索引放在此处,然后在构建图标时检查该索引。
关于flutter - 标签列表中一排排圆形按钮触发在一起,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59711703/