美文网首页
Flutter - 记录一下遇到的问题

Flutter - 记录一下遇到的问题

作者: 天明依旧 | 来源:发表于2021-06-22 16:55 被阅读0次
1.保持页面状态

在使用tabbar切换页面时发现每次页面都会被重置刷新,flutter中提供了AutomaticKeepAliveClientMixin帮我们进行页面状态的保存。前提是使用的页面是StatefulWidget,然后重写wantKeepAlive方法。

class HomePage extends StatefulWidget {
  @override
  _HomePageState createState() => _HomePageState();
}

class _HomePageState extends State<HomePage>
    with AutomaticKeepAliveClientMixin {
  @override
  bool get wantKeepAlive => true;

  @override
  void initState() {
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    super.build(context);
    return CupertinoPageScaffold(
        navigationBar: CupertinoNavigationBar(
          middle: Text("Cupertino Demo"),
        ),
        child: SafeArea(child: GridView()));
  }
}
2.There are multiple heroes that share the same tag within a subtree.

在我创建的底部导航栏中有俩个页面,它们都拥有自己的CupertinoNavigationBar,在使用第二个页面push到新页面时出现了如下错误:

════════ Exception caught by scheduler library ═════════════════════════════════
The following assertion was thrown during a scheduler callback:
There are multiple heroes that share the same tag within a subtree.
Within each subtree for which heroes are to be animated (i.e. a PageRoute subtree), each Hero must have a unique non-null tag.
In this case, multiple heroes had the following tag: Default Hero tag for Cupertino navigation bars with navigator NavigatorState#1fadc(tickers: tracking 2 tickers)

说的也很明确每个hero必须有一个唯一的非空标签,我在网上搜索了一下CupertinoNavigationBarFloatingActionButton一样都需要有一个唯一标识heroTag来区分,官方对于heroTag说明如下:

如果没有明确设置,那么每个路由(也就是每个屏幕)只能有一个FloatingActionButton,否则就会出现标签冲突(一个路由上的多个英雄不能有相同的标签)。材料设计规范建议每个屏幕只使用一个浮动动作按钮。

在添加了唯一的heroTag后又抛出了如下异常:

Cannot specify a heroTag override if this navigation bar does not transition due to transitionBetweenRoutes = false.

'package:flutter/src/cupertino/nav_bar.dart':

Failed assertion: line 243 pos 10: '!transitionBetweenRoutes || identical(heroTag, _defaultHeroTag)'

官方对于transitionBetweenRoutes说明如下:

是否在导航栏之间转换。

当transitionBetweenRoutes为真时,这个导航栏将过渡到路由的顶部而不是内部,如果路由被过渡到也有一个CupertinoNavigationBar或CupertinoSliverNavigationBar与transitionBetweenRoutes设置为真。

这种过渡也会发生在侧边向后滑动手势,就像在iOS上一样,但只有在下面的上一页在PageRoute上的maintainState设置为true的情况下。

当设置为true时,每个路由只能显示一个导航栏,除非同时设置了heroTag。

该值默认为true,不能为null。

我为我所有的CupertinoNavigationBar修改了heroTagtransitionBetweenRoutes这两个属性后暂时解决了这个问题;

相关文章

网友评论

      本文标题:Flutter - 记录一下遇到的问题

      本文链接:https://www.haomeiwen.com/subject/ofhdcltx.html