gpt4 book ai didi

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

转载 作者:IT王子 更新时间:2023-10-29 07:11:17 27 4
gpt4 key购买 nike

您好,如果我按下第一个页面选项卡上的按钮,我试图跳转到第二个页面选项卡。目前我只知道我的第二页小部件的调用路由但 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/

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