简体   繁体   English

如何通过提供程序在特定小部件中调用方法

[英]How to call method in specific widget by provider

Simple question for provider. 提供者的简单问题。

I have one screen name is intro.dart , it has 3 widgets in the widget tree. 我有一个屏幕名称是intro.dart ,它在小部件树中有3 个小部件。 Suppose widget "A", "B" and "C" all are stateLess widget. 假设小部件“ A”,“ B”和“ C”都是stateLess小部件。 I'm using 'Provider' package. 我正在使用“提供程序”包。 My question is, I just want to call method of "A" widget while clicked on on button of "C" widget. 我的问题是,我只想在单击“ C”小部件的按钮时调用“ A”小部件的方法。 how can I do by provider or Any other easy trick? 如何通过提供者或任何其他简单技巧来完成?

Well, that's impossible or at least wrongly designed. 好吧,那是不可能的,或者至少是错误设计的。 You are trying to violate the widget encapsulation and make them directly dependent. 您试图破坏小部件封装并使它们直接相关。

Depends on what exactly you want to achieve that should do the trick: 取决于要实现的目标到底该如何解决:

  1. Wrap all these A, B, C widgets with the provider (you said you are already using provider, but it doesn't mean you provide anything) intro.dart 将所有这些A,B,C小部件与提供程序包装在一起(您说过您已经在使用提供程序,但这并不意味着您提供了任何内容)
  2. In C widget consume the provider and call proper method (which would probably update some state) 在C小部件中使用提供程序并调用适当的方法(这可能会更新某些状态)
  3. A widget also consumes the state so it would be re-rendered automatically 小部件也会消耗状态,因此会自动重新渲染

If it doesn't solve your current situation - please update the question with exact problem you are facing. 如果仍不能解决您当前的情况-请使用您所遇到的确切问题来更新问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM