使用底部应用栏选项卡进行导航 - Flutter

3

我正在使用底部应用栏(bottom app bar)来在Flutter中实现底部导航。当点击底部应用栏的一个选项卡时,我希望底部应用栏和应用栏仍保持其固定位置,只有主体内容根据所选项目更改。

我尝试了push()方法,但它给我一个新页面,并带有一个返回按钮。

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],
                  ))
            ],
          ),
        ));
  }
}

我希望能够通过标签切换来实现仅更改页面内容,而不是通过后退按钮跳转到全新的页面。

请查看此帖子:https://medium.com/coding-with-flutter/flutter-case-study-multiple-navigators-with-bottomnavigationbar-90eb6caa6dbf - diegoveloper
您可以参考这个网站 底部导航 - Sean Ervinson
1个回答

1
你可以使用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;
   });
 }
}

Sample

更详细的解释请参考:

Flutter文档


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接