如何使Widget中的数据具有反应性?

时间:2019-08-21 09:52:09

标签: flutter

我有一个小部件,可以评估流中的数据。我想让此数据显示为反应性:

class BuilderLoading extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    TenderApiProvider apiProv = Provider.of<TenderApiProvider>(context);
    // apiProv.getToken();
    return StreamBuilder(
        stream: apiProv.resultController,
        builder: (BuildContext context, AsyncSnapshot snapshot) {
          String tokenLoadingState = '';
          String regionsLoadingState = '';
          String industryLoadingState = '';

          if (snapshot.data is ApiKeyLoadingState) {
            switch (snapshot.data) {
              case ApiKeyLoadingState.Progress:
                tokenLoadingState = "Loading";
                break;
              case ApiKeyLoadingState.Done:
                tokenLoadingState = "Done";
                break;
              case ApiKeyLoadingState.Error:
                tokenLoadingState = "Error";
                break;
              default:
                return Text("Unknown");
            }
          }

          else if (snapshot.data is RegionsLoadingState) {
            switch (snapshot.data) {
              case RegionsLoadingState.Progress:
                regionsLoadingState = "Loading";
                break;
              case RegionsLoadingState.Done:
                regionsLoadingState = "Done";
                break;
              case RegionsLoadingState.Error:
                regionsLoadingState = "Error";
                break;
              default:
                return Text("Unknown");
            }
          }

          else if (snapshot.data is IndustryLoadingState) {
            switch (snapshot.data) {
              case IndustryLoadingState.Progress:
                industryLoadingState = "Loading";
                break;
              case IndustryLoadingState.Done:
                industryLoadingState = "Done";
                break;
              case IndustryLoadingState.Error:
                industryLoadingState = "Error";
                break;
              default:
                return Text("Unknown");
            }    
          }       

          return Column(
            children: <Widget>[
              Text("Loading token $tokenLoadingState"),
              Text("Loading regions $regionsLoadingState"),
              Text("Loading industry $industryLoadingState"),
            ],
          );


        });
  }
}

但是逻辑上存在问题,我不知道如何解决。得到tokenLoadingState后,我想显示它的状态,而不是得到regionsLoadingState,我想一起显示tokenLoadingState和它。

但是似乎小部件每次都会重绘,而且我只能看到:

Loading token Done
Loading regions
Loading industry

然后:

Loading token 
Loading regions Done
Loading industry

然后

Loading token 
Loading regions
Loading industry Done

我还需要一种方法来评估所有条件是否都已完成并切换到HomePage();

2 个答案:

答案 0 :(得分:0)

如果要在所有条件均已满足的情况下导航到home,则必须在BLoC内创建另一个状态,因此,很明显,您将首先在条件树中对其进行检查,否则将不会执行。之后:

if(snapshot.data is AllConditionsDone){
  SchedulerBinding.instance.addPostFrameCallback((_) {
      Navigator.of(context).push('home');
  });
}

分享有关逻辑问题的更多信息,以便我们提供帮助。

答案 1 :(得分:0)

由于某种原因,您仅在每个分支中设置tokenLoadingStateregionsLoadingStateindustryLoadingState保持初始化为空字符串。

关于您评估多个条件的目标,我建议以下解决方案之一:

  • 将数据包装在包含所有三个状态的容器类中
  • 将每个状态分成不同的流,并使用嵌套的StreamBuilder