简体   繁体   English

Flutter BottomNavigationBar 调用导航器推送一个导航器项

[英]Flutter BottomNavigationBar call navigator push for one Navigator item

I have a BottomNavigationBar with 4 BottomNavigaton items and want 3 items to render different contents for the body, which works fine already.我有一个带有 4 个BottomNavigaton项目的BottomNavigationBar ,并且希望 3 个项目为正文呈现不同的内容,这已经很好了。 I want the first item to open the camera and link to a completely new page.我希望第一个项目打开相机并链接到一个全新的页面。 How can I do this?我怎样才能做到这一点?

What I already tried is attached below.我已经尝试过的内容附在下面。

I get errors like我收到类似的错误

setState() or markNeedsBuild() called during build.在构建期间调用 setState() 或 markNeedsBuild()。 This Overlay widget cannot be marked as needing to build because the framework is already in the process of building widgets.这个 Overlay 小部件不能被标记为需要构建,因为框架已经在构建小部件的过程中。

So I think I call the build method of CameraScreen too early, but I dont know how to avoid it.所以我觉得我调用CameraScreen的build方法太早了,但我不知道如何避免它。

class TabScreen extends StatefulWidget {
      int index;
    
      TabScreen(this.index);
      @override
      _TabScreenState createState() => _TabScreenState(index);
   }

class _TabScreenState extends State<TabScreen> {
  int _selectedPageIndex;

  _TabScreenState([this._selectedPageIndex = 1]);

  final List<Map<String, Object>> _pages = [
    {
      // index = 0 should push new Screen without appbar & bottom nav bar and open camera
      'page': null,
      'title': 'Cam',
    },
    {
      'page': ListScreen(),
      'title': 'List',
    },
    {
      'page': TransportScreen(),
      'title': 'Transport',
    },
    {
      'page': ExportScreen(),
      'title': 'Export',
    }
  ]; 

void _selectPage(int index, BuildContext ctx) {
      setState(() {
          _selectedPageIndex = index;
      });
      // this part does not work
      // if (_selectedPageIndex == 0){
      //   Navigator.of(ctx).pushNamed(CameraScreen.routeName);
      // }
  }

@override
  Widget build(BuildContext context) {

    final bottomBar = BottomNavigationBar(
      currentIndex: _selectedPageIndex,
      onTap: (i) => _selectPage(i, context),
      items: const <BottomNavigationBarItem>[
        BottomNavigationBarItem(
          icon: Icon(Icons.camera_alt_outlined),
          label: 'Cam',
          // backgroundColor: Colors.red,
        ),
        BottomNavigationBarItem(
          icon: Icon(Icons.article_outlined),
          label: 'List',
        ),
        BottomNavigationBarItem(
          icon: Icon(Icons.article_outlined),
          label: 'Transport',
        ),
        BottomNavigationBarItem(
          icon: Icon(Icons.arrow_forward),
          label: 'Export',
        ),
      ],
    );    

    return Scaffold(
      appBar: AppBar(
        title: Text(_pages[_selectedPageIndex]['title']),
      ),
      body: _pages[_selectedPageIndex]['page'],
      bottomNavigationBar: BottomAppBar(
        shape: CircularNotchedRectangle(),
        notchMargin: 4,
        clipBehavior: Clip.antiAlias,
        child: bottomBar,
      ),
      floatingActionButtonLocation: FloatingActionButtonLocation.centerDocked,
      floatingActionButton: _buildActionButton(),
  );
 }
}

Well I solved it myself.好吧,我自己解决了。 The above solution which uses Navigator.of(ctx).pushNamed(CameraScreen.routeName);上述使用Navigator.of(ctx).pushNamed(CameraScreen.routeName);的解决方案works indeed.确实有效。

My problem was in the CameraScreen File, which used some Widgets in its build function and became too large to fit on the screen.我的问题出在 CameraScreen 文件中,该文件在其构建 function 中使用了一些小部件,并且变得太大而无法显示在屏幕上。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM