【发布时间】:2019-11-03 16:34:16
【问题描述】:
我正在使用 Flutter 构建一个多页面应用程序,但我似乎在错误地处理导航。我注意到,当我在屏幕之间导航时,它似乎只是不断地将页面推送到我的堆栈上,并且每次都“评估”整个堆栈。我有一个应用抽屉小部件,它包含在我的所有页面上,如下所示:
@override
Widget build(BuildContext context) {
AuthenticationContext authenticationContext =
AuthenticationContext.of(context);
auth = authenticationContext.auth;
var drawerOptions = <Widget>[];
for (var i = 0; i < drawerItems.length; i++) {
var d = drawerItems[i];
drawerOptions.add(new ListTile(
leading: new Icon(d.icon),
title: new Text(d.title),
selected: i == _selectedDrawerIndex,
onTap: () => _onSelectItem(i),
));
}
return new Drawer(
child: new Column(
children: <Widget>[
UserAccountsDrawerHeader(
accountName: Text(widget.name != null ? widget.name : ""),
accountEmail: Text(widget.email),
currentAccountPicture: CircleAvatar(
child: new Text(
widget.photoUrl == null ? widget.email[0].toUpperCase() : ""),
),
),
new Column(children: drawerOptions)
],
),
);
}
_getDrawerItemWidget(int pos) {
switch (pos) {
case 0:
return new HomePage();
case 1:
return new UserPantryPage();
case 2:
return new ShoppingListPage();
case 3:
return new FavoritesPage();
case 4:
auth.signOut();
return new RootPage();
default:
return new Text("Error");
}
}
_onSelectItem(int index) {
Navigator.of(context).pop();
Navigator.of(context)
.push(MaterialPageRoute<Null>(builder: (BuildContext context) {
return _getDrawerItemWidget(index);
}));
}
我的每个页面小部件中都有调试打印语句,如果我开始在抽屉上单击以在页面之间导航,我注意到它将开始从主页打印调试语句,即使我'正在导航到一个完全独立的页面。我最初注意到它是因为我的一个页面调用了一些 API 来获取其构建方法中的数据,而且我使用该应用程序的次数越多,调用的 API 就越多。我将其追溯到这样一个事实,即所有页面小部件的构建方法似乎都被调用,即使当我要去另一个页面时,在多页面导航方面,我在这里做错了什么吗?页面应用?
【问题讨论】:
标签: flutter