gpt4 book ai didi

flutter - 导航到另一个页面 View 或任何 View 时,Bloc 小部件引用技术数据(重建)

转载 作者:行者123 更新时间:2023-12-03 20:25:29 25 4
gpt4 key购买 nike

上下文:我有一个具有 PageView 的应用程序使用 BottomNavigationBar 在五个屏幕之间导航, 一个页面正在使用 Bloc 模式(在 future 版本中几乎所有页面都将使用 Bloc)这些 Bloc 从后端服务获取数据,并用获取的数据填充 UI。

问题:在小部件树的任何级别的页面之间导航时。 Bloc 小部件“重置”并从存储库中重新获取数据。

如下图所示:

Screenshot that shows the problem

小部件树如下:

  • main.dart
  • 家.dart
  • 时间线.dart(屏幕截图中显示的屏幕)

  • 尝试过的解决方案:我添加了 AutomaticKeepAliveClientMixin到页面类和其他页面,但它没有完成这项工作。

    这是上面屏幕截图中显示的页面的构建方法的代码
       @override
    Widget build(BuildContext context) {

    super.build(context);

    return DefaultTabController(
    length: 4,
    child: Scaffold(
    backgroundColor: Colors.white,
    appBar: _builAppBarWithTabs(),
    body: Center(
    child: ConstrainedBox(
    constraints: BoxConstraints(maxWidth: 1200),
    child: ListView(
    scrollDirection: Axis.vertical,
    children: <Widget>[
    Padding(
    padding: const EdgeInsets.only(bottom: 150),
    child: Container(
    height: 800,
    child: CustomScrollView(
    scrollDirection: Axis.vertical,
    slivers: <Widget>[
    SliverToBoxAdapter(
    child: MasonryGrid(
    column: getResponsiveColumnNumber(context, 1, 2, 6),
    children: <Widget>[
    // First Bloc
    BlocProvider(
    create: (context) {
    BrandBloc(repository: _brandRepository);
    },
    child: Container(
    width: 200,
    alignment: Alignment.center,
    height: 80,
    child: BrandScreen(
    brandBloc: context.bloc(),
    ),
    ),
    ),
    CategoryScreen(
    // Second Bloc
    categoryBloc: CategoryBloc(
    repository: context.repository(),
    ),
    ),
    Container(
    width: 200,
    alignment: Alignment.center,
    height: 330,
    child: _buildFeaturedItemsList(),
    ),
    Placeholder(
    strokeWidth: 0,
    color: Colors.white,
    )
    ],
    ),
    ),
    ],
    ),
    ),
    ),


    ],
    ),
    ),
    ),
    ),
    );
    }

    here是类 timeline.dart 的代码

    here是类 home.dart 的代码包含 BottomNavigationBar类(class)

    问题:每次小部件重建时如何阻止块获取数据?

    最佳答案

    我终于解决了这个问题
    事实证明,这与 bloc 无关。与 PageView 相关的问题.

    我包裹了 PageViewPageStorage小部件如下:

    final PageStorageBucket bucket = PageStorageBucket(); //PageStorageBucket
    static List<Widget> pages = [
    Timeline(pageController: pageController, key: PageStorageKey('timeline')),
    Search(key: PageStorageKey('search')),
    LikeScreen(key: PageStorageKey('like')),
    CartScreen(key: PageStorageKey('cart')),
    storageService.getFromDisk('api_token') != null
    ? ProfileScreen(key: PageStorageKey('profile'))
    : AuthChooserScreen(key: PageStorageKey('auth')),
    ];
    @override
    Widget build(BuildContext context) {
    super.build(context);
    return SafeArea(
    top: true,
    bottom: true,
    child: Scaffold(
    backgroundColor: Colors.white,
    key: _scaffoldKey,
    //Wrapped PageView with PageStorage
    body: PageStorage(
    bucket: bucket,
    child: PageView(
    children: pages,
    controller: pageController,
    onPageChanged: onPageChanged,
    physics: NeverScrollableScrollPhysics(),
    ),
    ),
    bottomNavigationBar: _buildBottomNavigationBar(),
    ),
    );
    }

    每个页面都分配有一个 PageStorageKey,用于保存和恢复可以超过小部件的值

    感谢这篇出色的中等文章,它完美地解释了它:
    Keeping State with the Bottom Navigation Bar in Flutter

    关于flutter - 导航到另一个页面 View 或任何 View 时,Bloc 小部件引用技术数据(重建),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62270666/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com