当然,这是一个简单的解决方案,但我不知道它-我很新的颤音-仍然。有什么问题吗?
我有警报对话框与4个列表瓷砖,应该作为选择器的gps半径,但我有一个问题的单一选择。现在,当一个被选中的时候,所有的都是在同一时间被选择的。因此,我需要找到一种方法,如何为每一个选择(如果一个被选中,rest应该取消选择),然后传递它的价值,例如。10,25等,而“选择”按钮被按下。
class RadiusSelectorDialog extends StatefulWidget {
RadiusSelectorDialog({Key key}) : super(key: key);
@override
_RadiusSelectorDialogState createState() => _RadiusSelectorDialogState();
}
class _RadiusSelectorDialogState extends State<RadiusSelectorDialog>
with TitleDescriptionTextMixin {
bool selected = false;
@override
Widget build(BuildContext context) {
return AlertDialog(
scrollable: true,
contentPadding: EdgeInsets.all(4.0),
title: Center(child: smallerBoldText('Select radius:', fontSize: 15.0)),
content: Column(
children: [
Divider(
color: Colors.grey,
height: 2.0,
),
CustomListTile(
label: '10 km',
selected: selected,
onSelect: () => _onSelect(selected),
),
CustomListTile(
label: '25 km',
selected: selected,
onSelect: () => _onSelect(selected),
),
CustomListTile(
label: '50 km',
selected: selected,
onSelect: () => _onSelect(selected),
),
CustomListTile(
label: '75 km',
selected: selected,
onSelect: () => _onSelect(selected),
),
],
),
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.all(Radius.circular(16.0))),
actions: [
FlatButton(
child: Text('Select',
style: TextStyle(
color: LegionColors.primaryRedHigh,
fontWeight: FontWeight.bold)),
onPressed: () {
Navigator.pop(context);
})
],
);
}
void _onSelect(bool value) {
setState(() {
if (selected == false) {
selected = true;
} else {
selected = false;
}
});
}
}用于选择的自定义listTile。
class CustomListTile extends StatelessWidget {
final String label;
final bool selected;
final Function onSelect;
const CustomListTile(
{Key key,
@required this.selected,
@required this.onSelect,
@required this.label})
: super(key: key);
@override
Widget build(BuildContext context) {
return Column(
children: [
ListTile(
title: Center(
child: Text(label,
style: selected == false
? TextStyle(color: Colors.black)
: TextStyle(
color: LegionColors.primaryRedHigh,
fontWeight: FontWeight.bold,
))),
selected: selected,
onTap: onSelect,
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.all(Radius.circular(16.0))),
),
Divider(
color: Colors.grey,
height: 2.0,
),
],
);
}
}发布于 2021-01-27 10:03:43
您正在使用相同的变量selected来标识是否为所有项选择了该项。下面是发生的事,
tapped
selected为了避免它
您可以将标识符移动到StatefulWidget.
Map implementation。 HashMap<String, bool> selectedState = new HashMap();
void updateSelectionStatus(String selectedTileId, bool status) {
setState(() {
selectedState[selectedTileId] = status;
});
}然后你可以像这样使用它
CustomListTile(
label: '75 km',
selected: selectedState['your-unique-id'],
onSelect: () => updateSelectionStatus('your-unique-id', !selectedState['your-unique-id']),
)发布于 2021-01-27 10:03:32
https://stackoverflow.com/questions/65916465
复制相似问题