在显示的第一个页面上方,我显示了一条消息,用户可以向左滑动以转到下一页.在onPageChanged中,我设置了一个bool来忽略消息并调用setState.当我滑动到页面1时,在调用setState的时刻,页面重置为页面0.
当我删除条件文本时,页面正常滑动.
达特帕德主旨:https://dartpad.dev/?id=e30a98e4bc531d9cdc93780b6e47f972
这是一个错误,还是我错过了什么?
完整的示例代码如下:
import 'package:flutter/gestures.dart';
import 'package:flutter/material.dart';
void main() {
runApp(PageViewTestScreen());
}
class PageViewTestScreen extends StatefulWidget {
PageViewTestScreen({super.key});
@override
State<PageViewTestScreen> createState() => _PageViewTestScreenState();
}
class _PageViewTestScreenState extends State<PageViewTestScreen> {
PageController pageController = PageController(initialPage: 0);
bool lastPage = false;
bool userHasSlided = false;
int numberOfPages = 5;
@override
void initState() {
super.initState();
}
void onPageChanged(int index) {
userHasSlided = true;
lastPage = index == levensgebieden.length-1;
setState(() {});
}
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
floatingActionButton: lastPage
? FloatingActionButton(
onPressed: () {},
child: Icon(Icons.check),
)
: null,
appBar: AppBar(
foregroundColor: Colors.white,
title: const Text("PageView bug?"),
),
body: ListView(
children: [
const Text("Here are some pages"),
const SizedBox(height: 6),
if (!userHasSlided) const Text("Swipe left to continue."),
SizedBox(
height: 500,
child: PageView(
scrollBehavior: const ScrollBehavior().copyWith(dragDevices: {
PointerDeviceKind.touch,
PointerDeviceKind.mouse,
PointerDeviceKind.stylus,
}),
controller: pageController,
onPageChanged: (int index) {
onPageChanged(index);
},
children: [
for (int i = 0; i < 5; i++) card(i),
],
),
)
],
),
),
);
}
Widget card(int index) {
return Card(
child: SizedBox(
height: 500, width: 200, child: Center(child: Text("Page: $index"))),
);
}
}