Switch widget will properly work with these. But if I uncomment that section, Switch widget will freeze.
I need to set Switch initial value based on arguments condition. How to do that?
Switch Widget将与这些组件一起正常工作。但如果我取消对该部分的注释,Switch Widget将冻结。我需要根据参数条件设置开关初始值。如何做到这一点?
class BookingPageState extends State<BookingPage> {
bool reqPickup = false;
Widget build(BuildContext context) {
/* priceData = ModalRoute.of(context)?.settings.arguments;
if(priceData) setState(() {reqPickup = true; }); */
return Switch.adaptive(
value: reqPickup,
onChanged:(bool value) {
setState(() {reqPickup = value; });
}
)
}
}
更多回答
The problem is how do you set the initial data of your switch because every time that you set a new state the build method executes again and set your variable with the screen args
You can solve this using the initState method of your statefulwidget like this
问题是如何设置开关的初始数据,因为每次设置新状态时,Build方法都会再次执行,并使用屏幕参数设置变量,您可以使用statefulidget的initState方法来解决这个问题,如下所示
class BookingPageState extends State<BookingPage> {
bool reqPickup = false;
@override
void initState() {
super.initState();
WidgetsBinding.instance.addPostFrameCallback((_) {
priceData = ModalRoute.of(context)?.settings.arguments;
if (priceData != null && mounted)
setState(() {
reqPickup = true;
});
});
}
Widget build(BuildContext context) {
return Switch.adaptive(
value: reqPickup,
onChanged:(bool value) {
setState(() {reqPickup = value; });
}
)
}
}
Here you go. I believe this is what you want. It is the minimum viable code, under 30 lines. I hope it works. Feel free to provide acknowledgement in the comment below.
这就是你要的。我相信这就是你想要的。这是最小的可行代码,不超过30行。我希望它能起作用。请随时在下面的评论中提供确认。
class BookingPage extends StatelessWidget {
const BookingPage({super.key});
static bool reqPickup = false;
@override
Widget build(BuildContext context) {
return Scaffold(
body: SafeArea(
child: StatefulBuilder(
builder: (BuildContext ctx, void Function(void Function()) setState) {
final Object? object = ModalRoute.of(context)?.settings.arguments;
final bool priceData = (object ?? false) as bool;
if (priceData) {
reqPickup = true;
setState(() {});
} else {}
return Switch.adaptive(
value: reqPickup,
onChanged: (bool value) {
reqPickup = value;
setState(() {});
},
);
},
),
),
);
}
}
Considering @sebastian-garzon comments, finally I found what I need. Thanks
考虑到@Sebastian-Garzon的评论,我终于找到了我需要的东西。谢谢
class BookingPageState extends State<BookingPage> {
bool reqPickup = false;
TextEditingController inputCtrl = TextEditingController();
@override
void initState() {
super.initState();
WidgetsBinding.instance.addPostFrameCallback((_) {
priceData = ModalRoute.of(context)?.settings.arguments;
if (priceData != null && mounted){
inputCtrl.text = priceData.price
setState(() { reqPickup = true; });
}
});
}
Widget build(BuildContext context) {
return Column(
children: [
TextFormField(
controller: inputCtrl
),
Switch.adaptive(
value: reqPickup,
onChanged:(bool value) {
setState(() {reqPickup = value; });
}
)
]
}
}
}
更多回答
But how if I want to use priceData in the Widget build? For example, I added TextFormField(initialValue: priceData).
但是,如果我想在Widget构建中使用priceData怎么办?例如,我添加了TextFormField(初始值:priceData)。
Very concise answer, but why all setState function are blank?
答案非常简洁,但是为什么所有的setState函数都是空的?
setState function will update the UI, even if it's blank. So, it means you can perform a few operations (e.g. update a variable or call another function) above the setState and then the setState. It'll work as expected.
SetState函数将更新UI,即使它为空。因此,这意味着您可以在setState和setState之上执行一些操作(例如,更新一个变量或调用另一个函数)。它会像预期的那样工作。
我是一名优秀的程序员,十分优秀!