[英]why radio button don't change color onPressed - Flutter?
I have 2 custom radio buttons inside my bottom modal sheet.我的底部模式表中有 2 个自定义单选按钮。 when i click
onPressed
method, they don't change color instantly.当我点击
onPressed
方法时,它们不会立即改变颜色。 After I close and open again the modal menu, color is set properly.在我关闭并再次打开模式菜单后,颜色设置正确。
Widget customRadio(String text, int index){
return OutlinedButton(
onPressed: () {
setState((){
selected = index;
});
},
child: Text(
text, style: TextStyle(color: (selected == index) ?Colors.white: Colors.grey),
),
style: OutlinedButton.styleFrom(
primary: Colors.white,
backgroundColor: (selected == index)?Colors.deepOrange: Colors.white,
),
);
}
Create new statefulWidget
for widget BottomSheet
, add int selected = 0;
为小部件
BottomSheet
创建新的statefulWidget
,添加int selected = 0;
move to DialogStatefull
移至
DialogStatefull
Try this demo;试试这个演示;
import 'package:flutter/material.dart';
class SaveScreen extends StatefulWidget {
const SaveScreen({Key? key}) : super(key: key);
@override
State<SaveScreen> createState() => _SaveScreenState();
}
class _SaveScreenState extends State<SaveScreen> {
@override
Widget build(BuildContext context) {
return Center(
child: ElevatedButton(
child: const Text('showModalBottomSheet'),
onPressed: () {
showModalBottomSheet<void>(
context: context,
builder: (BuildContext context) {
return DialogStatefull();
},
);
},
),
);
}
}
class DialogStatefull extends StatefulWidget {
const DialogStatefull({Key? key}) : super(key: key);
@override
State<DialogStatefull> createState() => _DialogStatefullState();
}
class _DialogStatefullState extends State<DialogStatefull> {
int selected = 0;
@override
Widget build(BuildContext context) {
return StatefulBuilder(builder: (context, state) {
return Container(
height: 200,
color: Colors.amber,
child: Center(
child: Column(
children: [
customRadio("helo", 0),
customRadio("helo", 1),
customRadio("helo", 2),
],
),
),
);
});
}
Widget customRadio(String text, int index) {
return OutlinedButton(
onPressed: () {
setState(() {
selected = index;
print(index);
print(selected);
});
},
child: Text(
text,
style:
TextStyle(color: (selected == index) ? Colors.white : Colors.grey),
),
style: OutlinedButton.styleFrom(
primary: Colors.white,
backgroundColor: (selected == index) ? Colors.deepOrange : Colors.white,
),
);
}
}
If you want bottomsheet on same screen then you need to use StatefulBuilder with StateSetter argumen如果您想在同一屏幕上显示底页,则需要将 StatefulBuilder 与 StateSetter 参数一起使用
int selected = 0;
Widget customRadio(
{required String text, required int index, Function()? onTap,
}) {
return OutlinedButton(
onPressed: onTap,
style: OutlinedButton.styleFrom(
primary: Colors.white,
backgroundColor: (selected == index) ? Colors.deepOrange : Colors.white,
),
child: Text(
text,
style:
TextStyle(color: (selected == index) ? Colors.white : Colors.grey),
),
);
}
pass the function as argument in your widget that you need to change the ui将函数作为参数传递给您需要更改 ui 的小部件
_showBottomSheet() {
return showModalBottomSheet(
context: context,
builder: (context) {
return StatefulBuilder(
builder: (BuildContext context, StateSetter setState) {
return Column(
children: [
customRadio(
text: 'button1',
index: 1,
onTap: () {
setState(() {
selected = 1;
});
},
),
customRadio(
text: 'button2',
index: 2,
onTap: () {
setState(() {
selected = 2;
});
},
),
],
);
});
},
);
}
here is the bottomsheet with list of widgets这是带有小部件列表的底页
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.