Provider 中是否有 属性 明智的 notifyListerners 选项?
Is there any option for property wise notifyListerners in Provider?
我正在开发一个使用提供程序包进行状态管理的待办事项列表应用程序。在任务创建屏幕中,我有像
这样的小部件
- 任务名称
- 待办事项类型
- 任务颜色选择器
- 日期和时间
- 保存按钮
任务模型
class Task with ChangeNotifier {
String _name;
String _type;
Color _color;
String get name => _name;
set name(String name) {
_name = name;
notifyListeners();
}
Color get color => _color;
set color(Color color) {
_color = color;
notifyListeners();
}
String get type => _type;
set type(String type) {
_type = type;
notifyListeners();
}
}
我正在这样使用ChangeNotificationprovider
ChangeNotifierProvider<Task>.value(
value: Task(),
child: Consumer<Task>(
builder: (context, task, _) {
return Scaffold(...
...
NameWidget(),
ColorWidget(),
TypeWidget(),
.....
所以每个小部件都会更改任务模型的各个字段,但我面临的问题是每当一个小部件更新任务模型的一个字段时,Consumer
下的所有小部件都会更新,就像每当我update color
字段应用程序不仅会刷新颜色字段,还会刷新所有其他字段。是否有任何其他方法来设计此提供程序架构,例如仅向特定字段侦听器发送通知?
这是我试过的。
我没有将 Task
设为 ChangeNotifier
,而是尝试将每个字段创建为单独的 class 和 ChangeNotifier。比如name
个字段变成这样
class Task {
Name _name;
}
class Name with ChangeNotifier {
String _name;
}
但这似乎是太多的样板代码。
这不是最优雅的解决方案,但有效
首先创建一个 class 接受一个扩展 changeNotifier 的动态类型变量。
class NotifiedVariable<T> with ChangeNotifier {
T _value;
NotifiedVariable(this._value);
T get value => _value;
set value(T value) {
_value = value;
notifyListeners();
}
}
您现在可以将所有变量的类型设置为此
class c {
NotifiedVariable<int> integer;
NotifiedVariable<string> stringVal;
c(int integer, string stringVal) {
this.integer = NotifiedVariable<int>(integer);
this.stringVal = NotifiedVariable<string>(stringVal);
}
}
现在你可以注入这个class,我用get_it在别处获取值。使用 provider 作为其需要的正上方的值。如果需要多个值,这可能仍然不起作用。我建议制作另一个 class 并从 class c.
继承必要的值
ChangeNotifierProvider<T>.value(
value: locator.get<c>().integer,
child: Consumer<T>(
builder: (context, NotifiedVariable variable, child) => Widget(v: variable.value),
),
);
这是一种 hack,所以我建议研究更优雅的方法。
我正在开发一个使用提供程序包进行状态管理的待办事项列表应用程序。在任务创建屏幕中,我有像
这样的小部件- 任务名称
- 待办事项类型
- 任务颜色选择器
- 日期和时间
- 保存按钮
任务模型
class Task with ChangeNotifier {
String _name;
String _type;
Color _color;
String get name => _name;
set name(String name) {
_name = name;
notifyListeners();
}
Color get color => _color;
set color(Color color) {
_color = color;
notifyListeners();
}
String get type => _type;
set type(String type) {
_type = type;
notifyListeners();
}
}
我正在这样使用ChangeNotificationprovider
ChangeNotifierProvider<Task>.value(
value: Task(),
child: Consumer<Task>(
builder: (context, task, _) {
return Scaffold(...
...
NameWidget(),
ColorWidget(),
TypeWidget(),
.....
所以每个小部件都会更改任务模型的各个字段,但我面临的问题是每当一个小部件更新任务模型的一个字段时,Consumer
下的所有小部件都会更新,就像每当我update color
字段应用程序不仅会刷新颜色字段,还会刷新所有其他字段。是否有任何其他方法来设计此提供程序架构,例如仅向特定字段侦听器发送通知?
这是我试过的。
我没有将 Task
设为 ChangeNotifier
,而是尝试将每个字段创建为单独的 class 和 ChangeNotifier。比如name
个字段变成这样
class Task {
Name _name;
}
class Name with ChangeNotifier {
String _name;
}
但这似乎是太多的样板代码。
这不是最优雅的解决方案,但有效
首先创建一个 class 接受一个扩展 changeNotifier 的动态类型变量。
class NotifiedVariable<T> with ChangeNotifier {
T _value;
NotifiedVariable(this._value);
T get value => _value;
set value(T value) {
_value = value;
notifyListeners();
}
}
您现在可以将所有变量的类型设置为此
class c {
NotifiedVariable<int> integer;
NotifiedVariable<string> stringVal;
c(int integer, string stringVal) {
this.integer = NotifiedVariable<int>(integer);
this.stringVal = NotifiedVariable<string>(stringVal);
}
}
现在你可以注入这个class,我用get_it在别处获取值。使用 provider 作为其需要的正上方的值。如果需要多个值,这可能仍然不起作用。我建议制作另一个 class 并从 class c.
继承必要的值ChangeNotifierProvider<T>.value(
value: locator.get<c>().integer,
child: Consumer<T>(
builder: (context, NotifiedVariable variable, child) => Widget(v: variable.value),
),
);
这是一种 hack,所以我建议研究更优雅的方法。