在Flutter中使用StreamBuilder的PageView PageController.jumpPage

时间:2020-09-02 19:17:41

标签: flutter dart flutter-layout

这是代码。

Widget build(BuildContext context) {
    return StreamBuilder<List<Widget>>(
        stream: _getMealsViewWidgets(weekNum),
        builder: (BuildContext context, AsyncSnapshot<List<Widget>> snapshot) {
          if (snapshot.connectionState == ConnectionState.active) {
            return Center(child: CircularProgressIndicator());
          }

          if (snapshot.connectionState == ConnectionState.waiting) {
            return Center(child: CircularProgressIndicator());
          }

          if (snapshot.error != null) {
            return Center(child: Text('Error occurred.'));
          }

          if (snapshot.connectionState == ConnectionState.done) {
            return PageView(children: snapshot.data, controller: _pageController);
          }
          return Center(child: CircularProgressIndicator());
        });
  }

PageView修复了7页。我想在刷新到特定页面后跳转。问题是因为PageView是StreamBuilder的返回,刷新后我无法调用pagecontroller.jumpPage方法。返回后如何跳转到特定页面?

1 个答案:

答案 0 :(得分:0)

您尝试过使用

WidgetsBinding.instance.addPostFrameCallback((_) {
  // executes after build
})