Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/dart/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Flutter 颤振导航栏-从另一页更改选项卡_Flutter_Dart_Navigation - Fatal编程技术网

Flutter 颤振导航栏-从另一页更改选项卡

Flutter 颤振导航栏-从另一页更改选项卡,flutter,dart,navigation,Flutter,Dart,Navigation,我希望能够以编程方式更改导航栏选项卡。我在第1页中有一个按钮,可以导航到第2页。执行此操作时,导航栏将消失,因为我没有使用导航栏选择第2页 我有4个dart文件,分别是navigationbar.dart、page1.dart、page2.dart、page3.dart 导航页面是应用程序的主页,其中包含以下子项: final List<Widget> _children = [ Page1(), Page2(), Page3(), ];

我希望能够以编程方式更改导航栏选项卡。我在第1页中有一个按钮,可以导航到第2页。执行此操作时,导航栏将消失,因为我没有使用导航栏选择第2页

我有4个dart文件,分别是navigationbar.dart、page1.dart、page2.dart、page3.dart

导航页面是应用程序的主页,其中包含以下子项:

final List<Widget> _children = [
      Page1(),
      Page2(),
      Page3(),
    ];

return Scaffold(
      backgroundColor: Colors.black,
      body: _children[_selectedPage],
      bottomNavigationBar: _bottomNavigationBar(context),
      resizeToAvoidBottomPadding: true,
    );
final List\u children=[
第1页(),
第2页(),
第3页(),
];
返回脚手架(
背景颜色:Colors.black,
正文:_children[_selectedPage],
bottomNavigationBar:_bottomNavigationBar(上下文),
resizeToAvoidBottomPadding:true,
);
您可以使用

在按钮中:

onPressed: (){ 
    _currentIndex = 1;
   //or which value you want 
    setState((){});
}
在底部导航栏中:

currentIndex = _currentIndex
你得换个这样的发型

1*创建TabController实例

TabController _tabController;
2*在initState方法中使用

@override
void initState() {
    super.initState();
    _tabController = TabController(vsync: this, length: 3);
  }
3*添加一个Mixin到_HomeState

class _HomeState extends State<Home> with SingleTickerProviderStateMixin {....}
5*将控制器传递到您的页面

TabBarView(
    controller: _tabController,
    children:<Widget> [
  Page1(tabController:_tabController),
  Page2(tabController:_tabController),
  Page3(tabController:_tabController),
];
TabBarView(
控制器:\ tab控制器,
儿童:[
第1页(选项卡控制器:选项卡控制器),
第2页(选项卡控制器:选项卡控制器),
第3页(选项卡控制器:选项卡控制器),
];
6*从第1页调用tabController.animateTo()

class Page1 extends StatefulWidget {
final TabController tabController
Page1({this.tabController});
....}

class _Page1State extends  State<Page1>{
....
onButtonClick(){
  widget._tabController.animateTo(index); //index is the index of the page your are intending to (open. 1 for page2)
}
}
class Page1扩展了StatefulWidget{
最终选项卡控制器选项卡控制器
第1页({this.tabController});
....}
类_Page1State扩展状态{
....
onButtonClick(){
widget.\u tabController.animateTo(index);//index是您要打开的页面的索引(第2页打开.1)
}
}

希望有帮助

您想从第1页的按钮更改所选选项卡(导航到第2页)?是真的吗?是的,这很好,找到了。这很好!非常感谢
class Page1 extends StatefulWidget {
final TabController tabController
Page1({this.tabController});
....}

class _Page1State extends  State<Page1>{
....
onButtonClick(){
  widget._tabController.animateTo(index); //index is the index of the page your are intending to (open. 1 for page2)
}
}