简体   繁体   English

如何将提供者变量从一个视图页面传递到另一个视图页面以更改其值

[英]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.

相关问题 如何将变量值从一个颤动页面传递到另一个页面 - how to pass a variable value from one flutter page to another 如何使用 Provider 将数据从 TextFieldInput 传递到另一个页面 - How can I pass data from a TextFieldInput to another Page with Provider 如何将数据从一个提供程序模型传递到另一个? - How to pass data from one provider model to another? 如何传递来自另一个提供者的值大于1的数据? - how to pass data from another provider with more than 1 value? 无法在 Flutter 中将变量值从一个无状态 Widget 传递到另一个 - Cannot pass variable value from one stateless Widget to another in Flutter Flutter,如何将值从一个 function 传递到另一页上的另一个 function - Flutter, How to pass value from one function to another function on a different page 在颤振中将一个变量值传递给另一个变量 - pass one variables value to another variable in flutter 如何在另一个变更通知程序 class 提供程序中使用来自一个变更通知程序 class 的方法 - How to use method from one Change notifier class in another change notifier class provider 如何使用 flutter 中的提供程序将数据从一个 model 传递到另一个 model - How to pass data from one model to another model using provider in flutter 如何在 flutter 上将字符串从一页传递到另一页? - How to pass a string from one page to another on flutter?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM