简体   繁体   English

使用底部的应用程序栏选项卡导航-Flutter

[英]Navigate using bottom app bar tabs - flutter

I am using bottom app bar for bottomnavigation in flutter. 我正在使用底部应用程序栏在flutter中进行底部导航。 when tapped on one of the tab in the bottom app bar, i would still want the bottom app bar and app bar to remain as it is in it's fixed position but only the body content changes based on what is tapped. 当点击底部应用程序栏中的一个选项卡时,我仍然希望底部应用程序栏和应用程序栏保持其固定位置,但仅主体内容会根据所点击的内容而变化。

I have tried to use push() method but it gives me a new page instead with a back button. 我试图使用push()方法,但是它给了我一个新页面,而不是一个后退按钮。

Navigation_tabs.dart: Navigation_tabs.dart:

import 'package:flutter/material.dart';

class NavigationTabs extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return new Scaffold(
        floatingActionButtonLocation: FloatingActionButtonLocation.centerDocked,
        floatingActionButton: FloatingActionButton(
          child: const Icon(Icons.add),
          onPressed: () {},
        ),
        appBar: AppBar(
          title: Text('Dashboard'),
        ),
        bottomNavigationBar: BottomAppBar(
          shape: CircularNotchedRectangle(),
          notchMargin: 4.0,
          child: new Row(
            mainAxisSize: MainAxisSize.min,
            mainAxisAlignment: MainAxisAlignment.spaceBetween,
            children: <Widget>[
              IconButton(
                icon: Icon(
                  Icons.home,
                  color: Colors.cyan[700],
                ),
                onPressed: () {},
              ),
              new Container(
                  padding: EdgeInsets.only(left: 20),
                  child: IconButton(
                    icon: Icon(
                      Icons.list,
                      color: Colors.cyan[700],
                    ),
                    onPressed: () => Navigator.pushNamed(context, '/login'),
                  )),
              new Container(
                  padding: EdgeInsets.only(left: 120),
                  child: IconButton(
                    icon: Icon(
                      Icons.explore,
                      color: Colors.cyan[700],
                    ),
                    onPressed: () {},
                  )),
              new Container(
                  height: 22.0,
                  child: new RawMaterialButton(
                    onPressed: () {},
                    child: new Icon(
                      Icons.person,
                      color: Colors.white,
                      size: 20.0,
                    ),
                    shape: new CircleBorder(),
                    elevation: 1.0,
                    fillColor: Colors.cyan[700],
                  ))
            ],
          ),
        ));
  }
}

I want to be able to only make the page content change without a back button instead of going to a completely new page when one of the tabs is pressed. 我希望仅在不使用后退按钮的情况下更改页面内容,而不是在按下其中一个选项卡时转到全新的页面。

You can use a BottomNavigationBarItem instead of creating buttons and use the ontap of the bottomNavigationBar. 您可以使用BottomNavigationBarItem而不是创建按钮,并使用bottomNavigationBar的ontap。

class _MyHomePageState extends State<MyHomePage> {
  int _index = 0;
  final List<Widget> _children = [
    Center(child: Text("First Page"),),
    Center(child: Text("Second Page"),),
    Center(child: Text("Third Page"),)
  ];
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("Bottom Navigation"),
      ),
      body: Center(
        child: (_children[_index ]),
      ),
      bottomNavigationBar: BottomNavigationBar(
        onTap: onTabTapped,
        currentIndex: _currentIndex,
        items: [
          BottomNavigationBarItem(
            icon: Icon(Icons.looks_one),
            title: Text('First'),
          ),
          BottomNavigationBarItem(
            icon: Icon(Icons.looks_two),
            title: Text('Second'),
          ),
          BottomNavigationBarItem(
            icon: Icon(Icons.looks_3),
            title: Text('Third'),
          )
        ],
      ),
    );
  }

  void onTabTapped(int index) {
   setState(() {
     _index = index;
   });
 }
}

样品

For more detailed explanation: 有关更多详细说明:

Flutter documentation Flutter文档

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

相关问题 如何使用 Animation 底部导航栏导航 flutter - How to navigate using Animation Bottom Navigation Bar flutter 如何使用 flutter 中的底部导航栏导航到几个新页面? - How can i navigate to several new pages using the bottom navigation bar in flutter? 如何使用 flutter 中一个选项卡中的按钮在底部导航栏中的选项卡之间切换 - How to switch between tabs in bottom nav bar using button in one tab in flutter flutter 中的底部导航栏不会在点击选项卡时切换屏幕 - Bottom Navigation Bar in flutter not switching screens on tapping on the tabs 操作栏标签底部? - Action Bar Tabs at the bottom? 如何不使用 Android 中的底部导航栏导航回某些片段 - How to not navigate back to certain fragments using a bottom navigation bar in Android 在 Flutter 中为选项卡添加底部边框 - Add bottom border of to tabs in Flutter 样式工具栏,与底部应用栏一起使用 - style Toolbar for using with bottom app bar 使用片段通过底部导航栏关闭应用 - Close app by bottom navigation bar using fragment 当我在底部导航的屏幕之间导航时,我的 flutter 应用程序不断自我重建 - my flutter app keeps rebuilding itself when i navigate between screens on my bottom navigation
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM