Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

使用PageViewListenerMixin 在切换时不会触发对应页面的didPageView方法 #16

Open
hmTest6 opened this issue Jun 10, 2021 · 0 comments

Comments

@hmTest6
Copy link

hmTest6 commented Jun 10, 2021

`点击tabbar` 切换到我的页面 didPageView 不调用 ,请问是什么原因导致的?

首页代码如下:
class TabNavigator extends StatefulWidget {
  final int index;
  TabNavigator({Key key, this.index = 0}) : super(key: key);

  @override
  _TabNavigatorState createState() => _TabNavigatorState();
}

class _TabNavigatorState extends State<TabNavigator> {
  final _defaultColor = Color(0xff979797); // icon默认颜色
  TabsModel model = TabsModel();
  final List<String> _appBarTitles = ['首页', '商品分类', '常用清单', '购物车', '个人中心'];

  List<BottomNavigationBarItem> __buildDarkBottomNavigationBarItem() {
    const _tabsItem = [
      [
        'assets/images/tabs/home_icon.png',
        'assets/images/tabs/home_active_icon.png'
      ],
      [
        'assets/images/tabs/shopping_list_icon.png',
        'assets/images/tabs/shopping_list_active_icon.png'
      ],
      [
        'assets/images/tabs/common_list_icon.png',
        'assets/images/tabs/common_list_active_icon.png'
      ],
      [
        'assets/images/tabs/shop_card_icon.png',
        'assets/images/tabs/shop_card_active_icon.png'
      ],
      [
        'assets/images/tabs/mine_icon.png',
        'assets/images/tabs/mine_active_icon.png'
      ]
    ];
    return List.generate(
        _tabsItem.length,
        (index) => BottomNavigationBarItem(
            icon: BadgeItem(
                index: index,
                child: Image.asset(
                  _tabsItem[index][0],
                  width: 24.0.w,
                  height: 24.0.h,
                  color: _defaultColor,
                )),
            activeIcon: BadgeItem(
                index: index,
                child: Image.asset(_tabsItem[index][1],
                    width: 24.0.w, height: 24.0.h)),
            title: Padding(
              padding: EdgeInsets.only(top: 4.0),
              child: Text(_appBarTitles[index]),
            )));
  }


  @override
  void initState() {
    controller = PageController(initialPage: widget.index);
    model.value = widget.index;
    super.initState();
  }

  @override
  void dispose() {
    super.dispose();
    controller.dispose();
  }

  @override
  Widget build(BuildContext context) {
    print(context);
    // 设计基于iPhone6(iPhone6 750*1334)的大小
    ScreenUtil.init(context, width: 375, height: 667, allowFontScaling: true);

    return DoubleTapBackExitApp(
      child: Scaffold(
          body: PageViewWrapper(
              changeDelegate: PageViewChangeDelegate(controller),
              pageAmount: 4,
              initialPage: controller.initialPage,
              child: PageView(
                controller: controller,
                physics: NeverScrollableScrollPhysics(),
                onPageChanged: (index) => model.value = index,
                children: <Widget>[
                  HomePage(index: 0),
                  GoodsListPage(index: 1),
                  CommonMainPage(index: 2),
                  ShoppingCartPage(index: 3),
                  MinePage(index: 4)
                ],
              )),
          bottomNavigationBar: ProviderWidget<TabsModel>(
            model: model,
            builder: (_, model, __) {
              return BottomNavigationBar(
                  currentIndex: model.value,
                  selectedFontSize: 10.0,
                  unselectedFontSize: 10.0,
                  unselectedItemColor: _defaultColor,
                  selectedItemColor: HmColors.colorMain,
                  onTap: (index) {
                    // 埋点
                    _eventTracking(index);
                    PageViewToPage.jumpToPage(index, context);
                  },
                  type: BottomNavigationBarType.fixed,
                  items: __buildDarkBottomNavigationBarItem());
            },
          )),
    );
  }
}

我的页面如下:

class MinePage extends StatefulWidget {
  final int index;
  MinePage({Key key, this.index}) : super(key: key);
  @override
  _MinePageState createState() => _MinePageState();
}

class _MinePageState extends State<MinePage>
    with
        AutomaticKeepAliveClientMixin<MinePage>,
        PageTrackerAware,
        PageViewListenerMixin {
  int get pageViewIndex => widget.index;

  MineModel model = MineModel();
  EasyRefreshController refreshController;

  @override
  void didPageView() {
    super.didPageView();
    PageViewEventTracking.pageEnter();
    model.load();
  }

  @override
  void didPageExit() {
    super.didPageExit();
    PageViewEventTracking.pageLeave(widget.index);
  }

  @override
  void initState() {
    super.initState();
    refreshController = EasyRefreshController();
  }

  @override
  void didChangeDependencies() {
    super.didChangeDependencies();
    Global.appPrecacheImage.precacheMineImages(context);
  }

  @override
  bool get wantKeepAlive => true;

  @override
  Widget build(BuildContext context) {
    super.build(context);
    double _mediaQueryTop = MediaQuery.of(context).padding.top; // padding高度
    return AnnotatedRegion(
      value: SystemUiOverlayStyle.dark,
      child: Scaffold(
          body: EasyRefresh(
        enableControlFinishRefresh: true,
        controller: refreshController,
        header: HmCustomHeader(
            bgColor: Colors.transparent,
            textColor: Colors.black87,
            extent: _mediaQueryTop + 60,
            triggerDistance: _mediaQueryTop + 70,
            showInfo: false),
        onRefresh: () async {
          await model.load();
          refreshController.finishRefresh();
        },
        child: Container(
          child: Column(
            children: <Widget>[
              ProviderWidget<MineModel>(
                model: model,
                onModelReady: (MineModel model) => model.load(),
                type: BaseType.NORMAL,
                child: Column(
                  children: <Widget>[
                    MineHeaderView(model: model),
                    MineOrderView(model: model),
                    CommonFunctionView(model: model),
                  ],
                ),
              ),
           
            ],
          ),
        ),
      )),
    );
  }
}

@hmTest6 hmTest6 changed the title PageViewListenerMixin 在切换时不会触发Tab的didPageView方法 使用PageViewListenerMixin 在切换时不会触发对应页面的didPageView方法 Jun 10, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant