【发布时间】:2020-09-30 06:49:45
【问题描述】:
我有一个应用程序,它有一个基本页面,其中包含许多不同对象的 ListTile,这些对象基于全局主列表填充。如果用户单击其中一个 ListTile 磁贴,它将导航到一个新页面(第二页),其中包含有关该对象的特定信息。我还有左右按钮,允许用户导航全局列表而无需跳回主页。如果用户看到他们喜欢的对象,他们可以将其添加到他们的“已保存”列表中,这会将其从潜在对象的全局列表中删除,并将其添加到已保存列表中。在他们添加对象后,我使用 popUntil 返回到主页,但是“保存的”对象没有被删除。根据阅读下面的答案,我了解到我的问题可能与颤振无法识别列表中的更改因此不会重绘页面这一事实有关。我看到了一个关于添加多余计数器的建议,但无法弄清楚如何将它集成到我自己的 popUntil 实现中,因为这个问题稍微简单一些并且不使用 popUntil。当我 popUntil 我到达那里时,我应该怎么做才能强制应用重绘主页?
Force Flutter navigator to reload state when popping
这是第一页(假设我在到达这里之前将其命名为“主”路由。)
final List<Object> _potentials = List<Object>();
final List<Object> _saved = List<Object>();
class MainPage extends StatefulWidget{
@override
_MainPageState createState() => _MainPageState();
}
class _MainPageState extends State<MainPage> {
@override
build (BuildContext context) {
final tiles = _potentials.map(
(Object thing) {
return ListTile(
leading: Icon(Icons.person),
title: Text(thing.name),
onTap: () {
selectedThing = thing;
Navigator.of(context).push(
MaterialPageRoute(
builder: (context) => SecondPage()
)
);
},
);
},
);
... //there is more stuff here building out the tile views
}
}
这是 popUntil 所在的第二页
class SecondPage extends StatefulWidget{
@override
_SecondPageState createState() => _SecondPageState();
}
class _SecondPageState extends State<SecondPage>{
@override
build (BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(selectedThing.name),
leading: IconButton(icon: Icon(Icons.arrow_back), onPressed: () {
Navigator.popUntil(context, ModalRoute.withName('Main'));
}),
actions: [
IconButton(icon: Icon(Icons.person_add), onPressed: (){
_saved.add(selectedThing);
_potentials.remove(selectedThing);
Navigator.popUntil(context, ModalRoute.withName('Main'));
} ),
],
),
... //more stuff here that isn't important
// (including the left and right navigation buttons which take you to a new second page and update selectedThing)
}
}
【问题讨论】: