flutter3.0学习笔记

FancyBottomNavigation

Preview

要使用FacyBottomNavigation,需要在项目中声明此库。我们可以打开flutter插件库https://pub.dev/ 搜索FacyBottomNavigation,选一个自己喜欢,然后在pubspect.yaml文件并添加以下:

image.png

dependencies:
  ...
  fancy_bottom_navigation_2: ^0.3.5
import 'package:fancy_bottom_navigation_2/fancy_bottom_navigation.dart';
//···省略多余代码
class MyHomePage extends StatefulWidget {
  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  int currentPage = 0;

  GlobalKey bottomNavigationKey = GlobalKey();

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text("Fancy Bottom Navigation"),
      ),
      body: Container(
        decoration: const BoxDecoration(color: Colors.white),
        child: Center(
          child: _getPage(currentPage),
        ),
      ),
      bottomNavigationBar: FancyBottomNavigation(
        tabs: [
          TabData(
              iconData: Icons.home,
              title: "Home",
              onclick: () {
                final FancyBottomNavigationState fState = bottomNavigationKey
                    .currentState as FancyBottomNavigationState;
                fState.setPage(2);
              }),
          TabData(
              iconData: Icons.search,
              title: "Search",
              onclick: () => Navigator.of(context)
                  .push(MaterialPageRoute(builder: (context) => TextPage()))),
          TabData(iconData: Icons.shopping_cart, title: "Basket")
        ],
        initialSelection: 1,
        key: bottomNavigationKey,
        onTabChangedListener: (position) {
          setState(() {
            currentPage = position;
          });
        },
      ),
      drawer: Drawer(
        child: ListView(
          children: const <Widget>[Text("Hello"), Text("World")],
        ),
      ),
    );
  }

  _getPage(int page) {
    switch (page) {
      case 0:
        return Column(
          mainAxisSize: MainAxisSize.min,
          children: <Widget>[
            const Text("This is the home page"),
            ElevatedButton(
              child: const Text(
                "Start new page",
                style: TextStyle(color: Colors.white),
              ),
              onPressed: () {
                Navigator.of(context).push(
                    MaterialPageRoute(builder: (context) => TextPage()));
              },
            ),
            ElevatedButton(
              child: const Text(
                "Change to page 3",
                style: TextStyle(color: Colors.white),
              ),
              onPressed: () {
                final FancyBottomNavigationState fState = bottomNavigationKey
                    .currentState as FancyBottomNavigationState;
                fState.setPage(2);
              },
            )
          ],
        );
      case 1:
        return Column(
          mainAxisSize: MainAxisSize.min,
          children: <Widget>[
            const Text("This is the search page"),
            ElevatedButton(
              child: const Text(
                "Start new page",
                style: TextStyle(color: Colors.white),
              ),
              onPressed: () {
                Navigator.of(context).push(
                    MaterialPageRoute(builder: (context) => TextPage()));
              },
            )
          ],
        );
      default:
        return Column(
          mainAxisSize: MainAxisSize.min,
          children: <Widget>[
            const Text("This is the basket page"),
            ElevatedButton(
              child: const Text(
                "Start new page",
                style: TextStyle(color: Colors.white),
              ),
              onPressed: () {},
            )
          ],
        );
    }
  }
}

image.png

还有很多好看的底部导航。