Flutter 为什么我无法在Future builder中更改PageView.builder中的页面?

Flutter 为什么我无法在Future builder中更改PageView.builder中的页面?,flutter,dart,future,Flutter,Dart,Future,我在未来的生成器中使用pageview.builder时遇到问题。它正在分页的项目是卡片,我以前确实有这样的功能,但在实现逻辑以获取注释和内容时,我使用了一个未来的生成器,并将此注释集移动到它自己的类中。我想我在什么地方做了一个突破性的改变,但我不确定。奇怪的是,我可以制作并看到卡片。封闭的手势检测器也可以正常工作,但我无法翻阅我的pageview.builder 非常感谢您的帮助 class NoteSet extends StatefulWidget { VoidCallback cal

我在未来的生成器中使用pageview.builder时遇到问题。它正在分页的项目是卡片,我以前确实有这样的功能,但在实现逻辑以获取注释和内容时,我使用了一个未来的生成器,并将此注释集移动到它自己的类中。我想我在什么地方做了一个突破性的改变,但我不确定。奇怪的是,我可以制作并看到卡片。封闭的手势检测器也可以正常工作,但我无法翻阅我的pageview.builder

非常感谢您的帮助

class NoteSet extends StatefulWidget {
  VoidCallback callback;
  final Category category;

  NoteSet(this.category, this.callback);

  @override
  _NoteSetState createState() => _NoteSetState();
}

class _NoteSetState extends State<NoteSet> {
  List<NoteEntry> sortedNotes = [];
  double currentPage;
  bool largeCards = false;

  SortMethod currentSortMethod = SortMethod.CreatedDescending;

  Future<List<NoteEntry>> getNotes() async {
    return await readCategoryNotes(widget.category);
  }

  void sortNotes(){...}

  refresh() {...}

  deleteCategory() async {
    deleteCategoryDialog(context, widget.category, refresh);
  }

  void toggleModifiedSort(){
    currentSortMethod == SortMethod.ModifiedDescending ? currentSortMethod = SortMethod.ModifiedAscending : currentSortMethod = SortMethod.ModifiedDescending;
    refresh();
  }

  void toggleCreatedSort(){
    currentSortMethod == SortMethod.CreatedDescending ? currentSortMethod = SortMethod.CreatedAscending : currentSortMethod = SortMethod.CreatedDescending;
    refresh();
  }

  void toggleCardSize(){
    largeCards = !largeCards;
    refresh();
  }

  void moveThisCategoryUp() async {
    await moveCategoryUp(widget.category);
    refresh();
  }

  void moveThisCategoryDown() async {
    await moveCategoryDown(widget.category);
    refresh();
  }

  @override
  Widget build(BuildContext context) {
    PageController controller = PageController();
    currentPage = (sortedNotes.length - 1).roundToDouble();
    return Column(
      children: <Widget>[
        Padding(
          padding: EdgeInsets.symmetric(horizontal: 10.0),
          child: Row(
            mainAxisAlignment: MainAxisAlignment.spaceBetween,
            children: <Widget>[
              Text(widget.category.toString().split(".").last,
                style: TextStyle(
                  color: Colors.white,
                  fontSize: 46.0,
                  fontWeight: FontWeight.bold,
                  letterSpacing: 1.0,
                )
              ),
              PopupMenuButton<VoidCallback>(...),
            ],
          ),
        ),
        FutureBuilder(
          future: getNotes(),
          builder: (context, snapshot){
            if(snapshot.hasData){
              sortedNotes = snapshot.data;
              sortNotes();
              controller.addListener(() {
                setState(() {
                  currentPage = controller.page;
                });
              });
              return Column(
                children: <Widget>[
                  Padding(
                    padding: const EdgeInsets.only(left: 15.0),
                    child: Row(
                      children: <Widget>[
                        Text("${sortedNotes.length} Entries",
                            style: TextStyle(color: Colors.tealAccent[200]))
                      ],
                    ),
                  ),
                  Stack(
                    children: <Widget>[
                      CardScrollWidget(currentPage, sortedNotes, largeCards ? 0.8 : 1.2),
                      Positioned.fill(
                        child: GestureDetector(
                          onTap: () {
                            editNoteDialog(context, sortedNotes[currentPage.round()], refresh);
                          },
                          onLongPress: () {
                            deleteNoteDialog(context, sortedNotes[currentPage.round()], refresh);
                          },
                          child: ScrollConfiguration(
                            behavior: PageScrollBehavior(),
                            child: PageView.builder(
                              itemCount: sortedNotes.length,
                              controller: controller,
                              reverse: true,
                              itemBuilder: (context, index) {
                                return Container();
                              },
                            ),
                          ),
                        ),
                      )
                    ],
                  ),
                ],
              );
            }
            else{
              return Padding(
                padding: const EdgeInsets.all(20.0),
                child: Center(
                  child: SpinKitPulse(
                    color: Colors.tealAccent,
                    size: 60.0,
                  ),
                ),
              );
            }
          },
        ),
        SizedBox(height: 15,),
      ],
    );
  }
}
class注释集扩展StatefulWidget{
无效回调;
最终类别;
NoteSet(this.category,this.callback);
@凌驾
_NoteSetState createState();
}
类_notestState扩展状态{
列表排序的节点=[];
双当前页;
bool largeCards=错误;
SortMethod currentSortMethod=SortMethod.CreatedDescending;
Future getNotes()异步{
return wait readCategoryNotes(widget.category);
}
void sortNotes(){…}
刷新(){…}
deleteCategory()异步{
deleteCategoryDialog(上下文、widget.category、刷新);
}
void toggleModifiedSort(){
currentSortMethod==SortMethod.ModifiedSceneding?currentSortMethod=SortMethod.ModifiedSceneding:currentSortMethod=SortMethod.ModifiedSceneding;
刷新();
}
void toggleCreatedSort(){
currentSortMethod==SortMethod.CreatedEssending?currentSortMethod=SortMethod.CreatedEssending:currentSortMethod=SortMethod.CreatedEssending;
刷新();
}
void toggleCardSize(){
大卡片=!大卡片;
刷新();
}
void moveThisCategoryUp()异步{
wait moveCategoryUp(widget.category);
刷新();
}
void moveThisCategoryDown()异步{
等待moveCategoryDown(widget.category);
刷新();
}
@凌驾
小部件构建(构建上下文){
PageController=PageController();
currentPage=(sortedNotes.length-1).roundToDouble();
返回列(
儿童:[
填充物(
填充:边缘组。对称(水平:10.0),
孩子:排(
mainAxisAlignment:mainAxisAlignment.spaceBetween,
儿童:[
文本(widget.category.toString().split(“.”)。最后,
样式:TextStyle(
颜色:颜色,白色,
字体大小:46.0,
fontWeight:fontWeight.bold,
字母间距:1.0,
)
),
弹出菜单按钮(…),
],
),
),
未来建设者(
future:getNotes(),
生成器:(上下文,快照){
if(snapshot.hasData){
sortedNotes=快照数据;
sortNotes();
controller.addListener(){
设置状态(){
currentPage=controller.page;
});
});
返回列(
儿童:[
填充物(
填充:仅限常量边集(左:15.0),
孩子:排(
儿童:[
文本(${sortedNotes.length}项),
样式:TextStyle(颜色:Colors.tealacent[200]))
],
),
),
堆叠(
儿童:[
CardScrollWidget(currentPage、sortedNotes、largeCards?0.8:1.2),
定位填充(
儿童:手势检测器(
onTap:(){
editNoteDialog(上下文,sortedNotes[currentPage.round()],刷新);
},
onLongPress:(){
deleteNoteDialog(上下文,sortedNotes[currentPage.round()],刷新);
},
子:滚动配置(
行为:PageScrollBehavior(),
子项:PageView.builder(
itemCount:分拣长度,
控制器:控制器,
相反:是的,
itemBuilder:(上下文,索引){
返回容器();
},
),
),
),
)
],
),
],
);
}
否则{
返回填充(
填充:常数边集全部(20.0),
儿童:中心(
孩子:SpinKitPulse(
颜色:Colors.tealacent,
尺寸:60.0,
),
),
);
}
},
),
尺寸箱(高度:15,),
],
);
}
}

好的,所以我发现了我的问题,每次构建NoteSet时,它都会将currentPage变量设置为初始页面。我还删除了future builder,而是在init上调用future,然后在找到数据时刷新。这是我更正的代码:

class NoteSet extends StatefulWidget {
  VoidCallback callback;
  final Category category;

  NoteSet(this.category, this.callback);

  @override
  _NoteSetState createState() => _NoteSetState();
}

class _NoteSetState extends State<NoteSet> {
  List<NoteEntry> sortedNotes = [];
  double currentPage = 0;
  bool largeCards = false;
  bool dataReady = false;

  SortMethod currentSortMethod = SortMethod.CreatedDescending;

  Future getNotes() async {
    dataReady = false;
    refresh();
    sortedNotes = await readCategoryNotes(widget.category);
    currentPage = (sortedNotes.length - 1).roundToDouble();
    dataReady = true;
    refresh();
  }

  void sortNotes(){...}

  refresh() {
    widget.callback();
    this.setState((){});
    sortNotes();
  }

...

  @override
  void initState() {
    getNotes();
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return Column(
      children: <Widget>[
        Padding(
          padding: EdgeInsets.symmetric(horizontal: 10.0),
          child: Row(
            mainAxisAlignment: MainAxisAlignment.spaceBetween,
            children: <Widget>[
              Text(widget.category.toString().split(".").last,
                style: TextStyle(
                  color: Colors.white,
                  fontSize: 46.0,
                  fontWeight: FontWeight.bold,
                  letterSpacing: 1.0,
                )
              ),
              PopupMenuButton<VoidCallback>(
                onSelected: (VoidCallback value){
                  value();
                },
                icon: Icon(
                  Icons.more_horiz,
                  size: 30.0,
                  color: Colors.white,
                ),
                itemBuilder: (BuildContext context) => <PopupMenuEntry<VoidCallback>>[...],
              ),
            ],
          ),
        ),
        ((){
            if(dataReady){
              PageController controller = PageController(initialPage: sortedNotes.length - 1);
              controller.addListener(() {
                setState(() {
                  currentPage = controller.page;
                });
              });
              return Column(
                children: <Widget>[
                  Padding(
                    padding: const EdgeInsets.only(left: 15.0),
                    child: Row(
                      children: <Widget>[
                        Text("${sortedNotes.length} Entries",
                            style: TextStyle(color: Colors.tealAccent[200]))
                      ],
                    ),
                  ),
                  Stack(
                    children: <Widget>[
                      CardScrollWidget(currentPage, sortedNotes, largeCards ? 0.8 : 1.2),
                      Positioned.fill(
                        child: GestureDetector(
                          onTap: () {
                            editNoteDialog(context, sortedNotes[currentPage.round()], refresh);
                          },
                          onLongPress: () {
                            deleteNoteDialog(context, sortedNotes[currentPage.round()], refresh);
                          },
                          child: ScrollConfiguration(
                            behavior: PageScrollBehavior(),
                            child: PageView.builder(
                              itemCount: sortedNotes.length,
                              controller: controller,
                              reverse: true,
                              itemBuilder: (context, index) {
                                return Container();
                              },
                            ),
                          ),
                        ),
                      )
                    ],
                  ),
                ],
              );
            }
            else{
              return Padding(
                padding: const EdgeInsets.all(20.0),
                child: Center(
                  child: SpinKitPulse(
                    color: Colors.tealAccent,
                    size: 60.0,
                  ),
                ),
              );
            }
          }()),
        SizedBox(height: 15,),
      ],
    );
  }
}
class注释集扩展StatefulWidget{
无效回调;
最终类别;
NoteSet(this.category,this.callback);
@凌驾
_NoteSetState createState();
}
类_notestState扩展状态{
列表排序的节点=[];
双currentPage=0;
bool largeCards=错误;
booldataready=false;
SortMethod currentSortMethod=SortMethod.CreatedDescending;
Future getNotes()异步{
dataReady=false;
刷新();
sortedNotes=等待readCategoryNotes(widget.category);
currentPage=(sortedNotes.length-1).roundToDouble();
dataReady=true;
刷新();
}
void sortNotes(){…}
刷新(){