flutter - Flutter:PageView保持停留在上一个屏幕
问题描述
我在屏幕中使用 PageView 导航到另一个屏幕。我有 3 个屏幕Home
,History
并且History Detail
.
一切都很好,我可以移动到history
屏幕上。直到我History Detail
从History
屏幕重定向到并返回Navigator.of(context).pop
,我重定向到Home
屏幕。我想从History Detail
我回来后一直留在History
屏幕上。
我已经将 initialPage 初始化为0,并更改了 initialPageonPageChanged
但没有任何反应,我仍然重定向到Home
屏幕。
我该怎么办?
这是我的源代码。
PageController _pageController;
int currentPage = 0;
@override
void initState() {
_pageController = PageController(initialPage: currentPage);
super.initState();
}
Widget build(BuildContext context) {
final GlobalKey<ScaffoldState> _scaffoldKey = GlobalKey<ScaffoldState>();
double mqHeight = MediaQuery.of(context).size.height;
return Scaffold(
key: _scaffoldKey,
body: FutureBuilder(
future: Hive.openBox("debt_box"),
builder: (ctx, snapshot) {
if (snapshot.connectionState == ConnectionState.done) {
if (snapshot.hasError)
return Text(snapshot.error.toString());
else
return Scaffold(
body: PageView(
scrollDirection: Axis.vertical,
onPageChanged: (index) {
currentPage = index;
print(currentPage);
},
physics: NeverScrollableScrollPhysics(),
controller: _pageController,
children: <Widget>[
CustomScrollView(
slivers: <Widget>[
SliverAppBarHome(),
SliverListHome(_scaffoldKey),
],
),
HistoryDebt(),
],
),
);
} else {
return Center(child: CircularProgressIndicator());
}
},
),
bottomNavigationBar: BottomAppBar(
shape: CircularNotchedRectangle(),
child: Container(
height: mqHeight * 0.08,
child: Row(
mainAxisSize: MainAxisSize.max,
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: <Widget>[
IconButton(
icon: Icon(Icons.home),
onPressed: () =>
functionHelper.previousButtonPageView(_pageController),
),
IconButton(
icon: Icon(Icons.insert_chart),
onPressed: () =>
functionHelper.nextButtonPageView(_pageController),
),
],
),
),
),
}
解决方案
我发现了这个问题。
当您按下后退按钮时,它会重新构建您的主小部件。在build
您拥有的方法中FutureBuilder
,它也可以重建。
所以它会告诉你CircularProgressIndicator
。由于Hive.openBox("debt_box")
立即给出结果,因此您无法看到它。
在显示CircularProgressIndicator
您PageView
已停用(从小部件树中删除)时。所以_pageController
失去了它的客户。
之后,将创建CircularProgressIndicator
一个新的。PageView
这就是为什么它显示Home
页面
解决方案:
根据文档,我们不应该future: Hive.openBox("debt_box")
在构建期间直接分配。而是创建一个Future
变量并在 中对其进行初始化initState
,然后将该变量传递future
给FutureBuilder
PageController _pageController;
int currentPage = 0;
Future _openBox;
@override
void initState() {
_pageController = PageController(initialPage: currentPage);
_openBox = Hive.openBox("debt_box"); //initialize here
super.initState();
}
Widget build(BuildContext context) {
final GlobalKey<ScaffoldState> _scaffoldKey = GlobalKey<ScaffoldState>();
double mqHeight = MediaQuery.of(context).size.height;
return Scaffold(
key: _scaffoldKey,
body: FutureBuilder(
future: _openBox, //pass the future variable here
builder: (ctx, snapshot) {
...
推荐阅读
- mysql - 如何在 JSON_OBJECT 中使用 JSON_ARRAYAGG 而不转义内容
- c++ - 为什么函数没有返回字符串?
- json - 在 Arduino 上解析传入的 HTTP 发布请求的最佳方法?
- javascript - 如何检查深度嵌套的对象未定义?
- python - 在 Python 中更改 pandas DataFrame 中的值
- sql - 构建Sql查询字符串处理
- delphi - 在文件夹中搜索大量特定文件名
- anaconda - 使用 conda 成功安装 cgal 后出现 ModuleNotFound 错误?
- shell - 得到:意外的 URISyntaxException
- kubernetes - Kubernetes:通过 traefik 入口控制器处理集群中多个 LoadBalancer 的连接