gpt4 book ai didi

dart - 用于隐藏和取消隐藏小部件的 Bloc Pattern

转载 作者:IT王子 更新时间:2023-10-29 06:58:39 25 4
gpt4 key购买 nike

我正在尝试从互联网上获取一些数据并将其显示在列表中。

以下是我的bloc代码

class StudentsBloc {
final _repository = Repository();
final _students = BehaviorSubject<StudentModel>();

final BehaviorSubject<bool> _showProgress = BehaviorSubject<bool>();
final BehaviorSubject<bool> _showNoInternetViews = BehaviorSubject<bool>();

Observable<StudentModel> get students => _students.stream;
Observable<bool> get showProgress => _showProgress.stream;
Observable<bool> get showNoInternetViews => _showNoInternetViews.stream;

//FetchStudent from my Api
fetchStudents(String disciplineId, String schoolId, String year_id,
String lastIndex) async {
final student = await _repository.fetchStudents(
disciplineId, schoolId, year_id, lastIndex);
_students.sink.add(student);
}

//Check to see if user has internet or not
isNetworkAvailable(String disciplineId, String schoolId, String year_id,
String lastIndex) async {
checkInternetConnection().then((isAvailable) {
if (isAvailable) {
fetchStudents(disciplineId, schoolId, year_id, lastIndex);
} else {
_students.sink.addError(NO_NETWORK_AVAILABLE);
}
});
}

Function(bool) get changeVisibilityOfProgress => _showProgress.sink.add;
Function(bool) get changeVisibilityOfNoInternetViews =>
_showNoInternetViews.sink.add;

dispose() {
_students.close();
_showProgress.close();
_showNoInternetViews.close();
}
}

以下是我隐藏unide Widgets的主要代码

Widget buildList(StudentsBloc bloc) {
return StreamBuilder(
stream: bloc.students,
builder: (context, AsyncSnapshot<StudentModel> snapshot) {
if (snapshot.hasError) {
bloc.changeVisibilityOfProgress(false);
bloc.changeVisibilityOfNoInternetViews(true);

return StreamBuilder(
stream: bloc.showNoInternetViews,
builder: (context, snapshot) {
bool showNoInternetView = snapshot.hasData ?? false;

return Visibility(
child: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Text("No Network Available"),
RaisedButton(
onPressed: () {
fetchStudents();
},
child: Text("Retry"),
)
],
),
),
visible: showNoInternetView ? true : false,
);
},
);
}

if (snapshot.hasData) {
bloc.changeVisibilityOfProgress(false);
bloc.changeVisibilityOfNoInternetViews(false);

return Refresh(
year_id: "2",
schoolId: "1",
lastIndex: "0",
disciplineId: "1",
child: ListView.builder(
itemBuilder: (context, int index) {
return buildTile(
snapshot.data.toBuilder().data.studentData[index]);
},
itemCount: snapshot.data.toBuilder().data.studentData.length,
),
);
}

if (!snapshot.hasData) {
return StreamBuilder(
builder: (context, snapshot) {
bool showProgressIndicator = snapshot.data ?? false;

return Visibility(
child: Center(
child: CircularProgressIndicator(),
),
visible: showProgressIndicator ? true : false,
);
},
stream: bloc.showProgress,
);
}
},
);
}

buildList方法在Scaffoldbody中被调用

void fetchStudents() {
bloc?.changeVisibilityOfNoInternetViews(false);
bloc?.changeVisibilityOfProgress(true);
bloc?.isNetworkAvailable("1", "1", "2", "0");
}

假设用户在打开应用程序时有互联网,那么我能够看到 circularprogressindicator 然后数据列表可见但是假设在开始时打开应用程序并且用户没有互联网然后我显示无网络可用文本和重试按钮,现在,如果用户已连接到互联网,然后单击按钮重试,我会在几秒钟后直接看到数据列表,而不是 circularprogressindicator。我无法理解为什么 NoInternetviews 未隐藏,progressindicator 在显示数据列表之前单击重试按钮时显示。

我的流没有在调用重试按钮时得到更新。 StreamBuilder 中的 StreamBuilder 是否有任何注意事项?

我尝试按照@ivenxu 在回答中提到的那样更改 StreamBuilder 顺序,但它仍然不起作用。以下是附加代码的链接 https://drive.google.com/file/d/15Z8jXw1OpwTB1CxDS8sHz8jKyHhLwJp7/view?usp=sharing https://drive.google.com/open?id=1gIXV20S1o5jYRnno_NADabuIj4w163fF

最佳答案

在 View 层中,您可以使用Visibility() 小部件并在从Internet 加载数据时传递可见参数true 或false。让我们考虑如何从 bloc 更改可见变量。Visibility() 的父级小部件 StreamBuilder() 以流式传输更改数据。对于您的情况,您需要一个 PublishSubject 在您的 Bloc 内流式传输和添加新数据,并且 Observable 在您的小部件上流式传输此数据。

让我们展示一段代码来帮助您实现它

该 block 包含PublishSubjectObservable 以流式传输和添加数据

  //this Subject allows sending data, error and done events to the listener
final PublishSubject<bool> _progressStateSubject = new PublishSubject();

//the listener are streaming on changes
Observable<bool> get progressStateStream => _progressStateSubject.stream;

//to change your progress state
void changeProgressState({bool state}) => _progressStateSubject.sink.add(state);

在这里你可以改变你的进步状态

void callWebService() async {
//when you call your func to fetch your data change your state to true
changeProgressState(state: true);
.
.
.
if(response != null){
//when your call is done change the state to false
changeProgressState(state: false);
}
}

您的进度小部件是

// Loading Widget
Widget _buildLoadingWidget(Bloc bloc) {
return StreamBuilder<bool>(
stream: bloc.progressStateStream,//streaming on changes
builder: (context, snapshot) {
return Visibility(
visible: snapshot.data ?? false,//calling when data changes
child: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Text("Loading data from API...",
textDirection: TextDirection.ltr),
CircularProgressIndicator()
],
),
),
);
});
}

关于dart - 用于隐藏和取消隐藏小部件的 Bloc Pattern,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54476006/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com