Flutter用BottomNavigationBar制作底部导航栏与页面跳转

看完flutter的基础资料,准备开始做一个简单的demo练练手,记录下练习效果。

1、创建页面

先创建几个界面类,作为主页面用来显示(创建三到四个,代码直接拷贝,修改下类名和appbar的属性就可以了)

import 'package:flutter/material.dart';
class KYVMHome extends StatefulWidget {
  @override
  _KYVMHomeState createState() => _KYVMHomeState();
}

class _KYVMHomeState extends State<KYVMHome> with AutomaticKeepAliveClientMixin {
  @override
  bool get wantKeepAlive => true;

  Widget build(BuildContext context) {
    super.build(context);

    return Scaffold(
      appBar: AppBar(
        title: Text('首页'),
        backgroundColor: Colors.blue,
      ),
      body: Container(
        color: Colors.blue,
      ),
    );
  }
}

2、创建导航类

创建一个底部导航类,本次使用BottomNavigationBar来作为应用的导航

import 'package:flutter/material.dart';
import 'package:my_test/home/kyvm_home.dart';
import 'package:my_test/message/kyvm_message.dart';
import 'package:my_test/person_center/kyvm_person_center.dart';


class KYVMNavigate extends StatefulWidget {
  @override
  _NavigateState createState() => _NavigateState();
}

class _NavigateState extends State<KYVMNavigate>
  with SingleTickerProviderStateMixin {
    
  final _bottomNavigationColor = Colors.grey;
  final _bottomNavigationSelectColor = Colors.blue;
  int _currentIndex = 0;
  var _controller = PageController(
    initialPage: 0,
  );

  @override
  void dispose() {
    super.dispose();
    _controller.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: PageView(
        controller: _controller,
        children: <Widget>[
          KYVMHome(),
          KYVMMessage(),
          KYVMPersonCenter(),
        ],
        physics: NeverScrollableScrollPhysics(),
      ),
      bottomNavigationBar: BottomNavigationBar(
        currentIndex: _currentIndex,
        onTap: (index) {
          _controller.jumpToPage(index);
          setState(() {
            _currentIndex = index;
          });
        },
        type: BottomNavigationBarType.fixed,
        items: [
          BottomNavigationBarItem(
            activeIcon: Icon(
              Icons.home,
              color: _bottomNavigationSelectColor,
            ),
            icon: Icon(
              Icons.home,
              color: _bottomNavigationColor,
            ),
            title: Text(
              '首页',
              style: TextStyle(color:_bottomNavigationSelectColor),
            ),
          ),
          BottomNavigationBarItem(
            activeIcon: Icon(
              Icons.message,
              color: _bottomNavigationSelectColor,
            ),
            icon: Icon(
              Icons.message,
              color: _bottomNavigationColor,
            ),
            title: Text(
              '消息',
              style: TextStyle(color:_bottomNavigationSelectColor),
            ),
          ),
          BottomNavigationBarItem(
            activeIcon: Icon(
              Icons.person,
              color: _bottomNavigationSelectColor,
            ),
            icon: Icon(
              Icons.person,
              color: _bottomNavigationColor,
            ),
            title: Text(
              '我的',
              style: TextStyle(color:_bottomNavigationSelectColor),
            ),
          ),
        ],
      ),
    );
  }
}

3、修改main.dart

main.dart 是我们程序的入口。就类似于 Java、OC 中的 main() ,作为一个程序的入口。我们将 main.dart 作为程序的启动入口,就不做过多的操作,只是指定去加载我们的导航类。

import 'package:flutter/material.dart';
import 'package:my_test/navigate/kyvm_navigate.dart';

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

class MyApp extends StatelessWidget {
  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        // This is the theme of your application.
        //
        // Try running your application with "flutter run". You'll see the
        // application has a blue toolbar. Then, without quitting the app, try
        // changing the primarySwatch below to Colors.green and then invoke
        // "hot reload" (press "r" in the console where you ran "flutter run",
        // or simply save your changes to "hot reload" in a Flutter IDE).
        // Notice that the counter didn't reset back to zero; the application
        // is not restarted.
        primarySwatch: Colors.blue,
      ),
      // home: MyHomePage(title: 'Flutter Demo Home Page'),
      home: KYVMNavigate(),
    );
  }
}

4、页面跳转

本次使用的是系统自带的方法
跳转到下一页面

Navigator.push(
     context,
     MaterialPageRoute(builder: (context) => CarLocation()),
 );

返回上一页面

Navigator.pop(context);

因为对各种控件和方法不太熟悉,暂时使用的都是系统自带方法,等熟悉后再自定义一些公共类之类的来提高开发效率

练习demo地址: https://github.com/liuxiangsheng/flutter_bottomNavigate

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 本文翻译自 Add-Flutter-to-existing-apps 在已有项目中继承Flutter 约定:本文中...
    黄二瓜阅读 9,117评论 3 21
  • 一、Flutter是什么? 根据Flutter中文官网(英文官网)的解释:Flutter是谷歌的移动UI框架,可以...
    鸿羽羽羽羽羽阅读 952评论 4 13
  • 特别说明 当前博客平台账号已废弃,如果有使用细节问题请前往我新博客平台进行讨论交流。 个人博客平台 HuRuWo的...
    善笃有余劫阅读 4,987评论 0 30
  • 英文官网:https://flutter.io/中文网:https://flutterchina.club/ 首先...
    超威蓝猫l阅读 2,836评论 1 3
  • 湖边的黄昏 太阳和水追逐戏弄 风儿捎来了妈妈的呼唤 于是,太阳慢慢的离去 不知为啥又红了脸...... ...
    阿荚阅读 233评论 0 6