如何取消选择 RadioListTile Flutter
How to unselect RadioListTile Flutter
我想通过另一个按钮取消选择带有 onPress 的 RadioListTile。这是我的代码,有人可以帮忙吗?
组值和值是从服务器动态获取的!
RadioListTile(
groupValue: _con.cart.tips.toString(),
title: Row(
children: <Widget>[
Expanded(
flex: 3,
child: Column(
crossAxisAlignment:
CrossAxisAlignment.start,
children: <Widget>[
Text(
_con.cart.defaultTipsOptions[index]
.toString(),
style: TextStyle(
fontSize: 16,
color:
_con.cart.defaultTipsOptions[
index] ==
_con.cart.tips
? Theme.of(context)
.accentColor
: Colors.blueGrey),
),
],
),
),
],
),
value: _con.cart.defaultTipsOptions[index]
.toString(),
onChanged: (val) {
state(() {
_selectedAmount =
double.parse(val).toString();
_con.cart.tips = double.parse(val);
});
},
activeColor: Theme.of(context).accentColor),
将 _value
传递给您的 groupValue。您可以将其初始化为您返回的数据。
然后在按下按钮时清除值,例如:
class _MyStatefulWidgetState extends State<MyStatefulWidget> {
double _value = 1;
double option1 = 1;
double option2 = 2;
@override
Widget build(BuildContext context) {
return Column(
children: <Widget>[
RadioListTile(
groupValue: _value.toString(),
title: Row(
children: <Widget>[
Expanded(
flex: 3,
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Text(
option1.toString(),
style: TextStyle(
fontSize: 16,
),
),
],
),
),
],
),
value: option1.toString(),
onChanged: (val) {
setState(() {
_value = double.parse(val);
});
},
activeColor: Theme.of(context).accentColor,
),
RadioListTile(
groupValue: _value.toString(),
title: Row(
children: <Widget>[
Expanded(
flex: 3,
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Text(
option2.toString(),
style: TextStyle(
fontSize: 16,
),
),
],
),
),
],
),
value: option2.toString(),
onChanged: (val) {
setState(() {
_value = double.parse(val);
});
},
activeColor: Theme.of(context).accentColor,
),
RaisedButton(
child: Text('Unselect'),
onPressed: () {
setState(() {
_value = null;
});
},
)
],
);
}
}
我想通过另一个按钮取消选择带有 onPress 的 RadioListTile。这是我的代码,有人可以帮忙吗?
组值和值是从服务器动态获取的!
RadioListTile(
groupValue: _con.cart.tips.toString(),
title: Row(
children: <Widget>[
Expanded(
flex: 3,
child: Column(
crossAxisAlignment:
CrossAxisAlignment.start,
children: <Widget>[
Text(
_con.cart.defaultTipsOptions[index]
.toString(),
style: TextStyle(
fontSize: 16,
color:
_con.cart.defaultTipsOptions[
index] ==
_con.cart.tips
? Theme.of(context)
.accentColor
: Colors.blueGrey),
),
],
),
),
],
),
value: _con.cart.defaultTipsOptions[index]
.toString(),
onChanged: (val) {
state(() {
_selectedAmount =
double.parse(val).toString();
_con.cart.tips = double.parse(val);
});
},
activeColor: Theme.of(context).accentColor),
将 _value
传递给您的 groupValue。您可以将其初始化为您返回的数据。
然后在按下按钮时清除值,例如:
class _MyStatefulWidgetState extends State<MyStatefulWidget> {
double _value = 1;
double option1 = 1;
double option2 = 2;
@override
Widget build(BuildContext context) {
return Column(
children: <Widget>[
RadioListTile(
groupValue: _value.toString(),
title: Row(
children: <Widget>[
Expanded(
flex: 3,
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Text(
option1.toString(),
style: TextStyle(
fontSize: 16,
),
),
],
),
),
],
),
value: option1.toString(),
onChanged: (val) {
setState(() {
_value = double.parse(val);
});
},
activeColor: Theme.of(context).accentColor,
),
RadioListTile(
groupValue: _value.toString(),
title: Row(
children: <Widget>[
Expanded(
flex: 3,
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Text(
option2.toString(),
style: TextStyle(
fontSize: 16,
),
),
],
),
),
],
),
value: option2.toString(),
onChanged: (val) {
setState(() {
_value = double.parse(val);
});
},
activeColor: Theme.of(context).accentColor,
),
RaisedButton(
child: Text('Unselect'),
onPressed: () {
setState(() {
_value = null;
});
},
)
],
);
}
}