我是Vaibhav Pathak。我在使用RadioListTile时遇到问题。我在onChnaged属性上创建了两个RadioListTile,以调用一个函数,其中在调用setState时将selectedRadio变量的值设置为我从该函数获得的值,并且它更改了变量值,但未反映对Ui的更改,意味着它确实不要选择按下的单选按钮。
我的代码在这里:
showPaymentOptions() {
showModalBottomSheet(
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.only(
topLeft: Radius.circular(25.0),
topRight: Radius.circular(25.0))),
elevation: 8.0,
context: context,
builder: (context) {
return Padding(
padding: const EdgeInsets.all(15.0),
child: Container(
height: 240,
child: Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: <Widget>[
Column(
children: <Widget>[
Align(
alignment: Alignment.topLeft,
child: Text(
"Select Payment Method : ",
style: TextStyle(
color: Colors.black54,
fontSize: 16.0,
fontWeight: FontWeight.bold),
),
),
SizedBox(
height: 7.0,
),
Card(
elevation: 8.0,
child: Padding(
padding: const EdgeInsets.all(10.0),
child: Column(
children: <Widget>[
RadioListTile(
activeColor: myTheme.primaryColor,
title: Text("COD"),
value: 1,
groupValue: selectedPaymentMethod,
onChanged: (value) =>
_changePayment(value)),
SizedBox(
height: 5.0,
),
RadioListTile(
activeColor: myTheme.primaryColor,
title: Text("Paytm"),
value: 2,
groupValue: selectedPaymentMethod,
onChanged: (value) =>
_changePayment(value)),
],
),
),
),
],
),
Padding(
padding: const EdgeInsets.all(10.0),
child: Stack(
alignment: Alignment.bottomCenter,
children: <Widget>[
Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: <Widget>[
Text(
"Pay ₹499",
style: TextStyle(
fontSize: 16.0,
fontWeight: FontWeight.bold),
),
isOrderSuccessful
? RaisedButton(
onPressed: () =>
placeOrder(selectedPaymentMethod),
color: myTheme.primaryColor,
textColor: Colors.white,
child: Text(
"Place Order",
style: TextStyle(
fontWeight: FontWeight.bold,
fontSize: 16.0),
))
: CircularProgressIndicator(
valueColor: AlwaysStoppedAnimation<Color>(
myTheme.primaryColor))
],
),
],
),
)
],
)),
);
});
}
setState函数的代码:
_changePayment(int value) {
print(value);
print(selectedPaymentMethod);
setState(() {
selectedPaymentMethod = value;
});
}
答案 0 :(得分:1)
朋友,我自己做过。我使用了StatefulBuilder
小部件,并将Column
包裹在两个孩子的列ReadioListTile
中,并在onChanged属性中放置了我从{ _setState
并将付款方式变量的值设置为其值,然后按我想要的方式正常工作。
答案代码:
StatefulBuilder