[英]How to use Provider for a Switch widget
From the examples and those am seeing online, it looks like by by using Provider, we can avoid using Stateful Widgets for Switch() toggling.从示例和在线看到的示例来看,通过使用 Provider,我们可以避免使用 Stateful Widgets 进行 Switch() 切换。 I could achieve this by using a Stateful widget by having a global variable which I could set via setState().
我可以通过使用一个有状态小部件来实现这一点,方法是使用一个全局变量,我可以通过 setState() 设置该变量。
An example: https://www.tutorialkart.com/flutter/flutter-switch/一个例子:https://www.tutorialkart.com/flutter/flutter-switch/
But can I achieve this as a stateless widget using Provider for a Switch Widget?但是我可以使用 Provider for a Switch Widget 作为无状态小部件来实现这一点吗?
This is my Switch widget:这是我的 Switch 小部件:
Switch(
inactiveTrackColor: Colors.greenAccent,
onChanged: (bool isNoti) {
Provider.of<Data>(context, listen: false).toggleNotification(isNotifiable: !isNoti);
print('Noti: $isNoti'); // always prints false
print('Noti: ${Provider.of<Data>(context, listen: false).isNotifiable}'); // always prints true
},
value: Provider.of<Data>(context, listen: false).isNotifiable, // value doesn't change thus no toggle
),
Data class used by Provider. Provider 使用的数据类。
class Data extends ChangeNotifier {
bool isNotifiable = false;
void toggleNotification({bool isNotifiable = true}) {
this.isNotifiable = isNotifiable;
notifyListeners();
}
}
Just to show it in completion, I do wrap it in ChangeNotifierProvider.只是为了在完成时显示它,我将它包装在 ChangeNotifierProvider 中。
class HomePage extends StatelessWidget {
@override
Widget build(BuildContext context) {
return ChangeNotifierProvider<Data>(
create: (context) => Data(),
child: ListTile(
leading: Icon(Icons.notifications),
title: Text('Notifications'),
trailing: Switch(
inactiveTrackColor: Colors.greenAccent,
onChanged: (bool isNoti) {
Provider.of<Data>(context, listen: false).toggleNotification(isNotifiable: !isNoti);
print('Noti: $isNoti');
print('Noti Provider: ${Provider.of<Data>(context, listen: false).isNotifiable}');
},
value: Provider.of<Data>(context, listen: false).isNotifiable,
),
),
);
}
}
Always get following output when I click the toggle.当我单击切换时,始终获得以下输出。
Noti: false
Noti Provider: true
Please advice.请指教。 Thank you.
谢谢你。
I think I found your mistake, You've done everything right expect your switch is not rebuilding and the mistake here is that you've mentioned so that it should not rebuild.我想我发现了你的错误,你做的一切都是正确的,希望你的交换机不会重建,这里的错误是你已经提到了,所以它不应该重建。
trailing: Switch(
inactiveTrackColor: Colors.greenAccent,
onChanged: (bool isNoti) {
Provider.of<Data>(context, listen: false).toggleNotification(isNotifiable: !isNoti);
print('Noti: $isNoti');
print('Noti Provider: ${Provider.of<Data>(context, listen: false).isNotifiable}');
},
value: Provider.of<Data>(context).isNotifiable, // remove `listen: false`
),
As value is what your switch depends on to rebuild.因为价值是您的交换机重建所依赖的。 But the provider is not listening to changed because you've mentioned
listen: false
.但是提供者没有监听更改,因为您已经提到了
listen: false
。
Use用
Provider.of<Data>(context, listen: false)
when you are reading data such as in callbacks. Provider.of<Data>(context, listen: false)
当您在回调中读取数据时。Provider.of<Data>(context)
use this when your widget needs to rebuild by listening to the changes Provider.of<Data>(context)
当您的小部件需要通过监听更改来重建时使用它
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.