[英]how to pass a provider variable from one view page to another to change its value
I have a question about a task that I want to do.我有一个关于我想做的任务的问题。 I have a form which has several steps which are divided into several classes, the variables of the controllers are all housed in a provider in order to centralize all that information when going to validate all that, the doubt that I have is the following.我有一个表格,它有几个步骤,分为几个类,控制器的变量都放在一个提供者中,以便在验证所有这些时集中所有这些信息,我的疑问如下。 I want to factor my code by creating a class of some CheckboxListTile that I have, I want to create a class of this to generate instances of it, since my idea is to make a group of Checkboxes of the same type, my question is how do I pass a variable from a provider from one class to another class, and in the child class to be able to receive the state of the form provider, the variable, and to be able to modify the value of the variable within the onChanged function of the CheckboxListTile.我想通过创建一个我拥有的CheckboxListTile的类来分解我的代码,我想创建一个这样的类来生成它的实例,因为我的想法是制作一组相同类型的复选框,我的问题是如何我是否将变量从一个类的提供者传递到另一个类,并且在子类中能够接收表单提供者的状态,变量,并能够在 onChanged 函数中修改变量的值CheckboxListTile 的。
my idea is to go from something like this:我的想法是从这样的事情开始:
Container(
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(5),
border: Border.all(color: Colors.black38)),
constraints:
const BoxConstraints(maxWidth: 160, minWidth: 100),
child: CheckboxListTile(
value: provider.variable,
title: const Text('Lunes'),
onChanged: (value) {
provider.variable = !provider.variable;
setState(() {});
},
),
),
to something like this:像这样:
CheckboxListTileDays(provider: provider, variable: variable, text: 'Monday'),
CheckboxListTileDays(provider: provider, variable: variable, text: 'Tuesday '),
CheckboxListTileDays(provider: provider, variable: variable, text: 'Wednesday '),
CheckboxListTileDays(provider: provider, variable: variable, text: 'Thursday '),
CheckboxListTileDays(provider: provider, variable: variable, text: 'Friday'),
class CheckboxListTileDaysextends StatefulWidget {
const CheckboxListTileDays({
Key? key,
required this.provider,
required this.variable,
required this.text,
}) : super(key: key);
final ClassProvider provider;
final String variable;
final String text;
@override
State<CheckboxListTileDays> createState() => _CheckboxListTileDaysState();
}
class _CheckboxListTileDaysState extends State<CheckboxListTileDays> {
@override
Widget build(BuildContext context) {
return Container(
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(5),
border: Border.all(color: Colors.black38)),
constraints: const BoxConstraints(maxWidth: 160, minWidth: 100),
child: CheckboxListTile(
value: widget.provider.variable,
title: Text(widget.text),
onChanged: (value) {
setState(() {});
},
),
);
}
}
and clearly I'm not sure if this can be done in reality, maybe it can't be done exactly like that, I would like to know any information on how to do this or something similar with the obligation to use the provider.很明显,我不确定这是否可以在现实中完成,也许不能完全那样做,我想知道有关如何执行此操作的任何信息或与使用提供者的义务类似的信息。
There is no reason to pass either provider or value to a child, just read a value eg by using the extension methods.没有理由将提供者或值传递给孩子,只需读取一个值,例如通过使用扩展方法。
final String variable = context.read<ClassProvider>().variable;
...
final String variable = context.watch<ClassProvider>().variable;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.