你好,堆栈溢出我在未来构建器中使用pageview.builder时遇到了麻烦。它所翻阅的项目是卡片,我之前确实有这个功能,但在植入逻辑来获取笔记和其他东西时,我转而使用未来构建器,并将这个NoteSet移到它自己的类中。我坚持认为我在某个地方做了一个突破性的改变,但我不确定。奇怪的是,我可以构建并看到卡片。附带的手势检测器也能正常工作,但我不能在我的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,),
],
);
}
}
好吧,我找到了我的问题,每次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,),
],
);
}
}