Flutter bottomNavigationBar如何通过ontab在Flatter中重建相同的页面?

Flutter bottomNavigationBar如何通过ontab在Flatter中重建相同的页面?,flutter,flutter-bottomnavigation,Flutter,Flutter Bottomnavigation,我将底部导航栏制作为如下所示 List<Widget> pages = [ Dashboard(), AllServices(), InformationPage(), ]; int _selectedIndex = 0; Widget _bottomNavigationBar(int selectedIndex) { return BottomNavigationBar( onTap: (int index) =&

我将底部导航栏制作为如下所示

  List<Widget> pages = [
    Dashboard(),
    AllServices(),
    InformationPage(),
  ];


  int _selectedIndex = 0;

  Widget _bottomNavigationBar(int selectedIndex)  {
    return BottomNavigationBar(
      onTap: (int index) => setState(() => _selectedIndex = index),
      currentIndex: selectedIndex,
      type: BottomNavigationBarType.fixed,
      items: <BottomNavigationBarItem>[
        BottomNavigationBarItem(
          icon: new Icon(Icons.home),
          title:  new Text("Dashboard"),
        ),
        BottomNavigationBarItem(
          icon: new Icon(Icons.location_on),
          title: new Text("AllServices"),
        ),
        BottomNavigationBarItem(
          icon: new Icon(Icons.person),
          title:new Text("InformationPage"),
      ],
    );

  }

  @override
  Widget build(BuildContext context) {

    return Scaffold(
      bottomNavigationBar: _bottomNavigationBar(_selectedIndex),
      body: pages[_selectedIndex],
    );
  }
列表页=[
仪表板(),
所有服务(),
InformationPage(),
];
int _selectedIndex=0;
小部件底部导航栏(int-selectedIndex){
返回底部导航栏(
onTap:(int index)=>setState(()=>_selectedIndex=index),
currentIndex:selectedIndex,
类型:BottomNavigationBarType.fixed,
项目:[
底部导航气压计(
图标:新图标(Icons.home),
标题:新文本(“仪表板”),
),
底部导航气压计(
图标:新图标(图标位置打开),
标题:新文本(“所有服务”),
),
底部导航气压计(
图标:新图标(Icons.person),
标题:新文本(“信息页”),
],
);
}
@凌驾
小部件构建(构建上下文){
返回脚手架(
bottomNavigationBar:_bottomNavigationBar(_selectedIndex),
正文:页面[_selectedIndex],
);
}

如果用户在页面之间导航,一切都是正确的,但是AllServices类有两个条件来呈现两个不同的小部件,我想如果用户在AllServices页面中再次ontap AllServices bottom navigate AllServices页面再次重建,但ontap无法为同一页面再次重建,如何解决此问题?

只需检查下面是我制作的代码:


import 'package:flutter/material.dart';
import 'package:wrap_text/dashboard.dart';


void main() => runApp(MyApp());

class MyApp extends StatefulWidget {
  @override
  _MyAppState createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        body: Dashboard(),
      ),
    );
  }
}

进口“包装:颤振/材料.省道”;
导入“package:wrap_text/dashboard.dart”;
void main()=>runApp(MyApp());
类MyApp扩展了StatefulWidget{
@凌驾
_MyAppState createState()=>\u MyAppState();
}
类MyAppState扩展了状态{
@凌驾
小部件构建(构建上下文){
返回材料PP(
家:脚手架(
主体:仪表板(),
),
);
}
}
下面是我制作bootomAppbar的helper_小部件文件:

import 'package:flutter/material.dart';
import 'package:wrap_text/dashboard.dart';


Widget bottomAppBar(context) {
  return BottomAppBar(
    color: Colors.black,
    child: Container(
      child: Row(
        mainAxisSize: MainAxisSize.max,
        mainAxisAlignment: MainAxisAlignment.spaceAround,
        children: <Widget>[
          IconButton(
            iconSize: 30.0,
            icon: Icon(Icons.home,color: Colors.white,),
            onPressed: () {
              Navigator.push(
                context,
                MaterialPageRoute(builder: (context) => Dashboard()),
              );
            },
          ),
          IconButton(
            iconSize: 30.0,
            icon: Icon(Icons.location_on,color: Colors.white,),
            onPressed: () {
              Navigator.push(
                context,
                MaterialPageRoute(builder: (context) => AllServices()),
              );
            },
          ),
          IconButton(
            iconSize: 30.0,
            icon: Icon(Icons.settings,color: Colors.white,),
            onPressed: () {
              Navigator.push(
                context,
                MaterialPageRoute(builder: (context) => InformationPage()),
              );
            },
          ),
        ],
      ),
    ),
  );
}



// this are the different pages for just a sample.
 class AllServices extends StatelessWidget {
   @override
   Widget build(BuildContext context) {
     return Container(

     );
   }
 }



class InformationPage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Container(
      child: Center(child: Text('Information PAge')),
    );
  }
}

导入“包装:颤振/材料.省道”;
导入“package:wrap_text/dashboard.dart”;
小部件底部AppBar(上下文){
返回底部AppBar(
颜色:颜色,黑色,
子:容器(
孩子:排(
mainAxisSize:mainAxisSize.max,
mainAxisAlignment:mainAxisAlignment.spaceAround,
儿童:[
图标按钮(
iconSize:30.0,
图标:图标(Icons.home,颜色:Colors.white,),
已按下:(){
导航器。推(
上下文
MaterialPackageRoute(生成器:(上下文)=>Dashboard()),
);
},
),
图标按钮(
iconSize:30.0,
图标:图标(图标。位置打开,颜色:颜色。白色,),
已按下:(){
导航器。推(
上下文
MaterialPage路由(生成器:(上下文)=>AllServices()),
);
},
),
图标按钮(
iconSize:30.0,
图标:图标(图标。设置,颜色:Colors.white,),
已按下:(){
导航器。推(
上下文
MaterialPage路由(生成器:(上下文)=>InformationPage()),
);
},
),
],
),
),
);
}
//这是一个示例的不同页面。
类AllServices扩展了无状态小部件{
@凌驾
小部件构建(构建上下文){
返回容器(
);
}
}
类信息页扩展了无状态小部件{
@凌驾
小部件构建(构建上下文){
返回容器(
子:中心(子:文本(“信息页”),
);
}
}
这是仪表板页面:

import 'package:flutter/material.dart';
import 'package:wrap_text/helper_widget.dart';

class Dashboard extends StatefulWidget {
  @override
  _DashBoardState createState() => _DashBoardState();
}

class _DashBoardState extends State<Dashboard> {

  @override
  Widget build(BuildContext context) {
     print('fguyhg');
    return Scaffold(
      appBar: AppBar(title: Text('sample'),),
      bottomNavigationBar: bottomAppBar(context),
          body: Container(
        child: Center(child: Text('Dashboard Page')),
      ),
    );
  }
}
导入“包装:颤振/材料.省道”;
导入“package:wrap_text/helper_widget.dart”;
类Dashboard扩展StatefulWidget{
@凌驾
_DashBoardState createState()=>\u DashBoardState();
}
类_仪表板状态扩展状态{
@凌驾
小部件构建(构建上下文){
打印(“fguyhg”);
返回脚手架(
appBar:appBar(标题:文本('sample'),),
bottomNavigationBar:bottomAppBar(上下文),
主体:容器(
子:居中(子:文本(“仪表板页”),
),
);
}
}

解决方案对您有效吗。@sagaracharya感谢您的帮助,但Navigator.push不会提供类似于底部导航的onTap的操作