【问题标题】:How to preserve the state of a page如何保存页面的状态
【发布时间】:2022-01-08 15:26:18
【问题描述】:

如何在PageView 中保留页面状态,或者如何在与BottomNavigationBar 等一起使用时保留小部件的状态。

final List<Widget> _pages = const [
const Page1(key: Key("Page1"),),
const Page2(key: Key("Page2"),),
];

int _index=0;

 @override
 Widget build(BuildContext context) {
    return Scaffold(
      body: _pages[_index],
      bottomNavigationBar: BottomNavigationBar(
          currentIndex: _index,
          onTap: (index) => setState(() => _index = index),
          items: [
            BottomNavigationBarItem(
              icon: Icon(
                Icons.edit_outlined,
              ),
            ),
            BottomNavigationBarItem(                  
              icon: Icon(
                Icons.edit_outlined,
              ),),               
          ]),
    );

Page1()Page2() 从互联网获取数据。

当移动到上一页时,它再次调用API。

【问题讨论】:

标签: flutter dart


【解决方案1】:

您可以使用Indexed Stack 保留所有页面上的数据。

缺点

无论用户是否访问该页面,都会构建所有页面(子页面)。

替代品

您可以使用 PageStorageKey

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-09-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多