flutter - pageview flutter如何跳转到另一个页面

标签 flutter navigation

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

  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);
}

将函数作为参数传递:

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),
      ],

关于flutter - pageview flutter如何跳转到另一个页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56540074/

相关文章:

flutter - 如何在flutter项目中使用flutter.widgets的fork?

ios - 适用于iOS的Flutter

user-interface - getter在flutter中的null上调用会显示错误屏幕

html - 删除导航文本下的行

css - 使用网站键盘的 Javascript 导航 - 需要帮助

flutter - 使用 'GeoLocator' 包时无法获取纬度和经度

Flutter - 在没有 AppBar 的情况下更改状态栏颜色

architecture - 一个页面如何发出消息以导航到 Elm 中的另一个页面?

javascript - addClass ('active' ) with window.location.pathname - url 没有文件扩展名

routing - 如何在 Flutter 中获取当前路由路径?