如何跳至浏览量波动的另一页 [英] How to jump to another page with pageview flutter

查看:49
本文介绍了如何跳至浏览量波动的另一页的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,如果我按第一页选项卡上的按钮,则尝试跳至第二页选项卡.目前,我只知道第二个页面小部件的调用路由,但是不存在bottomnavbar ...我不知道如何从我的第一个页面选项卡调用父小部件以跳至第二个页面选项卡.

Hello I tried to jump to my second page tab if I press on a button on my first Page tab. Currently I know only call route of my seconde page widget but bottomnavbar isn't present... I don't know how to call my parent widget from my first page tab to jump to the seconde page tab.

  class Parent  {

  int bottomSelectedIndex = 0;

  List<BottomNavigationBarItem> buildBottomNavBarItems() {
    return [
      BottomNavigationBarItem(
          icon: new Icon(Icons.home),
          title: new Text('First')
      ),
      BottomNavigationBarItem(
        icon: new Icon(Icons.search),
        title: new Text('Second'),
      ),

    ];
  }

  PageController pageController = PageController(
    initialPage: 0,
    keepPage: true,
  );

  Widget buildPageView() {
    return PageView(
      controller: pageController,
      onPageChanged: (index) {
        pageChanged(index);
      },
      children: <Widget>[
        First(),
        Second(),

      ],
    );
  }

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

  void pageChanged(int index) {
    setState(() {
      bottomSelectedIndex = index;
    });
  }

  void bottomTapped(int index) {
    setState(() {
      bottomSelectedIndex = index;
      pageController.animateToPage(index, duration: Duration(milliseconds: 500), curve: Curves.ease);
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
      ),
      body: buildPageView(),
      bottomNavigationBar: BottomNavigationBar(
        currentIndex: bottomSelectedIndex,
        onTap: (index) {
          bottomTapped(index);
        },
        items: buildBottomNavBarItems(),
      ),
    );
  }
  }


  class first {
        return Container(
    // here a pressbutton for jump to the second widget
        );

    }

----------------------------------------------------------


    class second
        return Container(

        );
    }

推荐答案

您可以使用此

void onAddButtonTapped(int index) {

  // use this to animate to the page
  pageController.animateToPage(index);

  // or this to jump to it without animating
  pageController.jumpToPage(index);
}

将该函数作为参数传递:

Pass the function as params:

class first {
  final void Function(int) onAddButtonTapped;

        return Container(
// call it here onAddButtonTapped(2);
        );

    }

class Second {
  final void Function(int) onAddButtonTapped;

        return Container(
        );

    }

children: <Widget>[
        First(onAddButtonTapped),
        Second(onAddButtonTapped),
      ],

这篇关于如何跳至浏览量波动的另一页的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆