如何将ProgressBar与StreamBuilder抖动

时间:2019-10-22 20:46:49

标签: flutter dart bloc

我有一个TextField,当调用onChanged时,我调用了在bloc的流控制器上调用.sink.add的bloc方法。返回的数据来自API。关键是,当用户更改输入时,我想在streamBuilder中显示一个圆形进度条,并关闭最后显示的数据。现在,Rigth刚好显示在用户写东西之前,而在输入东西后不再显示。

在这里放置我的小部件:

child: Column(
            children: <Widget>[
              SizedBox(height: 10,),
              Text("¡Escriba un número para obtener datos!",
              style: TextStyle(fontSize: 20, fontWeight: FontWeight.bold),),
              SizedBox(height: 25),
              Container(
                width: 200,
                child: TextField(
                  keyboardType: TextInputType.number,
                  decoration: InputDecoration(
                    border: OutlineInputBorder(borderRadius: BorderRadius.circular(5)),
                    hintText: "Ej: 20",
                  ),
                  onChanged: (input){
                    if(!input.isEmpty)
                      _bloc.getFacts(input);
                  },
                ),
              ),
              Container(
                child: StreamBuilder<String>(
                  initialData: "",
                  stream: _bloc.stream,
                  builder: (context, snapshot){

                    if(snapshot.hasError){
                      return _showDialog();
                    }
                    if(snapshot.connectionState == ConnectionState.waiting)
                      return CircularProgressIndicator();

                    if(snapshot.data.length == 0)
                      return Text("");

                    if(snapshot.hasData){
                      final facts = snapshot.data;
                      return Text(facts);
                    }

                    return CircularProgressIndicator();

                  },
                )
              ),
            ],
          ),
        ),
      ),
    ); 

这里是我的BlocMethod:

void getFacts(String numberString) async{
    _numberString = numberString;

    final isConnected = await ApiClient.isConnected();
    if(!isConnected)
      _streamController.add(Future.error("error"));


    final facts = await ApiClient.getFacts(numberString).catchError( (error) {
      _streamController.addError(error);
    });
    _streamController.sink.add(facts);
  }

0 个答案:

没有答案